Skip to content

Commit cd4c777

Browse files
authored
feat: add editor#removeControl (#266)
1 parent 842e836 commit cd4c777

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/editor.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,25 @@ class Editor {
6969
control.setMap(this.map);
7070
control.setEditor(this);
7171

72-
control.addEventListener('change:active', (e) => {
73-
this.activeStateChange(e.detail.control);
74-
});
72+
control.addEventListener('change:active', this.activeStateChange);
7573

7674
this.controls.push(control);
7775
}
7876

77+
/**
78+
* Remove a control from the editor
79+
* @param {ole.Control} control The control.
80+
*/
81+
removeControl(control) {
82+
control.deactivate(true);
83+
control.setMap(null);
84+
control.setEditor(null);
85+
control.removeEventListener('change:active', this.activeStateChange);
86+
87+
this.controls.remove(control);
88+
this.activeControls.remove(control);
89+
}
90+
7991
/**
8092
* Adds a service to the editor.
8193
*/
@@ -170,7 +182,8 @@ class Editor {
170182
* @param {ol.control.Control} control Control.
171183
* @private
172184
*/
173-
activeStateChange(ctrl) {
185+
activeStateChange = (e) => {
186+
const ctrl = e.detail.control;
174187
// Deactivate other controls that are not standalone
175188
if (ctrl.getActive() && ctrl.standalone) {
176189
for (let i = 0; i < this.controls.getLength(); i += 1) {
@@ -191,7 +204,7 @@ class Editor {
191204
} else {
192205
this.activeControls.remove(ctrl);
193206
}
194-
}
207+
};
195208
}
196209

197210
export default Editor;

0 commit comments

Comments
 (0)