Track the page number of search result in the url
authorMarcel Werk <burntime@woltlab.com>
Sat, 7 May 2022 16:38:01 +0000 (18:38 +0200)
committerMarcel Werk <burntime@woltlab.com>
Sat, 7 May 2022 16:38:01 +0000 (18:38 +0200)
ts/WoltLabSuite/Core/Ui/Search/Extended.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Search/Extended.js
wcfsetup/install/files/lib/data/search/SearchAction.class.php

index 51cdaac1c16324d2b7d3f3358ecdee1738d1cba5..c342995f3af0322dec90d67d4a11e698513583d5 100644 (file)
@@ -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) {
index cdd4d0900d709a1d59a750e35562809e7d1953b6..953ec3d66f6c8f36add61b81c8496810a0cc7e0a 100644 (file)
@@ -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) {
index 86eb1205d60e5f95f907bd678d76c9c880a6b930..b830e94ddd5d6f51a752edee6b390fb37411d546 100644 (file)
@@ -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']),
         ];