chore(show-hide): implemented destroy

This commit is contained in:
Johannes Eder 2021-07-26 18:20:46 +02:00 committed by Sarah Vaupel
parent 03b6e199f1
commit d18f822ca5
2 changed files with 14 additions and 7 deletions

View File

@ -10,6 +10,7 @@ export const EVENT_TYPE = {
INPUT : 'input',
FOCUS_OUT : 'focusout',
BEFOREUNLOAD : 'beforeunload',
HASH_CHANGE : 'hashchange',
};

View File

@ -1,5 +1,6 @@
import { Utility } from '../../core/utility';
import { StorageManager, LOCATION } from '../../lib/storage-manager/storage-manager';
import { EventManager, EventWrapper, EVENT_TYPE } from '../../lib/event-manager/event-manager';
import './show-hide.sass';
const SHOW_HIDE_LOCAL_STORAGE_KEY = 'SHOW_HIDE';
@ -16,6 +17,7 @@ export class ShowHide {
_showHideId;
_element;
_eventManager;
_storageManager = new StorageManager(SHOW_HIDE_LOCAL_STORAGE_KEY, '1.0.0', { location: LOCATION.LOCAL });
constructor(element) {
@ -24,13 +26,15 @@ export class ShowHide {
}
this._element = element;
this._eventManager = new EventManager();
if (this._element.classList.contains(SHOW_HIDE_INITIALIZED_CLASS)) {
return false;
}
// register click listener
this._addClickListener();
const clickEv = new EventWrapper(EVENT_TYPE.CLICK, this._clickHandler.bind(this), this._element);
this._eventManager.registerNewListener(clickEv);
// param showHideId
if (this._element.dataset.showHideId) {
@ -58,17 +62,19 @@ export class ShowHide {
}
this._checkHash();
window.addEventListener('hashchange', this._checkHash.bind(this));
const hashChangeEv = new EventWrapper(EVENT_TYPE.HASH_CHANGE, this._checkHash.bind(this), window);
this._eventManager.registerNewListener(hashChangeEv);
// mark as initialized
this._element.classList.add(SHOW_HIDE_INITIALIZED_CLASS, SHOW_HIDE_TOGGLE_CLASS);
}
destroy() {}
_addClickListener() {
this._element.addEventListener('click', this._clickHandler.bind(this));
destroy() {
this._eventManager.cleanUp();
this._storageManager.clear();
if(this._element.parentElement.contains(SHOW_HIDE_COLLAPSED_CLASS))
this._element.parentElement.classList.remove(SHOW_HIDE_COLLAPSED_CLASS);
this._element.classList.remove(SHOW_HIDE_INITIALIZED_CLASS, SHOW_HIDE_TOGGLE_CLASS);
}
_show() {