diff client/src/components/fairway/AvailableFairwayDepth.vue @ 3992:2f024d6189ca

Reverted merge with diagram-cleanup -- its not yet ready.
author Sascha Wilde <wilde@intevation.de>
date Wed, 17 Jul 2019 15:12:23 +0200
parents 2aaa1948b525
children 44060b9027da
line wrap: on
line diff
--- a/client/src/components/fairway/AvailableFairwayDepth.vue	Wed Jul 17 14:18:43 2019 +0200
+++ b/client/src/components/fairway/AvailableFairwayDepth.vue	Wed Jul 17 15:12:23 2019 +0200
@@ -1,6 +1,7 @@
 <template>
   <div class="d-flex flex-column flex-fill">
     <UIBoxHeader icon="chart-area" :title="title" :closeCallback="close" />
+    <UISpinnerOverlay v-if="loading" />
     <div class="d-flex flex-fill">
       <DiagramLegend>
         <div v-for="(entry, index) in legend" :key="index" class="legend">
@@ -43,17 +44,16 @@
         </div>
       </DiagramLegend>
       <div
-        class="d-flex flex-fill justify-content-center align-items-center"
+        ref="diagramContainer"
         :id="containerId"
-      >
-        <div v-if="!fwData.length">
-          <translate>No data available.</translate>
-        </div>
-      </div>
+        class="diagram-container flex-fill"
+      ></div>
     </div>
   </div>
 </template>
 
+<style></style>
+
 <script>
 /* This is Free Software under GNU Affero General Public License v >= 3.0
  * without warranty, see README.md and license for details.
@@ -71,30 +71,86 @@
  * * Fadi Abbud <fadi.abbud@intevation.de>
  */
 import * as d3 from "d3";
+import app from "@/main";
+import debounce from "debounce";
 import { mapState } from "vuex";
+import filters from "@/lib/filters.js";
 import { diagram, pdfgen, templateLoader } from "@/lib/mixins";
+import { HTTP } from "@/lib/http";
+import { displayError } from "@/lib/errors";
 import { FREQUENCIES } from "@/store/fairwayavailability";
-import filters from "@/lib/filters.js";
+import { defaultDiagramTemplate } from "@/lib/DefaultDiagramTemplate";
 
-// hoursInDays is a const instead of component method because it's used where
-// the component context is not available
 const hoursInDays = x => x / 24;
