Fix cleanup of event listeners in .destroy() and .removeFolder().

This commit is contained in:
Don McCurdy 2018-02-16 20:00:24 -08:00
parent ae3fbe213d
commit 39b3ba2d2a
2 changed files with 33 additions and 8 deletions

3
API.md
View File

@ -208,7 +208,8 @@ Removes the given controller from the GUI.
<a name="GUI+destroy"></a> <a name="GUI+destroy"></a>
### gui.destroy() ### gui.destroy()
Removes the GUI from the document and unbinds all event listeners. Removes the root GUI from the document and unbinds all event listeners.
For subfolders, use `gui.removeFolder(folder)` instead.
**Kind**: instance method of [<code>GUI</code>](#GUI) **Kind**: instance method of [<code>GUI</code>](#GUI)
<a name="GUI+addFolder"></a> <a name="GUI+addFolder"></a>

View File

@ -570,20 +570,30 @@ common.extend(
}, },
/** /**
* Removes the GUI from the document and unbinds all event listeners. * Removes the root GUI from the document and unbinds all event listeners.
* For subfolders, use `gui.removeFolder(folder)` instead.
* @instance * @instance
*/ */
destroy: function() { destroy: function() {
if (this.parent) {
throw new Error(
'Only the root GUI should be removed with .destroy(). ' +
'For subfolders, use gui.removeFolder(folder) instead.'
);
}
if (this.autoPlace) { if (this.autoPlace) {
autoPlaceContainer.removeChild(this.domElement); autoPlaceContainer.removeChild(this.domElement);
} }
dom.unbind(window, 'keydown', GUI._keydownHandler, false); const _this = this;
dom.unbind(window, 'resize', this.__resizeHandler); common.each(this.__folders, function(subfolder) {
_this.removeFolder(subfolder);
});
if (this.saveToLocalStorageIfPossible) { dom.unbind(window, 'keydown', GUI._keydownHandler, false);
dom.unbind(window, 'unload', this.saveToLocalStorageIfPossible);
} removeListeners(this);
}, },
/** /**
@ -645,7 +655,14 @@ common.extend(
delete this.load.folders[folder.name]; delete this.load.folders[folder.name];
} }
removeListeners(folder);
const _this = this; const _this = this;
common.each(folder.__folders, function(subfolder) {
folder.removeFolder(subfolder);
});
common.defer(function() { common.defer(function() {
_this.onResize(); _this.onResize();
}); });
@ -868,10 +885,17 @@ function addRow(gui, newDom, liBefore) {
return li; return li;
} }
function removeListeners(gui) {
dom.unbind(window, 'resize', gui.__resizeHandler);
if (gui.saveToLocalStorageIfPossible) {
dom.unbind(window, 'unload', gui.saveToLocalStorageIfPossible);
}
}
function markPresetModified(gui, modified) { function markPresetModified(gui, modified) {
const opt = gui.__preset_select[gui.__preset_select.selectedIndex]; const opt = gui.__preset_select[gui.__preset_select.selectedIndex];
// console.log('mark', modified, opt);
if (modified) { if (modified) {
opt.innerHTML = opt.value + '*'; opt.innerHTML = opt.value + '*';
} else { } else {