b5f35722bb16968489eb3144afe29430bb94cd60
[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', { static: true })
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(action: string) {
41         this.basketAction = action;
42         console.debug('Performing basket action', this.basketAction);
43
44         switch (this.basketAction) {
45             case 'view':
46                 // This does not propagate search params -- unclear if needed.
47                 this.router.navigate(['/staff/catalog/search'],
48                     {queryParams: {showBasket: true}});
49                 break;
50
51             case 'clear':
52                 this.basket.removeAllRecordIds();
53                 break;
54
55             case 'hold':
56                 this.basket.getRecordIds().then(ids => {
57                     this.router.navigate(['/staff/catalog/hold/T'],
58                         {queryParams: {target: ids}});
59                 });
60                 break;
61
62             case 'print':
63                 this.basket.getRecordIds().then(ids => {
64                     this.net.request(
65                         'open-ils.search',
66                         'open-ils.search.biblio.record.print', ids
67                     ).subscribe(
68                         at_event => {
69                             // check for event..
70                             const html = at_event.template_output().data();
71                             this.printer.print({
72                                 text: html,
73                                 printContext: 'default'
74                             });
75                         }
76                     );
77                 });
78                 break;
79
80             case 'email':
81                 this.basket.getRecordIds().then(ids => {
82                     this.net.request(
83                         'open-ils.search',
84                         'open-ils.search.biblio.record.email',
85                         this.auth.token(), ids
86                     ).toPromise(); // fire-and-forget
87                 });
88                 break;
89
90             case 'export_marc':
91                 this.router.navigate(
92                     ['/staff/cat/vandelay/export/basket'],
93                     {queryParamsHandling: 'merge'}
94                 );
95                 break;
96
97             case 'bucket':
98                 this.basket.getRecordIds().then(ids => {
99                     this.addToBucketDialog.bucketClass = 'biblio';
100                     this.addToBucketDialog.itemIds = ids;
101                     this.addToBucketDialog.open({size: 'lg'});
102                 });
103                 break;
104
105         }
106
107         // Resetting basketAction inside its onchange handler
108         // prevents the new value from propagating to Angular
109         // Reset after the current thread.
110         setTimeout(() => this.basketAction = ''); // reset
111     }
112 }
113
114