Draw.Feature.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. L.Draw = L.Draw || {};
  2. /**
  3. * @class L.Draw.Feature
  4. * @aka Draw.Feature
  5. */
  6. L.Draw.Feature = L.Handler.extend({
  7. includes: L.Mixin.Events,
  8. // @method initialize(): void
  9. initialize: function (map, options) {
  10. this._map = map;
  11. this._container = map._container;
  12. this._overlayPane = map._panes.overlayPane;
  13. this._popupPane = map._panes.popupPane;
  14. // Merge default shapeOptions options with custom shapeOptions
  15. if (options && options.shapeOptions) {
  16. options.shapeOptions = L.Util.extend({}, this.options.shapeOptions, options.shapeOptions);
  17. }
  18. L.setOptions(this, options);
  19. },
  20. // @method enable(): void
  21. // Enables this handler
  22. enable: function () {
  23. if (this._enabled) {
  24. return;
  25. }
  26. L.Handler.prototype.enable.call(this);
  27. this.fire('enabled', { handler: this.type });
  28. this._map.fire(L.Draw.Event.DRAWSTART, { layerType: this.type });
  29. },
  30. // @method initialize(): void
  31. disable: function () {
  32. if (!this._enabled) {
  33. return;
  34. }
  35. L.Handler.prototype.disable.call(this);
  36. this._map.fire(L.Draw.Event.DRAWSTOP, { layerType: this.type });
  37. this.fire('disabled', { handler: this.type });
  38. },
  39. // @method addHooks(): void
  40. // Add's event listeners to this handler
  41. addHooks: function () {
  42. var map = this._map;
  43. if (map) {
  44. L.DomUtil.disableTextSelection();
  45. map.getContainer().focus();
  46. this._tooltip = new L.Draw.Tooltip(this._map);
  47. L.DomEvent.on(this._container, 'keyup', this._cancelDrawing, this);
  48. }
  49. },
  50. // @method removeHooks(): void
  51. // Removes event listeners from this handler
  52. removeHooks: function () {
  53. if (this._map) {
  54. L.DomUtil.enableTextSelection();
  55. this._tooltip.dispose();
  56. this._tooltip = null;
  57. L.DomEvent.off(this._container, 'keyup', this._cancelDrawing, this);
  58. }
  59. },
  60. // @method setOptions(object): void
  61. // Sets new options to this handler
  62. setOptions: function (options) {
  63. L.setOptions(this, options);
  64. },
  65. _fireCreatedEvent: function (layer) {
  66. this._map.fire(L.Draw.Event.CREATED, { layer: layer, layerType: this.type });
  67. },
  68. // Cancel drawing when the escape key is pressed
  69. _cancelDrawing: function (e) {
  70. this._map.fire('draw:canceled', { layerType: this.type });
  71. if (e.keyCode === 27) {
  72. this.disable();
  73. }
  74. }
  75. });