chore: use WithTemplate for Caption

This commit is contained in:
Yohan Boniface 2024-12-19 10:33:12 +01:00
parent 24511d796d
commit 0e78d58c03

View file

@ -1,11 +1,39 @@
import { DomUtil } from '../../vendors/leaflet/leaflet-src.esm.js'
import { translate } from './i18n.js' import { translate } from './i18n.js'
import * as Utils from './utils.js' import * as Utils from './utils.js'
export default class Caption { const TEMPLATE = `
<div class="umap-caption">
<hgroup>
<h3>
<i class="icon icon-16 icon-caption icon-block"></i>
<span class="map-name" data-ref="name"></span>
</h3>
<h4 data-ref="author"></h4>
</hgroup>
<div class="umap-map-description text" data-ref="description"></div>
<div class="datalayer-container" data-ref="datalayersContainer"></div>
<div class="credits-container">
<details>
<summary>${translate('Credits')}</summary>
<fieldset>
<h5>${translate('User content credits')}</h5>
<p data-ref="userCredits"></p>
<p data-ref="licence">${translate('Map user content has been published under licence')} <a href="#" data-ref="licenceLink"></a></p>
<p data-ref="noLicence">${translate('No licence has been set')}</p>
<h5>${translate('Map background credits')}</h5>
<p><strong data-ref="bgName"></strong> <span data-ref="bgAttribution"></span></p>
<p data-ref="poweredBy"></p>
</fieldset>
</details>
</div>
</div>`
export default class Caption extends Utils.WithTemplate {
constructor(umap, leafletMap) { constructor(umap, leafletMap) {
super()
this._umap = umap this._umap = umap
this._leafletMap = leafletMap this._leafletMap = leafletMap
this.loadTemplate(TEMPLATE)
} }
isOpen() { isOpen() {
@ -18,94 +46,67 @@ export default class Caption {
} }
open() { open() {
const container = DomUtil.create('div', 'umap-caption') this.elements.name.textContent = this._umap.getDisplayName()
const hgroup = DomUtil.element({ tagName: 'hgroup', parent: container }) this.elements.author.innerHTML = ''
DomUtil.createTitle( this._umap.addAuthorLink(this.elements.author)
hgroup,
this._umap.getDisplayName(),
'icon-caption icon-block',
'map-name'
)
const title = Utils.loadTemplate('<h4></h4>')
hgroup.appendChild(title)
this._umap.addAuthorLink(title)
if (this._umap.properties.description) { if (this._umap.properties.description) {
const description = DomUtil.element({ this.elements.description.innerHTML = Utils.toHTML(
tagName: 'div', this._umap.properties.description
className: 'umap-map-description text', )
safeHTML: Utils.toHTML(this._umap.properties.description), } else {
parent: container, this.elements.description.hidden = true
})
} }
const datalayerContainer = DomUtil.create('div', 'datalayer-container', container) this.elements.datalayersContainer.innerHTML = ''
this._umap.eachDataLayerReverse((datalayer) => this._umap.eachDataLayerReverse((datalayer) =>
this.addDataLayer(datalayer, datalayerContainer) this.addDataLayer(datalayer, this.elements.datalayersContainer)
) )
const creditsContainer = DomUtil.create('div', 'credits-container', container) this.addCredits()
this.addCredits(creditsContainer) this._umap.panel.open({ content: this.element }).then(() => {
this._umap.panel.open({ content: container }).then(() => {
// Create the legend when the panel is actually on the DOM // Create the legend when the panel is actually on the DOM
this._umap.eachDataLayerReverse((datalayer) => datalayer.renderLegend()) this._umap.eachDataLayerReverse((datalayer) => datalayer.renderLegend())
}) })
} }
addDataLayer(datalayer, container) { addDataLayer(datalayer, parent) {
if (!datalayer.options.inCaption) return if (!datalayer.options.inCaption) return
const p = DomUtil.create('p', `caption-item ${datalayer.cssId}`, container) const template = `
const legend = DomUtil.create('span', 'datalayer-legend', p) <p class="caption-item ${datalayer.cssId}">
const headline = DomUtil.create('strong', '', p) <span class="datalayer-legend"></span>
<strong data-ref="toolbox"></strong>
<span data-ref="description"></span>
</p>`
const [element, { toolbox, description }] = Utils.loadTemplateWithRefs(template)
if (datalayer.options.description) { if (datalayer.options.description) {
DomUtil.element({ description.innerHTML = Utils.toHTML(datalayer.options.description)
tagName: 'span', } else {
parent: p, description.hidden = true
safeHTML: Utils.toHTML(datalayer.options.description),
})
} }
datalayer.renderToolbox(headline) datalayer.renderToolbox(toolbox)
DomUtil.add('span', '', headline, `${datalayer.options.name} `) parent.appendChild(element)
// Use textContent for security
const name = Utils.loadTemplate('<span></span>')
name.textContent = datalayer.options.name
toolbox.appendChild(name)
} }
addCredits(container) { addCredits() {
const credits = DomUtil.createFieldset(container, translate('Credits'))
let title = DomUtil.add('h5', '', credits, translate('User content credits'))
if (this._umap.properties.shortCredit || this._umap.properties.longCredit) { if (this._umap.properties.shortCredit || this._umap.properties.longCredit) {
DomUtil.element({ this.elements.userCredits.innerHTML = Utils.toHTML(
tagName: 'p', this._umap.properties.longCredit || this._umap.properties.shortCredit
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
) )
} else { } else {
DomUtil.add('p', '', credits, translate('No licence has been set')) this.elements.userCredits.hidden = true
} }
title = DomUtil.create('h5', '', credits) if (this._umap.properties.licence) {
title.textContent = translate('Map background credits') this.elements.licenceLink.href = this._umap.properties.licence.url
const tilelayerCredit = DomUtil.create('p', '', credits) this.elements.licenceLink.textContent = this._umap.properties.licence.name
DomUtil.element({ this.elements.noLicence.hidden = true
tagName: 'strong', } else {
parent: tilelayerCredit, this.elements.licence.hidden = true
textContent: `${this._leafletMap.selectedTilelayer.options.name} `, }
}) this.elements.bgName.textContent = this._leafletMap.selectedTilelayer.options.name
DomUtil.element({ this.elements.bgAttribution.innerHTML =
tagName: 'span', this._leafletMap.selectedTilelayer.getAttribution()
parent: tilelayerCredit,
safeHTML: this._leafletMap.selectedTilelayer.getAttribution(),
})
const urls = { const urls = {
leaflet: 'http://leafletjs.com', leaflet: 'http://leafletjs.com',
django: 'https://www.djangoproject.com', django: 'https://www.djangoproject.com',
@ -113,7 +114,7 @@ export default class Caption {
changelog: 'https://docs.umap-project.org/en/master/changelog/', changelog: 'https://docs.umap-project.org/en/master/changelog/',
version: this._umap.properties.umap_version, version: this._umap.properties.umap_version,
} }
const creditHTML = translate( this.elements.poweredBy.innerHTML = translate(
` `
Powered by <a href="{leaflet}">Leaflet</a> and Powered by <a href="{leaflet}">Leaflet</a> and
<a href="{django}">Django</a>, <a href="{django}">Django</a>,
@ -122,6 +123,5 @@ export default class Caption {
`, `,
urls urls
) )
DomUtil.element({ tagName: 'p', innerHTML: creditHTML, parent: credits })
} }
} }