From: Bill Erickson Date: Thu, 18 Apr 2019 20:05:07 +0000 (-0400) Subject: server template : angular admin ui X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=0e10034921586d2859c45c16dd7e34d072853210;p=working%2FEvergreen.git server template : angular admin ui Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml index 135cbb1202..d70568ee21 100644 --- a/Open-ILS/examples/fm_IDL.xml +++ b/Open-ILS/examples/fm_IDL.xml @@ -12817,7 +12817,7 @@ SELECT usr, oils_obj:fieldmapper="config::print_template" oils_persist:tablename="config.print_template" reporter:label="Print Templates"> - + diff --git a/Open-ILS/src/eg2/src/app/share/print/print.service.ts b/Open-ILS/src/eg2/src/app/share/print/print.service.ts index 54dcd9c3a4..d4d0263d1c 100644 --- a/Open-ILS/src/eg2/src/app/share/print/print.service.ts +++ b/Open-ILS/src/eg2/src/app/share/print/print.service.ts @@ -11,6 +11,8 @@ const PRINT_TEMPLATE_PATH = '/print_template'; export interface PrintRequest { template?: TemplateRef; templateName?: string; + templateOwner?: number; // org unit ID, follows ancestors + templateId?: number; // useful for testing templates contextData?: any; text?: string; printContext: string; @@ -60,7 +62,15 @@ export class PrintService { const formData: FormData = new FormData(); formData.append('ses', this.auth.token()); - formData.append('template_name', printReq.templateName); + if (printReq.templateName) { + formData.append('template_name', printReq.templateName); + } + if (printReq.templateId) { + formData.append('template_id', '' + printReq.templateId); + } + if (printReq.templateOwner) { + formData.append('owner', '' + printReq.templateOwner); + } formData.append('template_data', js2JSON(printReq.contextData)); formData.append('locale', this.locale.currentLocaleCode()); diff --git a/Open-ILS/src/eg2/src/app/staff/admin/server/print-template.component.html b/Open-ILS/src/eg2/src/app/staff/admin/server/print-template.component.html new file mode 100644 index 0000000000..c061cc7346 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/server/print-template.component.html @@ -0,0 +1,85 @@ + + + + + +
+
+
+
+ Owner +
+ + +
+
+
+
+
+ Template +
+ + +
+
+
+
+
+ Locale +
+ + +
+
+
+ + + + + + + + +
+
+ +
+
+
+
+

Template

+ +
+
+

Preview

+
+
+

Compiled Content

+
+
{{compiledContent}}
+
+
+
+
+
+ + + + + +
+ diff --git a/Open-ILS/src/eg2/src/app/staff/admin/server/print-template.component.ts b/Open-ILS/src/eg2/src/app/staff/admin/server/print-template.component.ts index aa482e5e00..c692f6e7e0 100644 --- a/Open-ILS/src/eg2/src/app/staff/admin/server/print-template.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/admin/server/print-template.component.ts @@ -1,51 +1,170 @@ import {Component, OnInit, ViewChild, TemplateRef} from '@angular/core'; import {ActivatedRoute} from '@angular/router'; -import {IdlService} from '@eg/core/idl.service'; -import {FmFieldOptions} from '@eg/share/fm-editor/fm-editor.component'; +import {IdlService, IdlObject} from '@eg/core/idl.service'; +import {PcrudService} from '@eg/core/pcrud.service'; +import {AuthService} from '@eg/core/auth.service'; +import {OrgService} from '@eg/core/org.service'; +import {ComboboxComponent, ComboboxEntry + } from '@eg/share/combobox/combobox.component'; +import {PrintService} from '@eg/share/print/print.service'; +import {LocaleService} from '@eg/core/locale.service'; +import {NgbTabset, NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap'; + +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 = { + 'address-label': { + patron: SAMPLE_JSON_DATA.patron, + address: SAMPLE_JSON_DATA.address + } +}; + /** - * Generic IDL class editor page. + * Print Template Admin Page */ @Component({ - template: ` - - - - - - - - ` + templateUrl: 'print-template.component.html' }) export class PrintTemplateComponent implements OnInit { - idlClass: string; - classLabel: string; - persistKeyPfx: string; - fieldOptions: {[field: string]: FmFieldOptions}; + contextOrg: IdlObject; + entries: ComboboxEntry[]; + template: IdlObject; + sampleJson: string; + localeCode: string; + localeEntries: ComboboxEntry[]; + compiledContent: string; + + @ViewChild('templateSelector') templateSelector: ComboboxComponent; + @ViewChild('tabs') tabs: NgbTabset; - @ViewChild('templateTemplate') templateTemplate: TemplateRef; + // Define some sample data that can be used for various templates constructor( private route: ActivatedRoute, - private idl: IdlService) {} + private idl: IdlService, + private org: OrgService, + private pcrud: PcrudService, + private auth: AuthService, + private locale: LocaleService, + private printer: PrintService + ) { + this.entries = []; + this.localeEntries = []; + } ngOnInit() { + this.contextOrg = this.org.get(this.auth.user().ws_ou()); + this.localeCode = this.locale.currentLocaleCode(); + this.locale.supportedLocales().subscribe( + l => this.localeEntries.push({id: l.code(), label: l.name()})); + this.setTemplateInfo(); + } + + onTabChange(evt: NgbTabChangeEvent) { + if (evt.nextId === 'template') { + this.refreshPreview(); + } + } + + + container(): any { + // Only present when its tab is visible + return document.getElementById('template-preview-pane'); + } + + orgOnChange(org: IdlObject) { + this.contextOrg = org; + this.setTemplateInfo(); + } + + localeOnChange(code: string) { + if (code) { + this.localeCode = code; + this.setTemplateInfo(); + } + } + + // Fetch name/id for all templates in range. + // Avoid fetching the template content until needed. + setTemplateInfo() { + this.entries = []; + this.template = null; + this.templateSelector.applyEntryId(null); + this.compiledContent = ''; + + this.pcrud.search('cpt', + { + owner: this.contextOrg.id(), + locale: this.localeCode + }, + { + select: {cpt: ['id', 'label']}, + order_by: {cpt: 'label'} + } + ).subscribe(tmpl => + this.entries.push({id: tmpl.id(), label: tmpl.label()}) + ); + } - this.fieldOptions = { - template: { - customTemplate: { - template: this.templateTemplate - } + selectTemplate(id: number) { + this.pcrud.retrieve('cpt', id).subscribe(t => { + this.template = t; + const data = SAMPLE_TEMPLATE_DATA[t.name()]; + if (data) { + this.sampleJson = JSON.stringify(data, null, 2); } - }; + }); + } + + refreshPreview() { + if (!this.sampleJson) return; + this.compiledContent = ''; + + let data; + try { + data = JSON.parse(this.sampleJson); + } catch (E) { + // TODO: i18n/AlertDialog + alert('Invalid Sample Data JSON'); + } + + this.printer.compileRemoteTemplate({ + templateId: this.template.id(), + contextData: data, + printContext: 'default' // required, has no impact here + + }).then(response => { + + this.compiledContent = response.content; + if (response.contentType === 'text/html') { + this.container().innerHTML = response.content; + } else { + // Assumes text/plain or similar + this.container().innerHTML = '
' + response.content + '
'; + } + }); + } - this.idlClass = 'cpt'; - const classDef = this.idl.classes[this.idlClass]; - this.classLabel = classDef.label; + applyChanges() { + this.container().innerHTML = ''; + this.pcrud.update(this.template).toPromise() + .then(() =>this.refreshPreview()); } } diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/PrintTemplate.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/PrintTemplate.pm index 194afd3612..f8209d451b 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/PrintTemplate.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/PrintTemplate.pm @@ -76,14 +76,15 @@ sub handler { # Let pcrud handle the authz $e->personality('open-ils.pcrud'); - my $owner = $e->requestor->ws_ou; + my $template_id = $cgi->param('template_id'); + my $owner = $cgi->param('owner') || $e->requestor->ws_ou; my $locale = $cgi->param('locale') || 'en-US'; my $template_name = $cgi->param('template_name'); my $template_data = $cgi->param('template_data'); - return Apache2::Const::FORBIDDEN unless $template_name; + return Apache2::Const::FORBIDDEN unless $template_name || $template_id; - my $template = find_template($e, $template_name, $locale, $owner) + my $template = find_template($e, $template_id, $template_name, $locale, $owner) or return Apache2::Const::NOT_FOUND; my $output = ''; @@ -123,7 +124,13 @@ sub handler { # Find the template closest to the specific org unit owner. my %template_cache; sub find_template { - my ($e, $name, $locale, $owner) = @_; + my ($e, $template_id, $name, $locale, $owner) = @_; + + if ($template_id) { + # Requesting by ID, generally used for testing, + # always pulls the latest value. + return $e->retrieve_config_print_template($template_id); + } return $template_cache{$owner}{$name}{$locale} if $template_cache{$owner} &&