diff options
author | Minteck <nekostarfan@gmail.com> | 2021-08-24 15:38:16 +0200 |
---|---|---|
committer | Minteck <nekostarfan@gmail.com> | 2021-08-24 15:38:16 +0200 |
commit | 529ffcbfa97ab51a64a97f6dff08aeb2bc0cc105 (patch) | |
tree | 8a50c30271b9b328cde0d907b1441f2dabdc341b /Neutron-trunk/resources/lib/pushbar.js/library.js | |
parent | 15e4724761c50b30803df1811a525c85058f70bf (diff) | |
download | electrode-529ffcbfa97ab51a64a97f6dff08aeb2bc0cc105.tar.gz electrode-529ffcbfa97ab51a64a97f6dff08aeb2bc0cc105.tar.bz2 electrode-529ffcbfa97ab51a64a97f6dff08aeb2bc0cc105.zip |
Update
Diffstat (limited to 'Neutron-trunk/resources/lib/pushbar.js/library.js')
-rw-r--r-- | Neutron-trunk/resources/lib/pushbar.js/library.js | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/Neutron-trunk/resources/lib/pushbar.js/library.js b/Neutron-trunk/resources/lib/pushbar.js/library.js new file mode 100644 index 0000000..4d50246 --- /dev/null +++ b/Neutron-trunk/resources/lib/pushbar.js/library.js @@ -0,0 +1,79 @@ +class Pushbar { + constructor(config = { overlay: true, blur: false }) { + this.activeId; + this.activeElement; + this.overlayElement; + if (config.overlay) { + this.overlayElement = document.createElement('div'); + this.overlayElement.classList.add('pushbar_overlay'); + document.querySelector('body').appendChild(this.overlayElement); + } + if (config.blur) { + const mainContent = document.querySelector('.pushbar_main_content'); + if (mainContent) { + mainContent.classList.add('pushbar_blur'); + } + } + this.bindEvents(); + } + + emitOpening() { + const event = new CustomEvent('pushbar_opening', { bubbles: true, detail: { element: this.activeElement, id: this.activeId } }); + this.activeElement.dispatchEvent(event); + } + + emitClosing() { + const event = new CustomEvent('pushbar_closing', { bubbles: true, detail: { element: this.activeElement, id: this.activeId } }); + this.activeElement.dispatchEvent(event); + } + + handleOpenEvent(e) { + e.preventDefault(); + const pushbarId = e.currentTarget.getAttribute('data-pushbar-target'); + this.open(pushbarId); + } + + handleCloseEvent(e) { + e.preventDefault(); + this.close(); + } + + handleKeyEvent(e) { + if (e.keyCode === 27) this.close(); + } + + bindEvents() { + const triggers = document.querySelectorAll('[data-pushbar-target]'); + const closers = document.querySelectorAll('[data-pushbar-close]'); + triggers.forEach(trigger => trigger.addEventListener('click', e => this.handleOpenEvent(e), false)); + closers.forEach(closer => closer.addEventListener('click', e => this.handleCloseEvent(e), false)); + if (this.overlayElement) { + this.overlayElement.addEventListener('click', e => this.handleCloseEvent(e), false); + } + document.addEventListener('keyup', e => this.handleKeyEvent(e)); + } + + open(pushbarId) { + if (this.activeId === String(pushbarId) || !pushbarId) return; + if (this.activeId && this.activeId !== String(pushbarId)) this.close(); + this.activeId = pushbarId + this.activeElement = document.querySelector(`[data-pushbar-id="${this.activeId}"]`) + if (!this.activeElement) return; + this.emitOpening(); + this.activeElement.classList.add('opened'); + const pageRootElement = document.querySelector('html') + pageRootElement.classList.add('pushbar_locked'); + pageRootElement.setAttribute('pushbar', pushbarId) + } + + close() { + if (!this.activeId) return; + this.emitClosing(); + this.activeElement.classList.remove('opened'); + const pageRootElement = document.querySelector('html') + pageRootElement.classList.remove('pushbar_locked'); + pageRootElement.removeAttribute('pushbar') + this.activeId = null; + this.activeElement = null; + } +}
\ No newline at end of file |