From c1746ae09f71f71ff92031f373f2484ae9710ca5 Mon Sep 17 00:00:00 2001 From: Nikola Plesa Date: Tue, 13 Oct 2020 10:42:28 +0200 Subject: [PATCH] Persist photo layer filters in the URL --- modules/renderer/photos.js | 31 +++++++++++++++++++++++++-- modules/ui/sections/photo_overlays.js | 4 ++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/modules/renderer/photos.js b/modules/renderer/photos.js index b93bab667..ca0b5fb0d 100644 --- a/modules/renderer/photos.js +++ b/modules/renderer/photos.js @@ -49,17 +49,35 @@ export function rendererPhotos(context) { return val === _dateFilters[0] ? _fromDate : _toDate; }; - photos.setDateFilter = function(type, val) { + photos.setDateFilter = function(type, val, updateUrl) { if (type === _dateFilters[0]) _fromDate = val; if (type === _dateFilters[1]) _toDate = val; dispatch.call('change', this); + if (updateUrl) { + setUrlFilterValue(type, val); + } }; - photos.setUsernameFilter = function(val) { + photos.setUsernameFilter = function(val, updateUrl) { _username = val; dispatch.call('change', this); + if (updateUrl) { + setUrlFilterValue('username', val); + } }; + function setUrlFilterValue(type, val) { + if (!window.mocha) { + var hash = utilStringQs(window.location.hash); + if (val) { + hash[type] = val; + } else { + delete hash[type]; + } + window.location.replace('#' + utilQsString(hash, true)); + } + } + function showsLayer(id) { var layer = context.layers().layer(id); return layer && layer.supported() && layer.enabled(); @@ -124,6 +142,15 @@ export function rendererPhotos(context) { if (layer) layer.enabled(true); }); } + if (hash.fromDate) { + this.setDateFilter('fromDate', hash.fromDate, false); + } + if (hash.toDate) { + this.setDateFilter('toDate', hash.toDate, false); + } + if (hash.username) { + this.setUsernameFilter(hash.username, false); + } context.layers().on('change.rendererPhotos', updateStorage); }; diff --git a/modules/ui/sections/photo_overlays.js b/modules/ui/sections/photo_overlays.js index 89faafa8c..deba09358 100644 --- a/modules/ui/sections/photo_overlays.js +++ b/modules/ui/sections/photo_overlays.js @@ -220,7 +220,7 @@ export function uiSectionPhotoOverlays(context) { }) .on('change', function(d) { var value = d3_select(this).property('value'); - context.photos().setDateFilter(d, value); + context.photos().setDateFilter(d, value, true); }); li @@ -275,7 +275,7 @@ export function uiSectionPhotoOverlays(context) { .property('value', context.photos().username()) .on('change', function() { var value = d3_select(this).property('value'); - context.photos().setUsernameFilter(value); + context.photos().setUsernameFilter(value, true); }); li