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.

104 lines
2.4 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. ajax('customCategory', { content: this.textarea.value }, (result) => {})
  34. return true
  35. }
  36. }
  37. applyContent (content) {
  38. this.content = content
  39. const id = 'custom:' + md5(content)
  40. const data = yaml.load(content)
  41. if (this.category) {
  42. this.category.remove()
  43. this.category = null
  44. }
  45. OpenStreetBrowserLoader.getCategoryFromData(id, {}, data, (err, category) => {
  46. if (err) {
  47. return global.alert(err)
  48. }
  49. this.category = category
  50. this.category.setParentDom(document.getElementById('contentListAddCategories'))
  51. this.category.setMap(global.map)
  52. if (this.category.tabEdit) {
  53. this.category.tools.remove(this.category.tabEdit)
  54. }
  55. this.category.tabEdit = new tabs.Tab({
  56. id: 'edit'
  57. })
  58. this.category.tools.add(this.category.tabEdit)
  59. this.category.tabEdit.header.innerHTML = '<i class="fa fa-pen"></i>'
  60. this.category.tabEdit.on('select', () => {
  61. this.category.tabEdit.unselect()
  62. this.edit()
  63. })
  64. this.category.open()
  65. })
  66. }
  67. }
  68. function createCustomCategory () {
  69. let category
  70. category = new CustomCategory()
  71. category.edit()
  72. return false
  73. }
  74. module.exports = function customCategory (content) {
  75. let div = document.createElement('div')
  76. let a = document.createElement('a')
  77. a.innerHTML = lang('customCategory:create')
  78. a.href = '#'
  79. a.onclick = createCustomCategory
  80. div.appendChild(a)
  81. content.appendChild(div)
  82. }