Skip to content

Commit 751d759

Browse files
committed
[fix] Make applyUrlFragmentState wait for onReady to complete
1 parent acc53f7 commit 751d759

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ NetJSON format used internally is based on [networkgraph](http://netjson.org/rfc
336336
You can customize the style of GeoJSON features using `style` property. The list of all available properties can be found in the [Leaflet documentation](https://leafletjs.com/reference.html#geojson).
337337

338338
<!-- Todo: Update this -->
339+
339340
- `hashParams`
340341

341342
Configuration for adding hash parameters to the URL when a node is clicked.

public/example_templates/netjsonmap-indoormap-overlay.html

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
left: 50%;
3737
transform: translate(-50%, -50%);
3838
width: 80%;
39-
height: 80%;
39+
height: 90%;
4040
padding: 20px;
4141
border-radius: 8px;
4242
align-items: center;
@@ -167,24 +167,23 @@
167167
n.properties.location = n.location;
168168
});
169169
},
170-
onReady: function () {
170+
onReady: async function () {
171171
const map = this.leaflet;
172172
map.eachLayer((layer) => layer._url && map.removeLayer(layer));
173173
const img = new Image();
174174
imageUrl = "../assets/images/floorplan.png";
175175
img.src = imageUrl;
176-
img.onload = () => {
177-
const aspectRatio = img.width / img.height;
178-
const h = 700;
179-
const w = aspectRatio * h;
180-
const zoom = map.getMaxZoom() - 1;
181-
const sw = map.unproject([0, h * 2], zoom);
182-
const ne = map.unproject([w * 2, 0], zoom);
183-
const bnds = new L.LatLngBounds(sw, ne);
184-
L.imageOverlay(imageUrl, bnds).addTo(map);
185-
map.fitBounds(bnds);
186-
map.setMaxBounds(bnds);
187-
};
176+
await img.decode()
177+
const aspectRatio = img.width / img.height;
178+
const h = 700;
179+
const w = aspectRatio * h;
180+
const zoom = map.getMaxZoom() - 1;
181+
const sw = map.unproject([0, h * 2], zoom);
182+
const ne = map.unproject([w * 2, 0], zoom);
183+
const bnds = new L.LatLngBounds(sw, ne);
184+
L.imageOverlay(imageUrl, bnds).addTo(map);
185+
map.fitBounds(bnds);
186+
map.setMaxBounds(bnds);
188187
},
189188
},
190189
);

public/example_templates/netjsonmap-indoormap.html

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,30 +121,23 @@
121121
});
122122
},
123123

124-
onReady: function presentIndoormap() {
124+
onReady: async function presentIndoormap() {
125125
const netjsonmap = this.leaflet;
126126
const image = new Image();
127127
const imageUrl = "../assets/images/floorplan.png";
128128
image.src = imageUrl;
129+
await image.decode()
130+
const aspectRatio = image.width / image.height;
131+
const h = 700;
132+
const w = aspectRatio * h;
133+
const zoom = netjsonmap.getMaxZoom() - 1;
134+
const bottomLeft = netjsonmap.unproject([0, h * 2], zoom);
135+
const upperRight = netjsonmap.unproject([w * 2, 0], zoom);
136+
const bounds = new L.LatLngBounds(bottomLeft, upperRight);
129137

130-
image.onload = function () {
131-
const aspectRatio = image.width / image.height;
132-
const h = 700;
133-
const w = aspectRatio * h;
134-
const zoom = netjsonmap.getMaxZoom() - 1;
135-
const bottomLeft = netjsonmap.unproject([0, h * 2], zoom);
136-
const upperRight = netjsonmap.unproject([w * 2, 0], zoom);
137-
const bounds = new L.LatLngBounds(bottomLeft, upperRight);
138-
139-
L.imageOverlay(imageUrl, bounds).addTo(netjsonmap);
140-
netjsonmap.fitBounds(bounds);
141-
netjsonmap.setMaxBounds(bounds);
142-
console.log(netjsonmap)
143-
if(graph.selectedNode){
144-
const {location, zoom} = graph.selectedNode
145-
netjsonmap.setView([location.lat, location.lng], zoom)
146-
}
147-
};
138+
L.imageOverlay(imageUrl, bounds).addTo(netjsonmap);
139+
netjsonmap.fitBounds(bounds);
140+
netjsonmap.setMaxBounds(bounds);
148141

149142
// Remove any default tile layers and show only the floorplan image.
150143
netjsonmap.eachLayer((layer) => {

src/js/netjsongraph.core.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,16 @@ class NetJSONGraph {
7171
const [JSONParam, ...resParam] = this.JSONParam;
7272

7373
this.config.onRender.call(this);
74-
this.event.once("onReady", this.config.onReady.bind(this));
74+
// Using
75+
const onReadyDone = new Promise((resolve) => {
76+
this.event.once("onReady", async () => {
77+
await this.config.onReady.call(this);
78+
resolve();
79+
});
80+
});
7581
this.event.once("onLoad", this.config.onLoad.bind(this));
76-
this.event.once("applyUrlFragmentState", () => {
82+
this.event.once("applyUrlFragmentState", async () => {
83+
await onReadyDone;
7784
this.utils.applyUrlFragmentState.call(this, this);
7885
});
7986
this.utils.paginatedDataParse

0 commit comments

Comments
 (0)