Skip to content

Commit 40c97af

Browse files
OlofSvahnVbgOlof Svahn
andauthored
Feature/1613 documenthandler open plugin map link (#1639)
* added open plugin logic from url params * updated to more secure code * added closing oopened plugins not specified in url * removed enableAppStateInHash check for showPlugins * added url udpate function * minor fixes * log fix * log removed * updated changelog --------- Co-authored-by: Olof Svahn <olof.svahn@varberg.se>
1 parent ec6db5f commit 40c97af

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232

3333
- Sketch: Added GPX import and export. [PR#1641](https://github.com/hajkmap/Hajk/pull/1641)
3434
- Sketch/Measurer: Disable snapping temporarily on keyDown (Space) ([issue](https://github.com/hajkmap/Hajk/issues/1616))
35+
- DocumentHandler: Now possible to add map-link that opens corresponding plugin (with/without enableAppStatInHash) ([issue](https://github.com/hajkmap/Hajk/issues/1613))
3536

3637
## [4.0.0] - 2025-04-04
3738

apps/client/src/models/AppModel.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,9 @@ class AppModel {
792792
// shown at start. All others should be hidden (no matter the setting in Admin).
793793
else {
794794
mapConfig.tools.forEach((t) => {
795-
t.options.visibleAtStart = pluginsToShow.includes(t.type);
795+
t.options.visibleAtStart = pluginsToShow.includes(
796+
t.type.toLowerCase()
797+
);
796798
});
797799
}
798800
}

apps/client/src/plugins/DocumentHandler/MapViewModel.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,61 @@ export default class MapViewModel {
3535
}
3636
};
3737

38+
showPluginsFromUrlParams = (url) => {
39+
try {
40+
//We check for plugin parameters in url
41+
const params = new Set(
42+
new URLSearchParams(url.replaceAll("#", ""))
43+
.getAll("p")
44+
.flatMap((p) => p.split(","))
45+
);
46+
// Open matching windows
47+
this.appModel.windows
48+
.filter((w) => params.has(w.type))
49+
.forEach((w) => w.showWindow());
50+
51+
// Close windows that are not in params, except "documentviewer"
52+
this.appModel.windows
53+
.filter((w) => !params.has(w.type) && w.type !== "documentviewer")
54+
.forEach((w) => w.closeWindow());
55+
} catch (error) {
56+
console.error("Error processing window parameters:", error);
57+
}
58+
};
59+
60+
updateUrl = (url) => {
61+
try {
62+
const params = new URLSearchParams(url.replaceAll("#", "&"));
63+
const pluginParams = params
64+
.getAll("p")
65+
.flatMap((p) => p.split(","))
66+
.filter(Boolean)
67+
.join(",");
68+
69+
if (pluginParams) {
70+
params.set("p", pluginParams);
71+
}
72+
73+
const newHash = "#" + params.toString();
74+
if (newHash !== window.location.hash) {
75+
window.location.hash = newHash;
76+
}
77+
} catch (error) {
78+
console.error("Error updating URL:", error);
79+
}
80+
};
81+
3882
bindSubscriptions = () => {
3983
this.localObserver.subscribe("fly-to", (url) => {
4084
this.globalObserver.publish("core.minimizeWindow");
4185
const mapSettings = this.convertMapSettingsUrlToOlSettings(url);
4286

87+
if (this.appModel.config.mapConfig.map.enableAppStateInHash === true) {
88+
this.updateUrl(url);
89+
} else {
90+
this.showPluginsFromUrlParams(url);
91+
}
92+
4393
if (mapSettings.layers !== null) {
4494
// Let's use Hajk's generic layer visibility
4595
// mechanism as exposed in AppModel

0 commit comments

Comments
 (0)