Edit.Marker.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. L.Edit = L.Edit || {};
  2. /**
  3. * @class L.Edit.Marker
  4. * @aka Edit.Marker
  5. */
  6. L.Edit.Marker = L.Handler.extend({
  7. // @method initialize(): void
  8. initialize: function (marker, options) {
  9. this._marker = marker;
  10. L.setOptions(this, options);
  11. },
  12. // @method addHooks(): void
  13. // Add listener hooks to this handler
  14. addHooks: function () {
  15. var marker = this._marker;
  16. marker.dragging.enable();
  17. marker.on('dragend', this._onDragEnd, marker);
  18. this._toggleMarkerHighlight();
  19. },
  20. // @method removeHooks(): void
  21. // Remove listener hooks from this handler
  22. removeHooks: function () {
  23. var marker = this._marker;
  24. marker.dragging.disable();
  25. marker.off('dragend', this._onDragEnd, marker);
  26. this._toggleMarkerHighlight();
  27. },
  28. _onDragEnd: function (e) {
  29. var layer = e.target;
  30. layer.edited = true;
  31. this._map.fire(L.Draw.Event.EDITMOVE, { layer: layer });
  32. },
  33. _toggleMarkerHighlight: function () {
  34. var icon = this._marker._icon;
  35. // Don't do anything if this layer is a marker but doesn't have an icon. Markers
  36. // should usually have icons. If using Leaflet.draw with Leaflet.markercluster there
  37. // is a chance that a marker doesn't.
  38. if (!icon) {
  39. return;
  40. }
  41. // This is quite naughty, but I don't see another way of doing it. (short of setting a new icon)
  42. icon.style.display = 'none';
  43. if (L.DomUtil.hasClass(icon, 'leaflet-edit-marker-selected')) {
  44. L.DomUtil.removeClass(icon, 'leaflet-edit-marker-selected');
  45. // Offset as the border will make the icon move.
  46. this._offsetMarker(icon, -4);
  47. } else {
  48. L.DomUtil.addClass(icon, 'leaflet-edit-marker-selected');
  49. // Offset as the border will make the icon move.
  50. this._offsetMarker(icon, 4);
  51. }
  52. icon.style.display = '';
  53. },
  54. _offsetMarker: function (icon, offset) {
  55. var iconMarginTop = parseInt(icon.style.marginTop, 10) - offset,
  56. iconMarginLeft = parseInt(icon.style.marginLeft, 10) - offset;
  57. icon.style.marginTop = iconMarginTop + 'px';
  58. icon.style.marginLeft = iconMarginLeft + 'px';
  59. }
  60. });
  61. L.Marker.addInitHook(function () {
  62. if (L.Edit.Marker) {
  63. this.editing = new L.Edit.Marker(this);
  64. if (this.options.editable) {
  65. this.editing.enable();
  66. }
  67. }
  68. });