changeset 3123:f0af359391a0

client: map layers: share certain layer objects across map
author Markus Kottlaender <markus@intevation.de>
date Mon, 29 Apr 2019 12:11:06 +0200
parents 3dffeb6df4a3
children 583b6748431f
files client/src/components/map/layers.js
diffstat 1 files changed, 102 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/components/map/layers.js	Mon Apr 29 11:58:20 2019 +0200
+++ b/client/src/components/map/layers.js	Mon Apr 29 12:11:06 2019 +0200
@@ -60,6 +60,106 @@
   };
 };
 
+// SHARED LAYERS:
+// DRAW- and CUTLAYER are shared across maps. E.g. you want to see the cross cut
+// arrow on both maps when comparing surveys. So we don't need to initialize a
+// new VectorLayer object for each map. Instead we use these two constants so
+// that all maps use the same object.
+const DRAWLAYER = new VectorLayer({
+  id: "DRAWTOOL",
+  label: "Draw Tool",
+  visible: true,
+  source: new VectorSource({ wrapX: false }),
+  style: function(feature) {
+    // adapted from OpenLayer's LineString Arrow Example
+    var geometry = feature.getGeometry();
+    var styles = [
+      // linestring
+      new Style({
+        stroke: new Stroke({
+          color: "#369aca",
+          width: 2
+        })
+      })
+    ];
+
+    if (geometry.getType() === "LineString") {
+      geometry.forEachSegment(function(start, end) {
+        var dx = end[0] - start[0];
+        var dy = end[1] - start[1];
+        var rotation = Math.atan2(dy, dx);
+        // arrows
+        styles.push(
+          new Style({
+            geometry: new Point(end),
+            image: new Icon({
+              // we need to make sure the image is loaded by Vue Loader
+              src: require("@/assets/linestring_arrow.png"),
+              // fiddling with the anchor's y value does not help to
+              // position the image more centered on the line ending, as the
+              // default line style seems to be slightly uncentered in the
+              // anti-aliasing, but the image is not placed with subpixel
+              // precision
+              anchor: [0.75, 0.5],
+              rotateWithView: true,
+              rotation: -rotation
+            })
+          })
+        );
+      });
+    }
+    return styles;
+  }
+});
+
+const CUTLAYER = new VectorLayer({
+  id: "CUTTOOL",
+  label: "Cut Tool",
+  visible: true,
+  source: new VectorSource({ wrapX: false }),
+  style: function(feature) {
+    // adapted from OpenLayer's LineString Arrow Example
+    var geometry = feature.getGeometry();
+    var styles = [
+      // linestring
+      new Style({
+        stroke: new Stroke({
+          color: "#333333",
+          width: 2,
+          lineDash: [7, 7]
+        })
+      })
+    ];
+
+    if (geometry.getType() === "LineString") {
+      geometry.forEachSegment(function(start, end) {
+        var dx = end[0] - start[0];
+        var dy = end[1] - start[1];
+        var rotation = Math.atan2(dy, dx);
+        // arrows
+        styles.push(
+          new Style({
+            geometry: new Point(end),
+            image: new Icon({
+              // we need to make sure the image is loaded by Vue Loader
+              src: require("@/assets/linestring_arrow_grey.png"),
+              // fiddling with the anchor's y value does not help to
+              // position the image more centered on the line ending, as the
+              // default line style seems to be slightly uncentered in the
+              // anti-aliasing, but the image is not placed with subpixel
+              // precision
+              anchor: [0.75, 0.5],
+              rotateWithView: true,
+              rotation: -rotation
+            })
+          })
+        );
+      });
+    }
+    return styles;
+  }
+});
+
 export default function() {
   return {
     get(id) {
@@ -393,99 +493,8 @@
           source
         });
       })(),
-      new VectorLayer({
-        id: "DRAWTOOL",
-        label: "Draw Tool",
-        visible: true,
-        source: new VectorSource({ wrapX: false }),
-        style: function(feature) {
-          // adapted from OpenLayer's LineString Arrow Example
-          var geometry = feature.getGeometry();
-          var styles = [
-            // linestring
-            new Style({
-              stroke: new Stroke({
-                color: "#369aca",
-                width: 2
-              })
-            })
-          ];
-
-          if (geometry.getType() === "LineString") {
-            geometry.forEachSegment(function(start, end) {
-              var dx = end[0] - start[0];
-              var dy = end[1] - start[1];
-              var rotation = Math.atan2(dy, dx);
-              // arrows
-              styles.push(
-                new Style({
-                  geometry: new Point(end),
-                  image: new Icon({
-                    // we need to make sure the image is loaded by Vue Loader
-                    src: require("@/assets/linestring_arrow.png"),
-                    // fiddling with the anchor's y value does not help to
-                    // position the image more centered on the line ending, as the
-                    // default line style seems to be slightly uncentered in the
-                    // anti-aliasing, but the image is not placed with subpixel
-                    // precision
-                    anchor: [0.75, 0.5],
-                    rotateWithView: true,
-                    rotation: -rotation
-                  })
-                })
-              );
-            });
-          }
-          return styles;
-        }
-      }),
-      new VectorLayer({
-        id: "CUTTOOL",
-        label: "Cut Tool",
-        visible: true,
-        source: new VectorSource({ wrapX: false }),
-        style: function(feature) {
-          // adapted from OpenLayer's LineString Arrow Example
-          var geometry = feature.getGeometry();
-          var styles = [
-            // linestring
-            new Style({
-              stroke: new Stroke({
-                color: "#333333",
-                width: 2,
-                lineDash: [7, 7]
-              })
-            })
-          ];
-
-          if (geometry.getType() === "LineString") {
-            geometry.forEachSegment(function(start, end) {
-              var dx = end[0] - start[0];
-              var dy = end[1] - start[1];
-              var rotation = Math.atan2(dy, dx);
-              // arrows
-              styles.push(
-                new Style({
-                  geometry: new Point(end),
-                  image: new Icon({
-                    // we need to make sure the image is loaded by Vue Loader
-                    src: require("@/assets/linestring_arrow_grey.png"),
-                    // fiddling with the anchor's y value does not help to
-                    // position the image more centered on the line ending, as the
-                    // default line style seems to be slightly uncentered in the
-                    // anti-aliasing, but the image is not placed with subpixel
-                    // precision
-                    anchor: [0.75, 0.5],
-                    rotateWithView: true,
-                    rotation: -rotation
-                  })
-                })
-              );
-            });
-          }
-          return styles;
-        }
-      })
+      DRAWLAYER,
+      CUTLAYER
     ]
   };
 }