You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by GitBox <gi...@apache.org> on 2022/12/02 15:57:56 UTC

[GitHub] [myfaces-tobago] github-code-scanning[bot] commented on a diff in pull request #3499: feat: selectManyList component

github-code-scanning[bot] commented on code in PR #3499:
URL: https://github.com/apache/myfaces-tobago/pull/3499#discussion_r1038287141


##########
tobago-theme/tobago-theme-standard/src/main/ts/tobago-select-many-list.ts:
##########
@@ -0,0 +1,421 @@
+/*
+ * 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.
+ */
+
+import {TobagoFilterRegistry} from "./tobago-filter-registry";
+import {createPopper, Instance} from "@popperjs/core";
+
+class SelectManyList extends HTMLElement {
+  private popper: Instance;
+
+  private readonly CssClass = {
+    DROPDOWN_MENU: "dropdown-menu",
+    SHOW: "show",
+    TABLE_ACTIVE: "table-active",
+    TABLE_PRIMARY: "table-primary",
+    TOBAGO_DISABLED: "tobago-disabled",
+    TOBAGO_FOCUS: "tobago-focus",
+    TOBAGO_MARK: "tobago-mark",
+    TOBAGO_OPTIONS: "tobago-options"
+  };
+
+  private readonly Key = {
+    ARROW_DOWN: "ArrowDown",
+    ARROW_UP: "ArrowUp",
+    ENTER: "Enter",
+    ESCAPE: "Escape",
+    SPACE: " "
+  };
+
+  constructor() {
+    super();
+  }
+
+  get hiddenSelect(): HTMLSelectElement {
+    return this.querySelector("select");
+  }
+
+  get selectField(): HTMLDivElement {
+    return this.querySelector(".tobago-select-field");
+  }
+
+  get badgeCloseButtons(): NodeListOf<HTMLButtonElement> {
+    return this.selectField.querySelectorAll("button.btn.badge");
+  }
+
+  get filter(): string {
+    return this.getAttribute("filter");
+  }
+
+  get filterInput(): HTMLInputElement {
+    return this.querySelector(".tobago-filter");
+  }
+
+  get dropdownMenu(): HTMLDivElement {
+    const root = this.getRootNode() as ShadowRoot | Document;
+    return root.querySelector(`.dropdown-menu[name='${this.id}']`);
+  }
+
+  get menuStore(): HTMLDivElement {
+    const root = this.getRootNode() as ShadowRoot | Document;
+    return root.querySelector(".tobago-page-menuStore");
+  }
+
+  get tbody(): HTMLElement {
+    const root = this.getRootNode() as ShadowRoot | Document;
+    return root.querySelector(`.tobago-options[name='${this.id}'] tbody`);
+  }
+
+  get enabledRows(): NodeListOf<HTMLTableRowElement> {
+    return this.tbody.querySelectorAll<HTMLTableRowElement>("tr:not(.tobago-disabled)");
+  }
+
+  get markedRow(): HTMLTableRowElement {
+    return this.tbody.querySelector<HTMLTableRowElement>("." + this.CssClass.TOBAGO_MARK);
+  }
+
+  connectedCallback(): void {
+    if (this.dropdownMenu) {
+      this.popper = createPopper(this.selectField, this.dropdownMenu, {});
+      window.addEventListener("resize", this.resizeEvent.bind(this));
+    }
+    document.addEventListener("click", this.clickEvent.bind(this));
+    this.filterInput.addEventListener("focus", this.focusEvent.bind(this));
+    this.filterInput.addEventListener("blur", this.blurEvent.bind(this));
+    this.selectField.addEventListener("keydown", this.keydownEvent.bind(this));
+
+    // init badges
+    this.querySelectorAll("option:checked").forEach(
+      option => this.sync(<HTMLOptionElement>option)
+    );
+
+    this.initList();
+
+    // init filter
+    if (this.filter) {
+      const input = this.filterInput;
+      input.addEventListener("keyup", this.filterEvent.bind(this));
+    }
+
+    // handle autofocus; trigger focus event
+    if (document.activeElement.id === this.filterInput.id) {
+      this.focusEvent();
+    }
+  }
+
+  select(event: MouseEvent): void {
+    const target = <HTMLElement>event.target;
+    const row = target.closest("tr");
+    this.selectRow(row);
+  }
+
+  selectRow(row: HTMLTableRowElement): void {
+    const itemValue = row.dataset.tobagoValue;
+    console.info("itemValue", itemValue);
+    const select = this.hiddenSelect;
+    const option: HTMLOptionElement = select.querySelector(`[value="${itemValue}"]`);
+    option.selected = !option.selected;
+    this.sync(option);
+  }
+
+  removeBadge(event: MouseEvent): void {
+    const target = <HTMLElement>event.target;
+    const group: HTMLElement = target.closest(".btn-group");
+    const itemValue = group.dataset.tobagoValue;
+    const select = this.hiddenSelect;
+    const option: HTMLOptionElement = select.querySelector(`[value="${itemValue}"]`);
+    option.selected = false;
+    this.sync(option);
+  }
+
+  sync(option: HTMLOptionElement) {
+    const itemValue = option.value;
+    const row: HTMLTableRowElement = this.tbody.querySelector(`[data-tobago-value="${itemValue}"]`);
+    if (option.selected) {
+      // create badge
+      const tabIndex: number = this.filterInput.tabIndex;
+      this.filterInput.insertAdjacentHTML("beforebegin",
+        this.getRowTemplate(itemValue, row.innerText, option.disabled || this.hiddenSelect.disabled, tabIndex));

Review Comment:
   ## DOM text reinterpreted as HTML
   
   [DOM text](1) is reinterpreted as HTML without escaping meta-characters.
   [DOM text](2) is reinterpreted as HTML without escaping meta-characters.
   
   [Show more details](https://github.com/apache/myfaces-tobago/security/code-scanning/41)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@myfaces.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org