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;
21 @Input() set recordId(id: number) {
23 // Only apply new record data after the initial load
30 private router: Router,
31 private cat: CatalogService,
32 private catUrl: CatalogUrlService,
33 private staffCat: StaffCatalogService,
42 this.findRecordAtIndex(0).then(id => {
43 const params = this.catUrl.toUrlParams(this.searchContext);
45 ['/staff/catalog/record/' + id], {queryParams: params});
50 this.findRecordAtIndex(
51 this.searchContext.result.count - 1
53 const params = this.catUrl.toUrlParams(this.searchContext);
55 ['/staff/catalog/record/' + id], {queryParams: params});
60 this.findRecordAtIndex(this.index + 1).then(id => {
61 const params = this.catUrl.toUrlParams(this.searchContext);
63 ['/staff/catalog/record/' + id], {queryParams: params});
68 this.findRecordAtIndex(this.index - 1).then(id => {
69 const params = this.catUrl.toUrlParams(this.searchContext);
71 ['/staff/catalog/record/' + id], {queryParams: params});
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);
97 if (this.index === null) {
99 'No search results found containing the focused record.');
106 // Find the record ID at the specified search index.
107 // If no data exists for the requested index, expand the search
108 // to include data for that index.
109 findRecordAtIndex(index: number): Promise<number> {
111 // First see if the selected record sits in the current page
112 // of search results.
113 return new Promise((resolve, reject) => {
114 const id = this.searchContext.resultIdAt(index);
115 if (id) { return resolve(id); }
118 'Record paginator unable to find record at index ' + index);
120 // If we have to re-run the search to find the record,
121 // expand the search limit out just enough to find the
122 // requested record plus one more.
123 return this.refreshSearch(index + 2).then(
125 const rid = this.searchContext.resultIdAt(index);
129 reject('no record found');
136 refreshSearch(limit?: number): Promise<any> {
138 console.debug('paginator refreshing search');
140 if (!this.searchContext.isSearchable()) {
141 return Promise.resolve();
144 const origPager = this.searchContext.pager;
145 const tmpPager = new Pager();
146 tmpPager.limit = limit || 1000;
148 this.searchContext.pager = tmpPager;
150 return this.cat.search(this.searchContext)
152 ok => this.searchContext.pager = origPager,
153 notOk => this.searchContext.pager = origPager
157 returnToSearch(): void {
158 // Fire the main search. This will direct us back to /results/
159 this.staffCat.search();