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.

122 lines
2.8 KiB

  1. var OverpassLayer = require('overpass-layer')
  2. function OpenStreetBrowserLoader () {
  3. this.types = {}
  4. this.categories = {}
  5. this.templates = {}
  6. this._loadClash = {} // if a category is being loaded multiple times, collect callbacks
  7. }
  8. OpenStreetBrowserLoader.prototype.setMap = function (map) {
  9. this.map = map
  10. }
  11. OpenStreetBrowserLoader.prototype.getCategory = function (id, callback) {
  12. if (id in this.categories) {
  13. callback(null, this.categories[id])
  14. return
  15. }
  16. if (id in this._loadClash) {
  17. this._loadClash[id].push(callback)
  18. return
  19. }
  20. this._loadClash[id] = []
  21. function reqListener (req) {
  22. if (req.status !== 200) {
  23. console.log(req)
  24. return callback(req.statusText, null)
  25. }
  26. var data = JSON.parse(req.responseText)
  27. this.getCategoryFromData(id, data, function (err, category) {
  28. if (category) {
  29. category.setMap(this.map)
  30. }
  31. callback(err, category)
  32. this._loadClash[id].forEach(function (c) {
  33. c(err, category)
  34. })
  35. delete this._loadClash[id]
  36. }.bind(this))
  37. }
  38. var req = new XMLHttpRequest()
  39. req.addEventListener('load', reqListener.bind(this, req))
  40. req.open('GET', config.categoriesDir + '/' + id + '.json?' + config.categoriesRev)
  41. req.send()
  42. }
  43. OpenStreetBrowserLoader.prototype.getTemplate = function (id, callback) {
  44. if (id in this.templates) {
  45. callback(null, this.templates[id])
  46. return
  47. }
  48. if (id in this._loadClash) {
  49. this._loadClash[id].push(callback)
  50. return
  51. }
  52. this._loadClash[id] = []
  53. function reqListener (req) {
  54. if (req.status !== 200) {
  55. console.log(req)
  56. return callback(req.statusText, null)
  57. }
  58. this.templates[id] = OverpassLayer.twig.twig({ data: req.responseText, autoescape: true })
  59. callback(null, this.templates[id])
  60. this._loadClash[id].forEach(function (c) {
  61. c(null, this.templates[id])
  62. }.bind(this))
  63. }
  64. var req = new XMLHttpRequest()
  65. req.addEventListener('load', reqListener.bind(this, req))
  66. req.open('GET', config.categoriesDir + '/' + id + '.html?' + config.categoriesRev)
  67. req.send()
  68. }
  69. OpenStreetBrowserLoader.prototype.getCategoryFromData = function (id, data, callback) {
  70. if (!data.type) {
  71. return callback(new Error('no type defined'), null)
  72. }
  73. if (!(data.type in this.types)) {
  74. return callback(new Error('unknown type'), null)
  75. }
  76. var layer = new this.types[data.type](id, data)
  77. layer.setMap(this.map)
  78. this.categories[id] = layer
  79. if ('load' in layer) {
  80. layer.load(function (err) {
  81. callback(err, layer)
  82. })
  83. } else {
  84. callback(null, layer)
  85. }
  86. }
  87. OpenStreetBrowserLoader.prototype.forget = function (id) {
  88. this.categories[id].remove()
  89. delete this.categories[id]
  90. }
  91. OpenStreetBrowserLoader.prototype.registerType = function (type, classObject) {
  92. this.types[type] = classObject
  93. }
  94. module.exports = new OpenStreetBrowserLoader()