]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts
4ee4ebc0f73d92a8512d35b3ced1fed60cf51632
[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
18 @Component({
19   templateUrl: 'sandbox.component.html'
20 })
21 export class SandboxComponent implements OnInit {
22
23     @ViewChild('progressDialog')
24     private progressDialog: ProgressDialogComponent;
25
26     @ViewChild('dateSelect')
27     private dateSelector: DateSelectComponent;
28
29     @ViewChild('printTemplate')
30     private printTemplate: TemplateRef<any>;
31
32     // @ViewChild('helloStr') private helloStr: StringComponent;
33
34     gridDataSource: GridDataSource = new GridDataSource();
35
36     cbEntries: ComboboxEntry[];
37     // supplier of async combobox data
38     cbAsyncSource: (term: string) => Observable<ComboboxEntry>;
39
40     btSource: GridDataSource = new GridDataSource();
41     world = 'world'; // for local template version
42     btGridTestContext: any = {hello : this.world};
43
44     renderLocal = false;
45
46     testDate: any;
47
48     testStr: string;
49     @Input() set testString(str: string) {
50         this.testStr = str;
51     }
52
53     oneBtype: IdlObject;
54
55     name = 'Jane';
56
57     dynamicTitleText: string;
58
59     complimentEvergreen: (rows: IdlObject[]) => void;
60     notOneSelectedRow: (rows: IdlObject[]) => boolean;
61
62     // selector field value on metarecord object
63     aMetarecord: string;
64
65     constructor(
66         private idl: IdlService,
67         private org: OrgService,
68         private pcrud: PcrudService,
69         private strings: StringService,
70         private toast: ToastService,
71         private format: FormatService,
72         private printer: PrintService
73     ) {
74     }
75
76     ngOnInit() {
77
78         this.gridDataSource.data = [
79             {name: 'Jane', state: 'AZ'},
80             {name: 'Al', state: 'CA'},
81             {name: 'The Tick', state: 'TX'}
82         ];
83
84         this.pcrud.retrieveAll('cmrcfld', {order_by: {cmrcfld: 'name'}})
85         .subscribe(format => {
86             if (!this.cbEntries) { this.cbEntries = []; }
87             this.cbEntries.push({id: format.id(), label: format.name()});
88         });
89
90         this.cbAsyncSource = term => {
91             return this.pcrud.search(
92                 'cmrcfld',
93                 {name: {'ilike': `%${term}%`}}, // could -or search on label
94                 {order_by: {cmrcfld: 'name'}}
95             ).pipe(map(marcField => {
96                 return {id: marcField.id(), label: marcField.name()};
97             }));
98         };
99
100         this.btSource.getRows = (pager: Pager, sort: any[]) => {
101
102             const orderBy: any = {cbt: 'name'};
103             if (sort.length) {
104                 orderBy.cbt = sort[0].name + ' ' + sort[0].dir;
105             }
106
107             return this.pcrud.retrieveAll('cbt', {
108                 offset: pager.offset,
109                 limit: pager.limit,
110                 order_by: orderBy
111             }).pipe(map(cbt => {
112                 // example of inline fleshing
113                 cbt.owner(this.org.get(cbt.owner()));
114                 cbt.datetime_test = new Date();
115                 this.oneBtype = cbt;
116                 return cbt;
117             }));
118         };
119
120         this.complimentEvergreen = (rows: IdlObject[]) => alert('Evergreen is great!');
121         this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length !== 1);
122
123         this.pcrud.retrieve('bre', 1, {}, {fleshSelectors: true})
124         .subscribe(bib => {
125             // Format service will automatically find the selector
126             // value to display from our fleshed metarecord field.
127             this.aMetarecord = this.format.transform({
128                 value: bib.metarecord(),
129                 idlClass: 'bre',
130                 idlField: 'metarecord'
131             });
132         });
133     }
134
135     btGridRowClassCallback(row: any): string {
136         if (row.id() === 1) {
137             return 'text-uppercase font-weight-bold text-danger';
138         }
139     }
140
141     btGridRowFlairCallback(row: any): GridRowFlairEntry {
142         const flair = {icon: null, title: null};
143         if (row.id() === 2) {
144             flair.icon = 'priority_high';
145             flair.title = 'I Am ID 2';
146         } else if (row.id() === 3) {
147             flair.icon = 'not_interested';
148         }
149         return flair;
150     }
151
152     // apply to all 'name' columns regardless of row
153     btGridCellClassCallback(row: any, col: GridColumn): string {
154         if (col.name === 'name') {
155             if (row.id() === 7) {
156                 return 'text-lowercase font-weight-bold text-info';
157             }
158             return 'text-uppercase font-weight-bold text-success';
159         }
160     }
161
162     doPrint() {
163         this.printer.print({
164             template: this.printTemplate,
165             contextData: {world : this.world},
166             printContext: 'default'
167         });
168
169         this.printer.print({
170             text: '<b>hello</b>',
171             printContext: 'default'
172         });
173
174     }
175
176     changeDate(date) {
177         console.log('HERE WITH ' + date);
178         this.testDate = date;
179     }
180
181     showProgress() {
182         this.progressDialog.open();
183
184         // every 250ms emit x*10 for 0-10
185         observableTimer(0, 250).pipe(
186             map(x => x * 10),
187             take(11)
188         ).subscribe(
189             val => this.progressDialog.update({value: val, max: 100}),
190             err => {},
191             ()  => this.progressDialog.close()
192         );
193     }
194
195     testToast() {
196         this.toast.success('HELLO TOAST TEST');
197         setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
198     }
199
200     testStrings() {
201         this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
202             .then(txt => this.toast.success(txt));
203
204         setTimeout(() => {
205             this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
206                 .then(txt => this.toast.success(txt));
207         }, 4000);
208     }
209 }
210
211