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