Skip to content

Commit

Permalink
Allow lazy loading of markers (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagomartines11 authored May 22, 2022
1 parent d1268fc commit abde1f9
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/_js/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
this.floor = 7;
this.mapFloors = [];
this.markersLayers = [];
this.showMarkers = true;
this.markersLayerVisible = false;
this.options = {};
}
const URL_PREFIX = 'https://tibiamaps.github.io/tibia-map-data/';
Expand Down Expand Up @@ -251,13 +251,17 @@
}
};
TibiaMap.prototype._toggleMarkers = function () {
this.showMarkers = !this.showMarkers;
this._tryShowMarkers();
this.markersLayerVisible = !this.markersLayerVisible;
if (this.markersLayers.length === 0) {
this._loadMarkers(); // Lazy load in case markers were originally disabled
} else {
this._tryShowMarkers();
}
};
TibiaMap.prototype._tryShowMarkers = function () {
const _this = this;
this.markersLayers.forEach((layer, floor) => {
if (floor === _this.floor && _this.showMarkers) { _this.map.addLayer(layer); }
if (floor === _this.floor && _this.markersLayerVisible) { _this.map.addLayer(layer); }
else { _this.map.removeLayer(layer); }
});
};
Expand Down Expand Up @@ -379,6 +383,7 @@
map: _this
}).addTo(map);
if (_this.options.markersEnabled === 'true') {
_this.markersLayerVisible = true;
_this._loadMarkers();
}
};
Expand Down

0 comments on commit abde1f9

Please sign in to comment.