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 {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
17 import {FormGroup, FormControl} from '@angular/forms';
18 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
19 import {FormatService} from '@eg/core/format.service';
20 import {StringComponent} from '@eg/share/string/string.component';
21 import {GridComponent} from '@eg/share/grid/grid.component';
24 templateUrl: 'sandbox.component.html'
26 export class SandboxComponent implements OnInit {
28 @ViewChild('progressDialog')
29 private progressDialog: ProgressDialogComponent;
31 @ViewChild('dateSelect')
32 private dateSelector: DateSelectComponent;
34 @ViewChild('printTemplate')
35 private printTemplate: TemplateRef<any>;
37 @ViewChild('fmRecordEditor')
38 private fmRecordEditor: FmRecordEditorComponent;
40 @ViewChild('numConfirmDialog')
41 private numConfirmDialog: ConfirmDialogComponent;
45 @ViewChild('bresvEditor')
46 private bresvEditor: FmRecordEditorComponent;
49 // @ViewChild('helloStr') private helloStr: StringComponent;
51 gridDataSource: GridDataSource = new GridDataSource();
53 cbEntries: ComboboxEntry[];
54 // supplier of async combobox data
55 cbAsyncSource: (term: string) => Observable<ComboboxEntry>;
57 btSource: GridDataSource = new GridDataSource();
58 acpSource: GridDataSource = new GridDataSource();
59 editSelected: (rows: IdlObject[]) => void;
60 @ViewChild('acpGrid') acpGrid: GridComponent;
61 @ViewChild('acpEditDialog') editDialog: FmRecordEditorComponent;
62 @ViewChild('successString') successString: StringComponent;
63 @ViewChild('updateFailedString') updateFailedString: StringComponent;
64 world = 'world'; // for local template version
65 btGridTestContext: any = {hello : this.world};
72 @Input() set testString(str: string) {
80 dynamicTitleText: string;
82 badOrgForm: FormGroup;
84 complimentEvergreen: (rows: IdlObject[]) => void;
85 notOneSelectedRow: (rows: IdlObject[]) => boolean;
87 // selector field value on metarecord object
90 // cross-tab communications example
91 private sbChannel: any;
92 sbChannelText: string;
95 private idl: IdlService,
96 private org: OrgService,
97 private pcrud: PcrudService,
98 private strings: StringService,
99 private toast: ToastService,
100 private format: FormatService,
101 private printer: PrintService
103 // BroadcastChannel is not yet defined in PhantomJS and elsewhere
104 this.sbChannel = (typeof BroadcastChannel === 'undefined') ?
105 {} : new BroadcastChannel('eg.sbChannel');
106 this.sbChannel.onmessage = (e) => this.sbChannelHandler(e);
110 this.badOrgForm = new FormGroup({
111 'badOrgSelector': new FormControl(
112 {'id': 4, 'includeAncestors': false, 'includeDescendants': true}, (c: FormControl) => {
113 // An Angular custom validator
114 if (c.value.orgIds && c.value.orgIds.length > 5) {
115 return { tooMany: 'That\'s too many fancy libraries!' };
122 this.badOrgForm.get('badOrgSelector').valueChanges.subscribe(bad => {
123 this.toast.danger('The fanciest libraries are: ' + JSON.stringify(bad.orgIds));
126 this.gridDataSource.data = [
127 {name: 'Jane', state: 'AZ'},
128 {name: 'Al', state: 'CA'},
129 {name: 'The Tick', state: 'TX'}
132 this.pcrud.retrieveAll('cmrcfld', {order_by: {cmrcfld: 'name'}})
133 .subscribe(format => {
134 if (!this.cbEntries) { this.cbEntries = []; }
135 this.cbEntries.push({id: format.id(), label: format.name()});
138 this.cbAsyncSource = term => {
139 return this.pcrud.search(
141 {name: {'ilike': `%${term}%`}}, // could -or search on label
142 {order_by: {cmrcfld: 'name'}}
143 ).pipe(map(marcField => {
144 return {id: marcField.id(), label: marcField.name()};
148 this.btSource.getRows = (pager: Pager, sort: any[]) => {
150 const orderBy: any = {cbt: 'name'};
152 orderBy.cbt = sort[0].name + ' ' + sort[0].dir;
155 return this.pcrud.retrieveAll('cbt', {
156 offset: pager.offset,
160 // example of inline fleshing
161 cbt.owner(this.org.get(cbt.owner()));
162 cbt.datetime_test = new Date();
168 this.acpSource.getRows = (pager: Pager, sort: any[]) => {
169 const orderBy: any = {acp: 'id'};
171 orderBy.acp = sort[0].name + ' ' + sort[0].dir;
174 // base query to grab everything
175 const base: Object = {};
176 base[this.idl.classes['acp'].pkey] = {'!=' : null};
177 const query: any = new Array();
180 // and add any filters
181 Object.keys(this.acpSource.filters).forEach(key => {
182 Object.keys(this.acpSource.filters[key]).forEach(key2 => {
183 query.push(this.acpSource.filters[key][key2]);
186 return this.pcrud.search('acp',
189 flesh_fields: {acp: ['location']},
190 offset: pager.offset,
196 this.editSelected = (idlThings: IdlObject[]) => {
198 // Edit each IDL thing one at a time
199 const editOneThing = (thing: IdlObject) => {
200 if (!thing) { return; }
202 this.showEditDialog(thing).then(
203 () => editOneThing(idlThings.shift()));
206 editOneThing(idlThings.shift());
208 this.acpGrid.onRowActivate.subscribe(
209 (acpRec: IdlObject) => { this.showEditDialog(acpRec); }
212 this.complimentEvergreen = (rows: IdlObject[]) => alert('Evergreen is great!');
213 this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length !== 1);
215 this.pcrud.retrieve('bre', 1, {}, {fleshSelectors: true})
217 // Format service will automatically find the selector
218 // value to display from our fleshed metarecord field.
219 this.aMetarecord = this.format.transform({
220 value: bib.metarecord(),
222 idlField: 'metarecord'
226 const b = this.idl.create('bresv');
227 b.cancel_time('2019-03-25T11:07:59-0400');
228 this.bresvEditor.mode = 'create';
229 this.bresvEditor.record = b;
232 sbChannelHandler = msg => {
233 setTimeout(() => { this.sbChannelText = msg.data.msg; });
236 sendMessage($event) {
237 this.sbChannel.postMessage({msg : $event.target.value});
240 // Example of click handler for row action
241 complimentEvergreen2(rows: IdlObject[]) {
242 alert('I know, right?');
246 this.fmRecordEditor.open({size: 'lg'}).subscribe(
247 pcrudResult => console.debug('Record editor performed action'),
248 err => console.error(err),
249 () => console.debug('Dialog closed')
253 btGridRowClassCallback(row: any): string {
254 if (row.id() === 1) {
255 return 'text-uppercase font-weight-bold text-danger';
259 btGridRowFlairCallback(row: any): GridRowFlairEntry {
260 const flair = {icon: null, title: null};
261 if (row.id() === 2) {
262 flair.icon = 'priority_high';
263 flair.title = 'I Am ID 2';
264 } else if (row.id() === 3) {
265 flair.icon = 'not_interested';
270 // apply to all 'name' columns regardless of row
271 btGridCellClassCallback(row: any, col: GridColumn): string {
272 if (col.name === 'name') {
273 if (row.id() === 7) {
274 return 'text-lowercase font-weight-bold text-info';
276 return 'text-uppercase font-weight-bold text-success';
282 template: this.printTemplate,
283 contextData: {world : this.world},
284 printContext: 'default'
288 text: '<b>hello</b>',
289 printContext: 'default'
295 template: this.printTemplate,
296 contextData: {world : this.world},
297 printContext: 'default',
303 console.log('HERE WITH ' + date);
304 this.testDate = date;
308 this.progressDialog.open();
310 // every 250ms emit x*10 for 0-10
311 observableTimer(0, 250).pipe(
315 val => this.progressDialog.update({value: val, max: 100}),
317 () => this.progressDialog.close()
322 this.toast.success('HELLO TOAST TEST');
323 setTimeout(() => this.toast.danger('DANGER TEST AHHH!'), 4000);
327 this.strings.interpolate('staff.sandbox.test', {name : 'janey'})
328 .then(txt => this.toast.success(txt));
331 this.strings.interpolate('staff.sandbox.test', {name : 'johnny'})
332 .then(txt => this.toast.success(txt));
336 confirmNumber(num: number): void {
337 this.numThings = num;
338 console.log(this.numThings);
339 this.numConfirmDialog.open();
342 showEditDialog(idlThing: IdlObject) {
343 this.editDialog.mode = 'update';
344 this.editDialog.recId = idlThing['id']();
345 return this.editDialog.open({size: 'lg'}).then(
347 this.successString.current()
348 .then(str => this.toast.success(str));
349 this.acpGrid.reloadSansPagerReset();
352 if (!rejection.dismissed) {
353 this.updateFailedString.current()
354 .then(str => this.toast.danger(str));