Skip to content

Commit

Permalink
On change of map provider/layer save change to local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ddrury committed Jan 20, 2022
1 parent 648a131 commit e53cc44
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
20 changes: 10 additions & 10 deletions app/Module/BingMaps.php
Expand Up @@ -111,32 +111,32 @@ public function leafletJsTileLayers(): array
(object) [
'bingMapsKey' => $api_key,
'default' => false,
'imagerySet' => 'Aerial',
'label' => 'Aerial',
'imagerySet' => 'Aerial',
'label' => 'Aerial',
],
(object) [
'bingMapsKey' => $api_key,
'default' => false,
'imagerySet' => 'CanvasDark',
'label' => 'Dark',
'imagerySet' => 'CanvasDark',
'label' => 'Dark',
],
(object) [
'bingMapsKey' => $api_key,
'default' => false,
'imagerySet' => 'CanvasGray',
'label' => 'Grey',
'imagerySet' => 'CanvasGray',
'label' => 'Grey',
],
(object) [
'bingMapsKey' => $api_key,
'default' => true,
'imagerySet' => 'CanvasLight',
'label' => 'Light',
'imagerySet' => 'CanvasLight',
'label' => 'Light',
],
(object) [
'bingMapsKey' => $api_key,
'default' => false,
'imagerySet' => 'Road',
'label' => 'Road',
'imagerySet' => 'Road',
'label' => 'Road',
],
];
}
Expand Down
22 changes: 17 additions & 5 deletions resources/js/webtrees.js
Expand Up @@ -630,27 +630,37 @@
zoomoutTitle: config.i18n.zoomOut,
});

const preferredLayer = localStorage.getItem('map_default_layer');
let defaultLayer = null;
let systemDefaultLayer = null;

for (let [, provider] of Object.entries(config.mapProviders)) {
for (let [, child] of Object.entries(provider.children)) {
child.name = provider.label + '-' + child.label;

if ('bingMapsKey' in child) {
child.layer = L.tileLayer.bing(child);
} else {
child.layer = L.tileLayer(child.url, child);
}
if (provider.default && child.default) {
systemDefaultLayer = child.layer;
}
if (preferredLayer === child['name']) {
defaultLayer = child.layer;
}
}
}

if (defaultLayer === null) {
console.log('No default map layer defined - using the first one.');
let defaultLayer = config.mapProviders[0].children[0].layer;
if (systemDefaultLayer === null) {
console.log('No default map layer defined - using the first one.');
defaultLayer = config.mapProviders[0].children[0].layer;
} else {
defaultLayer = systemDefaultLayer;
}
}


// Create the map with all controls and layers
return L.map(id, {
zoomControl: false,
Expand All @@ -660,8 +670,10 @@
.addControl(L.control.layers.tree(config.mapProviders, null, {
closedSymbol: config.icons.expand,
openedSymbol: config.icons.collapse,
}));

}))
.on('baselayerchange', (l) => {
localStorage.setItem('map_default_layer', l.layer.options.name);
});
};
}(window.webtrees = window.webtrees || {}));

Expand Down

0 comments on commit e53cc44

Please sign in to comment.