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