From 41844a28bebd5870668e4c730c64f5d3c6e13c75 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= <skunk@xover.mud.at>
Date: Sun, 18 Nov 2018 14:29:29 +0100
Subject: [PATCH] exportAll(): use sub modules ExportGeoJSON() and
 ExportOSMXML()

---
 lang/ast.json        |  3 +-
 lang/ca.json         |  3 +-
 lang/cs.json         |  3 +-
 lang/da.json         |  3 +-
 lang/de.json         |  3 +-
 lang/el.json         |  3 +-
 lang/en.json         |  4 +--
 lang/es.json         |  3 +-
 lang/et.json         |  3 +-
 lang/fr.json         |  3 +-
 lang/hu.json         |  3 +-
 lang/it.json         |  3 +-
 lang/ja.json         |  3 +-
 lang/nl.json         |  3 +-
 lang/pl.json         |  3 +-
 lang/pt-br.json      |  3 +-
 lang/pt.json         |  3 +-
 lang/ro.json         |  3 +-
 lang/ru.json         |  3 +-
 lang/sr.json         |  3 +-
 lang/template.json   |  3 +-
 lang/uk.json         |  3 +-
 src/ExportGeoJSON.js | 34 +++++++++++++++++++++++
 src/ExportOSMXML.js  | 20 ++++++++++++++
 src/exportAll.js     | 65 ++++++++++++++------------------------------
 25 files changed, 119 insertions(+), 67 deletions(-)
 create mode 100644 src/ExportGeoJSON.js
 create mode 100644 src/ExportOSMXML.js

diff --git a/lang/ast.json b/lang/ast.json
index 6792c3f9..14e3629d 100644
--- a/lang/ast.json
+++ b/lang/ast.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/ca.json b/lang/ca.json
index 92fbcbad..09abd388 100644
--- a/lang/ca.json
+++ b/lang/ca.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/cs.json b/lang/cs.json
index 6a00b20d..2a7bdff8 100644
--- a/lang/cs.json
+++ b/lang/cs.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/da.json b/lang/da.json
index 246c041a..1a01e3e8 100644
--- a/lang/da.json
+++ b/lang/da.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/de.json b/lang/de.json
index 36a8c64e..76e55319 100644
--- a/lang/de.json
+++ b/lang/de.json
@@ -3,12 +3,13 @@
     "category-info-tooltip": "Info & Legende",
     "closed": "geschlossen",
     "default": "Standard",
-    "download:geojson": "Als GeoJSON runterladen",
     "edit": "bearbeiten",
     "error": {
         "message": "Fehler",
         "!=1": "Fehler"
     },
+    "export:GeoJSON": "Als GeoJSON runterladen",
+    "export:OSMXML": null,
     "facilities": "Einrichtungen",
     "header:attributes": "Attribute",
     "header:export": "Export",
diff --git a/lang/el.json b/lang/el.json
index 92acb3fc..82412698 100644
--- a/lang/el.json
+++ b/lang/el.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/en.json b/lang/en.json
index d85e1e15..1791c258 100644
--- a/lang/en.json
+++ b/lang/en.json
@@ -3,8 +3,6 @@
     "category-info-tooltip": "Info & Map key",
     "closed": "closed",
     "default": "default",
-    "download:geojson": "Download as GeoJSON",
-    "download:osmxml": "Download as OSMXML",
     "edit": "edit",
     "error": {
         "message": "Error",
@@ -12,6 +10,8 @@
     },
     "export-all": "Export all visible map features",
     "export-prepare": "Prepare download",
+    "export:GeoJSON": "Download as GeoJSON",
+    "export:OSMXML": "Download as OSMXML",
     "facilities": "Facilities",
     "header:attributes": "Attributes",
     "header:export": "Export",
diff --git a/lang/es.json b/lang/es.json
index 62837550..4f34b83a 100644
--- a/lang/es.json
+++ b/lang/es.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/et.json b/lang/et.json
index 9e346130..2cfc0df7 100644
--- a/lang/et.json
+++ b/lang/et.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/fr.json b/lang/fr.json
index 2e9720c6..afd65d26 100644
--- a/lang/fr.json
+++ b/lang/fr.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "Info & Légende",
     "closed": "Fermé",
     "default": "Par défaut",
-    "download:geojson": "Télécharger comme GeoJSON",
     "edit": "éditer",
     "error": "Erreur",
