From 73e95dcf07268772fd6ee6f4a4ca2f1201e282a2 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 8 Sep 2023 16:44:45 +0200 Subject: [PATCH] Hide create/delete datalayers button + map settings to users without rights --- umap/static/umap/js/umap.controls.js | 54 +++++++++++++++------------- umap/static/umap/map.css | 3 ++ umap/views.py | 5 ++- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index 444982ee..13769369 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -273,7 +273,12 @@ L.U.ContinueLineAction = L.U.BaseVertexAction.extend({ }) // Leaflet.Toolbar doesn't allow twice same toolbar class… -L.U.SettingsToolbar = L.Toolbar.Control.extend({}) +L.U.SettingsToolbar = L.Toolbar.Control.extend({ + addTo: function (map) { + if (map.options.allowMapEdit === false) return + L.Toolbar.Control.prototype.addTo.call(this, map) + }, +}) L.U.DrawToolbar = L.Toolbar.Control.extend({ initialize: function (options) { L.Toolbar.Control.prototype.initialize.call(this, options) @@ -608,21 +613,26 @@ L.U.DataLayer.include({ edit.title = L._('Edit') table.title = L._('Edit properties in a table') remove.title = L._('Delete layer') + if (this.isReadOnly()) { + L.DomUtil.addClass(container, 'readonly') + } + else { + L.DomEvent.on(edit, 'click', this.edit, this) + L.DomEvent.on(table, 'click', this.tableEdit, this) + L.DomEvent.on( + remove, + 'click', + function () { + if (!this.isVisible()) return + if (!confirm(L._('Are you sure you want to delete this layer?'))) return + this._delete() + this.map.ui.closePanel() + }, + this + ) + } L.DomEvent.on(toggle, 'click', this.toggle, this) L.DomEvent.on(zoomTo, 'click', this.zoomTo, this) - L.DomEvent.on(edit, 'click', this.edit, this) - L.DomEvent.on(table, 'click', this.tableEdit, this) - L.DomEvent.on( - remove, - 'click', - function () { - if (!this.isVisible()) return - if (!confirm(L._('Are you sure you want to delete this layer?'))) return - this._delete() - this.map.ui.closePanel() - }, - this - ) L.DomUtil.addClass(container, this.getHidableClass()) L.DomUtil.classIf(container, 'off', !this.isVisible()) container.dataset.id = L.stamp(this) @@ -1130,14 +1140,10 @@ L.U.Map.include({ toggleCaveat() const download = L.DomUtil.create('a', 'button', container) download.textContent = L._('Download data') - L.DomEvent.on( - download, - 'click', - () => { - if (typeInput.value === 'umap') this.fullDownload() - else this.download(typeInput.value) - } - ) + L.DomEvent.on(download, 'click', () => { + if (typeInput.value === 'umap') this.fullDownload() + else this.download(typeInput.value) + }) this.ui.openPanel({ data: { html: container } }) }, @@ -1153,11 +1159,11 @@ L.U.Map.include({ let name = this.options.name || 'data' name = name.replace(/[^a-z0-9]/gi, '_').toLowerCase() const filename = name + type.ext - return {content, filetype: type.filetype, filename} + return { content, filetype: type.filetype, filename } }, download: function (mode) { - const {content, filetype, filename} = this.format(mode) + const { content, filetype, filename } = this.format(mode) const blob = new Blob([content], { type: filetype }) window.URL = window.URL || window.webkitURL const el = document.createElement('a') diff --git a/umap/static/umap/map.css b/umap/static/umap/map.css index d830fc0a..3ffe7c52 100644 --- a/umap/static/umap/map.css +++ b/umap/static/umap/map.css @@ -756,15 +756,18 @@ a.map-name:after { .umap-toggle-edit { background-position: -44px -48px; } +.readonly .layer-table-edit, .off .layer-table-edit { background-position: -74px -1px; } +.readonly .layer-edit, .off .layer-edit { background-position: -51px -72px; } .off .layer-zoom_to { background-position: -25px -54px; } +.readonly .layer-delete, .off .layer-delete { background-position: -122px -121px; } diff --git a/umap/views.py b/umap/views.py index d285a987..9759a24d 100644 --- a/umap/views.py +++ b/umap/views.py @@ -448,7 +448,10 @@ class MapDetailMixin: properties = { "urls": _urls_for_js(), "tilelayers": TileLayer.get_list(), - "allowEdit": self.is_edit_allowed(), + "allowEdit": self.is_edit_allowed(), # showEditMode + "allowMapEdit": self.object.can_edit(self.request.user, self.request) + if self.object + else True, # FIXME naming "default_iconUrl": "%sumap/img/marker.png" % settings.STATIC_URL, # noqa "umap_id": self.get_umap_id(), "starred": self.is_starred(),