You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by hn...@apache.org on 2020/01/23 09:06:08 UTC

[myfaces-tobago] branch master updated: tobago-dropdown: custom elements, remove jQuery

This is an automated email from the ASF dual-hosted git repository.

hnoeth pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/master by this push:
     new 12ebe7e  tobago-dropdown: custom elements, remove jQuery
12ebe7e is described below

commit 12ebe7e777c0ea016b315745f08f298384ca8e42
Author: Henning Noeth <hn...@apache.org>
AuthorDate: Thu Jan 23 10:05:56 2020 +0100

    tobago-dropdown: custom elements, remove jQuery
    
    * cleanup imports
    * fix: sometimes the dropdown menu opens on the second click
    * implement disconnectedCallback() and remove listener
    
    issue: TOBAGO-1633: TS refactoring
---
 .../src/main/npm/ts/tobago-dropdown.ts             | 42 +++++++++++++++-------
 1 file changed, 30 insertions(+), 12 deletions(-)

diff --git a/tobago-theme/tobago-theme-standard/src/main/npm/ts/tobago-dropdown.ts b/tobago-theme/tobago-theme-standard/src/main/npm/ts/tobago-dropdown.ts
index 86d18d2..f6de568 100644
--- a/tobago-theme/tobago-theme-standard/src/main/npm/ts/tobago-dropdown.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/npm/ts/tobago-dropdown.ts
@@ -15,24 +15,35 @@
  * limitations under the License.
  */
 
-import "/webjars/popper.js/1.14.3/umd/popper.js";
 import Popper from "popper.js";
 
 class Dropdown extends HTMLElement {
 
-  private blurFlag: boolean = false;
+  private _deselectComponent = this.deselectComponent.bind(this);
+  private _toggleDropdown = this.toggleDropdown.bind(this);
+  private _setCloseFlag = this.setCloseFlag.bind(this);
+  private closeFlag: boolean = false;
 
   constructor() {
     super();
   }
 
   connectedCallback(): void {
-    this.toggle.addEventListener("click", this.toggleDropdown.bind(this));
-    this.toggle.addEventListener("blur", this.deselectComponent.bind(this));
-    window.addEventListener("mouseup", this.deselectComponent.bind(this));
+    this.toggleButton.addEventListener("mouseup", this._toggleDropdown);
+    this.toggleButton.addEventListener("blur", this._setCloseFlag);
+    window.addEventListener("mouseup", this._deselectComponent);
+    //TODO add keyboard support
+  }
+
+  disconnectedCallback(): void {
+    this.toggleButton.removeEventListener("mouseup", this._toggleDropdown);
+    this.toggleButton.removeEventListener("blur", this._setCloseFlag);
+    window.removeEventListener("mouseup", this._deselectComponent);
   }
 
   toggleDropdown(event: Event): void {
+    this.resetCloseFlag();
+
     const visible: boolean = this.dropdownMenu.classList.contains("show");
     if (visible) {
       this.closeDropdown();
@@ -42,10 +53,9 @@ class Dropdown extends HTMLElement {
   }
 
   deselectComponent(event: Event): void {
-    if (event.type === "blur") {
-      this.blurFlag = true;
-    } else if (this.blurFlag) {
-      this.blurFlag = false;
+    const visible: boolean = this.dropdownMenu.classList.contains("show");
+    if (this.closeFlag && visible) {
+      this.resetCloseFlag();
 
       const target: HTMLElement = event.target as HTMLElement;
       this.closeDropdown();
@@ -56,7 +66,7 @@ class Dropdown extends HTMLElement {
   openDropdown(): void {
     if (!this.inStickyHeader()) {
       this.menuStore.appendChild(this.dropdownMenu);
-      new Popper(this.toggle, this.dropdownMenu, {
+      new Popper(this.toggleButton, this.dropdownMenu, {
         placement: "bottom-start"
       });
     }
@@ -69,8 +79,8 @@ class Dropdown extends HTMLElement {
     this.appendChild(this.dropdownMenu);
   }
 
-  private get toggle(): HTMLElement {
-    return this.querySelector(":scope > [data-toggle='dropdown']");
+  private get toggleButton(): HTMLElement {
+    return this.querySelector(":scope > button[data-toggle='dropdown']");
   }
 
   private inStickyHeader(): boolean {
@@ -87,6 +97,14 @@ class Dropdown extends HTMLElement {
     const root = this.getRootNode() as ShadowRoot | Document;
     return root.querySelector(".tobago-page-menuStore");
   }
+
+  setCloseFlag(event: Event): void {
+    this.closeFlag = true;
+  }
+
+  resetCloseFlag(): void {
+    this.closeFlag = false;
+  }
 }
 
 document.addEventListener("DOMContentLoaded", function (event: Event): void {