]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/pagination.component.ts
LP2045292 Color contrast for AngularJS patron bills
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / record / pagination.component.ts
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';
8
9
10 @Component({
11     selector: 'eg-catalog-record-pagination',
12     templateUrl: 'pagination.component.html',
13     styleUrls: ['pagination.component.css']
14 })
15 export class RecordPaginationComponent implements OnInit {
16
17     id: number;
18     index: number;
19     initDone = false;
20     searchContext: CatalogSearchContext;
21
22     _recordTab: string;
23     @Input() set recordTab(tab: string) {
24         this._recordTab = tab;
25     }
26     get recordTab(): string {
27         return this._recordTab;
28     }
29
30     @Input() set recordId(id: number) {
31         this.id = id;
32         // Only apply new record data after the initial load
33         if (this.initDone) {
34             this.setIndex();
35         }
36     }
37
38     constructor(
39         private router: Router,
40         private cat: CatalogService,
41         private catUrl: CatalogUrlService,
42         private staffCat: StaffCatalogService,
43     ) {}
44
45     ngOnInit() {
46         this.initDone = true;
47         this.setIndex();
48     }
49
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});
55     }
56
57     firstRecord(): void {
58         this.findRecordAtIndex(0)
59             .then(id => this.routeToRecord(id));
60     }
61
62     lastRecord(): void {
63         this.findRecordAtIndex(this.searchContext.result.count - 1)
64             .then(id => this.routeToRecord(id));
65     }
66
67     nextRecord(): void {
68         this.findRecordAtIndex(this.index + 1)
69             .then(id => this.routeToRecord(id));
70     }
71
72     prevRecord(): void {
73         this.findRecordAtIndex(this.index - 1)
74             .then(id => this.routeToRecord(id));
75     }
76
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;
80     }
81
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;
87         this.index = null;
88
89         return new Promise((resolve, reject) => {
90
91             this.index = this.searchContext.indexForResult(this.id);
92             if (this.index !== null) {
93                 return resolve();
94             }
95
96             return this.refreshSearch().then(ok => {
97                 this.index = this.searchContext.indexForResult(this.id);
98                 resolve();
99             });
100         });
101     }
102
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> {
107
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); }
113
114             console.debug(
115                 'Record paginator unable to find record at index ' + index);
116
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(
121                 ok => {
122                     const rid = this.searchContext.resultIdAt(index);
123                     if (rid) {
124                         resolve(rid);
125                     } else {
126                         reject('no record found');
127                     }
128                 }
129             );
130         });
131     }
132
133     refreshSearch(limit?: number): Promise<any> {
134
135         console.debug('paginator refreshing search');
136
137         if (!this.searchContext.isSearchable()) {
138             return Promise.resolve();
139         }
140
141         const ctx = this.searchContext;
142
143         const origPager = ctx.pager;
144         const tmpPager = new Pager();
145         tmpPager.limit = limit || 1000;
146
147         ctx.pager = tmpPager;
148
149         // Avoid fetching highlight data for a potentially large
150         // list of record IDs
151         ctx.getHighlightsFor = this.id;
152
153         return this.cat.search(ctx)
154             .then(_ => {
155                 ctx.pager = origPager;
156                 ctx.getHighlightsFor = null;
157             });
158     }
159
160     returnToSearch(): void {
161         // Fire the main search.  This will direct us back to /results/
162         this.staffCat.search();
163     }
164
165 }
166
167