Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Layer sharing improve
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresKasekamp committed Dec 28, 2023
1 parent 260c834 commit 711a6ec
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 31 deletions.
34 changes: 18 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// TODO hiljem proovida ka vajalike kihtide koosseis säilitada
// TODO seda peaks ilmselt tegema hard codega esialgse seisuga ja siis listide võrdlemine
// TODO los arendus
// TODO kui määrata id ja jagada läbi selle?
// TODO fronti nupud jäävad jagamisele muutmata, kuidagi peab lahendama, et topelt muutus ei tuleks

require([
"esri/widgets/CoordinateConversion/support/Conversion",
Expand Down Expand Up @@ -128,7 +130,6 @@ require([
view.ui.move("navigation-toggle", "top-right");
view.ui.move("compass", "top-right");


/**************************************
* Line of Sight analysis custom
**************************************/
Expand Down Expand Up @@ -267,12 +268,9 @@ require([
rajatisedGroup.add(communicationTower);

/**************************************
* Collecting visible layers
* Collecting visible layers before modification and rerendering
**************************************/

// TODO see on pärast funktsiooniks teha
const initVisibleLayers = initLayers.getVisibleLayers(view);
console.log("Initial visibilit", initVisibleLayers)

/**************************************
* Calcite CSS/JS
Expand Down Expand Up @@ -310,17 +308,20 @@ require([

if (nextWidget === "share") {
const visibleLayersCurrently = initLayers.getVisibleLayers(view);
console.log("Visible layers now", visibleLayersCurrently)

const difference1 = initVisibleLayers.filter(item => !visibleLayersCurrently.includes(item));
const difference2 = visibleLayersCurrently.filter(item => !initVisibleLayers.includes(item));

const layerVisibilityChanged = [];
layerVisibilityChanged.push(...difference1.map(obj => obj.title));
layerVisibilityChanged.push(...difference2.map(obj => obj.title));

console.log(layerVisibilityChanged)
const sharedLocation = goToLocation.createURL(view, layerVisibilityChanged);
const [regularLayers, elevationChanged] =
initLayers.compareVisibleLayers(
initVisibleLayers,
visibleLayersCurrently
);
console.log(regularLayers);
console.log(elevationChanged);

const sharedLocation = goToLocation.createURL(
view,
regularLayers,
elevationChanged
);
goToLocation.copyTextToClipboard(sharedLocation);

// Displaying popup
Expand Down Expand Up @@ -349,7 +350,8 @@ require([
// Going to specified location at runtime
const locationArray = goToLocation.getLocation();
goToLocation.getUndergroundInfo(view);
goToLocation.getLayerVisibility(view)
goToLocation.getLayerVisibility(view);
goToLocation.getElevationVisibility(view);

if (locationArray !== null) {
const viewpoint = goToLocation.setupViewPoint(locationArray);
Expand Down
43 changes: 33 additions & 10 deletions modules/goToLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ define(["esri/Viewpoint", "esri/Camera", "esri/geometry/Point"], (
}
},

getElevationVisibility: (view) => {
const urlString = new URL(window.location.href);

// Create a URL object
const url = new URLSearchParams(urlString.search);

// TODO mis juhtub, kui on puudu ja kas on mingid probleemid ka id parsimiga?
const layersParamStr = url.get("elevation");

// Changing layer visibility back and front end
if (layersParamStr !== null) {
const layersParamArr = layersParamStr.split(","); // Splitting the string at commas

view.map.ground.layers.forEach((obj) => {
if (layersParamArr.includes(obj.title)) {
obj.visible = !obj.visible;
}
});

}
},

// TODO mis juhtub kui üks element jääb puud, ilmselt kasutada ka ; kui on kaamera parameetrid?
getLocation: () => {
// Sample URL with parameters
Expand Down Expand Up @@ -76,15 +98,10 @@ define(["esri/Viewpoint", "esri/Camera", "esri/geometry/Point"], (
},

async copyTextToClipboard(text) {
try {
await navigator.clipboard.writeText(text);
console.log("Text copied to clipboard:", text);
} catch (err) {
console.error("Error copying text to clipboard:", err);
}
await navigator.clipboard.writeText(text);
},

createURL: (view, layerVisibility) => {
createURL: (view, regularLayers, elevationLayers) => {
// Get the current URL
const currentURL = window.location.href;

Expand All @@ -108,9 +125,15 @@ define(["esri/Viewpoint", "esri/Camera", "esri/geometry/Point"], (
queryParams.set("underground", "true");
}

if (layerVisibility.length !== 0) {
const layerVisibilityJoined = layerVisibility.join(",");
queryParams.set("layers", layerVisibilityJoined);

if (regularLayers.length !== 0) {
const regularLayerVisibilityJoined = regularLayers.join(",");
queryParams.set("layers", regularLayerVisibilityJoined);
}

if (elevationLayers.length !== 0) {
const elevationLayerVisibilityJoined = elevationLayers.join(",");
queryParams.set("elevation", elevationLayerVisibilityJoined);
}

// Append parameters to the URL
Expand Down
32 changes: 27 additions & 5 deletions modules/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,43 @@ define([
return { items: returnLayers };
},

// TODO ilmselt not visible välja võtta
getVisibleLayers: (view) => {
const { items } = view.map.allLayers;
const { initVisible, initNotVisible } = items.reduce(
const { initVisible } = items.reduce(
(acc, obj) => {
if (obj.visible === true) {
acc.initVisible.push(obj);
} else {
acc.initNotVisible.push(obj);
}
return acc;
},
{ initVisible: [], initNotVisible: [] }
{ initVisible: [] }
);

return initVisible;
},

compareVisibleLayers: (initVisibleLayers, visibleLayersCurrently) => {
const difference1 = initVisibleLayers.filter(
(item) => !visibleLayersCurrently.includes(item)
);
const difference2 = visibleLayersCurrently.filter(
(item) => !initVisibleLayers.includes(item)
);

const getTitle = (obj) => obj.title;
const layerVisibilityChanged = [
...difference1.map(getTitle),
...difference2.map(getTitle),
];

const elevationTitles = ["Kõrgusmudel", "Aluspõhi 50m", "Aluskord 50m"];
const regularLayers = layerVisibilityChanged.filter(
(item) => !elevationTitles.includes(item)
);
const elevationChanged = layerVisibilityChanged.filter((item) =>
elevationTitles.includes(item)
);

return [regularLayers, elevationChanged];
},
}));

0 comments on commit 711a6ec

Please sign in to comment.