mirror of
https://github.com/hodgef/simple-keyboard.git
synced 2025-02-21 00:23:02 +08:00
Multiple Instance Sync Support
This commit is contained in:
parent
7d6e933a4e
commit
1cc5b9b0ee
@ -23,6 +23,7 @@ class SimpleKeyboard {
|
||||
this.input = {};
|
||||
this.input[this.options.inputName] = '';
|
||||
this.keyboardDOMClass = keyboardDOMQuery.split('.').join("");
|
||||
this.timers = {};
|
||||
|
||||
/**
|
||||
* Rendering keyboard
|
||||
@ -31,6 +32,16 @@ class SimpleKeyboard {
|
||||
this.render();
|
||||
else
|
||||
console.error(`"${keyboardDOMQuery}" was not found in the DOM.`);
|
||||
|
||||
/**
|
||||
* Saving instance
|
||||
* This enables multiple simple-keyboard support with easier management
|
||||
*/
|
||||
if(!window['SimpleKeyboardInstances'])
|
||||
window['SimpleKeyboardInstances'] = {};
|
||||
|
||||
window['SimpleKeyboardInstances'][Utilities.camelCase(this.keyboardDOMClass)] = this;
|
||||
|
||||
}
|
||||
|
||||
handleButtonClicked = (button) => {
|
||||
@ -66,6 +77,12 @@ class SimpleKeyboard {
|
||||
if(debug)
|
||||
console.log('Input changed:', this.input);
|
||||
|
||||
/**
|
||||
* syncInstanceInputs
|
||||
*/
|
||||
if(this.options.syncInstanceInputs)
|
||||
this.syncInstanceInputs(this.input);
|
||||
|
||||
/**
|
||||
* Calling onChange
|
||||
*/
|
||||
@ -78,19 +95,48 @@ class SimpleKeyboard {
|
||||
}
|
||||
}
|
||||
|
||||
syncInstanceInputs = () => {
|
||||
this.dispatch((section) => {
|
||||
section.replaceInput(this.input);
|
||||
});
|
||||
}
|
||||
|
||||
clearInput = (inputName) => {
|
||||
inputName = inputName || this.options.inputName;
|
||||
this.input[this.options.inputName] = '';
|
||||
|
||||
/**
|
||||
* syncInstanceInputs
|
||||
*/
|
||||
if(this.options.syncInstanceInputs)
|
||||
this.syncInstanceInputs(this.input);
|
||||
}
|
||||
|
||||
getInput = (inputName) => {
|
||||
inputName = inputName || this.options.inputName;
|
||||
|
||||
/**
|
||||
* syncInstanceInputs
|
||||
*/
|
||||
if(this.options.syncInstanceInputs)
|
||||
this.syncInstanceInputs(this.input);
|
||||
|
||||
return this.input[this.options.inputName];
|
||||
}
|
||||
|
||||
setInput = (input, inputName) => {
|
||||
inputName = inputName || this.options.inputName;
|
||||
this.input[inputName] = input;
|
||||
|
||||
/**
|
||||
* syncInstanceInputs
|
||||
*/
|
||||
if(this.options.syncInstanceInputs)
|
||||
this.syncInstanceInputs(this.input);
|
||||
}
|
||||
|
||||
replaceInput = (inputObj) => {
|
||||
this.input = inputObj;
|
||||
}
|
||||
|
||||
setOptions = option => {
|
||||
@ -104,6 +150,17 @@ class SimpleKeyboard {
|
||||
this.keyboardDOM.className = this.keyboardDOMClass;
|
||||
}
|
||||
|
||||
dispatch = (callback) => {
|
||||
if(!window['SimpleKeyboardInstances']){
|
||||
console.error("SimpleKeyboardInstances is not defined. Dispatch cannot be called.")
|
||||
return false;
|
||||
}
|
||||
|
||||
return Object.keys(window['SimpleKeyboardInstances']).forEach((key) => {
|
||||
callback(window['SimpleKeyboardInstances'][key], key);
|
||||
})
|
||||
}
|
||||
|
||||
render = () => {
|
||||
/**
|
||||
* Clear keyboard
|
||||
|
Loading…
Reference in New Issue
Block a user