EditToolbar.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*L.Map.mergeOptions({
  2. editControl: true
  3. });*/
  4. /**
  5. * @class L.EditToolbar
  6. * @aka EditToolbar
  7. */
  8. L.EditToolbar = L.Toolbar.extend({
  9. statics: {
  10. TYPE: 'edit'
  11. },
  12. options: {
  13. edit: {
  14. selectedPathOptions: {
  15. dashArray: '10, 10',
  16. fill: true,
  17. fillColor: '#fe57a1',
  18. fillOpacity: 0.1,
  19. // Whether to user the existing layers color
  20. maintainColor: false
  21. }
  22. },
  23. remove: {},
  24. poly: null,
  25. featureGroup: null /* REQUIRED! TODO: perhaps if not set then all layers on the map are selectable? */
  26. },
  27. // @method intialize(): void
  28. initialize: function (options) {
  29. // Need to set this manually since null is an acceptable value here
  30. if (options.edit) {
  31. if (typeof options.edit.selectedPathOptions === 'undefined') {
  32. options.edit.selectedPathOptions = this.options.edit.selectedPathOptions;
  33. }
  34. options.edit.selectedPathOptions = L.extend({}, this.options.edit.selectedPathOptions, options.edit.selectedPathOptions);
  35. }
  36. if (options.remove) {
  37. options.remove = L.extend({}, this.options.remove, options.remove);
  38. }
  39. if (options.poly) {
  40. options.poly = L.extend({}, this.options.poly, options.poly);
  41. }
  42. this._toolbarClass = 'leaflet-draw-edit';
  43. L.Toolbar.prototype.initialize.call(this, options);
  44. this._selectedFeatureCount = 0;
  45. },
  46. // @method getModeHandlers(): object
  47. // Get mode handlers information
  48. getModeHandlers: function (map) {
  49. var featureGroup = this.options.featureGroup;
  50. return [
  51. {
  52. enabled: this.options.edit,
  53. handler: new L.EditToolbar.Edit(map, {
  54. featureGroup: featureGroup,
  55. selectedPathOptions: this.options.edit.selectedPathOptions,
  56. poly: this.options.poly
  57. }),
  58. title: L.drawLocal.edit.toolbar.buttons.edit
  59. },
  60. {
  61. enabled: this.options.remove,
  62. handler: new L.EditToolbar.Delete(map, {
  63. featureGroup: featureGroup
  64. }),
  65. title: L.drawLocal.edit.toolbar.buttons.remove
  66. }
  67. ];
  68. },
  69. // @method getActions(): object
  70. // Get actions information
  71. getActions: function () {
  72. return [
  73. {
  74. title: L.drawLocal.edit.toolbar.actions.save.title,
  75. text: L.drawLocal.edit.toolbar.actions.save.text,
  76. callback: this._save,
  77. context: this
  78. },
  79. {
  80. title: L.drawLocal.edit.toolbar.actions.cancel.title,
  81. text: L.drawLocal.edit.toolbar.actions.cancel.text,
  82. callback: this.disable,
  83. context: this
  84. },
  85. {
  86. title: L.drawLocal.edit.toolbar.actions.clearAll.title,
  87. text: L.drawLocal.edit.toolbar.actions.clearAll.text,
  88. callback: this._clearAllLayers,
  89. context: this
  90. }
  91. ];
  92. },
  93. // @method addToolbar(map): L.DomUtil
  94. // Adds the toolbar to the map
  95. addToolbar: function (map) {
  96. var container = L.Toolbar.prototype.addToolbar.call(this, map);
  97. this._checkDisabled();
  98. this.options.featureGroup.on('layeradd layerremove', this._checkDisabled, this);
  99. return container;
  100. },
  101. // @method removeToolbar(): void
  102. // Removes the toolbar from the map
  103. removeToolbar: function () {
  104. this.options.featureGroup.off('layeradd layerremove', this._checkDisabled, this);
  105. L.Toolbar.prototype.removeToolbar.call(this);
  106. },
  107. // @method disable(): void
  108. // Disables the toolbar
  109. disable: function () {
  110. if (!this.enabled()) {
  111. return;
  112. }
  113. this._activeMode.handler.revertLayers();
  114. L.Toolbar.prototype.disable.call(this);
  115. },
  116. _save: function () {
  117. this._activeMode.handler.save();
  118. if (this._activeMode) {
  119. this._activeMode.handler.disable();
  120. }
  121. },
  122. _clearAllLayers:function(){
  123. this._activeMode.handler.removeAllLayers();
  124. if (this._activeMode) {
  125. this._activeMode.handler.disable();
  126. }
  127. },
  128. _checkDisabled: function () {
  129. var featureGroup = this.options.featureGroup,
  130. hasLayers = featureGroup.getLayers().length !== 0,
  131. button;
  132. if (this.options.edit) {
  133. button = this._modes[L.EditToolbar.Edit.TYPE].button;
  134. if (hasLayers) {
  135. L.DomUtil.removeClass(button, 'leaflet-disabled');
  136. } else {
  137. L.DomUtil.addClass(button, 'leaflet-disabled');
  138. }
  139. button.setAttribute(
  140. 'title',
  141. hasLayers ?
  142. L.drawLocal.edit.toolbar.buttons.edit
  143. : L.drawLocal.edit.toolbar.buttons.editDisabled
  144. );
  145. }
  146. if (this.options.remove) {
  147. button = this._modes[L.EditToolbar.Delete.TYPE].button;
  148. if (hasLayers) {
  149. L.DomUtil.removeClass(button, 'leaflet-disabled');
  150. } else {
  151. L.DomUtil.addClass(button, 'leaflet-disabled');
  152. }
  153. button.setAttribute(
  154. 'title',
  155. hasLayers ?
  156. L.drawLocal.edit.toolbar.buttons.remove
  157. : L.drawLocal.edit.toolbar.buttons.removeDisabled
  158. );
  159. }
  160. }
  161. });