]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts
9b058cd5b1cc1b6791654099a795bbe093478cfd
[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     constructor(
70         private idl: IdlService,
71         private org: OrgService,
72         private pcrud: PcrudService,
73         private strings: StringService,
74         private toast: ToastService,
75         private format: FormatService,
76         private printer: PrintService
77     ) {
78     }
79
80     ngOnInit() {
81
82         this.gridDataSource.data = [
83             {name: 'Jane', state: 'AZ'},
84             {name: 'Al', state: 'CA'},
85             {name: 'The Tick', state: 'TX'}
86         ];
87
88         this.pcrud.retrieveAll('cmrcfld', {order_by: {cmrcfld: 'name'}})
89         .subscribe(format => {
90             if (!this.cbEntries) { this.cbEntries = []; }
91             this.cbEntries.push({id: format.id(), label: format.name()});
92         });
93
94         this.cbAsyncSource = term => {
95             return this.pcrud.search(
96                 'cmrcfld',
97                 {name: {'ilike': `%${term}%`}}, // could -or search on label
98                 {order_by: {cmrcfld: 'name'}}
99             ).pipe(map(marcField => {
100                 return {id: marcField.id(), label: marcField.name()};
101             }));
102         };
103
104         this.btSource.getRows = (pager: Pager, sort: any[]) => {
105
106             const orderBy: any = {cbt: 'name'};
107             if (sort.length) {
108                 orderBy.cbt = sort[0].name + ' ' + sort[0].dir;
109             }
110
111             return this.pcrud.retrieveAll('cbt', {
112                 offset: pager.offset,
113                 limit: pager.limit,
114                 order_by: orderBy
115             }).pipe(map(cbt => {
116                 // example of inline fleshing
117                 cbt.owner(this.org.get(cbt.owner()));
118                 cbt.datetime_test = new Date();
119                 this.oneBtype = cbt;
120                 return cbt;
121             }));
122         };
123
124         this.complimentEvergreen = (rows: IdlObject[]) => alert('Evergreen is great!');
125         this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length !== 1);
126
127         this.pcrud.retrieve('bre', 1, {}, {fleshSelectors: true})
128         .subscribe(bib => {
129             // Format service will automatically find the selector
130             // value to display from our fleshed metarecord field.
131             this.aMetarecord = this.format.transform({
132                 value: bib.metarecord(),
133                 idlClass: 'bre',
134                 idlField: 'metarecord'
135             });
136         });
137     }
138
139     openEditor() {
140         this.fmRecordEditor.open({size: 'lg'}).then(
141             ok => { console.debug(ok); },
142             err => {
143                 if (err && err.dismissed) {
144                     console.debug('dialog was dismissed');
145                 } else {
146                     console.error(err);
147                 }
148             }
149         );
150     }
151
152     btGridRowClassCallback(row: any): string {
153         if (row.id() === 1) {
154             return 'text-uppercase font-weight-bold text-danger';
155         }
156     }
157
158     btGridRowFlairCallback(row: any): GridRowFlairEntry {
159         const flair = {icon: null, title: null};
160         if (row.id() === 2) {
161             flair.icon = 'priority_high';
162             flair.title = 'I Am ID 2';
163         } else if (row.id() === 3) {
164             flair.icon = 'not_interested';
165         }
166         return flair;
167     }
168
169     // apply to all 'name' columns regardless of row
170     btGridCellClassCallback(row: any, col: GridColumn): string {
171         if (col.name === 'name') {
172             if (row.id() === 7) {
173                 return 'text-lowercase font-weight-bold text-info';
174             }
175             return 'text-uppercase font-weight-bold text-success';
176         }
177     }
178
179     doPrint() {
180         this.printer.print({
181             template: this.printTemplate,
182             contextData: {world : this.world},
183             printContext: 'default'
184         });
185
186         this.printer.print({
187             text: '<b>hello</b>',
188             printContext: 'default'
189         });
190
191     }
192
193     changeDate(date) {
194         console.log('HERE WITH ' + date);
195         this.testDate = date;
196     }
197
198     showProgress() {
199         this.progressDialog.open();
200
201         // every 250ms emit x*10 for 0-10
202         observableTimer(0, 250).pipe(
203             map(x => x * 10),
204             take(11)
205         ).subscribe(
206             val => this.progressDialog.update({value: val, max: 100}),
207             err => {},
208             ()  => this.progressDialog.close()
209         );
210     }
211
212     testToast() {
213         this.toast.success('HELLO TOAST TEST');
214         setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
215     }
216
217     testStrings() {
218         this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
219             .then(txt => this.toast.success(txt));
220
221         setTimeout(() => {
222             this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
223                 .then(txt => this.toast.success(txt));
224         }, 4000);
225     }
226 }
227
228