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