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 2022/11/01 13:15:39 UTC

[myfaces-tobago] 01/02: refactor(typescript): MenuStore

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

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

commit b92cfe2070bf0a4aefb2638f160fa44440593d54
Author: Henning Noeth <hn...@apache.org>
AuthorDate: Tue Nov 1 14:00:19 2022 +0100

    refactor(typescript): MenuStore
    
    Use dedicated MenuStore Typescript Class.
---
 .../src/main/ts/tobago-dropdown.ts                 |  9 +++----
 .../src/main/ts/tobago-menu-store.ts               | 29 ++++++++++++++++++++++
 .../src/main/ts/tobago-popover.ts                  |  9 +++----
 .../src/main/ts/tobago-range.ts                    |  5 ----
 .../src/main/ts/tobago-suggest.ts                  |  8 ++----
 5 files changed, 37 insertions(+), 23 deletions(-)

diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-dropdown.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-dropdown.ts
index d542a0925b..edcfbd2194 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-dropdown.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-dropdown.ts
@@ -15,6 +15,8 @@
  * limitations under the License.
  */
 
+import {MenuStore} from "./tobago-menu-store";
+
 const TobagoDropdownEvent = {
   HIDE: "tobago.dropdown.hide",
   HIDDEN: "tobago.dropdown.hidden",
@@ -46,7 +48,7 @@ class Dropdown extends HTMLElement {
     this.dispatchEvent(new CustomEvent(TobagoDropdownEvent.SHOW));
 
     if (!this.insideNavbar()) {
-      this.menuStore.appendChild(this.dropdownMenu);
+      MenuStore.appendChild(this.dropdownMenu);
     }
 
     this.dispatchEvent(new CustomEvent(TobagoDropdownEvent.SHOWN));
@@ -73,11 +75,6 @@ class Dropdown extends HTMLElement {
     const root = this.getRootNode() as ShadowRoot | Document;
     return root.querySelector(`.dropdown-menu[name='${this.id}']`);
   }
-
-  private get menuStore(): HTMLDivElement {
-    const root = this.getRootNode() as ShadowRoot | Document;
-    return root.querySelector(".tobago-page-menuStore");
-  }
 }
 
 document.addEventListener("tobago.init", function (event: Event): void {
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-menu-store.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-menu-store.ts
new file mode 100644
index 0000000000..2d988b0bf9
--- /dev/null
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-menu-store.ts
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+export class MenuStore {
+  static appendChild<T extends Node>(node: T): T {
+    return MenuStore.get().appendChild(node);
+  }
+
+  static get(): HTMLDivElement {
+    const root = document.getRootNode() as ShadowRoot | Document;
+    return root.querySelector<HTMLDivElement>(".tobago-page-menuStore");
+  }
+}
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-popover.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-popover.ts
index 18558a1947..4a1b83167b 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-popover.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-popover.ts
@@ -20,6 +20,8 @@
 // import Popover from "bootstrap/dist/js/bootstrap.bundle";
 // import {createPopper} from "@popperjs/core/dist/esm/popper";
 import {Popover} from "bootstrap";
+import {MenuStore} from "./tobago-menu-store";
+
 // import {Popover} from "bootstrap/dist/js/bootstrap.bundle";
 
 class TobagoPopover extends HTMLElement {
@@ -32,18 +34,13 @@ class TobagoPopover extends HTMLElement {
 
   connectedCallback(): void {
     this.popover = new Popover(this.trigger, {
-      container: this.menuStore
+      container: MenuStore.get()
     });
   }
 
   get trigger(): HTMLElement {
     return this;
   }
-
-  private get menuStore(): HTMLDivElement {
-    const root = this.getRootNode() as ShadowRoot | Document;
-    return root.querySelector(".tobago-page-menuStore");
-  }
 }
 
 document.addEventListener("tobago.init", function (event: Event): void {
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-range.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-range.ts
index 0b99d0471c..dff2bf8139 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-range.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-range.ts
@@ -36,11 +36,6 @@ class TobagoRange extends HTMLElement {
     return this.querySelector(".tobago-tooltip");
   }
 
-  private get menuStore(): HTMLDivElement {
-    const root = this.getRootNode() as ShadowRoot | Document;
-    return root.querySelector(".tobago-page-menuStore");
-  }
-
   private updateTooltip(): void {
     this.tooltip.textContent = this.range.value;
 
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-suggest.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-suggest.ts
index 2a55b517f5..7c6ae9a33f 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-suggest.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-suggest.ts
@@ -17,6 +17,7 @@
 
 import Autocomplete from "@trevoreyre/autocomplete-js";
 import {SuggestFilter} from "./tobago-suggest-filter";
+import {MenuStore} from "./tobago-menu-store";
 
 export class Suggest {
 
@@ -86,7 +87,7 @@ export class Suggest {
 
     new Autocomplete(this.base, options);
     if (!this.localMenu) {
-      this.menuStore.append(this.resultList);
+      MenuStore.appendChild(this.resultList);
     }
   }
 
@@ -201,11 +202,6 @@ export class Suggest {
     return this.base.dataset.position;
   }
 
-  private get menuStore(): HTMLDivElement {
-    const root = this.base.getRootNode() as ShadowRoot | Document;
-    return root.querySelector(".tobago-page-menuStore");
-  }
-
   private get update(): boolean {
     return this.suggest.getAttribute("update") !== null;
   }