mirror of
https://github.com/hodgef/simple-keyboard.git
synced 2025-02-21 00:23:02 +08:00
Expand garbage collection
This commit is contained in:
parent
6fe56c3abc
commit
d92f84f5c3
@ -129,9 +129,8 @@ class SimpleKeyboard {
|
||||
if (!window["SimpleKeyboardInstances"])
|
||||
window["SimpleKeyboardInstances"] = {};
|
||||
|
||||
window["SimpleKeyboardInstances"][
|
||||
this.utilities.camelCase(this.keyboardDOMClass)
|
||||
] = this;
|
||||
this.currentInstanceName = this.utilities.camelCase(this.keyboardDOMClass);
|
||||
window["SimpleKeyboardInstances"][this.currentInstanceName] = this;
|
||||
|
||||
/**
|
||||
* Instance vars
|
||||
@ -723,22 +722,77 @@ class SimpleKeyboard {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute an operation on each button
|
||||
*/
|
||||
recurseButtons(fn) {
|
||||
if (!fn) return false;
|
||||
|
||||
Object.keys(this.buttonElements).forEach(buttonName =>
|
||||
this.buttonElements[buttonName].forEach(fn)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy keyboard listeners and DOM elements
|
||||
*/
|
||||
destroy() {
|
||||
if (this.options.debug)
|
||||
console.log(
|
||||
`Destroying simple-keyboard instance: ${this.currentInstanceName}`
|
||||
);
|
||||
|
||||
/**
|
||||
* Remove listeners
|
||||
* Remove document listeners
|
||||
*/
|
||||
document.removeEventListener("keyup", this.handleKeyUp);
|
||||
document.removeEventListener("keydown", this.handleKeyDown);
|
||||
document.removeEventListener("mouseup", this.handleMouseUp);
|
||||
document.removeEventListener("touchend", this.handleTouchEnd);
|
||||
document.onpointerup = null;
|
||||
document.ontouchend = null;
|
||||
document.ontouchcancel = null;
|
||||
document.onmouseup = null;
|
||||
|
||||
/**
|
||||
* Clear DOM
|
||||
* Remove buttons
|
||||
*/
|
||||
this.clear();
|
||||
let deleteButton = buttonElement => {
|
||||
buttonElement.onpointerdown = null;
|
||||
buttonElement.onpointerup = null;
|
||||
buttonElement.onpointercancel = null;
|
||||
buttonElement.ontouchstart = null;
|
||||
buttonElement.ontouchend = null;
|
||||
buttonElement.ontouchcancel = null;
|
||||
buttonElement.onclick = null;
|
||||
buttonElement.onmousedown = null;
|
||||
buttonElement.onmouseup = null;
|
||||
|
||||
buttonElement.remove();
|
||||
buttonElement = null;
|
||||
};
|
||||
|
||||
this.recurseButtons(deleteButton);
|
||||
|
||||
this.buttonElements = null;
|
||||
this.recurseButtons = null;
|
||||
deleteButton = null;
|
||||
|
||||
/**
|
||||
* Remove wrapper events
|
||||
*/
|
||||
this.keyboardDOM.onpointerdown = null;
|
||||
this.keyboardDOM.ontouchstart = null;
|
||||
this.keyboardDOM.onmousedown = null;
|
||||
|
||||
this.keyboardDOM.remove();
|
||||
this.keyboardDOM = null;
|
||||
|
||||
/**
|
||||
* Remove instance
|
||||
*/
|
||||
window["SimpleKeyboardInstances"][this.currentInstanceName] = null;
|
||||
delete window["SimpleKeyboardInstances"][this.currentInstanceName];
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user