Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logic to display dark map tiles
Browse files Browse the repository at this point in the history
hlfan authored Dec 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent c96e1da commit 2a3ec98
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions app/assets/javascripts/leaflet.map.js
Original file line number Diff line number Diff line change
@@ -32,6 +32,11 @@ L.OSM.Map = L.Map.extend({
layerOptions.apikey = OSM[value];
} else if (property === "leafletOsmId") {
layerConstructor = L.OSM[value];
} else if (property === "darkUrl") {
if (OSM.isDarkMap()) {
layerOptions.url = value;
layerOptions.schemeClass = 'dark';
}
} else {
layerOptions[property] = value;
}
@@ -52,9 +57,17 @@ L.OSM.Map = L.Map.extend({
code: "G"
});

this.on("layeradd", function (event) {
if (this.baseLayers.indexOf(event.layer) >= 0) {
this.setMaxZoom(event.layer.options.maxZoom);
this.on("layeradd", function ({layer}) {
if (this.baseLayers.indexOf(layer) >= 0) {
this.setMaxZoom(layer.options.maxZoom);
const container = layer.getContainer();
if (!container) return;
if (layer.options.schemeClass) container.classList.add(layer.options.schemeClass);
const filterRecievers = [container];
if (document.getElementById('map').contains(container))
filterRecievers.push(document.querySelector('.key-ui'));
for (let el of filterRecievers)
el.style.setProperty('--dark-mode-map-filter', layer.options.filter || 'none');
}
});

@@ -376,6 +389,17 @@ L.extend(L.Icon.Default.prototype, {
}
});

OSM.isDarkMap = function() {
var themeFlags = [
$('body').attr('data-map-theme'),
$('html').attr('data-bs-theme')
];
for (var themeFlag of themeFlags)
if (themeFlag)
return themeFlag === 'dark';
return window.matchMedia('(prefers-color-scheme: dark)').matches;
}

OSM.getUserIcon = function (url) {
return L.icon({
iconUrl: url || OSM.MARKER_RED,

0 comments on commit 2a3ec98

Please sign in to comment.