From f5c8c26c511a84e0310e49c39d96b168f2abc352 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 21 Aug 2023 19:26:30 -0700 Subject: [PATCH] Revert "Replace hetero-list YUI button and menu with new style button and tippy.js menu (#8340)" This reverts commit c1a6c6c9ddf432e8eae3db68d3ee13c1f762d785. --- .../filter-menu-button/filter-menu-button.js | 70 +++++ .../main/resources/lib/form/hetero-list.jelly | 5 +- .../lib/form/hetero-list/hetero-list.js | 201 +++++++++++++ .../test/java/lib/form/HeteroListTest.java | 8 +- .../js/components/dropdowns/hetero-list.js | 276 ------------------ war/src/main/js/components/dropdowns/index.js | 2 - .../main/js/components/dropdowns/templates.js | 9 +- war/src/main/js/components/dropdowns/utils.js | 9 +- war/src/main/js/util/keyboard.js | 50 +--- war/src/main/js/util/symbols.js | 2 - war/src/main/scss/modules/buttons.scss | 3 +- war/src/main/scss/modules/dropdowns.scss | 46 +-- 12 files changed, 292 insertions(+), 389 deletions(-) create mode 100644 core/src/main/resources/lib/form/filter-menu-button/filter-menu-button.js create mode 100644 core/src/main/resources/lib/form/hetero-list/hetero-list.js delete mode 100644 war/src/main/js/components/dropdowns/hetero-list.js diff --git a/core/src/main/resources/lib/form/filter-menu-button/filter-menu-button.js b/core/src/main/resources/lib/form/filter-menu-button/filter-menu-button.js new file mode 100644 index 000000000000..e81ccc43fe7a --- /dev/null +++ b/core/src/main/resources/lib/form/filter-menu-button/filter-menu-button.js @@ -0,0 +1,70 @@ +// prettier-ignore +window.createFilterMenuButton = function ( + button, + menu, + menuAlignment, + menuMinScrollHeight +) { + var MIN_NUM_OPTIONS = 5; + var menuButton = new YAHOO.widget.Button(button, { + type: "menu", + menu: menu, + menualignment: menuAlignment, + menuminscrollheight: menuMinScrollHeight, + }); + + var filter = _createFilterMenuButton(menuButton._menu); + + menuButton._menu.element.appendChild(filter); + menuButton._menu.showEvent.subscribe(function () { + _applyFilterKeyword(menuButton._menu, filter.firstElementChild); + filter.style.display = + _getItemList(menuButton._menu).children.length >= MIN_NUM_OPTIONS + ? "" + : "NONE"; + }); + menuButton._menu.setInitialFocus = function () { + setTimeout(function () { + filter.firstElementChild.focus(); + }, 0); + }; + + return menuButton; +}; + +function _createFilterMenuButton(menu) { + const filterInput = document.createElement("input"); + filterInput.classList.add("jenkins-input"); + filterInput.setAttribute("placeholder", "Filter"); + filterInput.setAttribute("spellcheck", "false"); + filterInput.setAttribute("type", "search"); + + filterInput.addEventListener("input", (event) => + _applyFilterKeyword(menu, event.currentTarget), + ); + filterInput.addEventListener("keypress", (event) => { + if (event.key === "Enter") { + event.preventDefault(); + } + }); + + const filterContainer = document.createElement("div"); + filterContainer.appendChild(filterInput); + + return filterContainer; +} + +function _applyFilterKeyword(menu, filterInput) { + const filterKeyword = (filterInput.value || "").toLowerCase(); + const itemList = _getItemList(menu); + let item, match; + for (item of itemList.children) { + match = item.innerText.toLowerCase().includes(filterKeyword); + item.style.display = match ? "" : "NONE"; + } + menu.align(); +} + +function _getItemList(menu) { + return menu.body.children[0]; +} diff --git a/core/src/main/resources/lib/form/hetero-list.jelly b/core/src/main/resources/lib/form/hetero-list.jelly index b817c936c4e8..66fe2dd91773 100644 --- a/core/src/main/resources/lib/form/hetero-list.jelly +++ b/core/src/main/resources/lib/form/hetero-list.jelly @@ -107,6 +107,8 @@ THE SOFTWARE. + +
@@ -154,8 +156,7 @@ THE SOFTWARE.
- +
diff --git a/core/src/main/resources/lib/form/hetero-list/hetero-list.js b/core/src/main/resources/lib/form/hetero-list/hetero-list.js new file mode 100644 index 000000000000..a51423bf6e0b --- /dev/null +++ b/core/src/main/resources/lib/form/hetero-list/hetero-list.js @@ -0,0 +1,201 @@ +// @include lib.form.filter-menu-button.filter-menu-button + +// do the ones that extract innerHTML so that they can get their original HTML before +// other behavior rules change them (like YUI buttons.) +Behaviour.specify( + "DIV.hetero-list-container", + "hetero-list", + -100, + function (e) { + if (isInsideRemovable(e)) { + return; + } + + // components for the add button + var menu = document.createElement("SELECT"); + // In case nested content also uses hetero-list + var btn = Array.from(e.querySelectorAll("INPUT.hetero-list-add")).pop(); + if (!btn) { + return; + } + YAHOO.util.Dom.insertAfter(menu, btn); + + var prototypes = e.lastElementChild; + while (!prototypes.classList.contains("prototypes")) { + prototypes = prototypes.previousElementSibling; + } + var insertionPoint = prototypes.previousElementSibling; // this is where the new item is inserted. + + // extract templates + var templates = []; + var children = prototypes.children; + for (var i = 0; i < children.length; i++) { + var n = children[i]; + var name = n.getAttribute("name"); + var tooltip = n.getAttribute("tooltip"); + var descriptorId = n.getAttribute("descriptorId"); + // YUI Menu interprets this