Skip to content

Commit 63c14fe

Browse files
committed
refactor shareBox into separate class
download() and format() functions as well as dialog itself and IFrameExporter helper moved into new file umap.share.js
1 parent 199a0dc commit 63c14fe

File tree

7 files changed

+277
-269
lines changed

7 files changed

+277
-269
lines changed

umap/static/umap/js/umap.controls.js

Lines changed: 2 additions & 237 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ L.Control.Embed = L.Control.extend({
404404
'',
405405
container,
406406
L._('Share, download and embed this map'),
407-
map.renderShareBox,
408-
map
407+
map.share.open,
408+
map.share
409409
)
410410
L.DomEvent.on(shareButton, 'dblclick', L.DomEvent.stopPropagation)
411411
return container
@@ -888,46 +888,6 @@ L.U.Map.include({
888888
this.ui.openPanel({ data: { html: container }, actions: actions })
889889
},
890890

891-
EXPORT_TYPES: {
892-
geojson: {
893-
formatter: function (map) {
894-
return JSON.stringify(map.toGeoJSON(), null, 2)
895-
},
896-
ext: '.geojson',
897-
filetype: 'application/json',
898-
},
899-
gpx: {
900-
formatter: function (map) {
901-
return togpx(map.toGeoJSON())
902-
},
903-
ext: '.gpx',
904-
filetype: 'application/gpx+xml',
905-
},
906-
kml: {
907-
formatter: function (map) {
908-
return tokml(map.toGeoJSON())
909-
},
910-
ext: '.kml',
911-
filetype: 'application/vnd.google-earth.kml+xml',
912-
},
913-
csv: {
914-
formatter: function (map) {
915-
const table = []
916-
map.eachFeature((feature) => {
917-
const row = feature.toGeoJSON()['properties'],
918-
center = feature.getCenter()
919-
delete row['_umap_options']
920-
row['Latitude'] = center.lat
921-
row['Longitude'] = center.lng
922-
table.push(row)
923-
})
924-
return csv2geojson.dsv.csvFormat(table)
925-
},
926-
ext: '.csv',
927-
filetype: 'text/csv',
928-
},
929-
},
930-
931891
renderEditToolbar: function () {
932892
const container = L.DomUtil.create(
933893
'div',
@@ -1075,130 +1035,6 @@ L.U.Map.include({
10751035
this
10761036
)
10771037
},
1078-
1079-
renderShareBox: function () {
1080-
const container = L.DomUtil.create('div', 'umap-share')
1081-
const title = L.DomUtil.create('h3', '', container)
1082-
title.textContent = L._('Share, download and embed this map')
1083-
if (this.options.shortUrl) {
1084-
L.DomUtil.createButton(
1085-
'button copy-button',
1086-
container,
1087-
L._('copy'),
1088-
() => navigator.clipboard.writeText(this.options.shortUrl),
1089-
this
1090-
)
1091-
L.DomUtil.add('h4', '', container, L._('Short URL'))
1092-
const shortUrlLabel = L.DomUtil.create('label', '', container)
1093-
shortUrlLabel.textContent = L._('Share this link to view the map')
1094-
const shortUrl = L.DomUtil.create('input', 'umap-short-url', container)
1095-
shortUrl.type = 'text'
1096-
shortUrl.value = this.options.shortUrl
1097-
L.DomUtil.create('hr', '', container)
1098-
}
1099-
1100-
L.DomUtil.add('h4', '', container, L._('Download data'))
1101-
const downloadLabel = L.DomUtil.create('label', '', container)
1102-
downloadLabel.textContent = L._('Choose the format of the data to export')
1103-
const exportCaveat = L.DomUtil.add(
1104-
'small',
1105-
'help-text',
1106-
container,
1107-
L._('Only visible features will be downloaded.')
1108-
)
1109-
console.log(this.EXPORT_TYPES)
1110-
const typeInput = L.DomUtil.create(
1111-
'div',
1112-
`button-bar by${Object.keys(this.EXPORT_TYPES).length}`,
1113-
container
1114-
)
1115-
let option
1116-
for (const key in this.EXPORT_TYPES) {
1117-
if (this.EXPORT_TYPES.hasOwnProperty(key)) {
1118-
L.DomUtil.createButton(
1119-
'button',
1120-
typeInput,
1121-
this.EXPORT_TYPES[key].name || key,
1122-
() => this.download(key),
1123-
this
1124-
)
1125-
}
1126-
}
1127-
L.DomUtil.create('hr', '', container)
1128-
1129-
L.DomUtil.add('h4', '', container, L._('Backup data'))
1130-
const backupLabel = L.DomUtil.create('label', '', container)
1131-
backupLabel.textContent = L._('Download all data and settings of the map')
1132-
const downloadUrl = L.Util.template(this.options.urls.map_download, {
1133-
map_id: this.options.umap_id,
1134-
})
1135-
const link = L.DomUtil.createLink(
1136-
'button',
1137-
container,
1138-
L._('Download full backup'),
1139-
downloadUrl
1140-
)
1141-
let name = this.options.name || 'data'
1142-
name = name.replace(/[^a-z0-9]/gi, '_').toLowerCase()
1143-
link.setAttribute('download', `${name}.umap`)
1144-
L.DomUtil.create('hr', '', container)
1145-
1146-
const embedTitle = L.DomUtil.add('h4', '', container, L._('Embed the map'))
1147-
const iframe = L.DomUtil.create('textarea', 'umap-share-iframe', container)
1148-
const urlTitle = L.DomUtil.add('h4', '', container, L._('Direct link'))
1149-
const shortUrlLabel = L.DomUtil.create('label', '', container)
1150-
shortUrlLabel.textContent = L._(
1151-
'Share this link to open a customized map view'
1152-
)
1153-
const exportUrl = L.DomUtil.create('input', 'umap-share-url', container)
1154-
exportUrl.type = 'text'
1155-
const UIFields = [
1156-
['dimensions.width', { handler: 'Input', label: L._('width') }],
1157-
['dimensions.height', { handler: 'Input', label: L._('height') }],
1158-
[
1159-
'options.includeFullScreenLink',
1160-
{ handler: 'Switch', label: L._('Include full screen link?') },
1161-
],
1162-
[
1163-
'options.currentView',
1164-
{ handler: 'Switch', label: L._('Current view instead of default map view?') },
1165-
],
1166-
[
1167-
'options.keepCurrentDatalayers',
1168-
{ handler: 'Switch', label: L._('Keep current visible layers') },
1169-
],
1170-
[
1171-
'options.viewCurrentFeature',
1172-
{ handler: 'Switch', label: L._('Open current feature on load') },
1173-
],
1174-
'queryString.moreControl',
1175-
'queryString.scrollWheelZoom',
1176-
'queryString.miniMap',
1177-
'queryString.scaleControl',
1178-
'queryString.onLoadPanel',
1179-
'queryString.captionBar',
1180-
'queryString.captionMenus',
1181-
]
1182-
for (let i = 0; i < this.HIDDABLE_CONTROLS.length; i++) {
1183-
UIFields.push(`queryString.${this.HIDDABLE_CONTROLS[i]}Control`)
1184-
}
1185-
const iframeExporter = new L.U.IframeExporter(this)
1186-
const buildIframeCode = () => {
1187-
iframe.innerHTML = iframeExporter.build()
1188-
exportUrl.value = window.location.protocol + iframeExporter.buildUrl()
1189-
}
1190-
buildIframeCode()
1191-
const builder = new L.U.FormBuilder(iframeExporter, UIFields, {
1192-
callback: buildIframeCode,
1193-
})
1194-
const iframeOptions = L.DomUtil.createFieldset(
1195-
container,
1196-
L._('Embed and link options')
1197-
)
1198-
iframeOptions.appendChild(builder.build())
1199-
1200-
this.ui.openPanel({ data: { html: container } })
1201-
},
12021038
})
12031039

12041040
L.U.TileLayerControl = L.Control.extend({
@@ -1490,77 +1326,6 @@ L.U.ContextMenu = L.Map.ContextMenu.extend({
14901326
},
14911327
})
14921328

1493-
L.U.IframeExporter = L.Evented.extend({
1494-
options: {
1495-
includeFullScreenLink: true,
1496-
currentView: false,
1497-
keepCurrentDatalayers: false,
1498-
viewCurrentFeature: false,
1499-
},
1500-
1501-
queryString: {
1502-
scaleControl: false,
1503-
miniMap: false,
1504-
scrollWheelZoom: false,
1505-
zoomControl: true,
1506-
editMode: 'disabled',
1507-
moreControl: true,
1508-
searchControl: null,
1509-
tilelayersControl: null,
1510-
embedControl: null,
1511-
datalayersControl: true,
1512-
onLoadPanel: 'none',
1513-
captionBar: false,
1514-
captionMenus: true,
1515-
},
1516-
1517-
dimensions: {
1518-
width: '100%',
1519-
height: '300px',
1520-
},
1521-
1522-
initialize: function (map) {
1523-
this.map = map
1524-
this.baseUrl = L.Util.getBaseUrl()
1525-
// Use map default, not generic default
1526-
this.queryString.onLoadPanel = this.map.options.onLoadPanel
1527-
},
1528-
1529-
getMap: function () {
1530-
return this.map
1531-
},
1532-
1533-
buildUrl: function (options) {
1534-
const datalayers = []
1535-
if (this.options.viewCurrentFeature && this.map.currentFeature) {
1536-
this.queryString.feature = this.map.currentFeature.getSlug()
1537-
}
1538-
if (this.options.keepCurrentDatalayers) {
1539-
this.map.eachDataLayer((datalayer) => {
1540-
if (datalayer.isVisible() && datalayer.umap_id) {
1541-
datalayers.push(datalayer.umap_id)
1542-
}
1543-
})
1544-
this.queryString.datalayers = datalayers.join(',')
1545-
} else {
1546-
delete this.queryString.datalayers
1547-
}
1548-
const currentView = this.options.currentView ? window.location.hash : ''
1549-
const queryString = L.extend({}, this.queryString, options)
1550-
return `${this.baseUrl}?${L.Util.buildQueryString(queryString)}${currentView}`
1551-
},
1552-
1553-
build: function () {
1554-
const iframeUrl = this.buildUrl()
1555-
let code = `<iframe width="${this.dimensions.width}" height="${this.dimensions.height}" frameborder="0" allowfullscreen allow="geolocation" src="${iframeUrl}"></iframe>`
1556-
if (this.options.includeFullScreenLink) {
1557-
const fullUrl = this.buildUrl({ scrollWheelZoom: true })
1558-
code += `<p><a href="${fullUrl}">${L._('See full screen')}</a></p>`
1559-
}
1560-
return code
1561-
},
1562-
})
1563-
15641329
L.U.Editable = L.Editable.extend({
15651330
initialize: function (map, options) {
15661331
L.Editable.prototype.initialize.call(this, map, options)

umap/static/umap/js/umap.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ L.U.Map.include({
252252
}
253253
this.initShortcuts()
254254
this.onceDataLoaded(function () {
255-
if (L.Util.queryString('share')) this.renderShareBox()
255+
if (L.Util.queryString('share')) this.share.open()
256256
else if (this.options.onLoadPanel === 'databrowser') this.openBrowser()
257257
else if (this.options.onLoadPanel === 'caption') this.displayCaption()
258258
else if (
@@ -345,6 +345,7 @@ L.U.Map.include({
345345
this.browser = new L.U.Browser(this)
346346
this.importer = new L.U.Importer(this)
347347
this.drop = new L.U.DropControl(this)
348+
this.share = new L.U.Share(this)
348349
this.renderControls()
349350
},
350351

@@ -844,28 +845,6 @@ L.U.Map.include({
844845
})
845846
},
846847

847-
format: function (mode) {
848-
const type = this.EXPORT_TYPES[mode]
849-
const content = type.formatter(this)
850-
let name = this.options.name || 'data'
851-
name = name.replace(/[^a-z0-9]/gi, '_').toLowerCase()
852-
const filename = name + type.ext
853-
return { content, filetype: type.filetype, filename }
854-
},
855-
856-
download: function (mode) {
857-
const { content, filetype, filename } = this.format(mode)
858-
const blob = new Blob([content], { type: filetype })
859-
window.URL = window.URL || window.webkitURL
860-
const el = document.createElement('a')
861-
el.download = filename
862-
el.href = window.URL.createObjectURL(blob)
863-
el.style.display = 'none'
864-
document.body.appendChild(el)
865-
el.click()
866-
document.body.removeChild(el)
867-
},
868-
869848
processFileToImport: function (file, layer, type) {
870849
type = type || L.Util.detectFileType(file)
871850
if (!type) {
@@ -1698,9 +1677,9 @@ L.U.Map.include({
16981677
L.DomUtil.createButton(
16991678
'button umap-download',
17001679
advancedButtons,
1701-
L._('Open download panel'),
1702-
this.renderShareBox,
1703-
this
1680+
L._('Open share & download panel'),
1681+
this.share.open,
1682+
this.share
17041683
)
17051684
},
17061685

0 commit comments

Comments
 (0)