]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts
LP1808268 eg2 grid action disableOnRows sanity check
[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     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
120     btGridRowClassCallback(row: any): string {
121         if (row.id() === 1) {
122             return 'text-uppercase font-weight-bold text-danger';
123         }
124     }
125
126     btGridRowFlairCallback(row: any): GridRowFlairEntry {
127         const flair = {icon: null, title: null};
128         if (row.id() === 2) {
129             flair.icon = 'priority_high';
130             flair.title = 'I Am ID 2';
131         } else if (row.id() === 3) {
132             flair.icon = 'not_interested';
133         }
134         return flair;
135     }
136
137     // apply to all 'name' columns regardless of row
138     btGridCellClassCallback(row: any, col: GridColumn): string {
139         if (col.name === 'name') {
140             if (row.id() === 7) {
141                 return 'text-lowercase font-weight-bold text-info';
142             }
143             return 'text-uppercase font-weight-bold text-success';
144         }
145     }
146
147     doPrint() {
148         this.printer.print({
149             template: this.printTemplate,
150             contextData: {world : this.world},
151             printContext: 'default'
152         });
153
154         this.printer.print({
155             text: '<b>hello</b>',
156             printContext: 'default'
157         });
158
159     }
160
161     changeDate(date) {
162         console.log('HERE WITH ' + date);
163         this.testDate = date;
164     }
165
166     showProgress() {
167         this.progressDialog.open();
168
169         // every 250ms emit x*10 for 0-10
170         Observable.timer(0, 250).pipe(
171             map(x => x * 10),
172             take(11)
173         ).subscribe(
174             val => this.progressDialog.update({value: val, max: 100}),
175             err => {},
176             ()  => this.progressDialog.close()
177         );
178     }
179
180     testToast() {
181         this.toast.success('HELLO TOAST TEST');
182         setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
183     }
184
185     testStrings() {
186         this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
187             .then(txt => this.toast.success(txt));
188
189         setTimeout(() => {
190             this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
191                 .then(txt => this.toast.success(txt));
192         }, 4000);
193     }
194 }
195
196