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