From a80b913b8321f633fcab1a7df015309063a0956b Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Wed, 18 Nov 2020 12:20:59 -0500 Subject: [PATCH] LPXXX Staff cat browse results paging WIP Signed-off-by: Bill Erickson --- .../staff/catalog/result/results.component.html | 41 +++++-- .../app/staff/catalog/result/results.component.ts | 125 ++++++++++++++++----- 2 files changed, 128 insertions(+), 38 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/staff/catalog/result/results.component.html b/Open-ILS/src/eg2/src/app/staff/catalog/result/results.component.html index fbcfe99266..0ddcbbb4e5 100644 --- a/Open-ILS/src/eg2/src/app/staff/catalog/result/results.component.html +++ b/Open-ILS/src/eg2/src/app/staff/catalog/result/results.component.html @@ -31,15 +31,13 @@
+
+
+

Results for browse entry "{{searchContext.termSearch.browseEntry.value()}}"

+
+
- -

Results for browse "{{searchContext.termSearch.browseEntry.value()}}"

-
- - -
-

Search Results ({{searchContext.result.count}})

@@ -56,11 +54,32 @@
-
-
- + + +
+ + + + + + +
-
+
+
+ +
+
+ + +
+
+ +
+
+
diff --git a/Open-ILS/src/eg2/src/app/staff/catalog/result/results.component.ts b/Open-ILS/src/eg2/src/app/staff/catalog/result/results.component.ts index bd80f38434..a7cd2dcabb 100644 --- a/Open-ILS/src/eg2/src/app/staff/catalog/result/results.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/catalog/result/results.component.ts @@ -1,6 +1,6 @@ import {Component, OnInit, OnDestroy, Input} from '@angular/core'; import {Observable, Subscription} from 'rxjs'; -import {map, switchMap, distinctUntilChanged} from 'rxjs/operators'; +import {tap, map, switchMap, distinctUntilChanged} from 'rxjs/operators'; import {ActivatedRoute, ParamMap} from '@angular/router'; import {CatalogService} from '@eg/share/catalog/catalog.service'; import {BibRecordService} from '@eg/share/catalog/bib-record.service'; @@ -28,8 +28,8 @@ export class ResultsComponent implements OnInit, OnDestroy { searchSub: Subscription; routeSub: Subscription; basketSub: Subscription; - prevBrowseResult: any; - nextBrowseResult: any; + browseResults: any[] = []; + browseLoading = false; constructor( private route: ActivatedRoute, @@ -74,7 +74,12 @@ export class ResultsComponent implements OnInit, OnDestroy { // Load browse data to support browse paging. - this.loadBrowsePaging(); + /* + if (this.searchContext.termSearch.hasBrowseEntry && + this.searchContext.browseSearch.value) { + this.fetchBrowseData().toPromise(); + } + */ } ngOnDestroy() { @@ -134,38 +139,104 @@ export class ResultsComponent implements OnInit, OnDestroy { } } - loadBrowsePaging() { - if (!this.searchContext.termSearch.hasBrowseEntry || - !this.searchContext.browseSearch.value) { return; } + // Grab a page of browse results + fetchBrowseData(prev?: boolean): Promise { + const ctx = this.searchContext.clone(); + ctx.termSearch.hasBrowseEntry = null; // avoid term search + + // 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 = []; + + return this.cat.browse(ctx) + .pipe(tap(result => results.push(result))) + .toPromise().then(_ => { + if (prev) { + this.browseResults = results.concat(this.browseResults); + } else { + this.browseResults = this.browseResults.concat(results); + } + }); + } + + // Find the browse entry for the next/prev page and navigate there + // if possible. Returns false if not enough data is available. + goToBrowsePage(prev?: boolean): boolean { + const ctx = this.searchContext; + + // See if we have enough info to direct to the selected page. const mbeId = Number( // for this page this.searchContext.termSearch.hasBrowseEntry.split(',')[0]); - const ctx = this.searchContext.clone(); - // force a browse search - ctx.termSearch.hasBrowseEntry = null; - - let previous: any; - - this.cat.browse(ctx).subscribe(result => { - if (!this.prevBrowseResult && result.browse_entry === mbeId) { - console.log('setting previous to ', previous.browse_entry); - this.prevBrowseResult = previous; - } else if (previous && previous.browse_entry === mbeId) { - console.log('setting next to ', result.browse_entry); - this.nextBrowseResult = result; + let target, previous; + + this.browseResults.forEach(result => { + if (!result.browse_entry) { return; } // ignore pivot entries + + if (previous) { + if (prev) { + if (result.browse_entry === mbeId) { + target = previous; + } + } else { + if (previous.browse_entry === mbeId) { + target = result; + } + } } previous = result; }); - } - browseNav(prev?: boolean) { - const ctx = this.searchContext; - const res = prev ? this.prevBrowseResult : this.nextBrowseResult - console.log('res is ', res); - ctx.termSearch.hasBrowseEntry = res.browse_entry + ',' + res.fields; + if (!target) { return false; } + + // Jump to the selected browse entry's page. + ctx.termSearch.hasBrowseEntry = target.browse_entry + ',' + target.fields; + ctx.pager.offset = 0; // this is a brand new search this.staffCat.search(); - this.loadBrowsePaging(); + + return true; + } + + + // Navigate to next or previous browse entry page. + // take1: navigate without fetching data if possible + // take2: fetch data and navigate if possible. + // take3: data fetched in take2 contains data for this page, but + // not the requested page. This happens when the user navigates + // cold to a browse page boundary and the current page of data + // does not yet exist. + browseNav(prev?: boolean, take?: number) { + if (!take) { take = 1; } + + this.browseLoading = true; + + if (take > 3) { + // We tried our best, but could not find the requested + // browse data Could happen if we reach the end of the data set. + this.browseLoading = false; + return; + } + + if (this.goToBrowsePage(prev)) { + // Navigation successful + this.browseLoading = false; + return; + } + + // We need another page of browse data before we can direct + // the user to the requested page. + + const results = this.browseResults; + if (results.length > 0) { + this.searchContext.browseSearch.pivot = prev ? + results[0].pivot_point : + results[results.length - 1].pivot_point; + } + + this.fetchBrowseData(prev).then(_ => this.browseNav(prev, take + 1)); } } -- 2.11.0