]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts
LP#1801984 Upgrading Angular 6 to Angular 7
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / sandbox / sandbox.component.ts
1
2 import {timer as observableTimer, Observable, of} from 'rxjs';
3 import {Component, OnInit, ViewChild, Input, TemplateRef} from '@angular/core';
4 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {StringService} from '@eg/share/string/string.service';
7 import {map, take} from 'rxjs/operators';
8 import {GridDataSource, GridColumn, GridRowFlairEntry} from '@eg/share/grid/grid';
9 import {IdlService, IdlObject} from '@eg/core/idl.service';
10 import {PcrudService} from '@eg/core/pcrud.service';
11 import {OrgService} from '@eg/core/org.service';
12 import {Pager} from '@eg/share/util/pager';
13 import {DateSelectComponent} from '@eg/share/date-select/date-select.component';
14 import {PrintService} from '@eg/share/print/print.service';
15 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
16
17 @Component({
18   templateUrl: 'sandbox.component.html'
19 })
20 export class SandboxComponent implements OnInit {
21
22     @ViewChild('progressDialog')
23     private progressDialog: ProgressDialogComponent;
24
25     @ViewChild('dateSelect')
26     private dateSelector: DateSelectComponent;
27
28     @ViewChild('printTemplate')
29     private printTemplate: TemplateRef<any>;
30
31     // @ViewChild('helloStr') private helloStr: StringComponent;
32
33     gridDataSource: GridDataSource = new GridDataSource();
34
35     cbEntries: ComboboxEntry[];
36     // supplier of async combobox data
37     cbAsyncSource: (term: string) => Observable<ComboboxEntry>;
38
39     btSource: GridDataSource = new GridDataSource();
40     world = 'world'; // for local template version
41     btGridTestContext: any = {hello : this.world};
42
43     renderLocal = false;
44
45     testDate: any;
46
47     testStr: string;
48     @Input() set testString(str: string) {
49         this.testStr = str;
50     }
51
52     oneBtype: IdlObject;
53
54     name = 'Jane';
55
56     complimentEvergreen: (rows: IdlObject[]) => void;
57     notOneSelectedRow: (rows: IdlObject[]) => boolean;
58
59     constructor(
60         private idl: IdlService,
61         private org: OrgService,
62         private pcrud: PcrudService,
63         private strings: StringService,
64         private toast: ToastService,
65         private printer: PrintService
66     ) {
67     }
68
69     ngOnInit() {
70
71         this.gridDataSource.data = [
72             {name: 'Jane', state: 'AZ'},
73             {name: 'Al', state: 'CA'},
74             {name: 'The Tick', state: 'TX'}
75         ];
76
77         this.pcrud.retrieveAll('cmrcfld', {order_by: {cmrcfld: 'name'}})
78         .subscribe(format => {
79             if (!this.cbEntries) { this.cbEntries = []; }
80             this.cbEntries.push({id: format.id(), label: format.name()});
81         });
82
83         this.cbAsyncSource = term => {
84             return this.pcrud.search(
85                 'cmrcfld',
86                 {name: {'ilike': `%${term}%`}}, // could -or search on label
87                 {order_by: {cmrcfld: 'name'}}
88             ).pipe(map(marcField => {
89                 return {id: marcField.id(), label: marcField.name()};
90             }));
91         };
92
93         this.btSource.getRows = (pager: Pager, sort: any[]) => {
94
95             const orderBy: any = {cbt: 'name'};
96             if (sort.length) {
97                 orderBy.cbt = sort[0].name + ' ' + sort[0].dir;
98             }
99
100             return this.pcrud.retrieveAll('cbt', {
101                 offset: pager.offset,
102                 limit: pager.limit,
103                 order_by: orderBy
104             }).pipe(map(cbt => {
105                 // example of inline fleshing
106                 cbt.owner(this.org.get(cbt.owner()));
107                 cbt.datetime_test = new Date();
108                 this.oneBtype = cbt;
109                 return cbt;
110             }));
111         };
112
113         this.complimentEvergreen = (rows: IdlObject[]) => alert('Evergreen is great!');
114         this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length !== 1);
115
116     }
117
118     btGridRowClassCallback(row: any): string {
119         if (row.id() === 1) {
120             return 'text-uppercase font-weight-bold text-danger';
121         }
122     }
123
124     btGridRowFlairCallback(row: any): GridRowFlairEntry {
125         const flair = {icon: null, title: null};
126         if (row.id() === 2) {
127             flair.icon = 'priority_high';
128             flair.title = 'I Am ID 2';
129         } else if (row.id() === 3) {
130             flair.icon = 'not_interested';
131         }
132         return flair;
133     }
134
135     // apply to all 'name' columns regardless of row
136     btGridCellClassCallback(row: any, col: GridColumn): string {
137         if (col.name === 'name') {
138             if (row.id() === 7) {
139                 return 'text-lowercase font-weight-bold text-info';
140             }
141             return 'text-uppercase font-weight-bold text-success';
142         }
143     }
144
145     doPrint() {
146         this.printer.print({
147             template: this.printTemplate,
148             contextData: {world : this.world},
149             printContext: 'default'
150         });
151
152         this.printer.print({
153             text: '<b>hello</b>',
154             printContext: 'default'
155         });
156
157     }
158
159     changeDate(date) {
160         console.log('HERE WITH ' + date);
161         this.testDate = date;
162     }
163
164     showProgress() {
165         this.progressDialog.open();
166
167         // every 250ms emit x*10 for 0-10
168         observableTimer(0, 250).pipe(
169             map(x => x * 10),
170             take(11)
171         ).subscribe(
172             val => this.progressDialog.update({value: val, max: 100}),
173             err => {},
174             ()  => this.progressDialog.close()
175         );
176     }
177
178     testToast() {
179         this.toast.success('HELLO TOAST TEST');
180         setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
181     }
182
183     testStrings() {
184         this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
185             .then(txt => this.toast.success(txt));
186
187         setTimeout(() => {
188             this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
189                 .then(txt => this.toast.success(txt));
190         }, 4000);
191     }
192 }
193
194