Control.Draw.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @class L.Control.Draw
  3. * @aka L.Draw
  4. */
  5. L.Control.Draw = L.Control.extend({
  6. // Options
  7. options: {
  8. position: 'topleft',
  9. draw: {},
  10. edit: false
  11. },
  12. // @method initialize(): void
  13. // Initializes draw control, toolbars from the options
  14. initialize: function (options) {
  15. if (L.version < '0.7') {
  16. throw new Error('Leaflet.draw 0.2.3+ requires Leaflet 0.7.0+. Download latest from https://github.com/Leaflet/Leaflet/');
  17. }
  18. L.Control.prototype.initialize.call(this, options);
  19. var toolbar;
  20. this._toolbars = {};
  21. // Initialize toolbars
  22. if (L.DrawToolbar && this.options.draw) {
  23. toolbar = new L.DrawToolbar(this.options.draw);
  24. this._toolbars[L.DrawToolbar.TYPE] = toolbar;
  25. // Listen for when toolbar is enabled
  26. this._toolbars[L.DrawToolbar.TYPE].on('enable', this._toolbarEnabled, this);
  27. }
  28. if (L.EditToolbar && this.options.edit) {
  29. toolbar = new L.EditToolbar(this.options.edit);
  30. this._toolbars[L.EditToolbar.TYPE] = toolbar;
  31. // Listen for when toolbar is enabled
  32. this._toolbars[L.EditToolbar.TYPE].on('enable', this._toolbarEnabled, this);
  33. }
  34. L.toolbar = this; //set global var for editing the toolbar
  35. },
  36. // @method onAdd(): container
  37. // Adds the toolbar container to the map
  38. onAdd: function (map) {
  39. var container = L.DomUtil.create('div', 'leaflet-draw'),
  40. addedTopClass = false,
  41. topClassName = 'leaflet-draw-toolbar-top',
  42. toolbarContainer;
  43. for (var toolbarId in this._toolbars) {
  44. if (this._toolbars.hasOwnProperty(toolbarId)) {
  45. toolbarContainer = this._toolbars[toolbarId].addToolbar(map);
  46. if (toolbarContainer) {
  47. // Add class to the first toolbar to remove the margin
  48. if (!addedTopClass) {
  49. if (!L.DomUtil.hasClass(toolbarContainer, topClassName)) {
  50. L.DomUtil.addClass(toolbarContainer.childNodes[0], topClassName);
  51. }
  52. addedTopClass = true;
  53. }
  54. container.appendChild(toolbarContainer);
  55. }
  56. }
  57. }
  58. return container;
  59. },
  60. // @method onRemove(): void
  61. // Removes the toolbars from the map toolbar container
  62. onRemove: function () {
  63. for (var toolbarId in this._toolbars) {
  64. if (this._toolbars.hasOwnProperty(toolbarId)) {
  65. this._toolbars[toolbarId].removeToolbar();
  66. }
  67. }
  68. },
  69. // @method setDrawingOptions(options): void
  70. // Sets options to all toolbar instances
  71. setDrawingOptions: function (options) {
  72. for (var toolbarId in this._toolbars) {
  73. if (this._toolbars[toolbarId] instanceof L.DrawToolbar) {
  74. this._toolbars[toolbarId].setOptions(options);
  75. }
  76. }
  77. },
  78. _toolbarEnabled: function (e) {
  79. var enabledToolbar = e.target;
  80. for (var toolbarId in this._toolbars) {
  81. if (this._toolbars[toolbarId] !== enabledToolbar) {
  82. this._toolbars[toolbarId].disable();
  83. }
  84. }
  85. }
  86. });
  87. L.Map.mergeOptions({
  88. drawControlTooltips: true,
  89. drawControl: false
  90. });
  91. L.Map.addInitHook(function () {
  92. if (this.options.drawControl) {
  93. this.drawControl = new L.Control.Draw();
  94. this.addControl(this.drawControl);
  95. }
  96. });