LP1808268: Add [disable] option to <eg-grid-toolbar-action> in eg2
[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                 this.oneBtype = cbt;
110                 return cbt;
111             }));
112         };
113
114         this.complimentEvergreen = (rows: IdlObject[]) => alert('Evergreen is great!');
115         this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length != 1);
116
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         Observable.timer(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