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('helloStr') private helloStr: StringComponent;
45 gridDataSource: GridDataSource = new GridDataSource();
47 cbEntries: ComboboxEntry[];
48 // supplier of async combobox data
49 cbAsyncSource: (term: string) => Observable<ComboboxEntry>;
51 btSource: GridDataSource = new GridDataSource();
52 world = 'world'; // for local template version
53 btGridTestContext: any = {hello : this.world};
60 @Input() set testString(str: string) {
68 dynamicTitleText: string;
70 badOrgForm: FormGroup;
72 complimentEvergreen: (rows: IdlObject[]) => void;
73 notOneSelectedRow: (rows: IdlObject[]) => boolean;
75 // selector field value on metarecord object
78 // cross-tab communications example
79 private sbChannel: any;
80 sbChannelText: string;
83 private idl: IdlService,
84 private org: OrgService,
85 private pcrud: PcrudService,
86 private strings: StringService,
87 private toast: ToastService,
88 private format: FormatService,
89 private printer: PrintService
91 // BroadcastChannel is not yet defined in PhantomJS and elsewhere
92 this.sbChannel = (typeof BroadcastChannel === 'undefined') ?
93 {} : new BroadcastChannel('eg.sbChannel');
94 this.sbChannel.onmessage = (e) => this.sbChannelHandler(e);
98 this.badOrgForm = new FormGroup({
99 'badOrgSelector': new FormControl(
100 {'id': 4, 'includeAncestors': false, 'includeDescendants': true}, (c: FormControl) => {
101 // An Angular custom validator
102 if (c.value.orgIds && c.value.orgIds.length > 5) {
103 return { tooMany: 'That\'s too many fancy libraries!' };
110 this.badOrgForm.get('badOrgSelector').valueChanges.subscribe(bad => {
111 this.toast.danger('The fanciest libraries are: ' + JSON.stringify(bad.orgIds));
114 this.gridDataSource.data = [
115 {name: 'Jane', state: 'AZ'},
116 {name: 'Al', state: 'CA'},
117 {name: 'The Tick', state: 'TX'}
120 this.pcrud.retrieveAll('cmrcfld', {order_by: {cmrcfld: 'name'}})
121 .subscribe(format => {
122 if (!this.cbEntries) { this.cbEntries = []; }
123 this.cbEntries.push({id: format.id(), label: format.name()});
126 this.cbAsyncSource = term => {
127 return this.pcrud.search(
129 {name: {'ilike': `%${term}%`}}, // could -or search on label
130 {order_by: {cmrcfld: 'name'}}
131 ).pipe(map(marcField => {
132 return {id: marcField.id(), label: marcField.name()};
136 this.btSource.getRows = (pager: Pager, sort: any[]) => {
138 const orderBy: any = {cbt: 'name'};
140 orderBy.cbt = sort[0].name + ' ' + sort[0].dir;
143 return this.pcrud.retrieveAll('cbt', {
144 offset: pager.offset,
148 // example of inline fleshing
149 cbt.owner(this.org.get(cbt.owner()));
150 cbt.datetime_test = new Date();
156 this.complimentEvergreen = (rows: IdlObject[]) => alert('Evergreen is great!');
157 this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length !== 1);
159 this.pcrud.retrieve('bre', 1, {}, {fleshSelectors: true})
161 // Format service will automatically find the selector
162 // value to display from our fleshed metarecord field.
163 this.aMetarecord = this.format.transform({
164 value: bib.metarecord(),
166 idlField: 'metarecord'
171 sbChannelHandler = msg => {
172 setTimeout(() => { this.sbChannelText = msg.data.msg; });
175 sendMessage($event) {
176 this.sbChannel.postMessage({msg : $event.target.value});
179 // Example of click handler for row action
180 complimentEvergreen2(rows: IdlObject[]) {
181 alert('I know, right?');
185 this.fmRecordEditor.open({size: 'lg'}).subscribe(
186 pcrudResult => console.debug('Record editor performed action'),
187 err => console.error(err),
188 () => console.debug('Dialog closed')
192 btGridRowClassCallback(row: any): string {
193 if (row.id() === 1) {
194 return 'text-uppercase font-weight-bold text-danger';
198 btGridRowFlairCallback(row: any): GridRowFlairEntry {
199 const flair = {icon: null, title: null};
200 if (row.id() === 2) {
201 flair.icon = 'priority_high';
202 flair.title = 'I Am ID 2';
203 } else if (row.id() === 3) {
204 flair.icon = 'not_interested';
209 // apply to all 'name' columns regardless of row
210 btGridCellClassCallback(row: any, col: GridColumn): string {
211 if (col.name === 'name') {
212 if (row.id() === 7) {
213 return 'text-lowercase font-weight-bold text-info';
215 return 'text-uppercase font-weight-bold text-success';
221 template: this.printTemplate,
222 contextData: {world : this.world},
223 printContext: 'default'
227 text: '<b>hello</b>',
228 printContext: 'default'
234 template: this.printTemplate,
235 contextData: {world : this.world},
236 printContext: 'default',
242 console.log('HERE WITH ' + date);
243 this.testDate = date;
247 this.progressDialog.open();
249 // every 250ms emit x*10 for 0-10
250 observableTimer(0, 250).pipe(
254 val => this.progressDialog.update({value: val, max: 100}),
256 () => this.progressDialog.close()
261 this.toast.success('HELLO TOAST TEST');
262 setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
266 this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
267 .then(txt => this.toast.success(txt));
270 this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
271 .then(txt => this.toast.success(txt));
275 confirmNumber(num: number): void {
276 this.numThings = num;
277 console.log(this.numThings);
278 this.numConfirmDialog.open();