Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow enabling of markers via attributes (default: false) #47

Merged
merged 1 commit into from
May 22, 2022
Merged
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
23 changes: 14 additions & 9 deletions src/_js/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
this.mapFloors = [];
this.markersLayers = [];
this.showMarkers = true;
this.options = {}
}
const URL_PREFIX = 'https://tibiamaps.github.io/tibia-map-data/';
// `KNOWN_TILES` is a placeholder for the whitelist of known tiles:
Expand Down Expand Up @@ -200,7 +201,6 @@
});

function getMarkersSource() {
const mapContainer = document.querySelector('#map');
const urlParams = new URLSearchParams(window.location.search);
// Possible markers sources
// A) https://example.com?markers=<base64-json-str>#32368,32198,7:0
Expand All @@ -211,8 +211,8 @@
try {
if (urlParams.get('markers')) return JSON.parse(atob(urlParams.get('markers')));
if (urlParams.get('markersUrl')) return urlParams.get('markersUrl');
if (mapContainer.dataset.markers) return JSON.parse(mapContainer.dataset.markers);
if (mapContainer.dataset.markersUrl) return mapContainer.dataset.markersUrl;
if (_this.options.markers) return JSON.parse(_this.options.markers);
if (_this.options.markersUrl) return _this.options.markersUrl;
} catch (error) {
console.error('Invalid custom markers data. Falling back to default markers');
}
Expand Down Expand Up @@ -263,8 +263,9 @@
};


TibiaMap.prototype.init = function() {
TibiaMap.prototype.init = function(options) {
const _this = this;
_this.options = options;
modifyLeaflet();
// Taken from https://tibiamaps.github.io/tibia-map-data/bounds.json, which
// rarely (if ever) changes.
Expand Down Expand Up @@ -372,15 +373,19 @@
L.ExivaButton.btns = L.exivaButton({
crosshairs: this.crosshairs
}).addTo(map);
L.MarkersButton.btns = L.markersButton({
map: _this
}).addTo(map);
_this._showHoverTile();
_this._loadMarkers();

if (_this.options.markersEnabled === 'true') {
L.MarkersButton.btns = L.markersButton({
map: _this
}).addTo(map);
mathiasbynens marked this conversation as resolved.
Show resolved Hide resolved
_this._loadMarkers();
}
};

const mapContainer = document.querySelector('#map');
const map = new TibiaMap();
map.init();
map.init(mapContainer.dataset);
L.LevelButtons.btns.setTibiaMap(map);

const fakeClick = function(target) {
Expand Down
6 changes: 3 additions & 3 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<link rel="icon" href="favicon.ico">
</head>
<body>
<!--<div id="map" data-markers='[{"description":"Shops","icon":"bag","x":32368,"y":32198,"z":7},{"description":"Temple","icon":"cross","x":32369,"y":32241,"z":7}]'></div>-->
<!--<div id="map" data-markers-url="https://tibiamaps.github.io/tibia-map-data/poi-markers.json"></div>-->
<div id="map"></div>
<!--<div id="map" data-markers-enabled="true" data-markers='[{"description":"Shops","icon":"bag","x":32368,"y":32198,"z":7},{"description":"Temple","icon":"cross","x":32369,"y":32241,"z":7}]'></div>-->
<!--<div id="map" data-markers-enabled="true" data-markers-url="https://tibiamaps.github.io/tibia-map-data/poi-markers.json"></div>-->
<div id="map" data-markers-enabled="true"></div>
<script src="../dist/map.js"></script>
</body>
</html>