LP1819053 Angular staff catalog basket export
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / basket-actions.component.ts
1 import {Component, 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 {BucketDialogComponent
8     } from '@eg/staff/share/buckets/bucket-dialog.component';
9
10 @Component({
11   selector: 'eg-catalog-basket-actions',
12   templateUrl: 'basket-actions.component.html'
13 })
14 export class BasketActionsComponent implements OnInit {
15
16     basketAction: string;
17
18     @ViewChild('addBasketToBucketDialog')
19         addToBucketDialog: BucketDialogComponent;
20
21     constructor(
22         private router: Router,
23         private net: NetService,
24         private auth: AuthService,
25         private printer: PrintService,
26         private basket: BasketService
27     ) {
28         this.basketAction = '';
29     }
30
31     ngOnInit() {
32     }
33
34     basketCount(): number {
35         return this.basket.recordCount();
36     }
37
38     // TODO: confirmation dialogs?
39
40     applyAction() {
41         console.debug('Performing basket action', this.basketAction);
42
43         switch (this.basketAction) {
44             case 'view':
45                 // This does not propagate search params -- unclear if needed.
46                 this.router.navigate(['/staff/catalog/search'],
47                     {queryParams: {showBasket: true}});
48                 break;
49
50             case 'clear':
51                 this.basket.removeAllRecordIds();
52                 break;
53
54             case 'hold':
55                 this.basket.getRecordIds().then(ids => {
56                     this.router.navigate(['/staff/catalog/hold/T'],
57                         {queryParams: {target: ids}});
58                 });
59                 break;
60
61             case 'print':
62                 this.basket.getRecordIds().then(ids => {
63                     this.net.request(
64                         'open-ils.search',
65                         'open-ils.search.biblio.record.print', ids
66                     ).subscribe(
67                         at_event => {
68                             // check for event..
69                             const html = at_event.template_output().data();
70                             this.printer.print({
71                                 text: html,
72                                 printContext: 'default'
73                             });
74                         }
75                     );
76                 });
77                 break;
78
79             case 'email':
80                 this.basket.getRecordIds().then(ids => {
81                     this.net.request(
82                         'open-ils.search',
83                         'open-ils.search.biblio.record.email',
84                         this.auth.token(), ids
85                     ).toPromise(); // fire-and-forget
86                 });
87                 break;
88
89             case 'export_marc':
90                 this.router.navigate(
91                     ['/staff/cat/vandelay/export/basket'],
92                     {queryParamsHandling: 'merge'}
93                 );
94                 break;
95
96             case 'bucket':
97                 this.basket.getRecordIds().then(ids => {
98                     this.addToBucketDialog.bucketClass = 'biblio';
99                     this.addToBucketDialog.itemIds = ids;
100                     this.addToBucketDialog.open({size: 'lg'}).then(
101                         ok => {},
102                         dismissed => {}
103                     );
104                 });
105                 break;
106
107         }
108
109         // Resetting basketAction inside its onchange handler
110         // prevents the new value from propagating to Angular
111         // Reset after the current thread.
112         setTimeout(() => this.basketAction = ''); // reset
113     }
114 }
115
116