1 import {Component, OnInit, Input} from '@angular/core';
2 import {Router} from '@angular/router';
3 import {CatalogService} from '@eg/share/catalog/catalog.service';
4 import {CatalogSearchContext} from '@eg/share/catalog/search-context';
5 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
6 import {StaffCatalogService} from '../catalog.service';
7 import {Pager} from '@eg/share/util/pager';
11 selector: 'eg-catalog-record-pagination',
12 templateUrl: 'pagination.component.html',
13 styleUrls: ['pagination.component.css']
15 export class RecordPaginationComponent implements OnInit {
20 searchContext: CatalogSearchContext;
23 @Input() set recordTab(tab: string) {
24 this._recordTab = tab;
26 get recordTab(): string {
27 return this._recordTab;
30 @Input() set recordId(id: number) {
32 // Only apply new record data after the initial load
39 private router: Router,
40 private cat: CatalogService,
41 private catUrl: CatalogUrlService,
42 private staffCat: StaffCatalogService,
50 routeToRecord(id: number) {
51 let url = '/staff/catalog/record/' + id;
52 if (this.recordTab) { url += '/' + this.recordTab; }
53 const params = this.catUrl.toUrlParams(this.searchContext);
54 this.router.navigate([url], {queryParams: params});
58 this.findRecordAtIndex(0)
59 .then(id => this.routeToRecord(id));
63 this.findRecordAtIndex(this.searchContext.result.count - 1)
64 .then(id => this.routeToRecord(id));
68 this.findRecordAtIndex(this.index + 1)
69 .then(id => this.routeToRecord(id));
73 this.findRecordAtIndex(this.index - 1)
74 .then(id => this.routeToRecord(id));
77 // Returns the offset of the record within the search results as a whole.
78 searchIndex(idx: number): number {
79 return idx + this.searchContext.pager.offset;
82 // Find the position of the current record in the search results
83 // If no results are present or the record is not found, expand
84 // the search scope to find the record.
85 setIndex(): Promise<void> {
86 this.searchContext = this.staffCat.searchContext;
89 return new Promise((resolve, reject) => {
91 this.index = this.searchContext.indexForResult(this.id);
92 if (this.index !== null) {
96 return this.refreshSearch().then(ok => {
97 this.index = this.searchContext.indexForResult(this.id);
103 // Find the record ID at the specified search index.
104 // If no data exists for the requested index, expand the search
105 // to include data for that index.
106 findRecordAtIndex(index: number): Promise<number> {
108 // First see if the selected record sits in the current page
109 // of search results.
110 return new Promise((resolve, reject) => {
111 const id = this.searchContext.resultIdAt(index);
112 if (id) { return resolve(id); }
115 'Record paginator unable to find record at index ' + index);
117 // If we have to re-run the search to find the record,
118 // expand the search limit out just enough to find the
119 // requested record plus one more.
120 return this.refreshSearch(index + 2).then(
122 const rid = this.searchContext.resultIdAt(index);
126 reject('no record found');
133 refreshSearch(limit?: number): Promise<any> {
135 console.debug('paginator refreshing search');
137 if (!this.searchContext.isSearchable()) {
138 return Promise.resolve();
141 const ctx = this.searchContext;
143 const origPager = ctx.pager;
144 const tmpPager = new Pager();
145 tmpPager.limit = limit || 1000;
147 ctx.pager = tmpPager;
149 // Avoid fetching highlight data for a potentially large
150 // list of record IDs
151 ctx.getHighlightsFor = this.id;
153 return this.cat.search(ctx)
155 ctx.pager = origPager;
156 ctx.getHighlightsFor = null;
160 returnToSearch(): void {
161 // Fire the main search. This will direct us back to /results/
162 this.staffCat.search();