Source: editor.js

  1. import Collection from 'ol/Collection';
  2. import Toolbar from './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. }
  55. /**
  56. * Adds a new control to the editor.
  57. * @param {ole.Control} control The control.
  58. */
  59. addControl(control) {
  60. control.setMap(this.map);
  61. control.setEditor(this);
  62. control.addEventListener('change:active', (e) => {
  63. this.activeStateChange(e.detail.control);
  64. });
  65. this.controls.push(control);
  66. }
  67. /**
  68. * Adds a service to the editor.
  69. */
  70. addService(service) {
  71. service.setMap(this.map);
  72. service.setEditor(this);
  73. service.activate();
  74. this.services.push(service);
  75. }
  76. /**
  77. * Adds a collection of controls to the editor.
  78. * @param {ol.Collection<ole.Control>} controls Collection of controls.
  79. */
  80. addControls(controls) {
  81. const ctrls =
  82. controls instanceof Collection ? controls : new Collection(controls);
  83. for (let i = 0; i < ctrls.getLength(); i += 1) {
  84. this.addControl(ctrls.item(i));
  85. }
  86. }
  87. /**
  88. * Removes the editor from the map.
  89. */
  90. remove() {
  91. this.controls.forEach((c) => {
  92. c.deactivate(true);
  93. });
  94. if (this.toolbar) {
  95. this.toolbar.destroy();
  96. }
  97. }
  98. /**
  99. * Returns a list of ctive controls.
  100. * @returns {ol.Collection.<ole.Control>} Active controls.
  101. */
  102. getControls() {
  103. return this.controls;
  104. }
  105. /**
  106. * Returns a list of active controls.
  107. * @returns {ol.Collection.<ole.Control>} Active controls.
  108. */
  109. getActiveControls() {
  110. return this.activeControls;
  111. }
  112. /**
  113. * Sets an instance of the feature that is edited.
  114. * Some controls need information about the feature
  115. * that is currently edited (e.g. for not snapping on them).
  116. * @param {ol.Feature|null} feature The editfeature (or null if none)
  117. * @protected
  118. */
  119. setEditFeature(feature) {
  120. this.editFeature = feature;
  121. }
  122. /**
  123. * Returns the feature that is currently edited.
  124. * @returns {ol.Feature|null} The edit feature.
  125. */
  126. getEditFeature() {
  127. return this.editFeature;
  128. }
  129. /**
  130. * Sets an instance of the feature that is being drawn.
  131. * Some controls need information about the feature
  132. * that is currently being drawn (e.g. for not snapping on them).
  133. * @param {ol.Feature|null} feature The drawFeature (or null if none).
  134. * @protected
  135. */
  136. setDrawFeature(feature) {
  137. this.drawFeature = feature;
  138. }
  139. /**
  140. * Returns the feature that is currently being drawn.
  141. * @returns {ol.Feature|null} The drawFeature.
  142. */
  143. getDrawFeature() {
  144. return this.drawFeature;
  145. }
  146. /**
  147. * Controls use this function for triggering activity state changes.
  148. * @param {ol.control.Control} control Control.
  149. * @private
  150. */
  151. activeStateChange(ctrl) {
  152. // Deactivate other controls that are not standalone
  153. if (ctrl.getActive() && ctrl.standalone) {
  154. for (let i = 0; i < this.controls.getLength(); i += 1) {
  155. const otherCtrl = this.controls.item(i);
  156. if (
  157. otherCtrl !== ctrl &&
  158. otherCtrl.getActive() &&
  159. otherCtrl.standalone
  160. ) {
  161. otherCtrl.deactivate();
  162. this.activeControls.remove(otherCtrl);
  163. }
  164. }
  165. }
  166. if (ctrl.getActive()) {
  167. this.activeControls.push(ctrl);
  168. } else {
  169. this.activeControls.remove(ctrl);
  170. }
  171. }
  172. }
  173. export default Editor;