Spaces:
Running
Running
File size: 7,935 Bytes
f655b1d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
/**
* @class L.EditToolbar.Edit
* @aka EditToolbar.Edit
*/
L.EditToolbar.Edit = L.Handler.extend({
statics: {
TYPE: 'edit'
},
includes: L.Mixin.Events,
// @method intialize(): void
initialize: function (map, options) {
L.Handler.prototype.initialize.call(this, map);
L.setOptions(this, options);
// Store the selectable layer group for ease of access
this._featureGroup = options.featureGroup;
if (!(this._featureGroup instanceof L.FeatureGroup)) {
throw new Error('options.featureGroup must be a L.FeatureGroup');
}
this._uneditedLayerProps = {};
// Save the type so super can fire, need to do this as cannot do this.TYPE :(
this.type = L.EditToolbar.Edit.TYPE;
},
// @method enable(): void
// Enable the edit toolbar
enable: function () {
if (this._enabled || !this._hasAvailableLayers()) {
return;
}
this.fire('enabled', { handler: this.type });
//this disable other handlers
this._map.fire(L.Draw.Event.EDITSTART, { handler: this.type });
//allow drawLayer to be updated before beginning edition.
L.Handler.prototype.enable.call(this);
this._featureGroup
.on('layeradd', this._enableLayerEdit, this)
.on('layerremove', this._disableLayerEdit, this);
},
// @method disable(): void
// Disable the edit toolbar
disable: function () {
if (!this._enabled) {
return;
}
this._featureGroup
.off('layeradd', this._enableLayerEdit, this)
.off('layerremove', this._disableLayerEdit, this);
L.Handler.prototype.disable.call(this);
this._map.fire(L.Draw.Event.EDITSTOP, { handler: this.type });
this.fire('disabled', { handler: this.type });
},
// @method addHooks(): void
// Add listener hooks for this handler
addHooks: function () {
var map = this._map;
if (map) {
map.getContainer().focus();
this._featureGroup.eachLayer(this._enableLayerEdit, this);
this._tooltip = new L.Draw.Tooltip(this._map);
this._tooltip.updateContent({
text: L.drawLocal.edit.handlers.edit.tooltip.text,
subtext: L.drawLocal.edit.handlers.edit.tooltip.subtext
});
// Quickly access the tooltip to update for intersection checking
map._editTooltip = this._tooltip;
this._updateTooltip();
this._map
.on('mousemove', this._onMouseMove, this)
.on('touchmove', this._onMouseMove, this)
.on('MSPointerMove', this._onMouseMove, this)
.on(L.Draw.Event.EDITVERTEX, this._updateTooltip, this);
}
},
// @method removeHooks(): void
// Remove listener hooks for this handler
removeHooks: function () {
if (this._map) {
// Clean up selected layers.
this._featureGroup.eachLayer(this._disableLayerEdit, this);
// Clear the backups of the original layers
this._uneditedLayerProps = {};
this._tooltip.dispose();
this._tooltip = null;
this._map
.off('mousemove', this._onMouseMove, this)
.off('touchmove', this._onMouseMove, this)
.off('MSPointerMove', this._onMouseMove, this)
.off(L.Draw.Event.EDITVERTEX, this._updateTooltip, this);
}
},
// @method revertLayers(): void
// Revert each layer's geometry changes
revertLayers: function () {
this._featureGroup.eachLayer(function (layer) {
this._revertLayer(layer);
}, this);
},
// @method save(): void
// Save the layer geometries
save: function () {
var editedLayers = new L.LayerGroup();
this._featureGroup.eachLayer(function (layer) {
if (layer.edited) {
editedLayers.addLayer(layer);
layer.edited = false;
}
});
this._map.fire(L.Draw.Event.EDITED, { layers: editedLayers });
},
_backupLayer: function (layer) {
var id = L.Util.stamp(layer);
if (!this._uneditedLayerProps[id]) {
// Polyline, Polygon or Rectangle
if (layer instanceof L.Polyline || layer instanceof L.Polygon || layer instanceof L.Rectangle) {
this._uneditedLayerProps[id] = {
latlngs: L.LatLngUtil.cloneLatLngs(layer.getLatLngs())
};
} else if (layer instanceof L.Circle) {
this._uneditedLayerProps[id] = {
latlng: L.LatLngUtil.cloneLatLng(layer.getLatLng()),
radius: layer.getRadius()
};
} else if (layer instanceof L.Marker) { // Marker
this._uneditedLayerProps[id] = {
latlng: L.LatLngUtil.cloneLatLng(layer.getLatLng())
};
}
}
},
_getTooltipText: function () {
return ({
text: L.drawLocal.edit.handlers.edit.tooltip.text,
subtext: L.drawLocal.edit.handlers.edit.tooltip.subtext
});
},
_updateTooltip: function () {
this._tooltip.updateContent(this._getTooltipText());
},
_revertLayer: function (layer) {
var id = L.Util.stamp(layer);
layer.edited = false;
if (this._uneditedLayerProps.hasOwnProperty(id)) {
// Polyline, Polygon or Rectangle
if (layer instanceof L.Polyline || layer instanceof L.Polygon || layer instanceof L.Rectangle) {
layer.setLatLngs(this._uneditedLayerProps[id].latlngs);
} else if (layer instanceof L.Circle) {
layer.setLatLng(this._uneditedLayerProps[id].latlng);
layer.setRadius(this._uneditedLayerProps[id].radius);
} else if (layer instanceof L.Marker) { // Marker
layer.setLatLng(this._uneditedLayerProps[id].latlng);
}
layer.fire('revert-edited', { layer: layer });
}
},
_enableLayerEdit: function (e) {
var layer = e.layer || e.target || e,
pathOptions, poly;
// Back up this layer (if haven't before)
this._backupLayer(layer);
if (this.options.poly) {
poly = L.Util.extend({}, this.options.poly);
layer.options.poly = poly;
}
// Set different style for editing mode
if (this.options.selectedPathOptions) {
pathOptions = L.Util.extend({}, this.options.selectedPathOptions);
// Use the existing color of the layer
if (pathOptions.maintainColor) {
pathOptions.color = layer.options.color;
pathOptions.fillColor = layer.options.fillColor;
}
layer.options.original = L.extend({}, layer.options);
layer.options.editing = pathOptions;
}
if (layer instanceof L.Marker) {
if (layer.editing) {
layer.editing.enable();
}
layer.dragging.enable();
layer
.on('dragend', this._onMarkerDragEnd)
// #TODO: remove when leaflet finally fixes their draggable so it's touch friendly again.
.on('touchmove', this._onTouchMove, this)
.on('MSPointerMove', this._onTouchMove, this)
.on('touchend', this._onMarkerDragEnd, this)
.on('MSPointerUp', this._onMarkerDragEnd, this);
} else {
layer.editing.enable();
}
},
_disableLayerEdit: function (e) {
var layer = e.layer || e.target || e;
layer.edited = false;
if (layer.editing) {
layer.editing.disable();
}
delete layer.options.editing;
delete layer.options.original;
// Reset layer styles to that of before select
if (this._selectedPathOptions) {
if (layer instanceof L.Marker) {
this._toggleMarkerHighlight(layer);
} else {
// reset the layer style to what is was before being selected
layer.setStyle(layer.options.previousOptions);
// remove the cached options for the layer object
delete layer.options.previousOptions;
}
}
if (layer instanceof L.Marker) {
layer.dragging.disable();
layer
.off('dragend', this._onMarkerDragEnd, this)
.off('touchmove', this._onTouchMove, this)
.off('MSPointerMove', this._onTouchMove, this)
.off('touchend', this._onMarkerDragEnd, this)
.off('MSPointerUp', this._onMarkerDragEnd, this);
} else {
layer.editing.disable();
}
},
_onMouseMove: function (e) {
this._tooltip.updatePosition(e.latlng);
},
_onMarkerDragEnd: function (e) {
var layer = e.target;
layer.edited = true;
this._map.fire(L.Draw.Event.EDITMOVE, { layer: layer });
},
_onTouchMove: function (e) {
var touchEvent = e.originalEvent.changedTouches[0],
layerPoint = this._map.mouseEventToLayerPoint(touchEvent),
latlng = this._map.layerPointToLatLng(layerPoint);
e.target.setLatLng(latlng);
},
_hasAvailableLayers: function () {
return this._featureGroup.getLayers().length !== 0;
}
});
|