6d4e2eaafca96b6635411fa5466baabd3c560f5d
[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 import {FormatService} from '@eg/core/format.service';
17 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.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('fmRecordEditor')
34     private fmRecordEditor: FmRecordEditorComponent;
35
36     // @ViewChild('helloStr') private helloStr: StringComponent;
37
38     gridDataSource: GridDataSource = new GridDataSource();
39
40     cbEntries: ComboboxEntry[];
41     // supplier of async combobox data
42     cbAsyncSource: (term: string) => Observable<ComboboxEntry>;
43
44     btSource: GridDataSource = new GridDataSource();
45     world = 'world'; // for local template version
46     btGridTestContext: any = {hello : this.world};
47
48     renderLocal = false;
49
50     testDate: any;
51
52     testStr: string;
53     @Input() set testString(str: string) {
54         this.testStr = str;
55     }
56
57     oneBtype: IdlObject;
58
59     name = 'Jane';
60
61     dynamicTitleText: string;
62
63     complimentEvergreen: (rows: IdlObject[]) => void;
64     notOneSelectedRow: (rows: IdlObject[]) => boolean;
65
66     // selector field value on metarecord object
67     aMetarecord: string;
68
69     // cross-tab communications example
70     private sbChannel: any;
71     sbChannelText: string;
72
73     constructor(
74         private idl: IdlService,
75         private org: OrgService,
76         private pcrud: PcrudService,
77         private strings: StringService,
78         private toast: ToastService,
79         private format: FormatService,
80         private printer: PrintService
81     ) {
82         // BroadcastChannel is not yet defined in PhantomJS and elsewhere
83         this.sbChannel = (typeof BroadcastChannel === 'undefined') ?
84             {} : new BroadcastChannel('eg.sbChannel');
85         this.sbChannel.onmessage = (e) => this.sbChannelHandler(e);
86     }
87
88     ngOnInit() {
89
90         this.gridDataSource.data = [
91             {name: 'Jane', state: 'AZ'},
92             {name: 'Al', state: 'CA'},
93             {name: 'The Tick', state: 'TX'}
94         ];
95
96         this.pcrud.retrieveAll('cmrcfld', {order_by: {cmrcfld: 'name'}})
97         .subscribe(format => {
98             if (!this.cbEntries) { this.cbEntries = []; }
99             this.cbEntries.push({id: format.id(), label: format.name()});
100         });
101
102         this.cbAsyncSource = term => {
103             return this.pcrud.search(
104                 'cmrcfld',
105                 {name: {'ilike': `%${term}%`}}, // could -or search on label
106                 {order_by: {cmrcfld: 'name'}}
107             ).pipe(map(marcField => {
108                 return {id: marcField.id(), label: marcField.name()};
109             }));
110         };
111
112         this.btSource.getRows = (pager: Pager, sort: any[]) => {
113
114             const orderBy: any = {cbt: 'name'};
115             if (sort.length) {
116                 orderBy.cbt = sort[0].name + ' ' + sort[0].dir;
117             }
118
119             return this.pcrud.retrieveAll('cbt', {
120                 offset: pager.offset,
121                 limit: pager.limit,
122                 order_by: orderBy
123             }).pipe(map(cbt => {
124                 // example of inline fleshing
125                 cbt.owner(this.org.get(cbt.owner()));
126                 cbt.datetime_test = new Date();
127                 this.oneBtype = cbt;
128                 return cbt;
129             }));
130         };
131
132         this.complimentEvergreen = (rows: IdlObject[]) => alert('Evergreen is great!');
133         this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length !== 1);
134
135         this.pcrud.retrieve('bre', 1, {}, {fleshSelectors: true})
136         .subscribe(bib => {
137             // Format service will automatically find the selector
138             // value to display from our fleshed metarecord field.
139             this.aMetarecord = this.format.transform({
140                 value: bib.metarecord(),
141                 idlClass: 'bre',
142                 idlField: 'metarecord'
143             });
144         });
145     }
146
147     sbChannelHandler = msg => {
148         setTimeout(() => { this.sbChannelText = msg.data.msg; });
149     }
150
151     sendMessage($event) {
152         this.sbChannel.postMessage({msg : $event.target.value});
153     }
154
155     // Example of click handler for row action
156     complimentEvergreen2(rows: IdlObject[]) {
157         alert('I know, right?');
158     }
159
160     openEditor() {
161         this.fmRecordEditor.open({size: 'lg'}).then(
162             ok => { console.debug(ok); },
163             err => {
164                 if (err && err.dismissed) {
165                     console.debug('dialog was dismissed');
166                 } else {
167                     console.error(err);
168                 }
169             }
170         );
171     }
172
173     btGridRowClassCallback(row: any): string {
174         if (row.id() === 1) {
175             return 'text-uppercase font-weight-bold text-danger';
176         }
177     }
178
179     btGridRowFlairCallback(row: any): GridRowFlairEntry {
180         const flair = {icon: null, title: null};
181         if (row.id() === 2) {
182             flair.icon = 'priority_high';
183             flair.title = 'I Am ID 2';
184         } else if (row.id() === 3) {
185             flair.icon = 'not_interested';
186         }
187         return flair;
188     }
189
190     // apply to all 'name' columns regardless of row
191     btGridCellClassCallback(row: any, col: GridColumn): string {
192         if (col.name === 'name') {
193             if (row.id() === 7) {
194                 return 'text-lowercase font-weight-bold text-info';
195             }
196             return 'text-uppercase font-weight-bold text-success';
197         }
198     }
199
200     doPrint() {
201         this.printer.print({
202             template: this.printTemplate,
203             contextData: {world : this.world},
204             printContext: 'default'
205         });
206
207         this.printer.print({
208             text: '<b>hello</b>',
209             printContext: 'default'
210         });
211     }
212
213     printWithDialog() {
214         this.printer.print({
215             template: this.printTemplate,
216             contextData: {world : this.world},
217             printContext: 'default',
218             showDialog: true
219         });
220     }
221
222     changeDate(date) {
223         console.log('HERE WITH ' + date);
224         this.testDate = date;
225     }
226
227     showProgress() {
228         this.progressDialog.open();
229
230         // every 250ms emit x*10 for 0-10
231         observableTimer(0, 250).pipe(
232             map(x => x * 10),
233             take(11)
234         ).subscribe(
235             val => this.progressDialog.update({value: val, max: 100}),
236             err => {},
237             ()  => this.progressDialog.close()
238         );
239     }
240
241     testToast() {
242         this.toast.success('HELLO TOAST TEST');
243         setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
244     }
245
246     testStrings() {
247         this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
248             .then(txt => this.toast.success(txt));
249
250         setTimeout(() => {
251             this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
252                 .then(txt => this.toast.success(txt));
253         }, 4000);
254     }
255 }
256
257