Source: editor.js

  1. import Collection from 'ol/Collection';
  2. import Toolbar from './control/toolbar';
  3. /**
  4. * Core component of OLE.
  5. * All controls are added to this class.
  6. */
  7. class Editor {
  8. /**
  9. * Initialization of the editor.
  10. * @param {ol.Map} map The map object.
  11. * @param {Object} [options] Editor options.
  12. * @param {Boolean} [options.showToolbar] Whether to show the toolbar.
  13. * @param {HTMLElement} [options.target] Specify a target if you want
  14. * the toolbar to be rendered outside of the map's viewport.
  15. */
  16. constructor(map, opts) {
  17. /**
  18. * @private
  19. * @type {ol.Map}
  20. */
  21. this.map = map;
  22. /**
  23. * @private
  24. * @type {ol.Collection<ole.Control>}
  25. */
  26. this.controls = new Collection();
  27. /**
  28. * @private
  29. * @type {ol.Collection<ole.Control>}
  30. */
  31. this.activeControls = new Collection();
  32. /**
  33. * @private
  34. * @type {ol.Collection<ole.Service>}
  35. */
  36. this.services = new Collection();
  37. /**
  38. * @private
  39. * @type {Object}
  40. */
  41. this.options = opts || {};
  42. /**
  43. * Feature that is currently edited.
  44. * @private
  45. * @type {ol.Feature}
  46. */
  47. this.editFeature = null;
  48. if (typeof this.options.showToolbar === 'undefined') {
  49. this.options.showToolbar = true;
  50. }
  51. if (this.options.showToolbar) {
  52. this.toolbar = new Toolbar(this.map, this.controls, this.options.target);
  53. }
  54. this.activeStateChange = this.activeStateChange.bind(this);
  55. }
  56. /**
  57. * Adds a new control to the editor.
  58. * @param {ole.Control} control The control.
  59. */
  60. addControl(control) {
  61. control.setMap(this.map);
  62. control.setEditor(this);
  63. control.addEventListener('change:active', this.activeStateChange);
  64. this.controls.push(control);
  65. }
  66. /**
  67. * Remove a control from the editor
  68. * @param {ole.Control} control The control.
  69. */
  70. removeControl(control) {
  71. control.deactivate();
  72. this.controls.remove(control);
  73. control.removeEventListener('change:active', this.activeStateChange);
  74. control.setEditor();
  75. control.setMap();
  76. }
  77. /**
  78. * Adds a service to the editor.
  79. */
  80. addService(service) {
  81. service.setMap(this.map);
  82. service.setEditor(this);
  83. service.activate();
  84. this.services.push(service);
  85. }
  86. /**
  87. * Adds a collection of controls to the editor.
  88. * @param {ol.Collection<ole.Control>} controls Collection of controls.
  89. */
  90. addControls(controls) {
  91. const ctrls =
  92. controls instanceof Collection ? controls : new Collection(controls);
  93. for (let i = 0; i < ctrls.getLength(); i += 1) {
  94. this.addControl(ctrls.item(i));
  95. }
  96. }
  97. /**
  98. * Removes the editor from the map.
  99. */
  100. remove() {
  101. const controls = [...this.controls.getArray()];
  102. controls.forEach((control) => {
  103. this.removeControl(control);
  104. });
  105. if (this.toolbar) {
  106. this.toolbar.destroy();
  107. }
  108. }
  109. /**
  110. * Returns a list of ctive controls.
  111. * @returns {ol.Collection.<ole.Control>} Active controls.
  112. */
  113. getControls() {
  114. return this.controls;
  115. }
  116. /**
  117. * Returns a list of active controls.
  118. * @returns {ol.Collection.<ole.Control>} Active controls.
  119. */
  120. getActiveControls() {
  121. return this.activeControls;
  122. }
  123. /**
  124. * Sets an instance of the feature that is edited.
  125. * Some controls need information about the feature
  126. * that is currently edited (e.g. for not snapping on them).
  127. * @param {ol.Feature|null} feature The editfeature (or null if none)
  128. * @protected
  129. */
  130. setEditFeature(feature) {
  131. this.editFeature = feature;
  132. }
  133. /**
  134. * Returns the feature that is currently edited.
  135. * @returns {ol.Feature|null} The edit feature.
  136. */
  137. getEditFeature() {
  138. return this.editFeature;
  139. }
  140. /**
  141. * Sets an instance of the feature that is being drawn.
  142. * Some controls need information about the feature
  143. * that is currently being drawn (e.g. for not snapping on them).
  144. * @param {ol.Feature|null} feature The drawFeature (or null if none).
  145. * @protected
  146. */
  147. setDrawFeature(feature) {
  148. this.drawFeature = feature;
  149. }
  150. /**
  151. * Returns the feature that is currently being drawn.
  152. * @returns {ol.Feature|null} The drawFeature.
  153. */
  154. getDrawFeature() {
  155. return this.drawFeature;
  156. }
  157. /**
  158. * Controls use this function for triggering activity state changes.
  159. * @param {ol.control.Control} control Control.
  160. * @private
  161. */
  162. activeStateChange(evt) {
  163. const ctrl = evt.detail.control;
  164. // Deactivate other controls that are not standalone
  165. if (ctrl.getActive() && ctrl.standalone) {
  166. for (let i = 0; i < this.controls.getLength(); i += 1) {
  167. const otherCtrl = this.controls.item(i);
  168. if (
  169. otherCtrl !== ctrl &&
  170. otherCtrl.getActive() &&
  171. otherCtrl.standalone
  172. ) {
  173. otherCtrl.deactivate();
  174. this.activeControls.remove(otherCtrl);
  175. }
  176. }
  177. }
  178. if (ctrl.getActive()) {
  179. this.activeControls.push(ctrl);
  180. } else {
  181. this.activeControls.remove(ctrl);
  182. }
  183. }
  184. }
  185. export default Editor;