LP1825851 UI continued
authorBill Erickson <berickxx@gmail.com>
Fri, 12 Jul 2019 18:16:10 +0000 (14:16 -0400)
committerBill Erickson <berickxx@gmail.com>
Fri, 12 Jul 2019 18:16:10 +0000 (14:16 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/eg2/src/app/staff/admin/server/print-template.component.html
Open-ILS/src/eg2/src/app/staff/admin/server/print-template.component.ts

index 462b281..91f7597 100644 (file)
@@ -12817,7 +12817,7 @@ SELECT  usr,
                oils_obj:fieldmapper="config::print_template" 
                oils_persist:tablename="config.print_template" 
                reporter:label="Print Templates">
-               <fields oils_persist:primary="id">
+               <fields oils_persist:primary="id" oils_persist:sequence="config.print_template_id_seq">
                        <field name="id" reporter:datatype="id"  reporter:selector="label"/>
                        <field name="name" reporter:datatype="text" oils_obj:required="true"/>
                        <field name="label" reporter:datatype="text" oils_obj:required="true" oils_persist:i18n="true"/>
index d5d06e1..da685c9 100644 (file)
   <ngb-tab title="Template" i18n-title id='template'>
     <ng-template ngbTabContent>
       <div class="row">
-        <div class="col-lg-6 mt-3">
-          <button class="btn btn-success" (click)="openEditDialog()" i18n>
+        <div class="col-lg-12 mt-3 d-flex">
+          <button class="btn btn-info" (click)="openEditDialog()" i18n>
             Edit Template Attributes
           </button>
-          <button class="btn btn-info ml-2" (click)="applyChanges()" i18n>
-            Save Template and Refresh Preview
+          <button class="btn btn-success ml-2" (click)="applyChanges()" i18n>
+            Save Template Changes
+          </button>
+          <button class="btn btn-info ml-2" (click)="cloneTemplate()" i18n>
+            Clone Template
+          </button>
+          <div class="flex-1"> </div>
+          <button class="btn btn-danger ml-2" (click)="deleteTemplate()" i18n>
+            Delete Template
           </button>
           <span *ngIf="invalidJson" class="badge badge-danger ml-2" i18n>
             Invalid Sample JSON!
@@ -64,7 +71,7 @@
       <div class="row mt-2">
         <div class="col-lg-6">
           <h4 i18n>
-            Template for "{{template.label()}}"
+            Template for "{{template.label()}} ({{getOwnerName(template.id())}})"
             <span class="pl-2 text-warning" *ngIf="template.active() == 'f'">
               (Inactive)
             </span>
index 700d084..b192469 100644 (file)
@@ -1,4 +1,6 @@
 import {Component, OnInit, ViewChild, TemplateRef} from '@angular/core';
+import {Observable} from 'rxjs';
+import {map} from 'rxjs/operators';
 import {ActivatedRoute} from '@angular/router';
 import {IdlService, IdlObject} from '@eg/core/idl.service';
 import {PcrudService} from '@eg/core/pcrud.service';
@@ -66,7 +68,7 @@ export class PrintTemplateComponent implements OnInit {
         this.localeCode = this.locale.currentLocaleCode();
         this.locale.supportedLocales().subscribe(
             l => this.localeEntries.push({id: l.code(), label: l.name()}));
-        this.setTemplateInfo();
+        this.setTemplateInfo().subscribe();
         this.fleshSampleData();
     }
 
@@ -118,21 +120,18 @@ export class PrintTemplateComponent implements OnInit {
 
     //orgOnChange(org: IdlObject) {
     orgOnChange(family: OrgFamily) {
-        console.log('existing = ', this.selectedOrgs);
-        console.log('new = ', family.orgIds);
-        if (this.arrayEquals(this.selectedOrgs, family.orgIds)) {
-            return;
+        if (!this.arrayEquals(this.selectedOrgs, family.orgIds)) {
+            this.selectedOrgs = family.orgIds;
+            this.setTemplateInfo().subscribe();
         }
-        this.selectedOrgs = family.orgIds;
-        this.setTemplateInfo();
     }
 
     arrayEquals(arr1: any[], arr2: any[]): boolean {
         if (arr1.length !== arr2.length) {
             return false;
         }
-        for (var i = 0; i < arr1.length; i++) { // exit on first failure.
-            if (!arr2.includes(arr1[i])) {
+        for (var i = 0; i < arr1.length; i++) {
+            if (arr1[i] !== arr2[i]) {
                 return false;
             }
         }
@@ -142,32 +141,31 @@ export class PrintTemplateComponent implements OnInit {
     localeOnChange(code: string) {
         if (code) {
             this.localeCode = code;
-            this.setTemplateInfo();
+            this.setTemplateInfo().subscribe();
         }
     }
 
     // Fetch name/id for all templates in range.
     // Avoid fetching the template content until needed.
-    setTemplateInfo() {
+    setTemplateInfo(): Observable<IdlObject> {
         this.entries = [];
         this.template = null;
         this.templateSelector.applyEntryId(null);
         this.compiledContent = '';
 
-        this.pcrud.search('cpt', 
+        return this.pcrud.search('cpt', 
             {
                 owner: this.selectedOrgs,
                 locale: this.localeCode
-            },
-            {
+            }, {
                 select: {cpt: ['id', 'label', 'owner']}, 
                 order_by: {cpt: 'label'}
             }
-        ).subscribe(tmpl => {
+        ).pipe(map(tmpl => {
             this.templateCache[tmpl.id()] = tmpl;
             this.entries.push({id: tmpl.id(), label: tmpl.label()})
-            console.log('entry count = ', this.entries.length);
-        });
+            return tmpl;
+        }));
     }
 
     getOwnerName(id: number): string {
@@ -175,6 +173,13 @@ export class PrintTemplateComponent implements OnInit {
     }
 
     selectTemplate(id: number) {
+
+        if (id === null) {
+            this.template = null;
+            this.compiledContent = '';
+            return;
+        }
+
         this.pcrud.retrieve('cpt', id).subscribe(t => {
             this.template = t;
             const data = this.sampleData[t.name()];
@@ -223,7 +228,31 @@ export class PrintTemplateComponent implements OnInit {
     openEditDialog() {
         this.editDialog.setRecord(this.template);
         this.editDialog.mode = 'update';
-        this.editDialog.open({size: 'lg'});
+        this.editDialog.open({size: 'lg'}).subscribe(id => {
+            const selectedId = this.template.id();
+            this.setTemplateInfo().toPromise().then(
+                _ => this.selectTemplate(selectedId)
+            );
+        });
+    }
+
+    cloneTemplate() {
+        const tmpl = this.idl.clone(this.template);
+        tmpl.id(null);
+        this.editDialog.setRecord(tmpl);
+        this.editDialog.mode = 'create';
+        this.editDialog.open({size: 'lg'}).subscribe(tmpl => {
+            this.setTemplateInfo().toPromise()
+                .then(_ => this.selectTemplate(tmpl.id()));
+
+        });
+    }
+
+    deleteTemplate() {
+        this.pcrud.remove(this.template).subscribe(_ => {
+            this.setTemplateInfo().toPromise()
+                .then(_ => this.selectTemplate(null));
+        });
     }
 }