]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/grid/grid-print.component.ts
LP2061136 - Stamping 1405 DB upgrade script
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / grid / grid-print.component.ts
1 import {Component, Input, TemplateRef, ViewChild} from '@angular/core';
2 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
3 import {PrintService} from '@eg/share/print/print.service';
4 import {GridContext} from '@eg/share/grid/grid';
5
6 @Component({
7     selector: 'eg-grid-print',
8     templateUrl: './grid-print.component.html'
9 })
10
11 /**
12  */
13 export class GridPrintComponent {
14
15     @Input() gridContext: GridContext;
16     @ViewChild('printTemplate', { static: true }) private printTemplate: TemplateRef<any>;
17     @ViewChild('progressDialog', { static: true })
18     private progressDialog: ProgressDialogComponent;
19
20     constructor(private printer: PrintService) {}
21
22     printGrid() {
23         this.progressDialog.open();
24         const columns = this.gridContext.columnSet.displayColumns();
25         const textItems = {columns: columns, rows: []};
26
27         this.gridContext.getAllRowsAsText().subscribe(
28             row => {
29                 this.progressDialog.increment();
30                 textItems.rows.push(row);
31             },
32             (err: unknown) => this.progressDialog.close(),
33             ()  => {
34                 this.progressDialog.close();
35                 this.printer.print({
36                     template: this.printTemplate,
37                     contextData: textItems,
38                     printContext: 'default'
39                 });
40             }
41         );
42     }
43
44     printSelectedRows(): void {
45         const columns = this.gridContext.columnSet.displayColumns();
46         const rows = this.gridContext.rowSelector.selected()
47             .reduce<{text: any; pos: number}[]>((pairs, index) => {
48                 const pos = this.gridContext.getRowPosition(index);
49                 if (pos === undefined) {return pairs;}
50
51                 const row = this.gridContext.dataSource.data[pos];
52                 if (row === undefined) {return pairs;}
53
54                 const text = this.gridContext.getRowAsFlatText(row);
55                 return pairs.concat({text, pos});
56             }, [])
57             .sort(({pos: a}, {pos: b}) => a - b)
58             .map(({text}) => text);
59
60         this.printer.print({
61             template: this.printTemplate,
62             contextData: {columns, rows},
63             printContext: 'default'
64         });
65     }
66 }
67
68