From ae1436a52d80ad3cdbcb0ff18744984e85847eeb Mon Sep 17 00:00:00 2001 From: Francisco Hodge Date: Wed, 6 Mar 2019 20:22:09 -0500 Subject: [PATCH] Reinstating method bindings --- src/lib/components/Keyboard.js | 28 +--------------------------- src/lib/services/Utilities.js | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/lib/components/Keyboard.js b/src/lib/components/Keyboard.js index 0afbf1e8..853e1c5a 100644 --- a/src/lib/components/Keyboard.js +++ b/src/lib/components/Keyboard.js @@ -79,33 +79,7 @@ class SimpleKeyboard { /** * Bindings */ - this.handleButtonClicked = this.handleButtonClicked.bind(this); - this.syncInstanceInputs = this.syncInstanceInputs.bind(this); - this.clearInput = this.clearInput.bind(this); - this.getInput = this.getInput.bind(this); - this.setInput = this.setInput.bind(this); - this.replaceInput = this.replaceInput.bind(this); - this.clear = this.clear.bind(this); - this.dispatch = this.dispatch.bind(this); - this.addButtonTheme = this.addButtonTheme.bind(this); - this.removeButtonTheme = this.removeButtonTheme.bind(this); - this.getButtonElement = this.getButtonElement.bind(this); - this.handleCaret = this.handleCaret.bind(this); - this.caretEventHandler = this.caretEventHandler.bind(this); - this.onInit = this.onInit.bind(this); - this.onRender = this.onRender.bind(this); - this.render = this.render.bind(this); - this.loadModules = this.loadModules.bind(this); - this.handleButtonMouseUp = this.handleButtonMouseUp.bind(this); - this.handleButtonMouseDown = this.handleButtonMouseDown.bind(this); - this.handleButtonHold = this.handleButtonHold.bind(this); - this.onModulesLoaded = this.onModulesLoaded.bind(this); - this.inputPatternIsValid = this.inputPatternIsValid.bind(this); - this.beforeFirstRender = this.beforeFirstRender.bind(this); - this.beforeRender = this.beforeRender.bind(this); - this.disableContextualWindow = this.disableContextualWindow.bind(this); - this.onTouchDeviceDetected = this.onTouchDeviceDetected.bind(this); - this.processAutoTouchEvents = this.processAutoTouchEvents.bind(this); + Utilities.bindMethods(SimpleKeyboard, this); /** * simple-keyboard uses a non-persistent internal input to keep track of the entered string (the variable `keyboard.input`). diff --git a/src/lib/services/Utilities.js b/src/lib/services/Utilities.js index b9559a4e..e1160d38 100644 --- a/src/lib/services/Utilities.js +++ b/src/lib/services/Utilities.js @@ -14,15 +14,7 @@ class Utilities { /** * Bindings */ - this.getButtonClass = this.getButtonClass.bind(this); - this.getButtonDisplayName = this.getButtonDisplayName.bind(this); - this.getUpdatedInput = this.getUpdatedInput.bind(this); - this.updateCaretPos = this.updateCaretPos.bind(this); - this.updateCaretPosAction = this.updateCaretPosAction.bind(this); - this.isMaxLengthReached = this.isMaxLengthReached.bind(this); - this.camelCase = this.camelCase.bind(this); - this.countInArray = this.countInArray.bind(this); - this.isTouchDevice = this.isTouchDevice.bind(this); + Utilities.bindMethods(Utilities, this); } /** @@ -380,6 +372,20 @@ class Utilities { return "ontouchstart" in window || navigator.maxTouchPoints; } + /** + * Bind all methods in a given class + */ + + static bindMethods(myClass, instance) { + for (let myMethod of Object.getOwnPropertyNames(myClass.prototype)) { + let excludeMethod = + myMethod === "constructor" || myMethod === "bindMethods"; + if (!excludeMethod) { + instance[myMethod] = instance[myMethod].bind(instance); + } + } + } + /** * Transforms an arbitrary string to camelCase *