From eb1ec9574929e9479bb7f1e737b61fe46593ed68 Mon Sep 17 00:00:00 2001 From: Aljaz Grilc Date: Thu, 28 Mar 2024 14:08:44 +0100 Subject: [PATCH] Implement new popup for service unregistration. The popup not only serves the immediate purpose of unregistering services but is also designed with future enhancements in mind. It can be used as a display element for showcasing all errors present in the current host element in upcoming versions of besService. --- images/turn-off-svgrepo-com.svg | 7 ++ service.js | 142 +++++++++++++++++++++++++++++++- 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 images/turn-off-svgrepo-com.svg diff --git a/images/turn-off-svgrepo-com.svg b/images/turn-off-svgrepo-com.svg new file mode 100644 index 0000000..6c31e9c --- /dev/null +++ b/images/turn-off-svgrepo-com.svg @@ -0,0 +1,7 @@ + + + + + + 1105 + \ No newline at end of file diff --git a/service.js b/service.js index dded9ca..cad462a 100644 --- a/service.js +++ b/service.js @@ -60,11 +60,13 @@ class BesService { false ) if (this.timer) clearTimeout(this.timer) - service.abortController.abort() + this.abortController.abort() besServices = besServices.filter(item => item !== this) this.hostElement.spellcheck = this.originalSpellcheck this.correctionPanel.remove() this.scrollPanel.remove() + this.statusDiv.remove() + this.statusIcon.remove() } /** @@ -223,6 +225,11 @@ class BesService { statusDiv.appendChild(statusIcon) this.setStatusDivPosition(hostElement, statusDiv) hostElement.parentNode.insertBefore(statusDiv, hostElement.nextSibling) + const statusPopup = document.createElement('bes-popup-status-el') + document.body.appendChild(statusPopup) + statusDiv.addEventListener('click', e => { + this.handleStatusClick(e, statusPopup) + }) return { correctionPanel, scrollPanel, statusDiv, statusIcon } } @@ -377,6 +384,10 @@ class BesService { this.statusIcon.classList.add(status) } + handleStatusClick(e, popup) { + popup.show(e.clientX, e.clientY, this) + } + /** * Tests if given element is block element. * @@ -1016,6 +1027,134 @@ class BesPopupEl extends HTMLElement { } } +class BesStatusPopup extends HTMLElement { + constructor() { + super() + this.attachShadow({ mode: 'open' }) + } + + render() { + this.shadowRoot.innerHTML = ` + +
+
+
Besana
+ +
+
+ + Če želite izključiti preverjanje pravopisa, kliknite na gumb. + + +
+
+ ` + } + + show(x, y, service) { + y = y + 20 + this.style.position = 'fixed' + this.style.left = `${x}px` + this.style.top = `${y}px` + this.style.display = 'block' + + const viewportWidth = window.innerWidth + const viewportHeight = window.innerHeight + const popupWidth = this.offsetWidth + const popupHeight = this.offsetHeight + const maxPositionX = viewportWidth - popupWidth + const maxPositionY = viewportHeight - popupHeight + const positionX = this.offsetLeft + const positionY = this.offsetTop + if (positionX > maxPositionX) { + this.style.left = maxPositionX + 'px' + } + if (positionY > maxPositionY) { + this.style.top = maxPositionY + 'px' + } + this.disableButton = this.shadowRoot.querySelector('.bes-service-btn') + if (service) { + this.disableButton.addEventListener('click', () => this.disable(service)) + } + this.classList.add('show') + } + + connectedCallback() { + this.render() + } + + disable(service) { + service.unregister() + BesStatusPopup.hide() + } + + static hide() { + const popup = document.querySelector('bes-popup-status-el.show') + popup?.classList?.remove('show') + } +} + window.onload = () => { document.querySelectorAll('.bes-service').forEach(hostElement => { if (hostElement.tagName === 'TEXTAREA') BesTAService.register(hostElement) @@ -1055,3 +1194,4 @@ window.onscroll = () => { } customElements.define('bes-popup-el', BesPopupEl) +customElements.define('bes-popup-status-el', BesStatusPopup)