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 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
18 import {FormGroup, FormControl} from '@angular/forms';
19 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
22 templateUrl: 'sandbox.component.html'
24 export class SandboxComponent implements OnInit {
26 @ViewChild('progressDialog')
27 private progressDialog: ProgressDialogComponent;
29 @ViewChild('dateSelect')
30 private dateSelector: DateSelectComponent;
32 @ViewChild('printTemplate')
33 private printTemplate: TemplateRef<any>;
35 @ViewChild('fmRecordEditor')
36 private fmRecordEditor: FmRecordEditorComponent;
38 @ViewChild('numConfirmDialog')
39 private numConfirmDialog: ConfirmDialogComponent;
43 @ViewChild('bresvEditor')
44 private bresvEditor: FmRecordEditorComponent;
47 // @ViewChild('helloStr') private helloStr: StringComponent;
49 gridDataSource: GridDataSource = new GridDataSource();
51 cbEntries: ComboboxEntry[];
52 // supplier of async combobox data
53 cbAsyncSource: (term: string) => Observable<ComboboxEntry>;
55 btSource: GridDataSource = new GridDataSource();
56 world = 'world'; // for local template version
57 btGridTestContext: any = {hello : this.world};
64 @Input() set testString(str: string) {
72 dynamicTitleText: string;
74 badOrgForm: FormGroup;
76 complimentEvergreen: (rows: IdlObject[]) => void;
77 notOneSelectedRow: (rows: IdlObject[]) => boolean;
79 // selector field value on metarecord object
82 // cross-tab communications example
83 private sbChannel: any;
84 sbChannelText: string;
87 private idl: IdlService,
88 private org: OrgService,
89 private pcrud: PcrudService,
90 private strings: StringService,
91 private toast: ToastService,
92 private format: FormatService,
93 private printer: PrintService
95 // BroadcastChannel is not yet defined in PhantomJS and elsewhere
96 this.sbChannel = (typeof BroadcastChannel === 'undefined') ?
97 {} : new BroadcastChannel('eg.sbChannel');
98 this.sbChannel.onmessage = (e) => this.sbChannelHandler(e);
102 this.badOrgForm = new FormGroup({
103 'badOrgSelector': new FormControl(
104 {'id': 4, 'includeAncestors': false, 'includeDescendants': true}, (c: FormControl) => {
105 // An Angular custom validator
106 if (c.value.orgIds && c.value.orgIds.length > 5) {
107 return { tooMany: 'That\'s too many fancy libraries!' };
114 this.badOrgForm.get('badOrgSelector').valueChanges.subscribe(bad => {
115 this.toast.danger('The fanciest libraries are: ' + JSON.stringify(bad.orgIds));
118 this.gridDataSource.data = [
119 {name: 'Jane', state: 'AZ'},
120 {name: 'Al', state: 'CA'},
121 {name: 'The Tick', state: 'TX'}
124 this.pcrud.retrieveAll('cmrcfld', {order_by: {cmrcfld: 'name'}})
125 .subscribe(format => {
126 if (!this.cbEntries) { this.cbEntries = []; }
127 this.cbEntries.push({id: format.id(), label: format.name()});
130 this.cbAsyncSource = term => {
131 return this.pcrud.search(
133 {name: {'ilike': `%${term}%`}}, // could -or search on label
134 {order_by: {cmrcfld: 'name'}}
135 ).pipe(map(marcField => {
136 return {id: marcField.id(), label: marcField.name()};
140 this.btSource.getRows = (pager: Pager, sort: any[]) => {
142 const orderBy: any = {cbt: 'name'};
144 orderBy.cbt = sort[0].name + ' ' + sort[0].dir;
147 return this.pcrud.retrieveAll('cbt', {
148 offset: pager.offset,
152 // example of inline fleshing
153 cbt.owner(this.org.get(cbt.owner()));
154 cbt.datetime_test = new Date();
160 this.complimentEvergreen = (rows: IdlObject[]) => alert('Evergreen is great!');
161 this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length !== 1);
163 this.pcrud.retrieve('bre', 1, {}, {fleshSelectors: true})
165 // Format service will automatically find the selector
166 // value to display from our fleshed metarecord field.
167 this.aMetarecord = this.format.transform({
168 value: bib.metarecord(),
170 idlField: 'metarecord'
174 const b = this.idl.create('bresv');
175 b.cancel_time('2019-03-25T11:07:59-0400');
176 this.bresvEditor.mode = 'create';
177 this.bresvEditor.record = b;
180 sbChannelHandler = msg => {
181 setTimeout(() => { this.sbChannelText = msg.data.msg; });
184 sendMessage($event) {
185 this.sbChannel.postMessage({msg : $event.target.value});
188 // Example of click handler for row action
189 complimentEvergreen2(rows: IdlObject[]) {
190 alert('I know, right?');
194 this.fmRecordEditor.open({size: 'lg'}).subscribe(
195 pcrudResult => console.debug('Record editor performed action'),
196 err => console.error(err),
197 () => console.debug('Dialog closed')
201 btGridRowClassCallback(row: any): string {
202 if (row.id() === 1) {
203 return 'text-uppercase font-weight-bold text-danger';
207 btGridRowFlairCallback(row: any): GridRowFlairEntry {
208 const flair = {icon: null, title: null};
209 if (row.id() === 2) {
210 flair.icon = 'priority_high';
211 flair.title = 'I Am ID 2';
212 } else if (row.id() === 3) {
213 flair.icon = 'not_interested';
218 // apply to all 'name' columns regardless of row
219 btGridCellClassCallback(row: any, col: GridColumn): string {
220 if (col.name === 'name') {
221 if (row.id() === 7) {
222 return 'text-lowercase font-weight-bold text-info';
224 return 'text-uppercase font-weight-bold text-success';
230 template: this.printTemplate,
231 contextData: {world : this.world},
232 printContext: 'default'
236 text: '<b>hello</b>',
237 printContext: 'default'
243 template: this.printTemplate,
244 contextData: {world : this.world},
245 printContext: 'default',
251 console.log('HERE WITH ' + date);
252 this.testDate = date;
256 this.progressDialog.open();
258 // every 250ms emit x*10 for 0-10
259 observableTimer(0, 250).pipe(
263 val => this.progressDialog.update({value: val, max: 100}),
265 () => this.progressDialog.close()
270 this.toast.success('HELLO TOAST TEST');
271 setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
275 this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
276 .then(txt => this.toast.success(txt));
279 this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
280 .then(txt => this.toast.success(txt));
284 confirmNumber(num: number): void {
285 this.numThings = num;
286 console.log(this.numThings);
287 this.numConfirmDialog.open();