--- /dev/null
+import {Injectable} from '@angular/core';
+import {IdlService, IdlObject} from '@eg/core/idl.service';
+
+/** Service for generating sample data for testing, demo, etc. */
+
+// TODO: I could also imagine this coming from a web service or
+// even a flat file of web-served JSON.
+
+// Copied from sample of Concerto data set
+const DATA = {
+ au: [
+ {first_given_name: 'Vincent', second_given_name: 'Kenneth', family_name: 'Moran'},
+ {first_given_name: 'Gregory', second_given_name: 'Adam', family_name: 'Jones'},
+ {first_given_name: 'Brittany', second_given_name: 'Geraldine', family_name: 'Walker'},
+ {first_given_name: 'Ernesto', second_given_name: 'Robert', family_name: 'Miller'},
+ {first_given_name: 'Robert', second_given_name: 'Louis', family_name: 'Hill'},
+ {first_given_name: 'Edward', second_given_name: 'Robert', family_name: 'Lopez'},
+ {first_given_name: 'Andrew', second_given_name: 'Alberto', family_name: 'Bell'},
+ {first_given_name: 'Jennifer', second_given_name: 'Dorothy', family_name: 'Mitchell'},
+ {first_given_name: 'Jo', second_given_name: 'Mai', family_name: 'Madden'},
+ {first_given_name: 'Maomi', second_given_name: 'Julie', family_name: 'Harding'}
+ ],
+ aua: [
+ {street1: '1809 Target Way', city: 'Vero beach', state: 'FL', post_code: 32961},
+ {street1: '3481 Facility Island', city: 'Campton', state: 'KY', post_code: 41301},
+ {street1: '5150 Dinner Expressway', city: 'Dodge center', state: 'MN', post_code: 55927},
+ {street1: '8496 Random Trust Points', city: 'Berryville', state: 'VA', post_code: 22611},
+ {street1: '7626 Secret Institute Courts', city: 'Anchorage', state: 'AK', post_code: 99502},
+ {street1: '7044 Regular Index Path', city: 'Livingston', state: 'KY', post_code: 40445},
+ {street1: '3403 Thundering Heat Meadows', city: 'Miami', state: 'FL', post_code: 33157},
+ {street1: '759 Doubtful Government Extension', city: 'Sellersville', state: 'PA', post_code: 18960},
+ {street1: '5431 Japanese Work Rapid', city: 'Society hill', state: 'SC', post_code: 29593},
+ {street1: '5253 Agricultural Exhibition Stravenue', city: 'La place', state: 'IL', post_code: 61936}
+ ]
+};
+
+
+@Injectable()
+export class SampleDataService {
+
+ constructor(private idl: IdlService) {}
+
+ randomValue(list: any[], field: string): string {
+ return list[Math.floor(Math.random() * list.length)][field];
+ }
+
+ listOfThings(idlClass: string, count: number = 1): IdlObject[] {
+ if (!(idlClass in DATA)) {
+ throw new Error(`No sample data for class ${idlClass}"`);
+ }
+
+ const things: IdlObject[] = [];
+ for (let i = 0; i < count; i++) {
+ const thing = this.idl.create(idlClass);
+ Object.keys(DATA[idlClass][0]).forEach(field =>
+ thing[field](this.randomValue(DATA[idlClass], field))
+ );
+ things.push(thing);
+ }
+
+ return things;
+ }
+}
+
+
import {LocaleService} from '@eg/core/locale.service';
import {NgbTabset, NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
-
-const SAMPLE_JSON_DATA: any = {
- patron: {
- first_given_name: 'Leela',
- second_given_name: '',
- family_name: 'Turanga'
- },
- address: {
- street1: '123 Pineapple Rd',
- city: 'North Southland',
- state: 'HI',
- post_code: 12321
- }
-};
-
-const SAMPLE_TEMPLATE_DATA: any = {
- patron_address: {
- patron: SAMPLE_JSON_DATA.patron,
- address: SAMPLE_JSON_DATA.address
- }
-};
-
+import {SampleDataService} from '@eg/share/util/sample-data.service';
/**
* Print Template Admin Page
@ViewChild('editDialog') editDialog: FmRecordEditorComponent;
// Define some sample data that can be used for various templates
+ // Data will be filled out via the sample data service.
+ sampleData: any = {
+ patron_address: {
+ patron: null,
+ address: null
+ }
+ }
constructor(
private route: ActivatedRoute,
private pcrud: PcrudService,
private auth: AuthService,
private locale: LocaleService,
- private printer: PrintService
+ private printer: PrintService,
+ private samples: SampleDataService
) {
this.entries = [];
this.localeEntries = [];
this.locale.supportedLocales().subscribe(
l => this.localeEntries.push({id: l.code(), label: l.name()}));
this.setTemplateInfo();
+ this.fleshSampleData();
+ }
+
+ fleshSampleData() {
+
+ // NOTE: server templates work fine with IDL objects, but
+ // vanilla hashes are easier to work with in the admin UI.
+ const patrons = this.idl.toHash(this.samples.listOfThings('au', 10));
+ const addresses = this.idl.toHash(this.samples.listOfThings('aua', 10));
+
+ this.sampleData.patron_address = {
+ patron: patrons[0],
+ address: addresses[0]
+ };
}
onTabChange(evt: NgbTabChangeEvent) {
selectTemplate(id: number) {
this.pcrud.retrieve('cpt', id).subscribe(t => {
this.template = t;
- const data = SAMPLE_TEMPLATE_DATA[t.name()];
+ const data = this.sampleData[t.name()];
if (data) {
this.sampleJson = JSON.stringify(data, null, 2);
this.refreshPreview();