From b8666fdcbe324ebcde8aaf7fe2d336af18fccd19 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Sat, 13 Jul 2024 17:21:51 +0200 Subject: [PATCH 01/12] New dumb `k-search` component --- panel/lab/components/search/index.php | 5 + panel/lab/components/search/index.vue | 60 +++++++ panel/src/components/Navigation/Search.vue | 200 +++++++++++++++++++++ panel/src/components/Navigation/index.js | 2 + 4 files changed, 267 insertions(+) create mode 100644 panel/lab/components/search/index.php create mode 100644 panel/lab/components/search/index.vue create mode 100644 panel/src/components/Navigation/Search.vue diff --git a/panel/lab/components/search/index.php b/panel/lab/components/search/index.php new file mode 100644 index 0000000000..dcf0e18c8c --- /dev/null +++ b/panel/lab/components/search/index.php @@ -0,0 +1,5 @@ + 'k-search', +]; diff --git a/panel/lab/components/search/index.vue b/panel/lab/components/search/index.vue new file mode 100644 index 0000000000..07feb8bdfc --- /dev/null +++ b/panel/lab/components/search/index.vue @@ -0,0 +1,60 @@ + + + + + diff --git a/panel/src/components/Navigation/Search.vue b/panel/src/components/Navigation/Search.vue new file mode 100644 index 0000000000..00135b7cd4 --- /dev/null +++ b/panel/src/components/Navigation/Search.vue @@ -0,0 +1,200 @@ + + + + + diff --git a/panel/src/components/Navigation/index.js b/panel/src/components/Navigation/index.js index 4cf6dac7ed..e1cb466aac 100644 --- a/panel/src/components/Navigation/index.js +++ b/panel/src/components/Navigation/index.js @@ -9,6 +9,7 @@ import Navigate from "./Navigate.vue"; import PageTree from "./PageTree.vue"; import Pagination from "./Pagination.vue"; import PrevNext from "./PrevNext.vue"; +import Search from "./Search.vue"; import Tag from "./Tag.vue"; import Tags from "./Tags.vue"; import Tree from "./Tree.vue"; @@ -31,6 +32,7 @@ export default { app.component("k-page-tree", PageTree); app.component("k-pagination", Pagination); app.component("k-prev-next", PrevNext); + app.component("k-search", Search); app.component("k-tag", Tag); app.component("k-tags", Tags); app.component("k-tree", Tree); From 8fc2c21e6d698c4af48e4084ab3d1b72e7b47741 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Sat, 13 Jul 2024 17:19:33 +0200 Subject: [PATCH 02/12] Clean up `k-dialog-search` --- panel/src/components/Dialogs/Elements/Search.vue | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/panel/src/components/Dialogs/Elements/Search.vue b/panel/src/components/Dialogs/Elements/Search.vue index 6b93668bd1..f55c23b235 100644 --- a/panel/src/components/Dialogs/Elements/Search.vue +++ b/panel/src/components/Dialogs/Elements/Search.vue @@ -2,11 +2,10 @@ @@ -23,7 +22,6 @@ export default { type: Boolean }, placeholder: { - default: () => window.panel.$t("search") + " …", type: String }, value: { From 88799dea372ef0a1c7111e76c16f7e58fbd9b478 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Sat, 13 Jul 2024 17:19:58 +0200 Subject: [PATCH 03/12] New `panel.searcher` module --- panel/src/panel/panel.js | 23 +++--------- panel/src/panel/search.js | 74 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 19 deletions(-) create mode 100644 panel/src/panel/search.js diff --git a/panel/src/panel/panel.js b/panel/src/panel/panel.js index 9677af4195..862157d935 100644 --- a/panel/src/panel/panel.js +++ b/panel/src/panel/panel.js @@ -9,6 +9,7 @@ import Notification from "./notification.js"; import Language from "./language.js"; import Plugins from "./plugins.js"; import Menu from "./menu.js"; +import Search from "./search.js"; import System from "./system.js"; import Translation from "./translation.js"; import { buildUrl, isUrl } from "@/helpers/url.js"; @@ -72,6 +73,7 @@ export default { this.activation = Activation(this); this.drag = Drag(this); this.events = Events(this); + this.searcher = Search(this); this.upload = Upload(this); // state objects @@ -286,7 +288,7 @@ export default { /** * Use one of the installed search types - * to search for content in the panel + * to search for content in the Panel * * @param {String} type * @param {Object} query @@ -294,24 +296,7 @@ export default { * @returns {Object} { code, path, referrer, results, timestamp } */ async search(type, query, options) { - // open the search dialog - if (!query) { - // close menu on mobile - this.menu.escape(); - - return this.dialog.open({ - component: "k-search-dialog", - props: { - type: type - } - }); - } - - const { $search } = await this.get(`/search/${type}`, { - query: { query, ...options } - }); - - return $search; + return this.searcher.query(type, query, options); }, /** diff --git a/panel/src/panel/search.js b/panel/src/panel/search.js new file mode 100644 index 0000000000..11c9b563cb --- /dev/null +++ b/panel/src/panel/search.js @@ -0,0 +1,74 @@ +/** + * @since 4.4.0 + */ +export default (panel) => { + return { + controller: null, + requests: 0, + + /** + * Whether any search requests are ongoing + */ + get isLoading() { + return this.requests > 0; + }, + + /** + * Opens the search dialog + * + * @param {String} type + */ + open(type) { + // close menu on mobile + panel.menu.escape(); + + panel.dialog.open({ + component: "k-search-dialog", + props: { + type + } + }); + }, + + /** + * Use one of the installed search types + * to search for content in the Panel + * + * @param {String} type + * @param {Object} query + * @param {Object} options { limit, page } + * @returns {Object} { code, path, referrer, results, timestamp } + */ + async query(type, query, options) { + if (!query) { + // open the search dialog + return this.open(type); + } + + // abort any previous ongoing search requests + this.controller?.abort(); + this.controller = new AbortController(); + + this.requests++; + + try { + const { $search } = await panel.get(`/search/${type}`, { + query: { query, ...options }, + signal: this.controller.signal + }); + return $search; + } catch (error) { + // if fails and not because request was aborted by subsequent request, + // return empty response + if (error.name !== "AbortError") { + return { + results: [], + pagination: {} + }; + } + } finally { + this.requests--; + } + } + }; +}; From e94ea2c52d6f1b9ed05535a71f5e2fff5498d30c Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Sat, 13 Jul 2024 17:20:14 +0200 Subject: [PATCH 04/12] SearchDialog: use `k-search` --- panel/src/components/Dialogs/SearchDialog.vue | 206 +++--------------- 1 file changed, 26 insertions(+), 180 deletions(-) diff --git a/panel/src/components/Dialogs/SearchDialog.vue b/panel/src/components/Dialogs/SearchDialog.vue index 686f7ae98e..870589cc04 100644 --- a/panel/src/components/Dialogs/SearchDialog.vue +++ b/panel/src/components/Dialogs/SearchDialog.vue @@ -1,123 +1,47 @@