-const COLORS = {
-  LDC: "#cdcdcd",
-  HIGHEST: "#3675ff",
-  REST: ["#782121", "#ff6c6c", "#ffaaaa"]
-};
 
 export default {
   mixins: [diagram, pdfgen, templateLoader],
+  components: {
+    DiagramLegend: () => import("@/components/DiagramLegend")
+  },
   data() {
     return {
       containerId: "availablefairwaydepth-diagram-container",
+      resizeListenerFunction: null,
+      loading: false,
       scalePaddingLeft: 60,
       scalePaddingRight: 0,
-      paddingTop: 25
+      paddingTop: 25,
+      pdf: {
+        doc: null,
+        width: null,
+        height: null
+      },
+      form: {
+        template: null
+      },
+      templateData: null,
+      templates: [],
+      defaultTemplate: defaultDiagramTemplate
     };
   },
+  created() {
+    this.resizeListenerFunction = debounce(this.drawDiagram, 100);
+    window.addEventListener("resize", this.resizeListenerFunction);
+  },
+  destroyed() {
+    window.removeEventListener("resize", this.resizeListenerFunction);
+  },
+  mounted() {
+    // Nasty but necessary if we don't want to use the updated hook to re-draw
+    // the diagram because this would re-draw it also for irrelevant reasons.
+    // In this case we need to wait for the child component (DiagramLegend) to
+    // render. According to the docs (https://vuejs.org/v2/api/#mounted) this
+    // should be possible with $nextTick() but it doesn't work because it does
+    // not guarantee that the DOM is not only updated but also re-painted on the
+    // screen.
+    setTimeout(this.drawDiagram, 150);
+
+    this.templates[0] = this.defaultTemplate;
+    this.form.template = this.templates[0];
+    this.templateData = this.form.template;
+    HTTP.get("/templates/diagram", {
+      headers: {
+        "X-Gemma-Auth": localStorage.getItem("token"),
+        "Content-type": "text/xml; charset=UTF-8"
+      }
+    })
+      .then(response => {
+        if (response.data.length) {
+          this.templates = response.data;
+          this.form.template = this.templates[0];
+          this.templates[this.templates.length] = this.defaultTemplate;
+          this.applyChange();
+        }
+      })
+      .catch(e => {
+        const { status, data } = e.response;
+        displayError({
+          title: this.$gettext("Backend Error"),
+          message: `${status}: ${data.message || data}`
+        });
+      });
+  },
   computed: {
     ...mapState("fairwayavailability", [
       "selectedFairwayAvailabilityFeature",
@@ -160,6 +216,40 @@
     }
   },
   methods: {
+    applyChange() {
+      if (this.form.template.hasOwnProperty("properties")) {
+        this.templateData = this.defaultTemplate;
+        return;
+      }
+      if (this.form.template) {
+        this.loadTemplates("/templates/diagram/" + this.form.template.name)
+          .then(response => {
+            this.prepareImages(response.data.template_data.elements).then(
+              values => {
+                values.forEach(v => {
+                  response.data.template_data.elements[v.index].url = v.url;
+                });
+                this.templateData = response.data.template_data;
+              }
+            );
+          })
+          .catch(e => {
+            const { status, data } = e.response;
+            displayError({
+              title: this.$gettext("Backend Error"),
+              message: `${status}: ${data.message || data}`
+            });
+          });
+      }
+    },
+    downloadPDF() {
+      let title = `Available Fairway Depth: ${this.featureName}`;
+      this.generatePDF({
+        templateData: this.templateData,
+        diagramTitle: title
+      });
+      this.pdf.doc.save(`Available Fairway Depth: ${this.featureName}`);
+    },
     addDiagramLegend(position, offset, color) {
       let x = offset.x,
         y = offset.y;
@@ -176,54 +266,60 @@
       }
 
       this.pdf.doc.setTextColor(color);
-      this.pdf.doc.setDrawColor(COLORS.LDC);
-      this.pdf.doc.setFillColor(COLORS.LDC);
+      this.pdf.doc.setDrawColor(this.$options.COLORS.LDC);
+      this.pdf.doc.setFillColor(this.$options.COLORS.LDC);
       this.pdf.doc.roundedRect(x, y, 10, 4, 1.5, 1.5, "FD");
       this.pdf.doc.text(this.legend[0], x + 12, y + 3);
 
-      this.pdf.doc.setDrawColor(COLORS.REST[0]);
-      this.pdf.doc.setFillColor(COLORS.REST[0]);
+      this.pdf.doc.setDrawColor(this.$options.COLORS.REST[0]);
+      this.pdf.doc.setFillColor(this.$options.COLORS.REST[0]);
       this.pdf.doc.roundedRect(x, y + 5, 10, 4, 1.5, 1.5, "FD");
       this.pdf.doc.text(this.legend[1], x + 12, y + 8);
 
-      this.pdf.doc.setDrawColor(COLORS.REST[1]);
-      this.pdf.doc.setFillColor(COLORS.REST[1]);
+      this.pdf.doc.setDrawColor(this.$options.COLORS.REST[1]);
+      this.pdf.doc.setFillColor(this.$options.COLORS.REST[1]);
       this.pdf.doc.roundedRect(x, y + 10, 10, 4, 1.5, 1.5, "FD");
       this.pdf.doc.text(this.legend[2], x + 12, y + 13);
 
-      this.pdf.doc.setDrawColor(COLORS.HIGHEST);
-      this.pdf.doc.setFillColor(COLORS.HIGHEST);
+      this.pdf.doc.setDrawColor(this.$options.COLORS.HIGHEST);
+      this.pdf.doc.setFillColor(this.$options.COLORS.HIGHEST);
       this.pdf.doc.roundedRect(x, y + 15, 10, 4, 1.5, 1.5, "FD");
       this.pdf.doc.text(this.legend[3], x + 12, y + 18);
     },
     legendStyle(index) {
       return [
-        `background-color: ${COLORS.LDC};`,
-        `background-color: ${COLORS.HIGHEST};`,
-        `background-color: ${COLORS.REST[1]};`,
-        `background-color: ${COLORS.REST[0]};`
+        `background-color: ${this.$options.COLORS.LDC};`,
+        `background-color: ${this.$options.COLORS.HIGHEST};`,
+        `background-color: ${this.$options.COLORS.REST[1]};`,
+        `background-color: ${this.$options.COLORS.REST[0]};`
       ][index];
     },
     close() {
       this.$store.commit("application/paneSetup", "DEFAULT");
     },
-    getPrintLayout() {
+    getPrintLayout(svgHeight) {
       return {
-        main: { top: 0, right: 20, bottom: 50, left: 20 }
+        main: { top: 0, right: 20, bottom: 50, left: 20 },
+        nav: {
+          top: svgHeight - 65,
+          right: 20,
+          bottom: 30,
+          left: 80
+        }
       };
     },
     drawDiagram() {
       const elem = document.querySelector("#" + this.containerId);
       const svgWidth = elem != null ? elem.clientWidth : 0;
       const svgHeight = elem != null ? elem.clientHeight : 0;
-      const layout = this.getPrintLayout();
+      const layout = this.getPrintLayout(svgHeight);
       const dimensions = this.getDimensions({
         svgHeight,
         svgWidth,
         ...layout
       });
-      d3.select("#" + this.containerId + " svg").remove();
-      this.renderTo({ element: "#" + this.containerId, dimensions });
+      d3.select(".diagram-container svg").remove();
+      this.renderTo({ element: ".diagram-container", dimensions });
     },
     renderTo({ element, dimensions }) {
       const diagram = d3
@@ -344,7 +440,7 @@
         .attr("x", ldcOffset + spaceBetween / 2)
         .attr("width", widthPerItem - ldcOffset - spaceBetween)
         .attr("fill", (d, i) => {
-          return COLORS.REST[i];
+          return this.$options.COLORS.REST[i];
         });
     },
     fnheight({ name, yScale }) {
@@ -383,7 +479,7 @@
           "transform",
           d => `translate(0 ${this.paddingTop + -1 * height(d)})`
         )
-        .attr("fill", COLORS.LDC)
+        .attr("fill", this.$options.COLORS.LDC)
         .attr("id", "ldc");
     },
     drawHighestLevel({
@@ -427,7 +523,7 @@
           "transform",
           d => `translate(0 ${this.paddingTop + -1 * height(d)})`
         )
-        .attr("fill", COLORS.HIGHEST);
+        .attr("fill", this.$options.COLORS.HIGHEST);
     },
     drawLabelPerBar({ everyBar, dimensions, widthPerItem }) {
       everyBar
@@ -441,7 +537,7 @@
     drawScaleLabel({ diagram, dimensions }) {
       diagram
         .append("text")
-        .text(this.$gettext("Sum of days"))
+        .text(this.$options.LEGEND)
         .attr("text-anchor", "middle")
         .attr("x", 0)
         .attr("y", 0)
@@ -519,6 +615,12 @@
     fwData() {
       this.drawDiagram();
     }
+  },
+  LEGEND: app.$gettext("Sum of days"),
+  COLORS: {
+    LDC: "#cdcdcd",
+    HIGHEST: "#3675ff",
+    REST: ["#782121", "#ff6c6c", "#ffaaaa"]
   }
 };
 </script>