mirror of
https://github.com/hodgef/simple-keyboard.git
synced 2025-04-25 22:52:31 +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.input[this.options.inputName] = '';
|
this.input[this.options.inputName] = '';
|
||||||
this.keyboardDOMClass = keyboardDOMQuery.split('.').join("");
|
this.keyboardDOMClass = keyboardDOMQuery.split('.').join("");
|
||||||
|
this.timers = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rendering keyboard
|
* Rendering keyboard
|
||||||
@ -31,6 +32,16 @@ class SimpleKeyboard {
|
|||||||
this.render();
|
this.render();
|
||||||
else
|
else
|
||||||
console.error(`"${keyboardDOMQuery}" was not found in the DOM.`);
|
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) => {
|
handleButtonClicked = (button) => {
|
||||||
@ -66,6 +77,12 @@ class SimpleKeyboard {
|
|||||||
if(debug)
|
if(debug)
|
||||||
console.log('Input changed:', this.input);
|
console.log('Input changed:', this.input);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* syncInstanceInputs
|
||||||
|
*/
|
||||||
|
if(this.options.syncInstanceInputs)
|
||||||
|
this.syncInstanceInputs(this.input);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calling onChange
|
* Calling onChange
|
||||||
*/
|
*/
|
||||||
@ -78,19 +95,48 @@ class SimpleKeyboard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syncInstanceInputs = () => {
|
||||||
|
this.dispatch((section) => {
|
||||||
|
section.replaceInput(this.input);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
clearInput = (inputName) => {
|
clearInput = (inputName) => {
|
||||||
inputName = inputName || this.options.inputName;
|
inputName = inputName || this.options.inputName;
|
||||||
this.input[this.options.inputName] = '';
|
this.input[this.options.inputName] = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* syncInstanceInputs
|
||||||
|
*/
|
||||||
|
if(this.options.syncInstanceInputs)
|
||||||
|
this.syncInstanceInputs(this.input);
|
||||||
}
|
}
|
||||||
|
|
||||||
getInput = (inputName) => {
|
getInput = (inputName) => {
|
||||||
inputName = inputName || this.options.inputName;
|
inputName = inputName || this.options.inputName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* syncInstanceInputs
|
||||||
|
*/
|
||||||
|
if(this.options.syncInstanceInputs)
|
||||||
|
this.syncInstanceInputs(this.input);
|
||||||
|
|
||||||
return this.input[this.options.inputName];
|
return this.input[this.options.inputName];
|
||||||
}
|
}
|
||||||
|
|
||||||
setInput = (input, inputName) => {
|
setInput = (input, inputName) => {
|
||||||
inputName = inputName || this.options.inputName;
|
inputName = inputName || this.options.inputName;
|
||||||
this.input[inputName] = input;
|
this.input[inputName] = input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* syncInstanceInputs
|
||||||
|
*/
|
||||||
|
if(this.options.syncInstanceInputs)
|
||||||
|
this.syncInstanceInputs(this.input);
|
||||||
|
}
|
||||||
|
|
||||||
|
replaceInput = (inputObj) => {
|
||||||
|
this.input = inputObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
setOptions = option => {
|
setOptions = option => {
|
||||||
@ -104,6 +150,17 @@ class SimpleKeyboard {
|
|||||||
this.keyboardDOM.className = this.keyboardDOMClass;
|
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 = () => {
|
render = () => {
|
||||||
/**
|
/**
|
||||||
* Clear keyboard
|
* Clear keyboard
|
||||||
|
Loading…
x
Reference in New Issue
Block a user