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.

215 lines
4.8 KiB

6 years ago
6 years ago
  1. /* global lang, ui_lang, options, alert */
  2. /* eslint camelcase: 0 */
  3. var OpenStreetBrowserLoader = require('./OpenStreetBrowserLoader')
  4. var tabs = require('modulekit-tabs')
  5. const ee = require('event-emitter')
  6. function CategoryBase (options, data, repository) {
  7. if (typeof options === 'string') {
  8. this.id = options
  9. this.options = {}
  10. } else {
  11. this.id = options.id
  12. this.options = options
  13. }
  14. this.repository = repository
  15. this.parentCategory = null
  16. this.childrenLoadingCount = 0
  17. this.data = data
  18. this.isOpen = false
  19. this.dom = document.createElement('div')
  20. this.dom.className = 'category category-' + data.type
  21. var name
  22. var a
  23. this.domHeader = document.createElement('header')
  24. this.dom.appendChild(this.domHeader)
  25. if ('name' in this.data) {
  26. if (typeof this.data.name === 'object') {
  27. name = lang(this.data.name)
  28. } else {
  29. name = this.data.name
  30. }
  31. } else if (('name:' + ui_lang) in this.data) {
  32. name = this.data['name:' + ui_lang]
  33. } else {
  34. name = lang('category:' + this.id)
  35. }
  36. a = document.createElement('a')
  37. a.appendChild(document.createTextNode(name))
  38. a.href = '#'
  39. a.onclick = this.toggle.bind(this)
  40. this.domHeader.appendChild(a)
  41. if (this.options.repositoryId && this.options.repositoryId !== 'default') {
  42. a = document.createElement('span')
  43. a.className = 'repoId'
  44. a.appendChild(document.createTextNode(this.options.repositoryId))
  45. this.domHeader.appendChild(a)
  46. }
  47. if (this.shallShowReload()) {
  48. a = document.createElement('a')
  49. a.appendChild(document.createTextNode('⟳'))
  50. a.title = lang('reload')
  51. a.className = 'reload'
  52. a.onclick = function () {
  53. var id = this.id
  54. var isOpen = this.isOpen
  55. this.reload(function (err, category) {
  56. if (err) {
  57. alert('Error reloading category ' + id + ': ' + err)
  58. }
  59. if (isOpen) {
  60. category.open()
  61. }
  62. })
  63. }.bind(this)
  64. this.domHeader.appendChild(a)
  65. }
  66. this.tools = new tabs.Tabs(this.dom)
  67. this.tools.node.classList.add('tools')
  68. this.domContent = document.createElement('div')
  69. this.domContent.className = 'content'
  70. this.dom.appendChild(this.domContent)
  71. }
  72. CategoryBase.prototype.load = function (callback) {
  73. callback()
  74. }
  75. CategoryBase.prototype.shallShowReload = function () {
  76. return options.debug
  77. }
  78. CategoryBase.prototype.setMap = function (map) {
  79. this.map = map
  80. }
  81. CategoryBase.prototype.setParent = function (parent) {
  82. this.parentCategory = parent
  83. if (this.isOpen) {
  84. this.parentCategory.open()
  85. }
  86. }
  87. CategoryBase.prototype.setParentDom = function (parentDom) {
  88. this.parentDom = parentDom
  89. if (typeof this.parentDom !== 'string') {
  90. this.parentDom.appendChild(this.dom)
  91. if (this.isOpen) {
  92. this.parentDom.parentNode.classList.add('open')
  93. }
  94. }
  95. }
  96. CategoryBase.prototype.open = function () {
  97. if (this.isOpen) {
  98. return
  99. }
  100. if (this.parentCategory) {
  101. this.parentCategory.open()
  102. }
  103. if (typeof this.parentDom === 'string') {
  104. var d = document.getElementById(this.parentDom)
  105. if (d) {
  106. this.parentDom = d
  107. this.parentDom.appendChild(this.dom)
  108. }
  109. }
  110. this.dom.classList.add('open')
  111. this.isOpen = true
  112. call_hooks('categoryOpen', this)
  113. this.emit('open')
  114. }
  115. CategoryBase.prototype.close = function () {
  116. if (!this.isOpen) {
  117. return
  118. }
  119. this.dom.classList.remove('open')
  120. this.isOpen = false
  121. call_hooks('categoryClose', this)
  122. this.emit('close')
  123. }
  124. CategoryBase.prototype.toggle = function () {
  125. if (this.isOpen) {
  126. this.close()
  127. } else {
  128. this.open()
  129. }
  130. return false
  131. }
  132. CategoryBase.prototype.reload = function (callback) {
  133. var parentCategory = this.parentCategory
  134. var parentDom = this.parentDom
  135. OpenStreetBrowserLoader.forget(this.id)
  136. OpenStreetBrowserLoader.getCategory(this.id, { force: true }, function (err, category) {
  137. if (err) {
  138. return callback(err)
  139. }
  140. category.setParent(parentCategory)
  141. category.setParentDom(parentDom)
  142. callback(null, category)
  143. })
  144. }
  145. CategoryBase.prototype.remove = function () {
  146. this.close()
  147. if (this.parentDom) {
  148. this.parentDom.removeChild(this.dom)
  149. }
  150. }
  151. CategoryBase.prototype.recalc = function () {
  152. }
  153. CategoryBase.prototype.notifyChildLoadStart = function (category) {
  154. if (this.childrenLoadingCount === 0 && this.parentCategory) {
  155. this.parentCategory.notifyChildLoadStart(this)
  156. } else {
  157. document.body.classList.add('loading')
  158. }
  159. this.childrenLoadingCount++
  160. }
  161. CategoryBase.prototype.notifyChildLoadEnd = function (category) {
  162. this.childrenLoadingCount--
  163. if (this.childrenLoadingCount === 0 && this.parentCategory) {
  164. this.parentCategory.notifyChildLoadEnd(this)
  165. } else {
  166. document.body.classList.remove('loading')
  167. }
  168. }
  169. CategoryBase.prototype.allMapFeatures = function (callback) {
  170. callback(null, [])
  171. }
  172. ee(CategoryBase.prototype)
  173. module.exports = CategoryBase