LP1904788 Browse paging show next/prev WIP
authorBill Erickson <berickxx@gmail.com>
Thu, 19 Nov 2020 20:48:01 +0000 (15:48 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 19 Nov 2020 20:48:01 +0000 (15:48 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/staff/catalog/result/browse-pager.component.ts

index ed34719..83054b6 100644 (file)
@@ -48,16 +48,13 @@ export class BrowsePagerComponent implements OnInit {
     // Grab a page of browse results
     fetchBrowseData(prev: boolean): Promise<any> {
         const ctx = this.searchContext.clone();
+        ctx.pager.limit = this.searchContext.pager.limit;
         ctx.termSearch.hasBrowseEntry = null; // avoid term search
 
         if (prev !== null) {
             ctx.browseSearch.pivot = this.getBoundaryPivot(prev);
         }
 
-        // Grab a few extras so we can last a bit longer before
-        // having to fetch more browse data.  Could make this bigger.
-        ctx.pager.limit = 20;
-
         const results = [];
         this.browseLoading = true;
 
@@ -83,14 +80,23 @@ export class BrowsePagerComponent implements OnInit {
 
     // Collect enough browse data to display previous, current, and
     // next heading.  This can mean fetching an additional page of data.
-    setPrevNext(): Promise<any> {
-        let previous: any;
+    setPrevNext(take: number = 1): Promise<any> {
+
+        // Should never have to call this more than 3 times, with the
+        // first 2 collecting data and the final assing prev/next.
+        if (take > 3) { return Promise.resolve(); }
+
+        let pageEntry, previous: any;
         const mbeId = this.pageEntryId();
 
         this.staffCat.browsePagerResults.forEach(result => {
             // ignore pivot and authority-only entries
             if (!result.sources) { return; }
 
+            if (result.browse_entry === mbeId) {
+                pageEntry = result;
+            }
+
             if (previous) {
                 if (result.browse_entry === mbeId) {
                     this.prevEntry = previous;
@@ -102,18 +108,27 @@ export class BrowsePagerComponent implements OnInit {
             previous = result;
         });
 
-        if (!this.prevEntry) {
-            return this.fetchBrowseData(true);
+        let promise;
+        if (!pageEntry) {
+            promise = this.fetchBrowseData(null)
+
+        } else if (!this.prevEntry) {
+            promise = this.fetchBrowseData(true);
+
         } else if (!this.nextEntry) {
-            return this.fetchBrowseData(false);
-        } else {
-            return Promise.resolve();
+            promise = this.fetchBrowseData(false);
         }
+
+        if (promise) {
+            return promise.then(_ => this.setPrevNext(take + 1));
+        }
+
+        return Promise.resolve();
     }
 
     getBoundaryPivot(prev?: boolean): number {
         const results = this.staffCat.browsePagerResults;
-        return prev ?  results[0].pivot_point :
+        return prev ? results[0].pivot_point :
             results[results.length - 1].pivot_point;
     }
 
@@ -124,31 +139,39 @@ export class BrowsePagerComponent implements OnInit {
         const mbeId = Number( // for this page
             this.searchContext.termSearch.hasBrowseEntry.split(',')[0]);
 
-        const targetMbe = prev ?
-            this.prevEntry.browse_entry : this.nextEntry.browse_entry;
+        const targetMbe = Number(
+            prev ?  this.prevEntry.browse_entry : this.nextEntry.browse_entry
+        );
 
         let results = [].concat(this.staffCat.browsePagerResults);
-        if (prev) { results.reverse(); }
+        if (prev) { results = results.reverse(); }
 
         let current;
+        let lastPivot = null;
+        let lastWasPivot = false;
         let done = false;
         results.forEach(result => {
             if (done) { return; }
+            const entryId = Number(result.browse_entry || -1);
+
+            // Pivots will be back-to-back in the full results set
+            // We only care about the first pivot encountered in a
+            // 2-pivot cluster.
+            if (result.pivot_point && !lastWasPivot) {
+                lastPivot = result.pivot_point;
+                lastWasPivot = true;
+                return;
+            }
 
-            if (current) {
+            lastWasPivot = false;
 
-                if (result.browse_entry === targetMbe) {
-                    // The target entry is on the current page of results
-                    // No pivot movement required.
-                    done = true;
+            if (current) {
 
-                } else if (result.pivot) {
-                    // We have crossed a pivot boundary.
-                    this.searchContext.browseSearch.pivot = result.pivot;
+                if (entryId === targetMbe && lastPivot) {
+                    this.searchContext.browseSearch.pivot = lastPivot;
                     done = true;
                 }
-
-            } else if (result.browse_entry === mbeId) {
+            } else if (entryId === mbeId) {
                 current = result;
             }
         });
@@ -162,7 +185,7 @@ export class BrowsePagerComponent implements OnInit {
 
         if (!target) { return false; }
 
-        this.setSearchPivot();
+        this.setSearchPivot(prev);
 
         // Jump to the selected browse entry's page.
         ctx.termSearch.hasBrowseEntry = target.browse_entry + ',' + target.fields;