diff --git a/src/lib/components/Keyboard.js b/src/lib/components/Keyboard.js index ac0d9544..cbf0bc61 100644 --- a/src/lib/components/Keyboard.js +++ b/src/lib/components/Keyboard.js @@ -91,6 +91,7 @@ class SimpleKeyboard { this.handleButtonMouseUp = this.handleButtonMouseUp.bind(this); this.handleButtonMouseDown = this.handleButtonMouseDown.bind(this); this.handleButtonHold = this.handleButtonHold.bind(this); + this.onModulesLoaded = this.onModulesLoaded.bind(this); /** * simple-keyboard uses a non-persistent internal input to keep track of the entered string (the variable `keyboard.input`). @@ -542,6 +543,14 @@ class SimpleKeyboard { this.options.onRender(); } + /** + * Executes the callback function once all modules have been loaded + */ + onModulesLoaded(){ + if(typeof this.options.onModulesLoaded === "function") + this.options.onModulesLoaded(); + } + /** * Register module */ @@ -566,9 +575,13 @@ class SimpleKeyboard { this.keyboardPluginClasses = this.keyboardPluginClasses + ` ${classStr}`; } - this.render(); module.init(this); }); + + this.keyboardPluginClasses = this.keyboardPluginClasses + ' modules-loaded'; + + this.render(); + this.onModulesLoaded(); } } diff --git a/src/lib/components/tests/Keyboard.test.js b/src/lib/components/tests/Keyboard.test.js index 2c108915..b2d3e598 100644 --- a/src/lib/components/tests/Keyboard.test.js +++ b/src/lib/components/tests/Keyboard.test.js @@ -838,4 +838,27 @@ it('Keyboard handleButtonMouseDown will work', () => { keyboard.getButtonElement("q").onmousedown(); document.onmouseup(); +}); + +it('Keyboard onModulesLoaded will work', () => { + testUtil.setDOM(); + + class myClass { + init = (module) => { + module.foo = "bar"; + }; + } + + let foo; + + let keyboard = new Keyboard({ + modules: [ + myClass + ], + onModulesLoaded: () => { + foo = "bar"; + } + }); + + expect(foo).toBe("bar"); }); \ No newline at end of file