diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index 1358a70..e1bfebf 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -213,8 +213,6 @@ class Choices { this._onTouchEnd = this._onTouchEnd.bind(this); this._onMouseDown = this._onMouseDown.bind(this); this._onMouseOver = this._onMouseOver.bind(this); - this._onPaste = this._onPaste.bind(this); - this._onInput = this._onInput.bind(this); // Monitor touch taps/scrolls this.wasTap = true; @@ -1494,10 +1492,10 @@ class Choices { this.containerOuter.element.addEventListener('blur', this._onBlur); } - this.input.element.addEventListener('input', this._onInput); - this.input.element.addEventListener('paste', this._onPaste); this.input.element.addEventListener('focus', this._onFocus); this.input.element.addEventListener('blur', this._onBlur); + + this.input.addEventListeners(); } /** @@ -1519,10 +1517,10 @@ class Choices { this.containerOuter.element.removeEventListener('blur', this._onBlur); } - this.input.element.removeEventListener('input', this._onInput); - this.input.element.removeEventListener('paste', this._onPaste); this.input.element.removeEventListener('focus', this._onFocus); this.input.element.removeEventListener('blur', this._onBlur); + + this.input.removeEventListeners(); } /** @@ -1751,17 +1749,6 @@ class Choices { this.canSearch = this.config.searchEnabled; } - /** - * Input event - * @return - * @private - */ - _onInput() { - if (!this.isSelectOneElement) { - this.input.setWidth(); - } - } - /** * Touch move event * @return @@ -1911,19 +1898,6 @@ class Choices { } } - /** - * Paste event - * @param {Object} e Event - * @return - * @private - */ - _onPaste(e) { - // Disable pasting into the input if option has been set - if (e.target === this.input.element && !this.config.paste) { - e.preventDefault(); - } - } - /** * Focus event * @param {Object} e Event diff --git a/assets/scripts/src/components/input.js b/assets/scripts/src/components/input.js index eb2a3f6..eed988b 100644 --- a/assets/scripts/src/components/input.js +++ b/assets/scripts/src/components/input.js @@ -8,6 +8,44 @@ export default class Input { this.instance = instance; this.element = element; this.classNames = classNames; + + // Bind event listeners + this.onPaste = this.onPaste.bind(this); + this.onInput = this.onInput.bind(this); + } + + addEventListeners() { + this.element.addEventListener('input', this.onInput); + this.element.addEventListener('paste', this.onPaste); + } + + removeEventListeners() { + this.element.removeEventListener('input', this.onInput); + this.element.removeEventListener('paste', this.onPaste); + } + + /** + * Input event + * @return + * @private + */ + onInput() { + if (!this.instance.isSelectOneElement) { + this.setWidth(); + } + } + + /** + * Paste event + * @param {Object} e Event + * @return + * @private + */ + onPaste(e) { + // Disable pasting into the input if option has been set + if (e.target === this.element && !this.instance.config.paste) { + e.preventDefault(); + } } /**