+    "export:GeoJSON": "Télécharger comme GeoJSON",
+    "export:OSMXML": null,
     "facilities": "Aménagements",
     "header:attributes": "Attributs",
     "header:export": "Export",
diff --git a/lang/hu.json b/lang/hu.json
index 49fe6e02..c8a12808 100644
--- a/lang/hu.json
+++ b/lang/hu.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "Információk és jelmagyarázat",
     "closed": "Lezárva",
     "default": "Alapértelmezett",
-    "download:geojson": "Letöltés GeoJSON formátumban",
     "edit": "Szerkesztés",
     "error": "Hiba",
+    "export:GeoJSON": "Letöltés GeoJSON formátumban",
+    "export:OSMXML": null,
     "facilities": "Létesítmények",
     "header:attributes": "Tulajdonságok",
     "header:export": "Exportálás",
diff --git a/lang/it.json b/lang/it.json
index caa1f358..f100e93c 100644
--- a/lang/it.json
+++ b/lang/it.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/ja.json b/lang/ja.json
index d1d9466f..9685ac1b 100644
--- a/lang/ja.json
+++ b/lang/ja.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/nl.json b/lang/nl.json
index 0c555511..ef7ccbee 100644
--- a/lang/nl.json
+++ b/lang/nl.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/pl.json b/lang/pl.json
index 2dbff807..bf0c7120 100644
--- a/lang/pl.json
+++ b/lang/pl.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/pt-br.json b/lang/pt-br.json
index 008e8b05..607fc336 100644
--- a/lang/pt-br.json
+++ b/lang/pt-br.json
@@ -3,12 +3,13 @@
     "category-info-tooltip": "Info & Legenda",
     "closed": "fechado",
     "default": "padrão",
-    "download:geojson": "Baixar como GeoJSON",
     "edit": "editar",
     "error": {
         "message": "Erro",
         "!=1": "Erros"
     },
+    "export:GeoJSON": "Baixar como GeoJSON",
+    "export:OSMXML": null,
     "facilities": "Instalações",
     "header:attributes": "Atributos",
     "header:export": "Exportar",
diff --git a/lang/pt.json b/lang/pt.json
index 42293725..372bc6f0 100644
--- a/lang/pt.json
+++ b/lang/pt.json
@@ -3,12 +3,13 @@
     "category-info-tooltip": "Info & Legenda",
     "closed": "fechado",
     "default": "padrão",
-    "download:geojson": "Descarregar como GeoJSON",
     "edit": "editar",
     "error": {
         "message": "Erro",
         "!=1": "Erros"
     },
+    "export:GeoJSON": "Descarregar como GeoJSON",
+    "export:OSMXML": null,
     "facilities": "Instalações",
     "header:attributes": "Atributos",
     "header:export": "Exportar",
diff --git a/lang/ro.json b/lang/ro.json
index 393b5e82..46d1fe29 100644
--- a/lang/ro.json
+++ b/lang/ro.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/ru.json b/lang/ru.json
index 1c8389ad..fc7c04a8 100644
--- a/lang/ru.json
+++ b/lang/ru.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/sr.json b/lang/sr.json
index b53e0904..d07af30e 100644
--- a/lang/sr.json
+++ b/lang/sr.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/template.json b/lang/template.json
index 09c4e48c..16e3970e 100644
--- a/lang/template.json
+++ b/lang/template.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/lang/uk.json b/lang/uk.json
index 11018c7e..8f514591 100644
--- a/lang/uk.json
+++ b/lang/uk.json
@@ -3,9 +3,10 @@
     "category-info-tooltip": "",
     "closed": "",
     "default": "",
-    "download:geojson": "",
     "edit": "",
     "error": "",
+    "export:GeoJSON": "",
+    "export:OSMXML": null,
     "facilities": "",
     "header:attributes": "",
     "header:export": "",
