Edit.SimpleShape.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. L.Edit = L.Edit || {};
  2. L.Edit.SimpleShape = L.Handler.extend({
  3. options: {
  4. moveIcon: new L.DivIcon({
  5. iconSize: new L.Point(8, 8),
  6. className: 'leaflet-div-icon leaflet-editing-icon leaflet-edit-move'
  7. }),
  8. resizeIcon: new L.DivIcon({
  9. iconSize: new L.Point(8, 8),
  10. className: 'leaflet-div-icon leaflet-editing-icon leaflet-edit-resize'
  11. })
  12. },
  13. initialize: function (shape, options) {
  14. this._shape = shape;
  15. L.Util.setOptions(this, options);
  16. },
  17. addHooks: function () {
  18. if (this._shape._map) {
  19. this._map = this._shape._map;
  20. if (!this._markerGroup) {
  21. this._initMarkers();
  22. }
  23. this._map.addLayer(this._markerGroup);
  24. }
  25. },
  26. removeHooks: function () {
  27. if (this._shape._map) {
  28. this._unbindMarker(this._moveMarker);
  29. for (var i = 0, l = this._resizeMarkers.length; i < l; i++) {
  30. this._unbindMarker(this._resizeMarkers[i]);
  31. }
  32. this._resizeMarkers = null;
  33. this._map.removeLayer(this._markerGroup);
  34. delete this._markerGroup;
  35. }
  36. this._map = null;
  37. },
  38. updateMarkers: function () {
  39. this._markerGroup.clearLayers();
  40. this._initMarkers();
  41. },
  42. _initMarkers: function () {
  43. if (!this._markerGroup) {
  44. this._markerGroup = new L.LayerGroup();
  45. }
  46. // Create center marker
  47. this._createMoveMarker();
  48. // Create edge marker
  49. this._createResizeMarker();
  50. },
  51. _createMoveMarker: function () {
  52. // Children override
  53. },
  54. _createResizeMarker: function () {
  55. // Children override
  56. },
  57. _createMarker: function (latlng, icon) {
  58. var marker = new L.Marker(latlng, {
  59. draggable: true,
  60. icon: icon,
  61. zIndexOffset: 10
  62. });
  63. this._bindMarker(marker);
  64. this._markerGroup.addLayer(marker);
  65. return marker;
  66. },
  67. _bindMarker: function (marker) {
  68. marker
  69. .on('dragstart', this._onMarkerDragStart, this)
  70. .on('drag', this._onMarkerDrag, this)
  71. .on('dragend', this._onMarkerDragEnd, this);
  72. },
  73. _unbindMarker: function (marker) {
  74. marker
  75. .off('dragstart', this._onMarkerDragStart, this)
  76. .off('drag', this._onMarkerDrag, this)
  77. .off('dragend', this._onMarkerDragEnd, this);
  78. },
  79. _onMarkerDragStart: function (e) {
  80. var marker = e.target? e.target: e.srcElement;
  81. marker.setOpacity(0);
  82. },
  83. _fireEdit: function () {
  84. this._shape.edited = true;
  85. this._shape.fire('edit');
  86. },
  87. _onMarkerDrag: function (e) {
  88. var marker = e.target? e.target: e.srcElement,
  89. latlng = marker.getLatLng();
  90. if (marker === this._moveMarker) {
  91. this._move(latlng);
  92. } else {
  93. this._resize(latlng);
  94. }
  95. this._shape.redraw();
  96. this._shape._map.fire('editDraging');
  97. },
  98. _onMarkerDragEnd: function (e) {
  99. var marker = e.target? e.target: e.srcElement;
  100. marker.setOpacity(1);
  101. this._shape._map.fire('edit');
  102. this._fireEdit();
  103. },
  104. _move: function () {
  105. // Children override
  106. },
  107. _resize: function () {
  108. // Children override
  109. }
  110. });