From 2c753cd407c72992ddd6436013ee38ff21b6c40d Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Sat, 7 May 2022 18:38:01 +0200 Subject: [PATCH] Track the page number of search result in the url --- ts/WoltLabSuite/Core/Ui/Search/Extended.ts | 18 ++++++++++++++++-- .../js/WoltLabSuite/Core/Ui/Search/Extended.js | 17 +++++++++++++++-- .../lib/data/search/SearchAction.class.php | 8 +++++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/ts/WoltLabSuite/Core/Ui/Search/Extended.ts b/ts/WoltLabSuite/Core/Ui/Search/Extended.ts index 51cdaac1c1..c342995f3a 100644 --- a/ts/WoltLabSuite/Core/Ui/Search/Extended.ts +++ b/ts/WoltLabSuite/Core/Ui/Search/Extended.ts @@ -20,6 +20,7 @@ type ResponseSearch = { count: number; title: string; pages?: number; + pageNo?: number; searchID?: number; template?: string; }; @@ -60,6 +61,7 @@ export class UiSearchExtended { private initEventListener(): void { this.form.addEventListener("submit", (event) => { event.preventDefault(); + this.activePage = 1; void this.search(); }); this.typeInput.addEventListener("change", () => this.changeType()); @@ -91,16 +93,16 @@ export class UiSearchExtended { const request = dboAction("search", "wcf\\data\\search\\SearchAction").payload(this.getFormData()); this.lastSearchRequest = request.getAbortController(); - const { count, searchID, title, pages, template } = (await request.dispatch()) as ResponseSearch; + const { count, searchID, title, pages, pageNo, template } = (await request.dispatch()) as ResponseSearch; document.querySelector(".contentTitle")!.textContent = title; this.searchID = searchID; - this.activePage = 1; this.removeSearchResults(); if (count > 0) { this.pages = pages!; + this.activePage = pageNo!; this.showSearchResults(template!); } } @@ -115,6 +117,9 @@ export class UiSearchExtended { parameters.push([key, value.toString().trim()]); } }); + if (this.activePage > 1) { + parameters.push(["pageNo", this.activePage.toString()]); + } url.search += new URLSearchParams(parameters); window.history.replaceState({}, document.title, url.toString()); @@ -127,6 +132,9 @@ export class UiSearchExtended { data[key] = value; } }); + if (this.activePage > 1) { + data["pageNo"] = this.activePage; + } return data; } @@ -134,6 +142,12 @@ export class UiSearchExtended { private initQueryString(): void { const url = new URL(window.location.href); url.searchParams.forEach((value, key) => { + if (key === "pageNo") { + this.activePage = parseInt(value, 10); + if (this.activePage < 1) this.activePage = 1; + return; + } + const element = this.form.elements[key] as HTMLElement; if (value && element) { if (element instanceof RadioNodeList) { diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Search/Extended.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Search/Extended.js index cdd4d0900d..953ec3d66f 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Search/Extended.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Search/Extended.js @@ -38,6 +38,7 @@ define(["require", "exports", "tslib", "../../Ajax", "../../Date/Picker", "../.. initEventListener() { this.form.addEventListener("submit", (event) => { event.preventDefault(); + this.activePage = 1; void this.search(); }); this.typeInput.addEventListener("change", () => this.changeType()); @@ -64,13 +65,13 @@ define(["require", "exports", "tslib", "../../Ajax", "../../Date/Picker", "../.. (_a = this.lastSearchRequest) === null || _a === void 0 ? void 0 : _a.abort(); const request = (0, Ajax_1.dboAction)("search", "wcf\\data\\search\\SearchAction").payload(this.getFormData()); this.lastSearchRequest = request.getAbortController(); - const { count, searchID, title, pages, template } = (await request.dispatch()); + const { count, searchID, title, pages, pageNo, template } = (await request.dispatch()); document.querySelector(".contentTitle").textContent = title; this.searchID = searchID; - this.activePage = 1; this.removeSearchResults(); if (count > 0) { this.pages = pages; + this.activePage = pageNo; this.showSearchResults(template); } } @@ -83,6 +84,9 @@ define(["require", "exports", "tslib", "../../Ajax", "../../Date/Picker", "../.. parameters.push([key, value.toString().trim()]); } }); + if (this.activePage > 1) { + parameters.push(["pageNo", this.activePage.toString()]); + } url.search += new URLSearchParams(parameters); window.history.replaceState({}, document.title, url.toString()); } @@ -93,11 +97,20 @@ define(["require", "exports", "tslib", "../../Ajax", "../../Date/Picker", "../.. data[key] = value; } }); + if (this.activePage > 1) { + data["pageNo"] = this.activePage; + } return data; } initQueryString() { const url = new URL(window.location.href); url.searchParams.forEach((value, key) => { + if (key === "pageNo") { + this.activePage = parseInt(value, 10); + if (this.activePage < 1) + this.activePage = 1; + return; + } const element = this.form.elements[key]; if (value && element) { if (element instanceof RadioNodeList) { diff --git a/wcfsetup/install/files/lib/data/search/SearchAction.class.php b/wcfsetup/install/files/lib/data/search/SearchAction.class.php index 86eb1205d6..b830e94ddd 100644 --- a/wcfsetup/install/files/lib/data/search/SearchAction.class.php +++ b/wcfsetup/install/files/lib/data/search/SearchAction.class.php @@ -60,6 +60,7 @@ class SearchAction extends AbstractDatabaseObjectAction $this->readString('endDate', true); $this->readString('sortField', true); $this->readString('sortOrder', true); + $this->readInteger('pageNo', true); if (empty($this->parameters['q']) && empty($this->parameters['username'])) { throw new UserInputException('q'); @@ -113,7 +114,11 @@ class SearchAction extends AbstractDatabaseObjectAction ]; } - $resultHandler = new SearchResultHandler($search); + $startIndex = 0; + if ($this->parameters['pageNo'] > 1) { + $startIndex = SEARCH_RESULTS_PER_PAGE * ($this->parameters['pageNo'] - 1); + } + $resultHandler = new SearchResultHandler($search, $startIndex); $resultHandler->loadSearchResults(); $templateName = $resultHandler->getTemplateName(); @@ -130,6 +135,7 @@ class SearchAction extends AbstractDatabaseObjectAction 'query' => $resultHandler->getQuery(), ]), 'pages' => \ceil($resultHandler->countSearchResults() / SEARCH_RESULTS_PER_PAGE), + 'pageNo' => $this->parameters['pageNo'] ?: 1, 'searchID' => $search->searchID, 'template' => WCF::getTPL()->fetch($templateName['templateName'], $templateName['application']), ]; -- 2.20.1