diff --git a/src/ExportGeoJSON.js b/src/ExportGeoJSON.js
new file mode 100644
index 00000000..71a3ada8
--- /dev/null
+++ b/src/ExportGeoJSON.js
@@ -0,0 +1,34 @@
+class ExportGeoJSON {
+  constructor (conf) {
+    this.conf = conf
+  }
+
+  each (ob, callback) {
+    callback(null, ob.object.GeoJSON(this.conf))
+  }
+
+  finishOne (object) {
+    return {
+      content: JSON.stringify(object, null, '    '),
+      fileType: 'application/json',
+      extension: 'geojson'
+    }
+  }
+
+  finish (list) {
+    if (list.length) {
+      list = {
+        type: 'FeatureCollection',
+        features: list
+      }
+    }
+
+    return {
+      content: JSON.stringify(list, null, '    '),
+      fileType: 'application/json',
+      extension: 'geojson'
+    }
+  }
+}
+
+module.exports = ExportGeoJSON
diff --git a/src/ExportOSMXML.js b/src/ExportOSMXML.js
new file mode 100644
index 00000000..9858b6e0
--- /dev/null
+++ b/src/ExportOSMXML.js
@@ -0,0 +1,20 @@
+class ExportOSMXML {
+  constructor (conf) {
+    this.conf = conf
+    this.parentNode = document.createElement('osm')
+  }
+
+  each (ob, callback) {
+    ob.object.exportOSMXML(this.conf, this.parentNode, callback)
+  }
+
+  finish (list) {
+    return {
+      content: '<?xml version="1.0" encoding="UTF-8"?><osm version="0.6" generator="OpenStreetBrowser">' + this.parentNode.innerHTML + '</osm>',
+      fileType: 'application/xml',
+      extension: 'osm.xml'
+    }
+  }
+}
+
+module.exports = ExportOSMXML
diff --git a/src/exportAll.js b/src/exportAll.js
index b5ca998f..4e823805 100644
--- a/src/exportAll.js
+++ b/src/exportAll.js
@@ -4,6 +4,11 @@ const FileSaver = require('file-saver')
 
 const chunkSplit = require('./chunkSplit')
 
+const types = {
+  GeoJSON: require('./ExportGeoJSON'),
+  OSMXML: require('./ExportOSMXML')
+}
+
 let tab
 let formExport
 
@@ -11,65 +16,35 @@ function prepareDownload (callback) {
   let conf = formExport.get_data()
   let fileType
   let extension
+  let type = types[conf.type]
+  let exportFun = new type(conf)
 
   global.baseCategory.allMapFeatures((err, data) => {
     let chunks = chunkSplit(data, 1000)
     let parentNode
 
-    switch (conf.type) {
-      case 'geojson':
-        break
-      case 'osmxml':
-        parentNode = document.createElement('osm')
-        break
-    }
-
     async.mapLimit(
       chunks,
       1,
       (chunk, done) => {
         async.map(chunk,
-          (ob, done) => {
-            switch (conf.type) {
-              case 'geojson':
-                done(null, ob.object.GeoJSON(conf))
-                break
-              case 'osmxml':
-                ob.object.exportOSMXML(conf, parentNode, done)
-                break
-              default:
-                done('wrong type')
-            }
-          },
+          (ob, done) => exportFun.each(ob, done),
           (err, result) => {
             global.setTimeout(() => done(err, result), 0)
           }
         )
       },
-      (err, result) => {
+      (err, data) => {
         if (err) {
           return callback(err)
         }
 
-        switch (conf.type) {
-          case 'geojson':
-            result = {
-              type: 'FeatureCollection',
-              features: result
-            }
-            result = JSON.stringify(result, null, '    ')
-            fileType = 'application/json'
-            extension = 'geojson'
-            break
-          case 'osmxml':
-            result = '<?xml version="1.0" encoding="UTF-8"?><osm version="0.6" generator="OpenStreetBrowser">' + parentNode.innerHTML + '</osm>'
-            fileType = 'application/xml'
-            extension = 'osm.xml'
-            break
-        }
+        data = data.reduce((all, chunk) => all.concat(chunk))
+
+        let result = exportFun.finish(data)
 
-        var blob = new Blob([ result ], { type: fileType + ';charset=utf-8' })
-        FileSaver.saveAs(blob, 'openstreetbrowser.' + extension)
+        var blob = new Blob([ result.content ], { type: result.fileType + ';charset=utf-8' })
+        FileSaver.saveAs(blob, 'openstreetbrowser.' + result.extension)
 
         callback()
       }
@@ -86,15 +61,17 @@ register_hook('init', function () {
   tab.header.innerHTML = '<i class="fa fa-download" aria-hidden="true"></i>'
   tab.content.innerHTML = lang('export-all')
 
+  let values = {}
+  Object.keys(types).forEach(type =>
+    values[type] = lang('export:' + type)
+  )
+
   formExport = new form('export', {
     type: {
       name: 'Type',
       type: 'radio',
-      values: {
-        geojson: lang('download:geojson'),
-        osmxml: lang('download:osmxml')
-      },
-      default: 'geojson'
+      values,
+      default: Object.keys(types)[0]
     }
   })