diff --git a/umap/static/umap/js/modules/caption.js b/umap/static/umap/js/modules/caption.js index e1a8babd..7e316945 100644 --- a/umap/static/umap/js/modules/caption.js +++ b/umap/static/umap/js/modules/caption.js @@ -1,11 +1,39 @@ -import { DomUtil } from '../../vendors/leaflet/leaflet-src.esm.js' import { translate } from './i18n.js' import * as Utils from './utils.js' -export default class Caption { +const TEMPLATE = ` +
+ + + +
` + const [element, { toolbox, description }] = Utils.loadTemplateWithRefs(template) if (datalayer.options.description) { - DomUtil.element({ - tagName: 'span', - parent: p, - safeHTML: Utils.toHTML(datalayer.options.description), - }) + description.innerHTML = Utils.toHTML(datalayer.options.description) + } else { + description.hidden = true } - datalayer.renderToolbox(headline) - DomUtil.add('span', '', headline, `${datalayer.options.name} `) + datalayer.renderToolbox(toolbox) + parent.appendChild(element) + // Use textContent for security + const name = Utils.loadTemplate('') + name.textContent = datalayer.options.name + toolbox.appendChild(name) } - addCredits(container) { - const credits = DomUtil.createFieldset(container, translate('Credits')) - let title = DomUtil.add('h5', '', credits, translate('User content credits')) + addCredits() { if (this._umap.properties.shortCredit || this._umap.properties.longCredit) { - DomUtil.element({ - tagName: 'p', - parent: credits, - safeHTML: Utils.toHTML( - this._umap.properties.longCredit || this._umap.properties.shortCredit - ), - }) - } - if (this._umap.properties.licence) { - const licence = DomUtil.add( - 'p', - '', - credits, - `${translate('Map user content has been published under licence')} ` - ) - DomUtil.createLink( - '', - licence, - this._umap.properties.licence.name, - this._umap.properties.licence.url + this.elements.userCredits.innerHTML = Utils.toHTML( + this._umap.properties.longCredit || this._umap.properties.shortCredit ) } else { - DomUtil.add('p', '', credits, translate('No licence has been set')) + this.elements.userCredits.hidden = true } - title = DomUtil.create('h5', '', credits) - title.textContent = translate('Map background credits') - const tilelayerCredit = DomUtil.create('p', '', credits) - DomUtil.element({ - tagName: 'strong', - parent: tilelayerCredit, - textContent: `${this._leafletMap.selectedTilelayer.options.name} `, - }) - DomUtil.element({ - tagName: 'span', - parent: tilelayerCredit, - safeHTML: this._leafletMap.selectedTilelayer.getAttribution(), - }) + if (this._umap.properties.licence) { + this.elements.licenceLink.href = this._umap.properties.licence.url + this.elements.licenceLink.textContent = this._umap.properties.licence.name + this.elements.noLicence.hidden = true + } else { + this.elements.licence.hidden = true + } + this.elements.bgName.textContent = this._leafletMap.selectedTilelayer.options.name + this.elements.bgAttribution.innerHTML = + this._leafletMap.selectedTilelayer.getAttribution() const urls = { leaflet: 'http://leafletjs.com', django: 'https://www.djangoproject.com', @@ -113,7 +114,7 @@ export default class Caption { changelog: 'https://docs.umap-project.org/en/master/changelog/', version: this._umap.properties.umap_version, } - const creditHTML = translate( + this.elements.poweredBy.innerHTML = translate( ` Powered by Leaflet and Django, @@ -122,6 +123,5 @@ export default class Caption { `, urls ) - DomUtil.element({ tagName: 'p', innerHTML: creditHTML, parent: credits }) } }