]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts
d3410b6d050adb88936deb4e621a29d5835f502f
[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 import {FormGroup, FormControl} from '@angular/forms';
19 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
20
21 @Component({
22   templateUrl: 'sandbox.component.html'
23 })
24 export class SandboxComponent implements OnInit {
25
26     @ViewChild('progressDialog')
27     private progressDialog: ProgressDialogComponent;
28
29     @ViewChild('dateSelect')
30     private dateSelector: DateSelectComponent;
31
32     @ViewChild('printTemplate')
33     private printTemplate: TemplateRef<any>;
34
35     @ViewChild('fmRecordEditor')
36     private fmRecordEditor: FmRecordEditorComponent;
37
38     @ViewChild('numConfirmDialog')
39     private numConfirmDialog: ConfirmDialogComponent;
40
41     public numThings = 0;
42
43     @ViewChild('bresvEditor')
44     private bresvEditor: FmRecordEditorComponent;
45
46
47     // @ViewChild('helloStr') private helloStr: StringComponent;
48
49     gridDataSource: GridDataSource = new GridDataSource();
50
51     cbEntries: ComboboxEntry[];
52     // supplier of async combobox data
53     cbAsyncSource: (term: string) => Observable<ComboboxEntry>;
54
55     btSource: GridDataSource = new GridDataSource();
56     world = 'world'; // for local template version
57     btGridTestContext: any = {hello : this.world};
58
59     renderLocal = false;
60
61     testDate: any;
62
63     testStr: string;
64     @Input() set testString(str: string) {
65         this.testStr = str;
66     }
67
68     oneBtype: IdlObject;
69
70     name = 'Jane';
71
72     dynamicTitleText: string;
73
74     badOrgForm: FormGroup;
75
76     complimentEvergreen: (rows: IdlObject[]) => void;
77     notOneSelectedRow: (rows: IdlObject[]) => boolean;
78
79     // selector field value on metarecord object
80     aMetarecord: string;
81
82     // cross-tab communications example
83     private sbChannel: any;
84     sbChannelText: string;
85
86     constructor(
87         private idl: IdlService,
88         private org: OrgService,
89         private pcrud: PcrudService,
90         private strings: StringService,
91         private toast: ToastService,
92         private format: FormatService,
93         private printer: PrintService
94     ) {
95         // BroadcastChannel is not yet defined in PhantomJS and elsewhere
96         this.sbChannel = (typeof BroadcastChannel === 'undefined') ?
97             {} : new BroadcastChannel('eg.sbChannel');
98         this.sbChannel.onmessage = (e) => this.sbChannelHandler(e);
99     }
100
101     ngOnInit() {
102         this.badOrgForm = new FormGroup({
103             'badOrgSelector': new FormControl(
104                 {'id': 4, 'includeAncestors': false, 'includeDescendants': true}, (c: FormControl) => {
105                     // An Angular custom validator
106                     if (c.value.orgIds && c.value.orgIds.length > 5) {
107                         return { tooMany: 'That\'s too many fancy libraries!' };
108                     } else {
109                         return null;
110                     }
111             } )
112         });
113
114         this.badOrgForm.get('badOrgSelector').valueChanges.subscribe(bad => {
115             this.toast.danger('The fanciest libraries are: ' + JSON.stringify(bad.orgIds));
116         });
117
118         this.gridDataSource.data = [
119             {name: 'Jane', state: 'AZ'},
120             {name: 'Al', state: 'CA'},
121             {name: 'The Tick', state: 'TX'}
122         ];
123
124         this.pcrud.retrieveAll('cmrcfld', {order_by: {cmrcfld: 'name'}})
125         .subscribe(format => {
126             if (!this.cbEntries) { this.cbEntries = []; }
127             this.cbEntries.push({id: format.id(), label: format.name()});
128         });
129
130         this.cbAsyncSource = term => {
131             return this.pcrud.search(
132                 'cmrcfld',
133                 {name: {'ilike': `%${term}%`}}, // could -or search on label
134                 {order_by: {cmrcfld: 'name'}}
135             ).pipe(map(marcField => {
136                 return {id: marcField.id(), label: marcField.name()};
137             }));
138         };
139
140         this.btSource.getRows = (pager: Pager, sort: any[]) => {
141
142             const orderBy: any = {cbt: 'name'};
143             if (sort.length) {
144                 orderBy.cbt = sort[0].name + ' ' + sort[0].dir;
145             }
146
147             return this.pcrud.retrieveAll('cbt', {
148                 offset: pager.offset,
149                 limit: pager.limit,
150                 order_by: orderBy
151             }).pipe(map(cbt => {
152                 // example of inline fleshing
153                 cbt.owner(this.org.get(cbt.owner()));
154                 cbt.datetime_test = new Date();
155                 this.oneBtype = cbt;
156                 return cbt;
157             }));
158         };
159
160         this.complimentEvergreen = (rows: IdlObject[]) => alert('Evergreen is great!');
161         this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length !== 1);
162
163         this.pcrud.retrieve('bre', 1, {}, {fleshSelectors: true})
164         .subscribe(bib => {
165             // Format service will automatically find the selector
166             // value to display from our fleshed metarecord field.
167             this.aMetarecord = this.format.transform({
168                 value: bib.metarecord(),
169                 idlClass: 'bre',
170                 idlField: 'metarecord'
171             });
172         });
173
174         const b = this.idl.create('bresv');
175         b.cancel_time('2019-03-25T11:07:59-0400');
176         this.bresvEditor.mode = 'create';
177         this.bresvEditor.record = b;
178     }
179
180     sbChannelHandler = msg => {
181         setTimeout(() => { this.sbChannelText = msg.data.msg; });
182     }
183
184     sendMessage($event) {
185         this.sbChannel.postMessage({msg : $event.target.value});
186     }
187
188     // Example of click handler for row action
189     complimentEvergreen2(rows: IdlObject[]) {
190         alert('I know, right?');
191     }
192
193     openEditor() {
194         this.fmRecordEditor.open({size: 'lg'}).subscribe(
195             pcrudResult => console.debug('Record editor performed action'),
196             err => console.error(err),
197             () => console.debug('Dialog closed')
198         );
199     }
200
201     btGridRowClassCallback(row: any): string {
202         if (row.id() === 1) {
203             return 'text-uppercase font-weight-bold text-danger';
204         }
205     }
206
207     btGridRowFlairCallback(row: any): GridRowFlairEntry {
208         const flair = {icon: null, title: null};
209         if (row.id() === 2) {
210             flair.icon = 'priority_high';
211             flair.title = 'I Am ID 2';
212         } else if (row.id() === 3) {
213             flair.icon = 'not_interested';
214         }
215         return flair;
216     }
217
218     // apply to all 'name' columns regardless of row
219     btGridCellClassCallback(row: any, col: GridColumn): string {
220         if (col.name === 'name') {
221             if (row.id() === 7) {
222                 return 'text-lowercase font-weight-bold text-info';
223             }
224             return 'text-uppercase font-weight-bold text-success';
225         }
226     }
227
228     doPrint() {
229         this.printer.print({
230             template: this.printTemplate,
231             contextData: {world : this.world},
232             printContext: 'default'
233         });
234
235         this.printer.print({
236             text: '<b>hello</b>',
237             printContext: 'default'
238         });
239     }
240
241     printWithDialog() {
242         this.printer.print({
243             template: this.printTemplate,
244             contextData: {world : this.world},
245             printContext: 'default',
246             showDialog: true
247         });
248     }
249
250     changeDate(date) {
251         console.log('HERE WITH ' + date);
252         this.testDate = date;
253     }
254
255     showProgress() {
256         this.progressDialog.open();
257
258         // every 250ms emit x*10 for 0-10
259         observableTimer(0, 250).pipe(
260             map(x => x * 10),
261             take(11)
262         ).subscribe(
263             val => this.progressDialog.update({value: val, max: 100}),
264             err => {},
265             ()  => this.progressDialog.close()
266         );
267     }
268
269     testToast() {
270         this.toast.success('HELLO TOAST TEST');
271         setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
272     }
273
274     testStrings() {
275         this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
276             .then(txt => this.toast.success(txt));
277
278         setTimeout(() => {
279             this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
280                 .then(txt => this.toast.success(txt));
281         }, 4000);
282     }
283
284     confirmNumber(num: number): void {
285       this.numThings = num;
286       console.log(this.numThings);
287       this.numConfirmDialog.open();
288     }
289
290 }
291
292