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.

129 lines
3.1 KiB

  1. /* global alert */
  2. var async = require('async')
  3. var OpenStreetBrowserLoader = require('./OpenStreetBrowserLoader')
  4. var CategoryBase = require('./CategoryBase')
  5. CategoryIndex.prototype = Object.create(CategoryBase.prototype)
  6. CategoryIndex.prototype.constructor = CategoryIndex
  7. function CategoryIndex (options, data, repository) {
  8. CategoryBase.call(this, options, data, repository)
  9. this.childrenDoms = {}
  10. this.childrenCategories = null
  11. this._loadChildrenCategories((err) => {
  12. if (err) {
  13. console.log('Category "' + this.id + '": error loading child categories:', err)
  14. }
  15. })
  16. }
  17. CategoryIndex.prototype.open = function () {
  18. if (this.isOpen) {
  19. return
  20. }
  21. CategoryBase.prototype.open.call(this)
  22. if (this.childrenCategories !== null) {
  23. this.isOpen = true
  24. }
  25. }
  26. CategoryIndex.prototype.recalc = function () {
  27. for (var k in this.childrenCategories) {
  28. if (this.childrenCategories[k]) {
  29. this.childrenCategories[k].recalc()
  30. }
  31. }
  32. }
  33. CategoryIndex.prototype._loadChildrenCategories = function (callback) {
  34. this.childrenCategories = {}
  35. async.forEach(this.data.subCategories,
  36. function (data, callback) {
  37. var childDom = document.createElement('div')
  38. childDom.className = 'categoryWrapper'
  39. this.domContent.appendChild(childDom)
  40. this.childrenDoms[data.id] = childDom
  41. this.childrenCategories[data.id] = null
  42. if ('type' in data) {
  43. OpenStreetBrowserLoader.getCategoryFromData(data.id, this.options, data, this._loadChildCategory.bind(this, data.id, callback))
  44. } else {
  45. OpenStreetBrowserLoader.getCategory(data.id, this.options, this._loadChildCategory.bind(this, data.id, callback))
  46. }
  47. }.bind(this),
  48. function (err) {
  49. if (callback) {
  50. callback(err)
  51. }
  52. }
  53. )
  54. }
  55. CategoryIndex.prototype._loadChildCategory = function (id, callback, err, category) {
  56. if (err) {
  57. return callback(err)
  58. }
  59. this.childrenCategories[id] = category
  60. category.setParent(this)
  61. category.setParentDom(this.childrenDoms[id])
  62. callback(err, category)
  63. }
  64. CategoryIndex.prototype.close = function () {
  65. if (!this.isOpen) {
  66. return
  67. }
  68. CategoryBase.prototype.close.call(this)
  69. for (var k in this.childrenCategories) {
  70. if (this.childrenCategories[k]) {
  71. this.childrenCategories[k].close()
  72. }
  73. }
  74. }
  75. CategoryIndex.prototype.toggleCategory = function (id) {
  76. OpenStreetBrowserLoader.getCategory(id, function (err, category) {
  77. if (err) {
  78. alert(err)
  79. return
  80. }
  81. category.setParent(this)
  82. category.setParentDom(this.childrenDoms[id])
  83. this.childrenCategories[id] = category
  84. category.toggle()
  85. }.bind(this))
  86. }
  87. CategoryIndex.prototype.allMapFeatures = function (callback) {
  88. let result = []
  89. async.each(this.childrenCategories,
  90. (category, done) => category.allMapFeatures(
  91. (err, data) => {
  92. if (err) {
  93. return done(err)
  94. }
  95. result = result.concat(data)
  96. global.setTimeout(done, 0)
  97. }
  98. ),
  99. (err) => callback(err, result)
  100. )
  101. }
  102. OpenStreetBrowserLoader.registerType('index', CategoryIndex)
  103. module.exports = CategoryIndex