You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
1.9 KiB

  1. const tabs = require('modulekit-tabs')
  2. const async = require('async')
  3. const chunkSplit = require('./chunkSplit')
  4. let tab
  5. let formExport
  6. function prepareDownload (callback) {
  7. let conf = formExport.get_data()
  8. let result = []
  9. global.baseCategory.allMapFeatures((err, data) => {
  10. console.log(data)
  11. let chunks = chunkSplit(data, 1000)
  12. async.eachLimit(
  13. chunks,
  14. 1,
  15. (chunk, done) => {
  16. result = result.concat(chunk.map(ob => {
  17. switch (conf.type) {
  18. case 'geojson':
  19. return ob.object.GeoJSON(conf)
  20. }
  21. }))
  22. global.setTimeout(done, 0)
  23. },
  24. (err) => {
  25. if (err) {
  26. return callback(err)
  27. }
  28. switch (conf.type) {
  29. case 'geojson':
  30. result = {
  31. type: 'FeatureCollection',
  32. features: result
  33. }
  34. result = JSON.stringify(result, null, ' ')
  35. }
  36. console.log(result)
  37. callback()
  38. }
  39. )
  40. })
  41. }
  42. register_hook('init', function () {
  43. tab = new tabs.Tab({
  44. id: 'export'
  45. })
  46. global.tabs.add(tab)
  47. tab.header.innerHTML = '<i class="fa fa-download" aria-hidden="true"></i>'
  48. tab.content.innerHTML = lang('export-all')
  49. formExport = new form('export', {
  50. type: {
  51. name: 'Type',
  52. type: 'radio',
  53. values: {
  54. geojson: lang('download:geojson')
  55. },
  56. default: 'geojson'
  57. }
  58. })
  59. let domForm = document.createElement('form')
  60. tab.content.appendChild(domForm)
  61. formExport.show(domForm)
  62. let submit = document.createElement('input')
  63. submit.type = 'submit'
  64. submit.value = lang('export-prepare')
  65. submit.onclick = () => {
  66. submit.setAttribute('disabled', 'disabled')
  67. prepareDownload((err) => {
  68. if (err) {
  69. alert(err)
  70. }
  71. submit.removeAttribute('disabled')
  72. })
  73. }
  74. tab.content.appendChild(submit)
  75. tab.on('select', () => {
  76. formExport.resize()
  77. })
  78. })