123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- L.Edit = L.Edit || {};
- L.Edit.SimpleShape = L.Handler.extend({
- options: {
- moveIcon: new L.DivIcon({
- iconSize: new L.Point(8, 8),
- className: 'leaflet-div-icon leaflet-editing-icon leaflet-edit-move'
- }),
- resizeIcon: new L.DivIcon({
- iconSize: new L.Point(8, 8),
- className: 'leaflet-div-icon leaflet-editing-icon leaflet-edit-resize'
- })
- },
- initialize: function (shape, options) {
- this._shape = shape;
- L.Util.setOptions(this, options);
- },
- addHooks: function () {
- if (this._shape._map) {
- this._map = this._shape._map;
- if (!this._markerGroup) {
- this._initMarkers();
- }
- this._map.addLayer(this._markerGroup);
- }
- },
- removeHooks: function () {
- if (this._shape._map) {
- this._unbindMarker(this._moveMarker);
- for (var i = 0, l = this._resizeMarkers.length; i < l; i++) {
- this._unbindMarker(this._resizeMarkers[i]);
- }
- this._resizeMarkers = null;
- this._map.removeLayer(this._markerGroup);
- delete this._markerGroup;
- }
- this._map = null;
- },
- updateMarkers: function () {
- this._markerGroup.clearLayers();
- this._initMarkers();
- },
- _initMarkers: function () {
- if (!this._markerGroup) {
- this._markerGroup = new L.LayerGroup();
- }
- // Create center marker
- this._createMoveMarker();
- // Create edge marker
- this._createResizeMarker();
- },
- _createMoveMarker: function () {
- // Children override
- },
- _createResizeMarker: function () {
- // Children override
- },
- _createMarker: function (latlng, icon) {
- var marker = new L.Marker(latlng, {
- draggable: true,
- icon: icon,
- zIndexOffset: 10
- });
- this._bindMarker(marker);
- this._markerGroup.addLayer(marker);
- return marker;
- },
- _bindMarker: function (marker) {
- marker
- .on('dragstart', this._onMarkerDragStart, this)
- .on('drag', this._onMarkerDrag, this)
- .on('dragend', this._onMarkerDragEnd, this);
- },
- _unbindMarker: function (marker) {
- marker
- .off('dragstart', this._onMarkerDragStart, this)
- .off('drag', this._onMarkerDrag, this)
- .off('dragend', this._onMarkerDragEnd, this);
- },
- _onMarkerDragStart: function (e) {
- var marker = e.target? e.target: e.srcElement;
- marker.setOpacity(0);
- },
- _fireEdit: function () {
- this._shape.edited = true;
- this._shape.fire('edit');
- },
- _onMarkerDrag: function (e) {
- var marker = e.target? e.target: e.srcElement,
- latlng = marker.getLatLng();
- if (marker === this._moveMarker) {
- this._move(latlng);
- } else {
- this._resize(latlng);
- }
- this._shape.redraw();
- this._shape._map.fire('editDraging');
- },
- _onMarkerDragEnd: function (e) {
- var marker = e.target? e.target: e.srcElement;
- marker.setOpacity(1);
- this._shape._map.fire('edit');
- this._fireEdit();
- },
- _move: function () {
- // Children override
- },
- _resize: function () {
- // Children override
- }
- });
|