You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2021/02/18 17:08:41 UTC
[myfaces-tobago] branch master updated: fix: suggest
This is an automated email from the ASF dual-hosted git repository.
lofwyr 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 c9b0f17 fix: suggest
c9b0f17 is described below
commit c9b0f17ccd7fb1d796563e972303911d46551252
Author: Henning Noeth <hn...@apache.org>
AuthorDate: Tue Feb 9 11:45:57 2021 +0100
fix: suggest
* cleanup dependency/import
* move getter/setter to the bottom
* use contains instead of startWith for query
* initialize suggest in tobago-in, because it would be reinitialized after ajax request
* impl features
** update
** delay
** maximumItems
** minimumCharacters
** localMenu
* result list is now appended to the menu store by default
* styling for "wait-spinner"
* improve result list styling (now more similar to bootstrap dropdown)
---
tobago-core/npm/scss/_tobago.scss | 160 ++++++----------
.../tobago-theme-standard/npm/package-lock.json | 5 -
.../tobago-theme-standard/npm/package.json | 1 -
.../tobago-theme-standard/npm/ts/tobago-in.ts | 6 +-
.../tobago-theme-standard/npm/ts/tobago-suggest.ts | 209 ++++++++++++++++-----
5 files changed, 225 insertions(+), 156 deletions(-)
diff --git a/tobago-core/npm/scss/_tobago.scss b/tobago-core/npm/scss/_tobago.scss
index 7394666..71f43cd 100644
--- a/tobago-core/npm/scss/_tobago.scss
+++ b/tobago-core/npm/scss/_tobago.scss
@@ -476,6 +476,36 @@ tobago-in {
}
}
}
+
+ &.autocomplete[data-loading=true] .autocomplete-pseudo-container {
+ position: absolute;
+
+ &:after {
+ content: "";
+ border: 3px solid rgba(0, 0, 0, .12);
+ border-right-color: rgba(0, 0, 0, .48);
+ border-radius: 100%;
+ width: 20px;
+ height: 20px;
+ position: absolute;
+ z-index: 5;
+ transform: translateY(-50%);
+ animation: rotate 1s linear infinite
+ }
+ }
+
+ tobago-suggest {
+ display: none;
+ }
+}
+
+@keyframes rotate {
+ 0% {
+ transform: translateY(-50%) rotate(0deg)
+ }
+ to {
+ transform: translateY(-50%) rotate(359deg)
+ }
}
.tobago-in {
@@ -493,6 +523,30 @@ tobago-in {
.tobago-input-group-outer {
}
+.autocomplete-result-list {
+ padding: $dropdown-padding-y $dropdown-padding-x;
+ list-style: none;
+ background-color: $dropdown-bg;
+ border: $dropdown-border-width solid $dropdown-border-color;
+ @include border-radius($dropdown-border-radius);
+ @include box-shadow($dropdown-box-shadow);
+ overflow-y: auto;
+
+ .autocomplete-result {
+ padding: $dropdown-item-padding-y $dropdown-item-padding-x;
+
+ &:hover, &:focus, &[aria-selected=true] {
+ color: $dropdown-link-hover-color;
+ @include gradient-bg($dropdown-link-hover-bg);
+ }
+
+ &:active {
+ color: $dropdown-link-active-color;
+ @include gradient-bg($dropdown-link-active-bg);
+ }
+ }
+}
+
/* label ----------------------------------------------------------- */
// todo remove
@@ -1346,112 +1400,6 @@ tobago-split-layout {
z-index: 10000; /* TODO: check, if needed (check also with iframes) */
}
-/* suggest ---------------------------------------------------------------------- */
-
-tobago-suggest {
- display: none;
-}
-
-// modified styles taken from @trevoreyre/autocomplete-js 2.2.0
-// begin
-
-.autocomplete-input {
- /*border: 1px solid #eee;*/
- /*border-radius: 8px;*/
- /*width: 100%;*/
- /*padding: 12px 12px 12px 48px;*/
- /*box-sizing: border-box;*/
- /*position: relative;*/
- /*font-size: 16px;*/
- /*line-height: 1.5;*/
- /*flex: 1;*/
- /*background-color: #eee;*/
- /*background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgZmlsbD0ibm9uZSIgc3Ryb2tlPSIjNjY2IiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCI+PGNpcmNsZSBjeD0iMTEiIGN5PSIxMSIgcj0iOCIvPjxwYXRoIGQ9Ik0yMSAyMWwtNC00Ii8+PC9zdmc+");*/
- /*background-repeat: no-repeat;*/
- /*background-position: 12px*/
-}
-
-.autocomplete-input:focus, .autocomplete-input[aria-expanded=true] {
- /*border-color: rgba(0, 0, 0, .12);*/
- /*background-color: #fff;*/
- /*outline: none;*/
- /*box-shadow: 0 2px 2px rgba(0, 0, 0, .16)*/
-}
-
-[data-position=below] .autocomplete-input[aria-expanded=true] {
- border-bottom-color: transparent;
- border-radius: 8px 8px 0 0
-}
-
-[data-position=above] .autocomplete-input[aria-expanded=true] {
- border-top-color: transparent;
- border-radius: 0 0 8px 8px;
- z-index: 2
-}
-
-.autocomplete[data-loading=true]:after {
- content: "";
- border: 3px solid rgba(0, 0, 0, .12);
- border-right-color: rgba(0, 0, 0, .48);
- border-radius: 100%;
- width: 20px;
- height: 20px;
- position: absolute;
- right: 12px;
- top: 50%;
- transform: translateY(-50%);
- animation: rotate 1s linear infinite
-}
-
-.autocomplete-result-list {
- margin: 0;
- border: 1px solid rgba(0, 0, 0, .12);
- padding: 0;
- box-sizing: border-box;
- max-height: 296px;
- overflow-y: auto;
- background: #fff;
- list-style: none;
- box-shadow: 0 2px 2px rgba(0, 0, 0, .16)
-}
-
-[data-position=below] .autocomplete-result-list {
- margin-top: -1px;
- border-top-color: transparent;
- border-radius: 0 0 8px 8px;
- padding-bottom: 8px
-}
-
-[data-position=above] .autocomplete-result-list {
- margin-bottom: -1px;
- border-bottom-color: transparent;
- border-radius: 8px 8px 0 0;
- padding-top: 8px
-}
-
-.autocomplete-result {
- cursor: default;
- padding: 12px 12px 12px 48px;
- /*background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgZmlsbD0ibm9uZSIgc3Ryb2tlPSIjY2NjIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCI+PGNpcmNsZSBjeD0iMTEiIGN5PSIxMSIgcj0iOCIvPjxwYXRoIGQ9Ik0yMSAyMWwtNC00Ii8+PC9zdmc+");*/
- /*background-repeat: no-repeat;*/
- /*background-position: 12px*/
-}
-
-.autocomplete-result:hover, .autocomplete-result[aria-selected=true] {
- background-color: rgba(0, 0, 0, .06)
-}
-
-@keyframes rotate {
- 0% {
- transform: translateY(-50%) rotate(0deg)
- }
- to {
- transform: translateY(-50%) rotate(359deg)
- }
-}
-
-// end
-
/* tab / tab-group ----------------------------------------------------------------- */
.tobago-tabGroup, .tobago-tab, .tobago-tab-content {
}
diff --git a/tobago-theme/tobago-theme-standard/npm/package-lock.json b/tobago-theme/tobago-theme-standard/npm/package-lock.json
index f83bedd..ca9f74d 100644
--- a/tobago-theme/tobago-theme-standard/npm/package-lock.json
+++ b/tobago-theme/tobago-theme-standard/npm/package-lock.json
@@ -785,11 +785,6 @@
"@sinonjs/commons": "^1.7.0"
}
},
- "@trevoreyre/autocomplete": {
- "version": "2.2.0",
- "resolved": "https://registry.npmjs.org/@trevoreyre/autocomplete/-/autocomplete-2.2.0.tgz",
- "integrity": "sha512-GMd2zgWcAK7TJl2h4med2duYjNlthao89xx1dL3PCfTeitOjztVUkmfsklaonEY5mhW6PaMF4cH/vtf8Xwi5eg=="
- },
"@trevoreyre/autocomplete-js": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/@trevoreyre/autocomplete-js/-/autocomplete-js-2.2.0.tgz",
diff --git a/tobago-theme/tobago-theme-standard/npm/package.json b/tobago-theme/tobago-theme-standard/npm/package.json
index 208598d..9cb60c1 100644
--- a/tobago-theme/tobago-theme-standard/npm/package.json
+++ b/tobago-theme/tobago-theme-standard/npm/package.json
@@ -52,7 +52,6 @@
"dev-cp": "cp -R dist/. ../../../tobago-example/tobago-example-demo/target/tobago-theme-standard/META-INF/resources/tobago/standard/$npm_package_version"
},
"dependencies": {
- "@trevoreyre/autocomplete": "^2.2.0",
"@trevoreyre/autocomplete-js": "^2.2.0",
"vanillajs-datepicker": "^1.1.2"
},
diff --git a/tobago-theme/tobago-theme-standard/npm/ts/tobago-in.ts b/tobago-theme/tobago-theme-standard/npm/ts/tobago-in.ts
index b49fde8..2752053 100644
--- a/tobago-theme/tobago-theme-standard/npm/ts/tobago-in.ts
+++ b/tobago-theme/tobago-theme-standard/npm/ts/tobago-in.ts
@@ -16,8 +16,8 @@
*/
import {Listener, Phase} from "./tobago-listener";
-import {DomUtils} from "./tobago-utils";
import {Focus} from "./tobago-focus";
+import {Suggest} from "./tobago-suggest";
export class In extends HTMLElement {
constructor() {
@@ -26,6 +26,10 @@ export class In extends HTMLElement {
connectedCallback(): void {
this.input.addEventListener("focus", Focus.setLastFocusId);
+ if (this.querySelector("tobago-suggest")) {
+ const suggest = new Suggest(this);
+ suggest.init();
+ }
}
get input(): HTMLInputElement {
diff --git a/tobago-theme/tobago-theme-standard/npm/ts/tobago-suggest.ts b/tobago-theme/tobago-theme-standard/npm/ts/tobago-suggest.ts
index 7b3b00b..f934f28 100644
--- a/tobago-theme/tobago-theme-standard/npm/ts/tobago-suggest.ts
+++ b/tobago-theme/tobago-theme-standard/npm/ts/tobago-suggest.ts
@@ -15,72 +15,195 @@
* limitations under the License.
*/
-import Autocomplete from "@trevoreyre/autocomplete-js/Autocomplete.js";
+import Autocomplete from "@trevoreyre/autocomplete-js";
-export class Suggest extends HTMLElement {
+export class Suggest {
autocomplete: Autocomplete;
+ tobagoIn: HTMLElement;
+ resolve: any;
- get hiddenInput(): HTMLInputElement {
- return this.querySelector(":scope > input[type=hidden]");
+ constructor(tobagoIn: HTMLElement) {
+ this.tobagoIn = tobagoIn;
}
- get suggestInput(): HTMLInputElement {
- const root = this.getRootNode() as ShadowRoot | Document;
- return root.getElementById(this.for) as HTMLInputElement;
+ public init(): void {
+ if (!this.suggest) {
+ console.warn("[tobago-suggest] could not find tobago-suggest");
+ return;
+ }
+
+ this.registerAjaxListener();
+ this.base.classList.add("autocomplete");
+ this.base.insertAdjacentHTML("afterbegin", `<div class="autocomplete-pseudo-container"></div>`);
+ this.suggestInput.classList.add("autocomplete-input");
+ this.suggestInput.insertAdjacentHTML("afterend", `<ul class="autocomplete-result-list"></ul>`);
+
+ const options = {
+ search: input => {
+ console.debug("[tobago-suggest] input = '" + input + "'");
+ const minChars = this.minChars ? this.minChars : 1;
+ if (input.length < minChars) {
+ return [];
+ }
+ this.hiddenInput.value = input.toLowerCase();
+
+ this.positioningSpinner();
+
+ return new Promise(resolve => {
+ if (input.length < 1) {
+ return resolve([]);
+ }
+
+ if (this.update) {
+ this.resolve = resolve;
+ const suggestId = this.suggest.id;
+ jsf.ajax.request(suggestId, null, {
+ "javax.faces.behavior.event": "suggest",
+ execute: suggestId,
+ render: suggestId
+ });
+ } else {
+ return resolve(this.filterItems());
+ }
+ });
+ },
+ onUpdate: (results, selectedIndex) => {
+ this.positioningResultList();
+ this.setResultListMaxHeight();
+ },
+ debounceTime: this.delay
+ };
+
+ new Autocomplete(this.base, options);
+ if (!this.localMenu) {
+ this.menuStore.append(this.resultList);
+ }
}
- get base(): HTMLElement {
- return this.suggestInput.closest("tobago-in");
+ private registerAjaxListener(): void {
+ jsf.ajax.addOnEvent(this.resolvePromise.bind(this));
}
- get for(): string {
- return this.getAttribute("for");
+ private resolvePromise(event: EventData): void {
+ if (event.source === this.suggest && event.status === "success") {
+ return this.resolve(this.filterItems());
+ }
}
- set for(forValue: string) {
- this.setAttribute("for", forValue);
+ private filterItems(): string[] {
+ return this.items.filter(item => {
+ return item.toLowerCase().indexOf(this.hiddenInput.value) > -1;
+ });
}
- get items(): string[] {
- return JSON.parse(this.getAttribute("items"));
+ private positioningSpinner(): void {
+ const baseRect = this.base.getBoundingClientRect();
+ const suggestInputRect = this.suggestInput.getBoundingClientRect();
+ const suggestInputStyle = getComputedStyle(this.suggestInput);
+ this.pseudoContainer.style.left = suggestInputRect.x - baseRect.x + suggestInputRect.width
+ - parseFloat(getComputedStyle(this.pseudoContainer, ":after").width)
+ - parseFloat(suggestInputStyle.marginRight)
+ - parseFloat(suggestInputStyle.borderRight)
+ - parseFloat(suggestInputStyle.paddingRight) + "px";
+ this.pseudoContainer.style.top = suggestInputRect.y - baseRect.y + (suggestInputRect.height / 2) + "px";
}
- constructor() {
- super();
+ private positioningResultList(): void {
+ const space: number = 2;
+
+ if (this.localMenu) {
+ const parentRect = this.suggestInput.parentElement.getBoundingClientRect();
+ const suggestInputRect = this.suggestInput.getBoundingClientRect();
+ this.resultList.style.marginLeft = (suggestInputRect.x - parentRect.x) + "px";
+ this.resultList.style.maxWidth = suggestInputRect.width + "px";
+ this.resultList.style.marginTop = space + "px";
+ this.resultList.style.marginBottom = space + "px";
+ } else {
+ const suggestInputRect = this.suggestInput.getBoundingClientRect();
+ this.resultList.style.minWidth = suggestInputRect.width + "px";
+ this.resultList.style.left = suggestInputRect.left + "px";
+ if (this.resultListPosition === "below") {
+ this.resultList.style.marginTop =
+ window.scrollY + suggestInputRect.top + suggestInputRect.height + space + "px";
+ this.resultList.style.marginBottom = null;
+ } else if (this.resultListPosition === "above") {
+ this.resultList.style.marginTop = null;
+ this.resultList.style.marginBottom = -(window.scrollY + suggestInputRect.top - space) + "px";
+ }
+ }
}
- connectedCallback(): void {
- console.log("* autocomplete init *********************************************************************");
+ private setResultListMaxHeight(): void {
+ const resultListEntry = this.resultList.querySelector(".autocomplete-result");
+ if (this.maxItems && resultListEntry) {
+ const resultListStyle = getComputedStyle(this.resultList);
+ this.resultList.style.maxHeight = (
+ parseFloat(resultListStyle.borderTop)
+ + parseFloat(resultListStyle.paddingTop)
+ + (this.maxItems * parseFloat(getComputedStyle(resultListEntry).height))
+ + parseFloat(resultListStyle.paddingBottom)
+ + parseFloat(resultListStyle.borderBottom)) + "px";
+ }
+ }
- this.base.classList.add("autocomplete");
- this.suggestInput.classList.add("autocomplete-input");
+ private get base(): HTMLElement {
+ return this.tobagoIn;
+ }
- this.suggestInput.insertAdjacentHTML("afterend", `<ul class="autocomplete-result-list"></ul>`);
+ private get pseudoContainer(): HTMLDivElement {
+ return this.base.querySelector(":scope > .autocomplete-pseudo-container");
+ }
- const options = {
- search: input => {
- console.debug("input = '" + input + "'");
- if (input.length < 1) {
- return [];
- }
- const inputLower = input.toLowerCase();
- let strings = this.items.filter(country => {
- return country.toLowerCase().startsWith(inputLower);
- });
- console.debug("out = '" + strings + "'");
- return strings;
- }
- };
- this.autocomplete = new Autocomplete(this.base, options);
+ private get suggestInput(): HTMLInputElement {
+ const root = this.base.getRootNode() as ShadowRoot | Document;
+ return root.getElementById(this.suggest.getAttribute("for")) as HTMLInputElement;
+ }
- console.log(this.autocomplete);
+ private get suggest(): HTMLElement {
+ return this.base.querySelector("tobago-suggest");
}
-}
+ private get hiddenInput(): HTMLInputElement {
+ return this.suggest.querySelector(":scope > input[type=hidden]");
+ }
+
+ private get items(): string[] {
+ return JSON.parse(this.suggest.getAttribute("items"));
+ }
+
+ private get resultList(): HTMLUListElement {
+ const root = this.base.getRootNode() as ShadowRoot | Document;
+ const resultListId = this.suggestInput.getAttribute("aria-owns");
+ return root.getElementById(resultListId) as HTMLUListElement;
+ }
+
+ private get resultListPosition(): string {
+ return this.base.dataset.position;
+ }
-document.addEventListener("tobago.init", function (event: Event): void {
- if (window.customElements.get("tobago-suggest") == null) {
- window.customElements.define("tobago-suggest", Suggest);
+ 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;
+ }
+
+ private get delay(): number {
+ return parseInt(this.suggest.getAttribute("delay"));
+ }
+
+ private get maxItems(): number {
+ return parseInt(this.suggest.getAttribute("max-items"));
+ }
+
+ private get minChars(): number {
+ return parseInt(this.suggest.getAttribute("min-chars"));
+ }
+
+ private get localMenu(): boolean {
+ return this.suggest.getAttribute("local-menu") !== null;
+ }
+}