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'
14 export class RecordPaginationComponent implements OnInit {
19 searchContext: CatalogSearchContext;
22 @Input() set recordTab(tab: string) {
23 this._recordTab = tab;
25 get recordTab(): string {
26 return this._recordTab;
29 @Input() set recordId(id: number) {
31 // Only apply new record data after the initial load
38 private router: Router,
39 private cat: CatalogService,
40 private catUrl: CatalogUrlService,
41 private staffCat: StaffCatalogService,
49 routeToRecord(id: number) {
50 let url = '/staff/catalog/record/' + id;
51 if (this.recordTab) { url += '/' + this.recordTab; }
52 const params = this.catUrl.toUrlParams(this.searchContext);
53 this.router.navigate([url], {queryParams: params});
57 this.findRecordAtIndex(0)
58 .then(id => this.routeToRecord(id));
62 this.findRecordAtIndex(this.searchContext.result.count - 1)
63 .then(id => this.routeToRecord(id));
67 this.findRecordAtIndex(this.index + 1)
68 .then(id => this.routeToRecord(id));
72 this.findRecordAtIndex(this.index - 1)
73 .then(id => this.routeToRecord(id));
76 // Returns the offset of the record within the search results as a whole.
77 searchIndex(idx: number): number {
78 return idx + this.searchContext.pager.offset;
81 // Find the position of the current record in the search results
82 // If no results are present or the record is not found, expand
83 // the search scope to find the record.
84 setIndex(): Promise<void> {
85 this.searchContext = this.staffCat.searchContext;
88 return new Promise((resolve, reject) => {
90 this.index = this.searchContext.indexForResult(this.id);
91 if (this.index !== null) {
95 return this.refreshSearch().then(ok => {
96 this.index = this.searchContext.indexForResult(this.id);
102 // Find the record ID at the specified search index.
103 // If no data exists for the requested index, expand the search
104 // to include data for that index.
105 findRecordAtIndex(index: number): Promise<number> {
107 // First see if the selected record sits in the current page
108 // of search results.
109 return new Promise((resolve, reject) => {
110 const id = this.searchContext.resultIdAt(index);
111 if (id) { return resolve(id); }
114 'Record paginator unable to find record at index ' + index);
116 // If we have to re-run the search to find the record,
117 // expand the search limit out just enough to find the
118 // requested record plus one more.
119 return this.refreshSearch(index + 2).then(
121 const rid = this.searchContext.resultIdAt(index);
125 reject('no record found');
132 refreshSearch(limit?: number): Promise<any> {
134 console.debug('paginator refreshing search');
136 if (!this.searchContext.isSearchable()) {
137 return Promise.resolve();
140 const origPager = this.searchContext.pager;
141 const tmpPager = new Pager();
142 tmpPager.limit = limit || 1000;
144 this.searchContext.pager = tmpPager;
146 return this.cat.search(this.searchContext)
148 ok => this.searchContext.pager = origPager,
149 notOk => this.searchContext.pager = origPager
153 returnToSearch(): void {
154 // Fire the main search. This will direct us back to /results/
155 this.staffCat.search();