changeset 3840:387f239018c7

pdf_tool: retain default template, when custom template is uploaded
author Thomas Junk <thomas.junk@intevation.de>
date Mon, 08 Jul 2019 14:11:19 +0200
parents 03e8e6cc0b79
children 8e47d6f12998 22f7677337a5
files client/src/components/Pdftool.vue
diffstat 1 files changed, 17 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/components/Pdftool.vue	Mon Jul 08 12:34:10 2019 +0200
+++ b/client/src/components/Pdftool.vue	Mon Jul 08 14:11:19 2019 +0200
@@ -108,12 +108,14 @@
 import { displayError } from "@/lib/errors";
 import { pdfgen, templateLoader } from "@/lib/mixins";
 
-var paperSizes = {
+const paperSizes = {
   // in millimeter, landscape [width, height]
   a3: [420, 297],
   a4: [297, 210]
 };
 
+const DEFAULT_TEMPLATE = "Default";
+
 export default {
   mixins: [pdfgen, templateLoader],
   name: "pdftool",
@@ -128,7 +130,7 @@
       },
       templates: [
         {
-          name: "Default",
+          name: DEFAULT_TEMPLATE,
           properties: {
             format: "landscape",
             paperSize: "a4",
@@ -150,7 +152,7 @@
             },
             {
               type: "northarrow",
-              position: "topright",
+              position: "topleft",
               offset: { x: 6, y: 4 },
               size: 2
             }
@@ -210,20 +212,17 @@
     // When a template is chosen from the dropdown, its propoerties are
     // applied to the rest of the form.
     applyTemplateToForm() {
-      if (this.form.template) {
+      if (this.form.template && this.form.template.name !== DEFAULT_TEMPLATE) {
         this.loadTemplates(
           `/templates/${this.form.template.type}/${this.form.template.name}`
         )
           .then(response => {
             this.prepareImages(response.data.template_data.elements).then(
               values => {
-                this.templateData = response.data.template_data;
                 values.forEach(v => {
                   response.data.template_data.elements[v.index].url = v.url;
                 });
-                this.form.format = this.templateData.properties.format;
-                this.form.paperSize = this.templateData.properties.paperSize;
-                this.form.resolution = this.templateData.properties.resolution;
+                this.setTemplate(response.data.template_data);
               }
             );
           })
@@ -234,8 +233,16 @@
               message: `${status}: ${data.message || data}`
             });
           });
+      } else {
+        this.setTemplate(this.templates[0]);
       }
     },
+    setTemplate(template) {
+      this.templateData = template;
+      this.form.format = this.templateData.properties.format;
+      this.form.paperSize = this.templateData.properties.paperSize;
+      this.form.resolution = this.templateData.properties.resolution;
+    },
     download() {
       // disable button while working on it
       this.readyToGenerate = false;
@@ -803,8 +810,8 @@
     })
       .then(response => {
         if (response.data.length) {
-          this.templates = response.data;
-          this.form.template = this.templates[0];
+          this.templates = [...this.templates, ...response.data];
+          this.form.template = this.templates[1];
           this.applyTemplateToForm();
         } else {
           this.form.template = this.templates[0];