Remove the hasDirty concept

And only save the map in case of an `advanced` `editMode`.
This commit is contained in:
David Larlet 2023-09-12 11:31:54 -04:00 committed by Yohan Boniface
parent 24e4aed8d3
commit ddada8fb2b
2 changed files with 25 additions and 36 deletions

View file

@ -192,22 +192,6 @@ L.U.Map.include({
this this
) )
// FIXME naming
let hasDirty = false // global status
try {
Object.defineProperty(this, 'hasDirty', {
get: function () {
return hasDirty || this.dirty_datalayers.length
},
set: function (status) {
if (!hasDirty && status) self.fire('hasdirty')
hasDirty = status
self.checkDirty()
},
})
} catch (e) {
// Certainly IE8, which has a limited version of defineProperty
}
let isDirty = false // self status let isDirty = false // self status
try { try {
Object.defineProperty(this, 'isDirty', { Object.defineProperty(this, 'isDirty', {
@ -216,7 +200,7 @@ L.U.Map.include({
}, },
set: function (status) { set: function (status) {
isDirty = status isDirty = status
if (status) hasDirty = true this.checkDirty()
}, },
}) })
} catch (e) { } catch (e) {
@ -282,7 +266,7 @@ L.U.Map.include({
if (L.Util.queryString('download')) this.download() if (L.Util.queryString('download')) this.download()
}) })
window.onbeforeunload = () => this.hasDirty || null window.onbeforeunload = () => this.isDirty || null
this.backup() this.backup()
this.initContextMenu() this.initContextMenu()
this.on('click contextmenu.show', this.closeInplaceToolbar) this.on('click contextmenu.show', this.closeInplaceToolbar)
@ -521,7 +505,7 @@ L.U.Map.include({
key === L.U.Keys.E && key === L.U.Keys.E &&
modifierKey && modifierKey &&
this.editEnabled && this.editEnabled &&
!this.hasDirty !this.isDirty
) { ) {
L.DomEvent.stop(e) L.DomEvent.stop(e)
this.disableEdit() this.disableEdit()
@ -529,11 +513,11 @@ L.U.Map.include({
} }
if (key === L.U.Keys.S && modifierKey) { if (key === L.U.Keys.S && modifierKey) {
L.DomEvent.stop(e) L.DomEvent.stop(e)
if (this.hasDirty) { if (this.isDirty) {
this.save() this.save()
} }
} }
if (key === L.U.Keys.Z && modifierKey && this.hasDirty) { if (key === L.U.Keys.Z && modifierKey && this.isDirty) {
L.DomEvent.stop(e) L.DomEvent.stop(e)
this.askForReset() this.askForReset()
} }
@ -1062,18 +1046,17 @@ L.U.Map.include({
this.dirty_datalayers = [] this.dirty_datalayers = []
this.updateDatalayersControl() this.updateDatalayersControl()
this.initTileLayers() this.initTileLayers()
this.hasDirty = false
this.isDirty = false this.isDirty = false
}, },
checkDirty: function () { checkDirty: function () {
L.DomUtil.classIf(this._container, 'umap-is-dirty', this.hasDirty) L.DomUtil.classIf(this._container, 'umap-is-dirty', this.isDirty)
}, },
addDirtyDatalayer: function (datalayer) { addDirtyDatalayer: function (datalayer) {
if (this.dirty_datalayers.indexOf(datalayer) === -1) { if (this.dirty_datalayers.indexOf(datalayer) === -1) {
this.dirty_datalayers.push(datalayer) this.dirty_datalayers.push(datalayer)
this.hasDirty = true this.isDirty = true
} }
}, },
@ -1228,7 +1211,7 @@ L.U.Map.include({
}, },
] ]
} }
} else if (!this.permissions.hasDirty) { } else if (!this.permissions.isDirty) {
// Do not override local changes to permissions, // Do not override local changes to permissions,
// but update in case some other editors changed them in the meantime. // but update in case some other editors changed them in the meantime.
this.permissions.setOptions(data.permissions) this.permissions.setOptions(data.permissions)
@ -1246,15 +1229,18 @@ L.U.Map.include({
}, },
save: function () { save: function () {
if (!this.hasDirty) return if (!this.isDirty) return
if (this._default_extent) this.updateExtent() if (this._default_extent) this.updateExtent()
this.backup() this.backup()
this.once('saved', () => { this.once('saved', () => {
this.hasDirty = false
this.isDirty = false this.isDirty = false
}) })
if (this.isDirty) this.saveSelf() if (this.options.editMode === 'advanced') {
else this.permissions.save() // Map itself has no change, check permissions and continue // Only save the map if the user has the rights to do so.
this.saveSelf()
} else {
this.permissions.save()
}
}, },
sendEditLink: function () { sendEditLink: function () {
@ -1783,7 +1769,7 @@ L.U.Map.include({
}, },
disableEdit: function () { disableEdit: function () {
if (this.hasDirty) return if (this.isDirty) return
L.DomUtil.removeClass(document.body, 'umap-edit-enabled') L.DomUtil.removeClass(document.body, 'umap-edit-enabled')
this.editedFeature = null this.editedFeature = null
this.editEnabled = false this.editEnabled = false
@ -1794,7 +1780,6 @@ L.U.Map.include({
return this.options.editMode === 'simple' || this.options.editMode === 'advanced' return this.options.editMode === 'simple' || this.options.editMode === 'advanced'
}, },
getDisplayName: function () { getDisplayName: function () {
return this.options.name || L._('Untitled map') return this.options.name || L._('Untitled map')
}, },
@ -1954,7 +1939,7 @@ L.U.Map.include({
if (this.hasEditMode()) { if (this.hasEditMode()) {
items.push('-') items.push('-')
if (this.editEnabled) { if (this.editEnabled) {
if (!this.hasDirty) { if (!this.isDirty) {
items.push({ items.push({
text: `${L._('Stop editing')} (Ctrl+E)`, text: `${L._('Stop editing')} (Ctrl+E)`,
callback: this.disableEdit, callback: this.disableEdit,

View file

@ -20,7 +20,7 @@ L.U.MapPermissions = L.Class.extend({
set: function (status) { set: function (status) {
isDirty = status isDirty = status
if (status) { if (status) {
self.map.hasDirty = status self.map.isDirty = status
} }
}, },
}) })
@ -60,7 +60,9 @@ L.U.MapPermissions = L.Class.extend({
title = L.DomUtil.create('h4', '', container) title = L.DomUtil.create('h4', '', container)
if (this.isAnonymousMap()) { if (this.isAnonymousMap()) {
if (this.options.anonymous_edit_url) { if (this.options.anonymous_edit_url) {
const helpText = `${L._('Secret edit link:')}<br>${this.options.anonymous_edit_url}` const helpText = `${L._('Secret edit link:')}<br>${
this.options.anonymous_edit_url
}`
L.DomUtil.add('p', 'help-text', container, helpText) L.DomUtil.add('p', 'help-text', container, helpText)
} }
} else { } else {
@ -182,6 +184,8 @@ L.U.MapPermissions = L.Class.extend({
}, },
getShareStatusDisplay: function () { getShareStatusDisplay: function () {
return Object.fromEntries(this.map.options.share_statuses)[this.options.share_status] return Object.fromEntries(this.map.options.share_statuses)[
} this.options.share_status
]
},
}) })