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.

103 lines
2.3 KiB

  1. const ModalWindow = require('window-modal')
  2. const tabs = require('modulekit-tabs')
  3. const yaml = require('js-yaml')
  4. const md5 = require('md5')
  5. const OpenStreetBrowserLoader = require('./OpenStreetBrowserLoader')
  6. class CustomCategory {
  7. constructor () {
  8. }
  9. edit () {
  10. if (this.modal) {
  11. this.modal.focused = true
  12. return
  13. }
  14. this.modal = new ModalWindow({
  15. title: 'Custom Category',
  16. hideMinimize: true,
  17. zIndex: '99999'
  18. })
  19. this.modal.addEventListener('close', () => {
  20. this.modal = null
  21. })
  22. this.textarea = document.createElement('textarea')
  23. this.modal.content.element.appendChild(this.textarea)
  24. if (this.content !== undefined) {
  25. this.textarea.value = this.content
  26. }
  27. const input = document.createElement('input')
  28. input.type = 'submit'
  29. input.value = lang('apply')
  30. this.modal.content.element.appendChild(input)
  31. input.onclick = () => {
  32. this.applyContent(this.textarea.value)
  33. return true
  34. }
  35. }
  36. applyContent (content) {
  37. this.content = content
  38. const id = 'custom:' + md5(content)
  39. const data = yaml.load(content)
  40. if (this.category) {
  41. this.category.remove()
  42. this.category = null
  43. }
  44. OpenStreetBrowserLoader.getCategoryFromData(id, {}, data, (err, category) => {
  45. if (err) {
  46. return global.alert(err)
  47. }
  48. this.category = category
  49. this.category.setParentDom(document.getElementById('contentListAddCategories'))
  50. this.category.setMap(global.map)
  51. if (this.category.tabEdit) {
  52. this.category.tools.remove(this.category.tabEdit)
  53. }
  54. this.category.tabEdit = new tabs.Tab({
  55. id: 'edit'
  56. })
  57. this.category.tools.add(this.category.tabEdit)
  58. this.category.tabEdit.header.innerHTML = '<i class="fa fa-pen"></i>'
  59. this.category.tabEdit.on('select', () => {
  60. this.category.tabEdit.unselect()
  61. this.edit()
  62. })
  63. this.category.open()
  64. })
  65. }
  66. }
  67. function createCustomCategory () {
  68. let category
  69. category = new CustomCategory()
  70. category.edit()
  71. return false
  72. }
  73. module.exports = function customCategory (content) {
  74. let div = document.createElement('div')
  75. let a = document.createElement('a')
  76. a.innerHTML = lang('customCategory:create')
  77. a.href = '#'
  78. a.onclick = createCustomCategory
  79. div.appendChild(a)
  80. content.appendChild(div)
  81. }