Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module.exports = {
extends: ["airbnb", "prettier"],
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
rules: {
"no-param-reassign": "off",
"class-methods-use-this": "off",
Expand Down
133 changes: 123 additions & 10 deletions public/example_templates/netjsonmap-indoormap-overlay.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@
#indoormap-container .njg-container .hidden .sideBarHandle {
left: 35px;
}
.njg-container .njg-sideBar {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I comment out this, I see no difference. What does this do?

display: none;
}
.njg-container .leaflet-popup-tip-container {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I comment this out, I see:

Image

Is this rule something that would be always needed? If so, move it to the main CSS.
Not sure why it's here?

I think 3.5% works better visually.

bottom: -5%;
}
.njg-container .default-popup .njg-popup-button-container {
display: flex;
justify-content: center;
Comment on lines +83 to +84
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this simpler alternative obtain the same effect?

Suggested change
display: flex;
justify-content: center;
text-align: center

}
.njg-container .default-popup .njg-popup-button {
padding: 6px 12px;
border: none;
border-radius: 5px;
background-color: black;
color: white;
cursor: pointer;
margin-top: 5px;
}
.njg-container .default-popup .njg-popup-button:hover {
background-color: rgb(85, 85, 85);
}
.njg-container .leaflet-popup-tip {
box-shadow: none;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as .njg-container .leaflet-popup-tip-container and should be listed inmediately after that rule.

</style>
</head>
<body>
Expand All @@ -95,6 +120,15 @@
},
},
baseOptions: {media: [{option: {tooltip: {show: true}}}]},
nodePopup: {
show: true,
content: createCustomPopup,
config: {
autoPan: true,
autoPanPadding: [25, 25],
offset: [-8, -8],
},
},
},
bookmarkableActions: {
enabled: true,
Expand All @@ -113,11 +147,7 @@
});
return data;
},
onClickElement(type, data) {
if (type === "node") {
openIndoorMap();
}
},
onClickElement(type, data) {},
Comment thread
dee077 marked this conversation as resolved.
});
netjsonmap.setUtils({
// Added to open popup for a specific location Id in selenium tests
Expand Down Expand Up @@ -154,6 +184,67 @@
// needed for selenium tests
window._geoMap = netjsonmap;

function getIndoorMapFragment() {
let decodedHash = "";
try {
decodedHash = decodeURIComponent(
window.location.hash.replace(/^#/, ""),
);
} catch (err) {
return null;
}
const fragments = decodedHash.split(";").filter(Boolean);

return fragments.find((fragment) => {
const params = new URLSearchParams(fragment);
return params.get("id") === "indoorMap";
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
if (getIndoorMapFragment()) {
openIndoorMap();
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

function createCustomPopup(node) {
const popupContent = document.createElement("div");
popupContent.classList.add("default-popup");
const location = node?.location || node?.properties?.location;
const fields = {
name: node?.name,
id: node?.id,
label: node?.label,
location: location
? `${location.lat.toFixed(8)}, ${location.lng.toFixed(8)}`
: null,
};
Object.keys(fields).forEach((key) => {
const value = fields[key];
if (!value) {
return;
}
const item = document.createElement("div");
item.classList.add("njg-tooltip-item");
const keyLabel = document.createElement("span");
keyLabel.classList.add("njg-tooltip-key");
keyLabel.textContent = key;
const valueLabel = document.createElement("span");
valueLabel.classList.add("njg-tooltip-value");
valueLabel.textContent = String(value);
item.appendChild(keyLabel);
item.appendChild(valueLabel);
popupContent.appendChild(item);
});
const buttonContainer = document.createElement("div");
buttonContainer.classList.add("njg-popup-button-container");

const button = document.createElement("button");
button.classList.add("njg-popup-button");
button.innerHTML = "Open Floorplan";
buttonContainer.appendChild(button);
popupContent.appendChild(buttonContainer);
button.addEventListener("click", () => openIndoorMap());
return popupContent;
}

function createIndoorMapContainer() {
const container = document.createElement("div");
container.id = "indoormap-container";
Expand All @@ -180,10 +271,13 @@
onClose();
}
container.remove();
window._indoorMap = null;
});
}

function openIndoorMap() {
if (window._indoorMap) {
return window._indoorMap;
}
let indoorMapContainer = document.getElementById("indoormap-container");
if (!indoorMapContainer) {
indoorMapContainer = createIndoorMapContainer();
Expand Down Expand Up @@ -211,6 +305,15 @@
animation: false,
},
baseOptions: {media: [{option: {tooltip: {show: true}}}]},
nodePopup: {
show: true,
content: null,
config: {
autoPan: true,
autoPanPadding: [25, 25],
offset: [-8, -8],
},
},
},
bookmarkableActions: {
enabled: true,
Expand Down Expand Up @@ -254,12 +357,14 @@

const mapOptions = this.echarts.getOption();
// Refer netjsonmap-indoormap.html for full explanation of this workaround
mapOptions.series[0].data.forEach((data) => {
mapOptions.series[0].data.forEach((data, index) => {
const node = data.node;
const px = Number(node.location.lng);
const py = -Number(node.location.lat);
const nodeProjected = L.point(topLeft.x + px, topLeft.y + py);
const nodeLatLng = map.unproject(nodeProjected, zoom);
this.data.nodes[index].location = nodeLatLng;
this.data.nodes[index].properties.location = nodeLatLng;
node.location = nodeLatLng;
node.properties.location = nodeLatLng;
data.value = [nodeLatLng.lng, nodeLatLng.lat];
Expand Down Expand Up @@ -302,6 +407,7 @@
map.setMaxBounds(bnds);
map.invalidateSize();
},
onClickElement: function () {},
},
);
indoor.setUtils({
Expand Down Expand Up @@ -337,9 +443,16 @@
const popstateHandler = () => {
const fragments = indoor.utils.parseUrlFragments();
const id = indoor.config.bookmarkableActions.id;
if (!fragments[id]) {
indoorMapContainer.remove();
window.removeEventListener("popstate", popstateHandler);
if (fragments[id]) {
if (!document.getElementById("indoormap-container")) {
openIndoorMap();
}
} else {
const container = document.getElementById("indoormap-container");
if (container) {
container.remove();
window._indoorMap = null;
}
}
};
window.addEventListener("popstate", popstateHandler);
Comment thread
dee077 marked this conversation as resolved.
Expand Down
40 changes: 40 additions & 0 deletions src/css/netjsongraph.css
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,46 @@
font-size: 18px;
}

.njg-container .default-popup {
padding: 18px;
}

.njg-container .default-popup a.leaflet-popup-close-button {
position: absolute;
top: 4px;
right: 4px;
font-size: 18px !important;
}

.njg-container .default-popup a.leaflet-popup-close-button:hover {
color: black !important;
}
Comment thread
dee077 marked this conversation as resolved.

.njg-container .default-popup .njg-tooltip-item {
display: flex;
align-items: center;
margin-bottom: 8px;
font-size: 14px;
}

.njg-container .default-popup .njg-tooltip-item:last-child {
margin-bottom: 0;
}

.njg-container .default-popup .njg-tooltip-key {
flex-basis: 45%;
font-weight: 600;
text-transform: capitalize;
color: black;
}

.njg-container .default-popup .njg-tooltip-value {
flex: 1;
overflow-wrap: anywhere;
word-break: normal;
color: black;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

@media only screen and (max-width: 850px) {
.njg-container .njg-sideBar {
top: 0;
Expand Down
9 changes: 9 additions & 0 deletions src/js/netjsongraph.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ const NetJSONGraphDefaultConfig = {
},
],
},
nodePopup: {
show: false,
content: null,
config: {
autoPan: true,
autoPanPadding: [25, 25],
offset: null,
},
},
},
mapTileConfig: [
{
Expand Down
110 changes: 110 additions & 0 deletions src/js/netjsongraph.gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,116 @@ class NetJSONGraphGUI {
};
}

/**
* Load and display a popup for a node on the map using leaflet popup
* @param {Object} node - The node data containing location and properties
* @returns {void}
*/
async loadNodePopup(node) {
if (!this.self.leaflet) {
console.error("Leaflet map not available. Cannot load popup.");
return;
}
if (this.self.echarts) {
this.self.echarts.dispatchAction({type: "hideTip"});
}
const nodeLocation = node?.properties?.location || node?.location;
if (!nodeLocation) {
console.error("Node location not available. Cannot load popup.");
return;
}
let popupContent = this.self.config.mapOptions.nodePopup.content;
if (popupContent == null) {
popupContent = this.createDefaultPopupContent(node);
} else if (popupContent && typeof popupContent === "function") {
const popupRequest = popupContent.call(this, node, this.self);
this.self.leaflet.currentPopupRequest = popupRequest;
try {
popupContent = await popupRequest;
} catch (error) {
if (this.self.leaflet.currentPopupRequest !== popupRequest) {
return;
}
console.error("Failed to build node popup content:", error);
return;
}
if (this.self.leaflet.currentPopupRequest !== popupRequest) {
return;
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const popupConfigInput = this.self.config.mapOptions.nodePopup.config || {};
const popupConfig = Object.fromEntries(
Object.entries(popupConfigInput).filter(([, value]) => value != null),
);

const popup = window.L.popup({
closeOnClick: false,
...popupConfig,
})
.setLatLng(nodeLocation)
.setContent(popupContent)
.openOn(this.self.leaflet);

this.self.leaflet.currentPopup = popup;
const {onOpen} = this.self.config.mapOptions.nodePopup;
if (onOpen && typeof onOpen === "function") {
try {
onOpen.call(this, this.self);
} catch (error) {
console.error("Failed to run popup onOpen callback:", error);
}
}
const popupElement = popup
.getElement()
.querySelector(".leaflet-popup-close-button");
popupElement.addEventListener("click", () => {
if (!this.self.config.bookmarkableActions?.enabled) {
return;
}
const fragments = this.self.utils.parseUrlFragments();
const {id} = this.self.config.bookmarkableActions;
const currentNodeId = fragments[id]?.get("nodeId");
const popupNodeId = node?.id || node?.properties?.id;
if (currentNodeId === popupNodeId) {
fragments[id].delete("nodeId");
this.self.utils.updateUrlFragments(fragments);
}
});
}

createDefaultPopupContent(node) {
const popupContent = document.createElement("div");
popupContent.classList.add("default-popup");
const location = node?.location || node?.properties?.location;
const lat = Number(location?.lat);
const lng = Number(location?.lng);
const hasCoords = Number.isFinite(lat) && Number.isFinite(lng);
const fields = {
name: node?.name,
id: node?.id,
label: node?.label,
location: hasCoords ? `${lat.toFixed(8)}, ${lng.toFixed(8)}` : null,
};
Object.keys(fields).forEach((key) => {
const value = fields[key];
if (!value) {
return;
}
const item = document.createElement("div");
item.classList.add("njg-tooltip-item");
const keyLabel = document.createElement("span");
keyLabel.classList.add("njg-tooltip-key");
keyLabel.textContent = key;
const valueLabel = document.createElement("span");
valueLabel.classList.add("njg-tooltip-value");
valueLabel.textContent = String(value);
item.appendChild(keyLabel);
item.appendChild(valueLabel);
popupContent.appendChild(item);
});
return popupContent;
}

init() {
this.sideBar = this.createSideBar();
if (this.self.config.switchMode) {
Expand Down
Loading
Loading