From ba0ba1a85d2b9296440009d78358e80ea31943d5 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Tue, 7 May 2024 12:28:13 +0200 Subject: [PATCH] wip: add a badge on filters title and datalayers icon when active --- umap/static/umap/base.css | 21 +++++++++++++++++++++ umap/static/umap/js/modules/browser.js | 8 ++++++++ umap/static/umap/js/modules/facets.js | 10 +++++++++- umap/static/umap/js/modules/utils.js | 8 ++++++++ umap/static/umap/js/umap.controls.js | 7 +++++++ umap/static/umap/vars.css | 1 + 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/umap/static/umap/base.css b/umap/static/umap/base.css index 62db0ee4..6abd0c78 100644 --- a/umap/static/umap/base.css +++ b/umap/static/umap/base.css @@ -323,6 +323,27 @@ fieldset legend { padding: 0 5px; } +[data-badge] { + position: relative; +} +[data-badge]:after { + position: absolute; + right: -8px; + top: -8px; + min-width: 8px; + min-height: 8px; + line-height: 8px; + padding: 2px; + font-weight: bold; + background-color: var(--color-flashyGreen); + color: var(--color-darkBlue); + text-align: center; + font-size: .75rem; + border-radius: 50%; + content: attr(data-badge); + border: solid .5px var(--color-darkBlue); +} + /* Switch */ input.switch:empty { display: none; diff --git a/umap/static/umap/js/modules/browser.js b/umap/static/umap/js/modules/browser.js index 4f00f41c..22811c86 100644 --- a/umap/static/umap/js/modules/browser.js +++ b/umap/static/umap/js/modules/browser.js @@ -114,6 +114,8 @@ export default class Browser { datalayer.resetLayer(true) this.updateDatalayer(datalayer) }) + U.Utils.toggleBadge(this.filtersTitle, this.hasFilters()) + U.Utils.toggleBadge('.umap-control-browse', this.hasFilters()) } redraw() { @@ -124,6 +126,10 @@ export default class Browser { return !!document.querySelector('.umap-browser') } + hasFilters() { + return !!this.options.filter || this.map.facets.isActive() + } + onMoveEnd() { if (!this.isOpen()) return const isListDynamic = this.options.inBbox @@ -157,6 +163,8 @@ export default class Browser { className: 'filters', icon: 'icon-filters', }) + this.filtersTitle = container.querySelector('summary') + U.Utils.toggleBadge(this.filtersTitle, this.hasFilters()) this.dataContainer = DomUtil.create('div', '', container) let fields = [ diff --git a/umap/static/umap/js/modules/facets.js b/umap/static/umap/js/modules/facets.js index 0cf3a592..c1ecf5b7 100644 --- a/umap/static/umap/js/modules/facets.js +++ b/umap/static/umap/js/modules/facets.js @@ -56,8 +56,16 @@ export default class Facets { return properties } - build() { + isActive() { + for (let { type, min, max, choices } of Object.values(this.selected)) { + if (min !== undefined || max != undefined || choices?.length) { + return true + } + } + return false + } + build() { const defined = this.getDefined() const names = Object.keys(defined) const facetProperties = this.compute(names, defined) diff --git a/umap/static/umap/js/modules/utils.js b/umap/static/umap/js/modules/utils.js index 340d1122..e7974a17 100644 --- a/umap/static/umap/js/modules/utils.js +++ b/umap/static/umap/js/modules/utils.js @@ -362,3 +362,11 @@ export function parseNaiveDate(value) { // Let's pretend naive date are UTC, and remove time… return new Date(Date.UTC(naive.getFullYear(), naive.getMonth(), naive.getDate())) } + +export function toggleBadge(element, value) { + if (!element.nodeType) element = document.querySelector(element) + if (!element) return + // True means simple badge, without content + if (value) element.dataset.badge = value === true ? ' ' : value + else delete element.dataset.badge +} diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index 7d2c4cf1..73793296 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -496,8 +496,11 @@ L.Control.Button = L.Control.extend({ this ) L.DomEvent.on(button, 'dblclick', L.DomEvent.stopPropagation) + this.afterAdd(container) return container }, + + afterAdd: function (container) {}, }) U.DataLayersControl = L.Control.Button.extend({ @@ -507,6 +510,10 @@ U.DataLayersControl = L.Control.Button.extend({ title: L._('See layers'), }, + afterAdd: function (container) { + U.Utils.toggleBadge(container, this.map.browser.hasFilters()) + }, + onClick: function () { this.map.openBrowser() }, diff --git a/umap/static/umap/vars.css b/umap/static/umap/vars.css index 86a1f38d..658ca890 100644 --- a/umap/static/umap/vars.css +++ b/umap/static/umap/vars.css @@ -5,6 +5,7 @@ --color-lightGray: #ddd; --color-darkGray: #323737; --color-light: white; + --color-flashyGreen: #b9f5d2; --background-color: var(--color-light);