Draw.SimpleShape.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. L.SimpleShape = {};
  2. /**
  3. * @class L.Draw.SimpleShape
  4. * @aka Draw.SimpleShape
  5. * @inherits L.Draw.Feature
  6. */
  7. L.Draw.SimpleShape = L.Draw.Feature.extend({
  8. options: {
  9. repeatMode: false
  10. },
  11. // @method initialize(): void
  12. initialize: function (map, options) {
  13. this._endLabelText = L.drawLocal.draw.handlers.simpleshape.tooltip.end;
  14. L.Draw.Feature.prototype.initialize.call(this, map, options);
  15. },
  16. // @method addHooks(): void
  17. // Add listener hooks to this handler.
  18. addHooks: function () {
  19. L.Draw.Feature.prototype.addHooks.call(this);
  20. if (this._map) {
  21. this._mapDraggable = this._map.dragging.enabled();
  22. if (this._mapDraggable) {
  23. this._map.dragging.disable();
  24. }
  25. //TODO refactor: move cursor to styles
  26. this._container.style.cursor = 'crosshair';
  27. this._tooltip.updateContent({ text: this._initialLabelText });
  28. this._map
  29. .on('mousedown', this._onMouseDown, this)
  30. .on('mousemove', this._onMouseMove, this)
  31. .on('touchstart', this._onMouseDown, this)
  32. .on('touchmove', this._onMouseMove, this);
  33. }
  34. },
  35. // @method removeHooks(): void
  36. // Remove listener hooks from this handler.
  37. removeHooks: function () {
  38. L.Draw.Feature.prototype.removeHooks.call(this);
  39. if (this._map) {
  40. if (this._mapDraggable) {
  41. this._map.dragging.enable();
  42. }
  43. //TODO refactor: move cursor to styles
  44. this._container.style.cursor = '';
  45. this._map
  46. .off('mousedown', this._onMouseDown, this)
  47. .off('mousemove', this._onMouseMove, this)
  48. .off('touchstart', this._onMouseDown, this)
  49. .off('touchmove', this._onMouseMove, this);
  50. L.DomEvent.off(document, 'mouseup', this._onMouseUp, this);
  51. L.DomEvent.off(document, 'touchend', this._onMouseUp, this);
  52. // If the box element doesn't exist they must not have moved the mouse, so don't need to destroy/return
  53. if (this._shape) {
  54. this._map.removeLayer(this._shape);
  55. delete this._shape;
  56. }
  57. }
  58. this._isDrawing = false;
  59. },
  60. _getTooltipText: function () {
  61. return {
  62. text: this._endLabelText
  63. };
  64. },
  65. _onMouseDown: function (e) {
  66. this._isDrawing = true;
  67. this._startLatLng = e.latlng;
  68. L.DomEvent
  69. .on(document, 'mouseup', this._onMouseUp, this)
  70. .on(document, 'touchend', this._onMouseUp, this)
  71. .preventDefault(e.originalEvent);
  72. },
  73. _onMouseMove: function (e) {
  74. var latlng = e.latlng;
  75. this._tooltip.updatePosition(latlng);
  76. if (this._isDrawing) {
  77. this._tooltip.updateContent(this._getTooltipText());
  78. this._drawShape(latlng);
  79. }
  80. },
  81. _onMouseUp: function () {
  82. if (this._shape) {
  83. this._fireCreatedEvent();
  84. }
  85. this.disable();
  86. if (this.options.repeatMode) {
  87. this.enable();
  88. }
  89. }
  90. });