diff --git a/frontend/src/utils/form/auto-submit-input.js b/frontend/src/utils/form/auto-submit-input.js index f442f2960..8ec7e869e 100644 --- a/frontend/src/utils/form/auto-submit-input.js +++ b/frontend/src/utils/form/auto-submit-input.js @@ -1,5 +1,6 @@ import * as debounce from 'lodash.debounce'; import { Utility } from '../../core/utility'; +import { EventManager, EventWrapper, EVENT_TYPE } from '../../lib/event-manager/event-manager'; export const AUTO_SUBMIT_INPUT_UTIL_SELECTOR = '[uw-auto-submit-input]'; @@ -12,6 +13,8 @@ export class AutoSubmitInput { _element; + _eventManager; + _form; _debouncedHandler; @@ -22,6 +25,8 @@ export class AutoSubmitInput { this._element = element; + this._eventManager = new EventManager(); + if (this._element.classList.contains(AUTO_SUBMIT_INPUT_INITIALIZED_CLASS)) { return false; } @@ -33,12 +38,16 @@ export class AutoSubmitInput { this._debouncedHandler = debounce(this.autoSubmit, 500); - this._element.addEventListener('input', this._debouncedHandler); + const inputEvent = new EventWrapper(EVENT_TYPE.INPUT, this._debouncedHandler.bind(this), this._element); + this._eventManager.registerNewListener(inputEvent); + this._element.classList.add(AUTO_SUBMIT_INPUT_INITIALIZED_CLASS); } destroy() { - this._element.removeEventListener('input', this._debouncedHandler); + this._eventManager.removeAllEventListenersFromUtil(); + if(this._element.classList.contains(AUTO_SUBMIT_INPUT_INITIALIZED_CLASS)) + this._element.classList.remove(AUTO_SUBMIT_INPUT_INITIALIZED_CLASS); } autoSubmit = () => { diff --git a/frontend/src/utils/form/auto-submit-input.spec.js b/frontend/src/utils/form/auto-submit-input.spec.js new file mode 100644 index 000000000..26c59cdd2 --- /dev/null +++ b/frontend/src/utils/form/auto-submit-input.spec.js @@ -0,0 +1,30 @@ +import { AutoSubmitInput, AUTO_SUBMIT_INPUT_INITIALIZED_CLASS } from './auto-submit-input.js'; + +describe('Auto-submit-input', () => { + + let autoSubmitInput; + + beforeEach(() => { + const form = document.createElement('form'); + const element = document.createElement('input'); + element.setAttribute('type', 'text'); + form.append(element); + autoSubmitInput = new AutoSubmitInput(element); + }); + + it('should create', () => { + expect(autoSubmitInput).toBeTruthy(); + }); + + it('should destory auto-submit-button', () => { + autoSubmitInput.destroy(); + expect(autoSubmitInput._eventManager._registeredListeners.length).toBe(0); + expect(autoSubmitInput._element.classList).not.toEqual(jasmine.arrayContaining([AUTO_SUBMIT_INPUT_INITIALIZED_CLASS])); + }); + + it('should throw if called without an element', () => { + expect(() => { + new AutoSubmitInput(); + }).toThrow(); + }); + }); \ No newline at end of file