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';
21 templateUrl: 'sandbox.component.html'
23 export class SandboxComponent implements OnInit {
25 @ViewChild('progressDialog')
26 private progressDialog: ProgressDialogComponent;
28 @ViewChild('dateSelect')
29 private dateSelector: DateSelectComponent;
31 @ViewChild('printTemplate')
32 private printTemplate: TemplateRef<any>;
34 @ViewChild('fmRecordEditor')
35 private fmRecordEditor: FmRecordEditorComponent;
37 // @ViewChild('helloStr') private helloStr: StringComponent;
39 gridDataSource: GridDataSource = new GridDataSource();
41 cbEntries: ComboboxEntry[];
42 // supplier of async combobox data
43 cbAsyncSource: (term: string) => Observable<ComboboxEntry>;
45 btSource: GridDataSource = new GridDataSource();
46 world = 'world'; // for local template version
47 btGridTestContext: any = {hello : this.world};
54 @Input() set testString(str: string) {
62 dynamicTitleText: string;
64 badOrgForm: FormGroup;
66 complimentEvergreen: (rows: IdlObject[]) => void;
67 notOneSelectedRow: (rows: IdlObject[]) => boolean;
69 // selector field value on metarecord object
72 // cross-tab communications example
73 private sbChannel: any;
74 sbChannelText: string;
77 private idl: IdlService,
78 private org: OrgService,
79 private pcrud: PcrudService,
80 private strings: StringService,
81 private toast: ToastService,
82 private format: FormatService,
83 private printer: PrintService
85 // BroadcastChannel is not yet defined in PhantomJS and elsewhere
86 this.sbChannel = (typeof BroadcastChannel === 'undefined') ?
87 {} : new BroadcastChannel('eg.sbChannel');
88 this.sbChannel.onmessage = (e) => this.sbChannelHandler(e);
92 this.badOrgForm = new FormGroup({
93 'badOrgSelector': new FormControl(
94 {'id': 4, 'includeAncestors': false, 'includeDescendants': true}, (c: FormControl) => {
95 // An Angular custom validator
96 if (c.value.orgIds && c.value.orgIds.length > 5) {
97 return { tooMany: 'That\'s too many bad libraries!' };
104 this.badOrgForm.get('badOrgSelector').valueChanges.subscribe(bad => {
105 this.toast.danger('The worst libraries are: ' + JSON.stringify(bad.orgIds));
108 this.gridDataSource.data = [
109 {name: 'Jane', state: 'AZ'},
110 {name: 'Al', state: 'CA'},
111 {name: 'The Tick', state: 'TX'}
114 this.pcrud.retrieveAll('cmrcfld', {order_by: {cmrcfld: 'name'}})
115 .subscribe(format => {
116 if (!this.cbEntries) { this.cbEntries = []; }
117 this.cbEntries.push({id: format.id(), label: format.name()});
120 this.cbAsyncSource = term => {
121 return this.pcrud.search(
123 {name: {'ilike': `%${term}%`}}, // could -or search on label
124 {order_by: {cmrcfld: 'name'}}
125 ).pipe(map(marcField => {
126 return {id: marcField.id(), label: marcField.name()};
130 this.btSource.getRows = (pager: Pager, sort: any[]) => {
132 const orderBy: any = {cbt: 'name'};
134 orderBy.cbt = sort[0].name + ' ' + sort[0].dir;
137 return this.pcrud.retrieveAll('cbt', {
138 offset: pager.offset,
142 // example of inline fleshing
143 cbt.owner(this.org.get(cbt.owner()));
144 cbt.datetime_test = new Date();
150 this.complimentEvergreen = (rows: IdlObject[]) => alert('Evergreen is great!');
151 this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length !== 1);
153 this.pcrud.retrieve('bre', 1, {}, {fleshSelectors: true})
155 // Format service will automatically find the selector
156 // value to display from our fleshed metarecord field.
157 this.aMetarecord = this.format.transform({
158 value: bib.metarecord(),
160 idlField: 'metarecord'
165 sbChannelHandler = msg => {
166 setTimeout(() => { this.sbChannelText = msg.data.msg; });
169 sendMessage($event) {
170 this.sbChannel.postMessage({msg : $event.target.value});
173 // Example of click handler for row action
174 complimentEvergreen2(rows: IdlObject[]) {
175 alert('I know, right?');
179 this.fmRecordEditor.open({size: 'lg'}).subscribe(
180 pcrudResult => console.debug('Record editor performed action'),
181 err => console.error(err),
182 () => console.debug('Dialog closed')
186 btGridRowClassCallback(row: any): string {
187 if (row.id() === 1) {
188 return 'text-uppercase font-weight-bold text-danger';
192 btGridRowFlairCallback(row: any): GridRowFlairEntry {
193 const flair = {icon: null, title: null};
194 if (row.id() === 2) {
195 flair.icon = 'priority_high';
196 flair.title = 'I Am ID 2';
197 } else if (row.id() === 3) {
198 flair.icon = 'not_interested';
203 // apply to all 'name' columns regardless of row
204 btGridCellClassCallback(row: any, col: GridColumn): string {
205 if (col.name === 'name') {
206 if (row.id() === 7) {
207 return 'text-lowercase font-weight-bold text-info';
209 return 'text-uppercase font-weight-bold text-success';
215 template: this.printTemplate,
216 contextData: {world : this.world},
217 printContext: 'default'
221 text: '<b>hello</b>',
222 printContext: 'default'
228 template: this.printTemplate,
229 contextData: {world : this.world},
230 printContext: 'default',
236 console.log('HERE WITH ' + date);
237 this.testDate = date;
241 this.progressDialog.open();
243 // every 250ms emit x*10 for 0-10
244 observableTimer(0, 250).pipe(
248 val => this.progressDialog.update({value: val, max: 100}),
250 () => this.progressDialog.close()
255 this.toast.success('HELLO TOAST TEST');
256 setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
260 this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
261 .then(txt => this.toast.success(txt));
264 this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
265 .then(txt => this.toast.success(txt));