Compare commits

..

No commits in common. "32acdc2070d6b9f5eb48227b0be3d2f6f180a623" and "a8ed8c0d3d512745efebd344e5f884d03ddf6e2d" have entirely different histories.

5 changed files with 23 additions and 72 deletions

View file

@ -199,9 +199,8 @@ class Feature {
this._umap.slideshow.current = this
}
this._umap.currentFeature = this
this.attachPopup().then(() => {
this.attachPopup()
this.ui.openPopup(latlng || this.center)
})
}
render(fields) {
@ -356,11 +355,9 @@ class Feature {
return loadPopup(this.getOption('popupShape') || old)
}
async attachPopup() {
attachPopup() {
const Class = this.getPopupClass()
const popup = new Class(this)
this.ui.bindPopup(popup)
return popup.loadContent()
this.ui.bindPopup(new Class(this))
}
async confirmDelete() {

View file

@ -21,22 +21,23 @@ export default function loadPopup(name) {
const Popup = BasePopup.extend({
initialize: function (feature) {
this.feature = feature
BasePopup.prototype.initialize.call(this, {}, feature.ui)
this.container = DomUtil.create('div', 'umap-popup')
this.format()
BasePopup.prototype.initialize.call(this, {}, feature)
this.setContent(this.container)
},
loadContent: async function () {
const container = DomUtil.create('div', 'umap-popup')
format: function () {
const name = this.feature.getOption('popupTemplate')
this.content = await loadTemplate(name, this.feature, container)
const elements = container.querySelectorAll('img,iframe')
this.content = loadTemplate(name, this.feature, this.container)
const elements = this.container.querySelectorAll('img,iframe')
for (const element of elements) {
this.onElementLoaded(element)
}
if (!elements.length && container.textContent.replace('\n', '') === '') {
container.innerHTML = ''
DomUtil.add('h3', '', container, this.feature.getDisplayName())
if (!elements.length && this.container.textContent.replace('\n', '') === '') {
this.container.innerHTML = ''
DomUtil.add('h3', '', this.container, this.feature.getDisplayName())
}
this.setContent(container)
},
onElementLoaded: function (el) {

View file

@ -2,9 +2,8 @@ import { DomUtil, DomEvent } from '../../../vendors/leaflet/leaflet-src.esm.js'
import { translate, getLocale } from '../i18n.js'
import * as Utils from '../utils.js'
import * as Icon from './icon.js'
import { Request } from '../request.js'
export default async function loadTemplate(name, feature, container) {
export default function loadTemplate(name, feature, container) {
let klass = PopupTemplate
switch (name) {
case 'GeoRSSLink':
@ -19,12 +18,9 @@ export default async function loadTemplate(name, feature, container) {
case 'OSM':
klass = OSM
break
case 'Wikipedia':
klass = Wikipedia
break
}
const content = new klass()
return await content.render(feature, container)
return content.render(feature, container)
}
class PopupTemplate {
@ -80,10 +76,10 @@ class PopupTemplate {
}
}
async render(feature, container) {
render(feature, container) {
const title = this.renderTitle(feature)
if (title) container.appendChild(title)
const body = await this.renderBody(feature)
const body = this.renderBody(feature)
if (body) DomUtil.add('div', 'umap-popup-content', container, body)
const footer = this.renderFooter(feature)
if (footer) container.appendChild(footer)
@ -115,7 +111,7 @@ class Table extends TitleMixin(PopupTemplate) {
)
}
async renderBody(feature) {
renderBody(feature) {
const table = document.createElement('table')
for (const key in feature.properties) {
@ -129,7 +125,7 @@ class Table extends TitleMixin(PopupTemplate) {
}
class GeoRSSImage extends TitleMixin(PopupTemplate) {
async renderBody(feature) {
renderBody(feature) {
const body = DomUtil.create('a')
body.href = feature.properties.link
body.target = '_blank'
@ -146,7 +142,7 @@ class GeoRSSImage extends TitleMixin(PopupTemplate) {
}
class GeoRSSLink extends PopupTemplate {
async renderBody(feature) {
renderBody(feature) {
if (feature.properties.link) {
return Utils.loadTemplate(
`<a href="${feature.properties.link}" target="_blank"><h3>${feature.getDisplayName()}</h3></a>`
@ -155,7 +151,7 @@ class GeoRSSLink extends PopupTemplate {
}
}
class OSM extends PopupTemplate {
class OSM extends TitleMixin(PopupTemplate) {
renderTitle(feature) {
const title = DomUtil.add('h3', 'popup-title')
const color = feature.getPreviewColor()
@ -176,7 +172,7 @@ class OSM extends PopupTemplate {
return props.name
}
async renderBody(feature) {
renderBody(feature) {
const props = feature.properties
const body = document.createElement('div')
const locale = getLocale()
@ -242,43 +238,3 @@ class OSM extends PopupTemplate {
return body
}
}
const _WIKIPEDIA_CACHE = {}
class Wikipedia extends PopupTemplate {
async callWikipedia(wikipedia) {
if (wikipedia && _WIKIPEDIA_CACHE[wikipedia]) return _WIKIPEDIA_CACHE[wikipedia]
// Wikipedia value should be in form of "{locale}:{title}", according to https://wiki.openstreetmap.org/wiki/Key:wikipedia
const [locale, page] = wikipedia.split(':')
const url = `https://${locale}.wikipedia.org/w/api.php?action=query&format=json&origin=*&pithumbsize=500&prop=extracts|pageimages&titles=${page}`
const request = new Request()
const response = await request.get(url)
if (response?.ok) {
const data = await response.json()
_WIKIPEDIA_CACHE[wikipedia] = data
return data
}
}
async renderBody(feature) {
const body = document.createElement('div')
const wikipedia = feature.properties.wikipedia
if (!wikipedia) return ''
const data = await this.callWikipedia(wikipedia)
if (data) {
const page = Object.values(data.query.pages)[0]
const title = page.title || feature.getDisplayName()
const extract = page.extract || ''
const thumbnail = page.thumbnail?.source
const [content, { image }] = Utils.loadTemplateWithRefs(
`<div><h3>${Utils.escapeHTML(title)}</h3><img data-ref="image" hidden src="" />${Utils.escapeHTML(extract)}</div>`
)
if (thumbnail) {
image.src = thumbnail
image.hidden = false
}
body.appendChild(content)
}
return body
}
}

View file

@ -404,7 +404,6 @@ export const SCHEMA = {
['GeoRSSImage', translate('GeoRSS (title + image)')],
['GeoRSSLink', translate('GeoRSS (only link)')],
['OSM', translate('OpenStreetMap')],
['Wikipedia', translate('Wikipedia')],
],
default: 'Default',
},

View file

@ -115,8 +115,6 @@ export function escapeHTML(s) {
'span',
'dt',
'dd',
'b',
'i',
],
ADD_ATTR: [
'target',