]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts
LP#1831786: add demo of cross-tab communications
[working/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     openEditor() {
156         this.fmRecordEditor.open({size: 'lg'}).then(
157             ok => { console.debug(ok); },
158             err => {
159                 if (err && err.dismissed) {
160                     console.debug('dialog was dismissed');
161                 } else {
162                     console.error(err);
163                 }
164             }
165         );
166     }
167
168     btGridRowClassCallback(row: any): string {
169         if (row.id() === 1) {
170             return 'text-uppercase font-weight-bold text-danger';
171         }
172     }
173
174     btGridRowFlairCallback(row: any): GridRowFlairEntry {
175         const flair = {icon: null, title: null};
176         if (row.id() === 2) {
177             flair.icon = 'priority_high';
178             flair.title = 'I Am ID 2';
179         } else if (row.id() === 3) {
180             flair.icon = 'not_interested';
181         }
182         return flair;
183     }
184
185     // apply to all 'name' columns regardless of row
186     btGridCellClassCallback(row: any, col: GridColumn): string {
187         if (col.name === 'name') {
188             if (row.id() === 7) {
189                 return 'text-lowercase font-weight-bold text-info';
190             }
191             return 'text-uppercase font-weight-bold text-success';
192         }
193     }
194
195     doPrint() {
196         this.printer.print({
197             template: this.printTemplate,
198             contextData: {world : this.world},
199             printContext: 'default'
200         });
201
202         this.printer.print({
203             text: '<b>hello</b>',
204             printContext: 'default'
205         });
206     }
207
208     printWithDialog() {
209         this.printer.print({
210             template: this.printTemplate,
211             contextData: {world : this.world},
212             printContext: 'default',
213             showDialog: true
214         });
215     }
216
217     changeDate(date) {
218         console.log('HERE WITH ' + date);
219         this.testDate = date;
220     }
221
222     showProgress() {
223         this.progressDialog.open();
224
225         // every 250ms emit x*10 for 0-10
226         observableTimer(0, 250).pipe(
227             map(x => x * 10),
228             take(11)
229         ).subscribe(
230             val => this.progressDialog.update({value: val, max: 100}),
231             err => {},
232             ()  => this.progressDialog.close()
233         );
234     }
235
236     testToast() {
237         this.toast.success('HELLO TOAST TEST');
238         setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
239     }
240
241     testStrings() {
242         this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
243             .then(txt => this.toast.success(txt));
244
245         setTimeout(() => {
246             this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
247                 .then(txt => this.toast.success(txt));
248         }, 4000);
249     }
250 }
251
252