chore: Extract a schema module from Map

This commit is contained in:
David Larlet 2024-02-27 14:58:19 -05:00 committed by Yohan Boniface
parent a6a1959c09
commit 68f3a9686a
4 changed files with 70 additions and 62 deletions

View file

@ -1,12 +1,12 @@
import URLs from './urls.js'
import Browser from './browser.js'
import * as Utils from './utils.js'
import SCHEMA from './schema.js'
import { Request, ServerRequest, RequestError, HTTPError, NOKError } from './request.js'
// Import modules and export them to the global scope.
// For the not yet module-compatible JS out there.
// Copy the leaflet module, it's expected by leaflet plugins to be writeable.
window.U = {
URLs,
Request,
@ -16,4 +16,5 @@ window.U = {
NOKError,
Browser,
Utils,
SCHEMA,
}

View file

@ -0,0 +1,62 @@
import { translate } from './i18n.js'
const SCHEMA = {
zoom: { type: undefined },
scrollWheelZoom: { type: Boolean },
scaleControl: { type: Boolean },
moreControl: { type: Boolean },
miniMap: { type: Boolean },
displayPopupFooter: { type: undefined },
onLoadPanel: { type: String },
defaultView: { type: String },
name: { type: String, label: translate('name') },
description: { type: String },
licence: { type: undefined },
tilelayer: { type: undefined },
overlay: { type: undefined },
limitBounds: { type: undefined },
color: { type: String },
iconClass: { type: String },
iconUrl: { type: String },
smoothFactor: { type: undefined },
iconOpacity: { type: undefined },
opacity: { type: undefined },
weight: { type: undefined },
fill: { type: undefined },
fillColor: { type: undefined },
fillOpacity: { type: undefined },
dashArray: { type: undefined },
popupShape: { type: String },
popupTemplate: { type: String },
popupContentTemplate: { type: String },
zoomTo: { type: Number },
captionBar: { type: Boolean },
captionMenus: { type: Boolean },
slideshow: { type: undefined },
sortKey: { type: undefined },
labelKey: { type: String },
filterKey: { type: undefined },
facetKey: { type: undefined },
slugKey: { type: undefined },
showLabel: { type: 'NullableBoolean' },
labelDirection: { type: undefined },
labelInteractive: { type: undefined },
outlinkTarget: { type: undefined },
shortCredit: { type: undefined },
longCredit: { type: undefined },
permanentCredit: { type: undefined },
permanentCreditBackground: { type: undefined },
zoomControl: { type: 'NullableBoolean' },
datalayersControl: { type: 'NullableBoolean' },
searchControl: { type: 'NullableBoolean' },
locateControl: { type: 'NullableBoolean' },
fullscreenControl: { type: 'NullableBoolean' },
editinosmControl: { type: 'NullableBoolean' },
embedControl: { type: 'NullableBoolean' },
measureControl: { type: 'NullableBoolean' },
tilelayersControl: { type: 'NullableBoolean' },
starControl: { type: 'NullableBoolean' },
easing: { type: undefined },
}
export default { SCHEMA }

View file

@ -1276,6 +1276,9 @@ U.FormBuilder = L.FormBuilder.extend({
initialize: function (obj, fields, options) {
this.map = obj.map || obj.getMap()
for (const [key, schema] of Object.entries(U.SCHEMA)) {
this.defaultOptions[key] = this.defaultOptions[key] || schema
}
L.FormBuilder.prototype.initialize.call(this, obj, fields, options)
this.on('finish', this.finish)
},

View file

@ -57,64 +57,6 @@ L.Map.mergeOptions({
U.Map = L.Map.extend({
includes: [ControlsMixin],
editableOptions: {
zoom: undefined,
scrollWheelZoom: Boolean,
scaleControl: Boolean,
moreControl: Boolean,
miniMap: Boolean,
displayPopupFooter: undefined,
onLoadPanel: String,
defaultView: String,
name: String,
description: String,
licence: undefined,
tilelayer: undefined,
overlay: undefined,
limitBounds: undefined,
color: String,
iconClass: String,
iconUrl: String,
smoothFactor: undefined,
iconOpacity: undefined,
opacity: undefined,
weight: undefined,
fill: undefined,
fillColor: undefined,
fillOpacity: undefined,
dashArray: undefined,
popupShape: String,
popupTemplate: String,
popupContentTemplate: String,
zoomTo: Number,
captionBar: Boolean,
captionMenus: Boolean,
slideshow: undefined,
sortKey: undefined,
labelKey: String,
filterKey: undefined,
facetKey: undefined,
slugKey: undefined,
showLabel: 'NullableBoolean',
labelDirection: undefined,
labelInteractive: undefined,
outlinkTarget: undefined,
shortCredit: undefined,
longCredit: undefined,
permanentCredit: undefined,
permanentCreditBackground: undefined,
zoomControl: 'NullableBoolean',
datalayersControl: 'NullableBoolean',
searchControl: 'NullableBoolean',
locateControl: 'NullableBoolean',
fullscreenControl: 'NullableBoolean',
editinosmControl: 'NullableBoolean',
embedControl: 'NullableBoolean',
measureControl: 'NullableBoolean',
tilelayersControl: 'NullableBoolean',
starControl: 'NullableBoolean',
easing: undefined,
},
initialize: function (el, geojson) {
// Locale name (pt_PT, en_US…)
@ -329,7 +271,7 @@ U.Map = L.Map.extend({
// FIXME retrocompat
L.Util.setBooleanFromQueryString(options, 'displayDataBrowserOnLoad')
L.Util.setBooleanFromQueryString(options, 'displayCaptionOnLoad')
for (const [key, type] of Object.entries(this.editableOptions)) {
for (const [key, { type }] of Object.entries(U.SCHEMA)) {
switch (type) {
case Boolean:
L.Util.setBooleanFromQueryString(options, key)
@ -904,7 +846,7 @@ U.Map = L.Map.extend({
let mustReindex = false
for (const option of Object.keys(this.editableOptions)) {
for (const option of Object.keys(U.SCHEMA)) {
if (typeof importedData.properties[option] !== 'undefined') {
this.options[option] = importedData.properties[option]
if (option === 'sortKey') mustReindex = true
@ -1033,7 +975,7 @@ U.Map = L.Map.extend({
exportOptions: function () {
const properties = {}
for (const option of Object.keys(this.editableOptions)) {
for (const option of Object.keys(U.SCHEMA)) {
if (typeof this.options[option] !== 'undefined') {
properties[option] = this.options[option]
}