comparison client/src/store/fairway.js @ 1096:aa1f5daf6fc9

refac: centralized stores
author Thomas Junk <thomas.junk@intevation.de>
date Tue, 30 Oct 2018 16:55:29 +0100
parents client/src/fairway/store.js@7242b5a427bc
children f106aee673e7
comparison
equal deleted inserted replaced
1095:2d6d8b676e3f 1096:aa1f5daf6fc9
1 /*
2 * This is Free Software under GNU Affero General Public License v >= 3.0
3 * without warranty, see README.md and license for details.
4 *
5 * SPDX-License-Identifier: AGPL-3.0-or-later
6 * License-Filename: LICENSES/AGPL-3.0.txt
7 *
8 * Copyright (C) 2018 by via donau
9 * – Österreichische Wasserstraßen-Gesellschaft mbH
10 * Software engineering by Intevation GmbH
11 *
12 * Author(s):
13 * Thomas Junk <thomas.junk@intevation.de>
14 * Markus Kottländer <markuks.kottlaender@intevation.de>
15 */
16 import Vue from "vue";
17 import { HTTP } from "../application/lib/http";
18 import { prepareProfile } from "../application/lib/geo";
19 import LineString from "ol/geom/LineString.js";
20 import { generateFeatureRequest } from "../application/lib/geo.js";
21
22 const DEMOLEVEL = 149.345;
23
24 const FairwayProfile = {
25 namespaced: true,
26 state: {
27 additionalSurvey: "",
28 availableSurveys: null,
29 totalLength: 0,
30 minAlt: 0,
31 maxAlt: 0,
32 currentProfile: {},
33 waterLevels: [{ year: "2016", level: DEMOLEVEL, color: "#005DFF" }],
34 selectedWaterLevel: DEMOLEVEL,
35 fairwayCoordinates: [],
36 startPoint: null,
37 endPoint: null,
38 selectedMorph: null
39 },
40 getters: {
41 length: state => {
42 return state.totalLength;
43 },
44 additionalSurvey: state => {
45 return state.additionalSurvey;
46 }
47 },
48 mutations: {
49 setAdditionalSurvey: (state, additionalSurvey) => {
50 state.additionalSurvey = additionalSurvey;
51 },
52 setSelectedMorph: (state, selectedMorph) => {
53 state.selectedMorph = selectedMorph;
54 },
55 setAvailableSurveys: (state, surveys) => {
56 state.availableSurveys = surveys;
57 },
58 setSelectedWaterLevel: (state, level) => {
59 state.selectedWaterLevel = level;
60 },
61 profileLoaded: (state, answer) => {
62 const { response, surveyDate } = answer;
63 const { data } = response;
64 const coordinates = data.geometry.coordinates;
65 if (!coordinates) return;
66 const startPoint = state.startPoint;
67 const endPoint = state.endPoint;
68 const geoJSON = data;
69 const result = prepareProfile({ geoJSON, startPoint, endPoint });
70 // Use Vue.set() to make new object properties rective
71 // https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats
72 Vue.set(state.currentProfile, surveyDate, result.points);
73 if (!state.minAlt || state.minAlt > result.minAlt) {
74 state.minAlt = result.minAlt;
75 }
76 if (!state.maxAlt || state.maxAlt < result.maxAlt) {
77 state.maxAlt = result.maxAlt;
78 }
79 if (!state.totalLength || state.totalLength < result.lengthPolyLine) {
80 state.totalLength = result.lengthPolyLine;
81 }
82 },
83 setStartPoint: (state, start) => {
84 state.startPoint = start;
85 },
86 setEndPoint: (state, end) => {
87 state.endPoint = end;
88 },
89 setFairwayCoordinates: (state, coordinates) => {
90 state.fairwayCoordinates = coordinates;
91 },
92 clearCurrentProfile: state => {
93 state.additionalSurvey = "";
94 state.currentProfile = {};
95 state.minAlt = null;
96 state.maxAlt = null;
97 state.totalLength = null;
98 state.fairwayCoordinates = [];
99 state.startPoint = null;
100 state.endPoint = null;
101 }
102 },
103 actions: {
104 loadProfile({ commit, state }, date_info) {
105 return new Promise((resolve, reject) => {
106 const profileLine = new LineString([state.startPoint, state.endPoint]);
107 const geoJSON = generateFeatureRequest(
108 profileLine,
109 state.selectedMorph.bottleneck_id,
110 date_info
111 );
112 HTTP.post("/cross", geoJSON, {
113 headers: { "X-Gemma-Auth": localStorage.getItem("token") }
114 })
115 .then(response => {
116 commit("profileLoaded", {
117 response: response,
118 surveyDate: date_info
119 });
120 resolve(response);
121 })
122 .catch(error => {
123 reject(error);
124 });
125 });
126 }
127 }
128 };
129
130 export default FairwayProfile;