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