LP1885179 Staff catalog add results to basket
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / basket-actions.component.ts
1 import {Component, Input, OnInit, ViewChild} from '@angular/core';
2 import {BasketService} from '@eg/share/catalog/basket.service';
3 import {Router} from '@angular/router';
4 import {NetService} from '@eg/core/net.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {PrintService} from '@eg/share/print/print.service';
7 import {CatalogService} from '@eg/share/catalog/catalog.service';
8 import {CatalogSearchContext, CatalogSearchState} from '@eg/share/catalog/search-context';
9 import {StaffCatalogService} from './catalog.service';
10 import {BucketDialogComponent
11     } from '@eg/staff/share/buckets/bucket-dialog.component';
12 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
13
14 const MAX_FROM_SEARCH_RESULTS = 1000;
15
16 @Component({
17   selector: 'eg-catalog-basket-actions',
18   templateUrl: 'basket-actions.component.html'
19 })
20 export class BasketActionsComponent implements OnInit {
21
22     basketAction: string;
23
24     @ViewChild('addBasketToBucketDialog', { static: true })
25         addToBucketDialog: BucketDialogComponent;
26
27     @ViewChild('addAllProgress', {static: true})
28         addAllProgress: ProgressDialogComponent;
29
30     constructor(
31         private router: Router,
32         private net: NetService,
33         private auth: AuthService,
34         private printer: PrintService,
35         private basket: BasketService,
36         private cat: CatalogService,
37         private staffCat: StaffCatalogService
38     ) {
39         this.basketAction = '';
40     }
41
42     ngOnInit() {
43     }
44
45     basketCount(): number {
46         return this.basket.recordCount();
47     }
48
49     isMetarecordSearch(): boolean {
50         return this.staffCat.searchContext &&
51             this.staffCat.searchContext.termSearch.isMetarecordSearch();
52     }
53
54     // TODO: confirmation dialogs?
55
56     applyAction(action: string) {
57         this.basketAction = action;
58
59         switch (this.basketAction) {
60
61             case 'add_all':
62                 // Add all search results to basket.
63
64                 this.addAllProgress.open();
65
66                 const ctx = this.staffCat.cloneContext(this.staffCat.searchContext);
67                 ctx.pager.offset = 0;
68                 ctx.pager.limit = MAX_FROM_SEARCH_RESULTS;
69
70                 this.cat.search(ctx)
71                 .then(_ => this.basket.addRecordIds(ctx.currentResultIds()))
72                 .then(_ => this.addAllProgress.close());
73
74                 break;
75
76             case 'view':
77                 // This does not propagate search params -- unclear if needed.
78                 this.router.navigate(['/staff/catalog/search'],
79                     {queryParams: {showBasket: true}});
80                 break;
81
82             case 'clear':
83                 this.basket.removeAllRecordIds();
84                 break;
85
86             case 'hold':
87                 this.basket.getRecordIds().then(ids => {
88                     this.router.navigate(['/staff/catalog/hold/T'],
89                         {queryParams: {target: ids}});
90                 });
91                 break;
92
93             case 'print':
94                 this.basket.getRecordIds().then(ids => {
95                     this.net.request(
96                         'open-ils.search',
97                         'open-ils.search.biblio.record.print', ids
98                     ).subscribe(
99                         at_event => {
100                             // check for event..
101                             const html = at_event.template_output().data();
102                             this.printer.print({
103                                 text: html,
104                                 printContext: 'default'
105                             });
106                         }
107                     );
108                 });
109                 break;
110
111             case 'email':
112                 this.basket.getRecordIds().then(ids => {
113                     this.net.request(
114                         'open-ils.search',
115                         'open-ils.search.biblio.record.email',
116                         this.auth.token(), ids
117                     ).toPromise(); // fire-and-forget
118                 });
119                 break;
120
121             case 'export_marc':
122                 this.router.navigate(
123                     ['/staff/cat/vandelay/export/basket'],
124                     {queryParamsHandling: 'merge'}
125                 );
126                 break;
127
128             case 'bucket':
129                 this.basket.getRecordIds().then(ids => {
130                     this.addToBucketDialog.bucketClass = 'biblio';
131                     this.addToBucketDialog.itemIds = ids;
132                     this.addToBucketDialog.open({size: 'lg'});
133                 });
134                 break;
135
136         }
137
138         // Resetting basketAction inside its onchange handler
139         // prevents the new value from propagating to Angular
140         // Reset after the current thread.
141         setTimeout(() => this.basketAction = ''); // reset
142     }
143 }
144
145