]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/pagination.component.ts
Docs: release notes for 3.4.2
[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 })
14 export class RecordPaginationComponent implements OnInit {
15
16     id: number;
17     index: number;
18     initDone = false;
19     searchContext: CatalogSearchContext;
20
21     _recordTab: string;
22     @Input() set recordTab(tab: string) {
23         this._recordTab = tab;
24     }
25     get recordTab(): string {
26         return this._recordTab;
27     }
28
29     @Input() set recordId(id: number) {
30         this.id = id;
31         // Only apply new record data after the initial load
32         if (this.initDone) {
33             this.setIndex();
34         }
35     }
36
37     constructor(
38         private router: Router,
39         private cat: CatalogService,
40         private catUrl: CatalogUrlService,
41         private staffCat: StaffCatalogService,
42     ) {}
43
44     ngOnInit() {
45         this.initDone = true;
46         this.setIndex();
47     }
48
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});
54     }
55
56     firstRecord(): void {
57         this.findRecordAtIndex(0)
58         .then(id => this.routeToRecord(id));
59     }
60
61     lastRecord(): void {
62         this.findRecordAtIndex(this.searchContext.result.count - 1)
63         .then(id => this.routeToRecord(id));
64     }
65
66     nextRecord(): void {
67         this.findRecordAtIndex(this.index + 1)
68         .then(id => this.routeToRecord(id));
69     }
70
71     prevRecord(): void {
72         this.findRecordAtIndex(this.index - 1)
73         .then(id => this.routeToRecord(id));
74     }
75
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;
79     }
80
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;
86         this.index = null;
87
88         return new Promise((resolve, reject) => {
89
90             this.index = this.searchContext.indexForResult(this.id);
91             if (this.index !== null) {
92                 return resolve();
93             }
94
95             return this.refreshSearch().then(ok => {
96                 this.index = this.searchContext.indexForResult(this.id);
97                 if (this.index === null) {
98                     console.warn(
99                         'No search results found containing the focused record.');
100                 }
101                 resolve();
102             });
103         });
104     }
105
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> {
110
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); }
116
117             console.debug(
118                 'Record paginator unable to find record at index ' + index);
119
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(
124                 ok => {
125                     const rid = this.searchContext.resultIdAt(index);
126                     if (rid) {
127                         resolve(rid);
128                     } else {
129                         reject('no record found');
130                     }
131                 }
132             );
133         });
134     }
135
136     refreshSearch(limit?: number): Promise<any> {
137
138         console.debug('paginator refreshing search');
139
140         if (!this.searchContext.isSearchable()) {
141             return Promise.resolve();
142         }
143
144         const origPager = this.searchContext.pager;
145         const tmpPager = new Pager();
146         tmpPager.limit = limit || 1000;
147
148         this.searchContext.pager = tmpPager;
149
150         return this.cat.search(this.searchContext)
151         .then(
152             ok => this.searchContext.pager = origPager,
153             notOk => this.searchContext.pager = origPager
154         );
155     }
156
157     returnToSearch(): void {
158         // Fire the main search.  This will direct us back to /results/
159         this.staffCat.search();
160     }
161
162 }
163
164