Edit.SimpleShape.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. L.Edit = L.Edit || {};
  2. /**
  3. * @class L.Edit.SimpleShape
  4. * @aka Edit.SimpleShape
  5. */
  6. L.Edit.SimpleShape = L.Handler.extend({
  7. options: {
  8. moveIcon: new L.DivIcon({
  9. iconSize: new L.Point(8, 8),
  10. className: 'leaflet-div-icon leaflet-editing-icon leaflet-edit-move'
  11. }),
  12. resizeIcon: new L.DivIcon({
  13. iconSize: new L.Point(8, 8),
  14. className: 'leaflet-div-icon leaflet-editing-icon leaflet-edit-resize'
  15. }),
  16. touchMoveIcon: new L.DivIcon({
  17. iconSize: new L.Point(20, 20),
  18. className: 'leaflet-div-icon leaflet-editing-icon leaflet-edit-move leaflet-touch-icon'
  19. }),
  20. touchResizeIcon: new L.DivIcon({
  21. iconSize: new L.Point(20, 20),
  22. className: 'leaflet-div-icon leaflet-editing-icon leaflet-edit-resize leaflet-touch-icon'
  23. }),
  24. },
  25. // @method intialize(): void
  26. initialize: function (shape, options) {
  27. // if touch, switch to touch icon
  28. if (L.Browser.touch) {
  29. this.options.moveIcon = this.options.touchMoveIcon;
  30. this.options.resizeIcon = this.options.touchResizeIcon;
  31. }
  32. this._shape = shape;
  33. L.Util.setOptions(this, options);
  34. },
  35. // @method addHooks(): void
  36. // Add listener hooks to this handler
  37. addHooks: function () {
  38. var shape = this._shape;
  39. if (this._shape._map) {
  40. this._map = this._shape._map;
  41. shape.setStyle(shape.options.editing);
  42. if (shape._map) {
  43. this._map = shape._map;
  44. if (!this._markerGroup) {
  45. this._initMarkers();
  46. }
  47. this._map.addLayer(this._markerGroup);
  48. }
  49. }
  50. },
  51. // @method removeHooks(): void
  52. // Remove listener hooks from this handler
  53. removeHooks: function () {
  54. var shape = this._shape;
  55. shape.setStyle(shape.options.original);
  56. if (shape._map) {
  57. this._unbindMarker(this._moveMarker);
  58. for (var i = 0, l = this._resizeMarkers.length; i < l; i++) {
  59. this._unbindMarker(this._resizeMarkers[i]);
  60. }
  61. this._resizeMarkers = null;
  62. this._map.removeLayer(this._markerGroup);
  63. delete this._markerGroup;
  64. }
  65. this._map = null;
  66. },
  67. // @method updateMarkers(): void
  68. // Remove the edit markers from this layer
  69. updateMarkers: function () {
  70. this._markerGroup.clearLayers();
  71. this._initMarkers();
  72. },
  73. _initMarkers: function () {
  74. if (!this._markerGroup) {
  75. this._markerGroup = new L.LayerGroup();
  76. }
  77. // Create center marker
  78. this._createMoveMarker();
  79. // Create edge marker
  80. this._createResizeMarker();
  81. },
  82. _createMoveMarker: function () {
  83. // Children override
  84. },
  85. _createResizeMarker: function () {
  86. // Children override
  87. },
  88. _createMarker: function (latlng, icon) {
  89. // Extending L.Marker in TouchEvents.js to include touch.
  90. var marker = new L.Marker.Touch(latlng, {
  91. draggable: true,
  92. icon: icon,
  93. zIndexOffset: 10
  94. });
  95. this._bindMarker(marker);
  96. this._markerGroup.addLayer(marker);
  97. return marker;
  98. },
  99. _bindMarker: function (marker) {
  100. marker
  101. .on('dragstart', this._onMarkerDragStart, this)
  102. .on('drag', this._onMarkerDrag, this)
  103. .on('dragend', this._onMarkerDragEnd, this)
  104. .on('touchstart', this._onTouchStart, this)
  105. .on('touchmove', this._onTouchMove, this)
  106. .on('MSPointerMove', this._onTouchMove, this)
  107. .on('touchend', this._onTouchEnd, this)
  108. .on('MSPointerUp', this._onTouchEnd, this);
  109. },
  110. _unbindMarker: function (marker) {
  111. marker
  112. .off('dragstart', this._onMarkerDragStart, this)
  113. .off('drag', this._onMarkerDrag, this)
  114. .off('dragend', this._onMarkerDragEnd, this)
  115. .off('touchstart', this._onTouchStart, this)
  116. .off('touchmove', this._onTouchMove, this)
  117. .off('MSPointerMove', this._onTouchMove, this)
  118. .off('touchend', this._onTouchEnd, this)
  119. .off('MSPointerUp', this._onTouchEnd, this);
  120. },
  121. _onMarkerDragStart: function (e) {
  122. var marker = e.target;
  123. marker.setOpacity(0);
  124. this._shape.fire('editstart');
  125. },
  126. _fireEdit: function () {
  127. this._shape.edited = true;
  128. this._shape.fire('edit');
  129. },
  130. _onMarkerDrag: function (e) {
  131. var marker = e.target,
  132. latlng = marker.getLatLng();
  133. if (marker === this._moveMarker) {
  134. this._move(latlng);
  135. } else {
  136. this._resize(latlng);
  137. }
  138. this._shape.redraw();
  139. this._shape.fire('editdrag');
  140. },
  141. _onMarkerDragEnd: function (e) {
  142. var marker = e.target;
  143. marker.setOpacity(1);
  144. this._fireEdit();
  145. },
  146. _onTouchStart: function (e) {
  147. L.Edit.SimpleShape.prototype._onMarkerDragStart.call(this, e);
  148. if (typeof(this._getCorners) === 'function') {
  149. // Save a reference to the opposite point
  150. var corners = this._getCorners(),
  151. marker = e.target,
  152. currentCornerIndex = marker._cornerIndex;
  153. marker.setOpacity(0);
  154. // Copyed from Edit.Rectangle.js line 23 _onMarkerDragStart()
  155. // Latlng is null otherwise.
  156. this._oppositeCorner = corners[(currentCornerIndex + 2) % 4];
  157. this._toggleCornerMarkers(0, currentCornerIndex);
  158. }
  159. this._shape.fire('editstart');
  160. },
  161. _onTouchMove: function (e) {
  162. var layerPoint = this._map.mouseEventToLayerPoint(e.originalEvent.touches[0]),
  163. latlng = this._map.layerPointToLatLng(layerPoint),
  164. marker = e.target;
  165. if (marker === this._moveMarker) {
  166. this._move(latlng);
  167. } else {
  168. this._resize(latlng);
  169. }
  170. this._shape.redraw();
  171. // prevent touchcancel in IOS
  172. // e.preventDefault();
  173. return false;
  174. },
  175. _onTouchEnd: function (e) {
  176. var marker = e.target;
  177. marker.setOpacity(1);
  178. this.updateMarkers();
  179. this._fireEdit();
  180. },
  181. _move: function () {
  182. // Children override
  183. },
  184. _resize: function () {
  185. // Children override
  186. }
  187. });