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.

305 lines
7.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /* globals map:true, overpassFrontend:true, currentPath:true, options:true, baseCategory:true, overpassUrl:true */
  2. var LeafletGeoSearch = require('leaflet-geosearch')
  3. var OverpassFrontend = require('overpass-frontend')
  4. var OpenStreetBrowserLoader = require('./OpenStreetBrowserLoader')
  5. var state = require('./state')
  6. var hash = require('sheet-router/hash')
  7. global.OpenStreetBrowserLoader = OpenStreetBrowserLoader
  8. require('./CategoryIndex')
  9. require('./CategoryOverpass')
  10. require('./category.css')
  11. global.map = null
  12. global.baseCategory = null
  13. global.overpassUrl = null
  14. global.overpassFrontend = null
  15. global.currentPath = null
  16. var lastPopupClose = 0
  17. // Optional modules
  18. require('./options')
  19. require('./language')
  20. require('./overpassChooser')
  21. require('./fullscreen')
  22. require('./mapLayers')
  23. require('./twigFunctions')
  24. require('./markers')
  25. require('./categories')
  26. require('./wikipedia')
  27. require('./image')
  28. require('./addCategories')
  29. window.onload = function () {
  30. initState = config.defaultView
  31. map = L.map('map')
  32. // due to php export, options may be an array -> fix
  33. if (Array.isArray(options)) {
  34. options = {}
  35. }
  36. call_hooks('init')
  37. call_hooks_callback('init_callback', initState, onload2.bind(this, initState))
  38. }
  39. function onload2 (initState) {
  40. // Add Geo Search
  41. var provider = new LeafletGeoSearch.OpenStreetMapProvider()
  42. var searchControl = new LeafletGeoSearch.GeoSearchControl({
  43. provider: provider,
  44. showMarker: false,
  45. retainZoomLevel: true
  46. })
  47. map.addControl(searchControl)
  48. // Geo location
  49. L.control.locate({
  50. keepCurrentZoomLevel: true,
  51. drawCircle: false,
  52. drawMarker: false,
  53. showPopup: false
  54. }).addTo(map)
  55. // Scale bar
  56. L.control.scale().addTo(map)
  57. if (!overpassUrl) {
  58. overpassUrl = config.overpassUrl
  59. if (Array.isArray(overpassUrl) && overpassUrl.length) {
  60. overpassUrl = overpassUrl[0]
  61. }
  62. }
  63. overpassFrontend = new OverpassFrontend(overpassUrl, {
  64. timeGap: 10,
  65. effortPerRequest: 100
  66. })
  67. OpenStreetBrowserLoader.setMap(map)
  68. var newState
  69. if (location.hash && location.hash.length > 1) {
  70. newState = state.parse(location.hash.substr(1))
  71. } else {
  72. newState = initState
  73. }
  74. // make sure the map has an initial location
  75. if (!('zoom' in newState) && !('lat' in newState) && !('lon' in newState)) {
  76. newState.zoom = initState.zoom
  77. newState.lat = initState.lat
  78. newState.lon = initState.lon
  79. }
  80. state.apply(newState)
  81. OpenStreetBrowserLoader.getCategory('index', function (err, category) {
  82. if (err) {
  83. alert(err)
  84. return
  85. }
  86. baseCategory = category
  87. category.setParentDom(document.getElementById('contentList'))
  88. category.open()
  89. })
  90. map.on('popupopen', function (e) {
  91. if (e.popup.object) {
  92. var url = e.popup.object.layer_id + '/' + e.popup.object.id
  93. if (location.hash.substr(1) !== url && location.hash.substr(1, url.length + 1) !== url + '/') {
  94. currentPath = url
  95. // only push state, when last popup close happened >1sec earlier
  96. state.update(null, Date.now() - lastPopupClose > 1000)
  97. }
  98. OpenStreetBrowserLoader.getCategory(e.popup.object.layer_id, function (err, category) {
  99. if (err) {
  100. alert(err)
  101. return
  102. }
  103. category.notifyPopupOpen(e.popup.object, e.popup)
  104. })
  105. }
  106. })
  107. map.on('popupclose', function (e) {
  108. lastPopupClose = Date.now()
  109. currentPath = null
  110. state.update(null, true)
  111. hide()
  112. })
  113. map.on('moveend', function (e) {
  114. state.update()
  115. })
  116. hash(function (loc) {
  117. state.apply(state.parse(loc.substr(1)))
  118. })
  119. state.update()
  120. }
  121. window.setPath = function (path) {
  122. currentPath = path
  123. if (!path) {
  124. map.closePopup()
  125. return
  126. }
  127. var param = {
  128. showDetails: !!path.match(/\/details$/)
  129. }
  130. show(path, param, function (err) {
  131. if (err) {
  132. alert(err)
  133. return
  134. }
  135. call_hooks('show', path, param)
  136. })
  137. }
  138. function show (id, options, callback) {
  139. if (options.showDetails) {
  140. call_hooks('hide-' + document.getElementById('content').className)
  141. document.getElementById('content').className = 'details'
  142. document.getElementById('contentDetails').innerHTML = 'Loading ...'
  143. }
  144. id = id.split('/')
  145. if (id.length < 2) {
  146. return callback(new Error('unknown request'))
  147. }
  148. OpenStreetBrowserLoader.getCategory(id[0], function (err, category) {
  149. if (err) {
  150. return callback(new Error('error loading category "' + id[0] + '": ' + err))
  151. }
  152. if (!category.parentDom) {
  153. category.setParentDom(document.getElementById('contentList'))
  154. }
  155. category.show(
  156. id[1],
  157. {
  158. },
  159. function (err, data) {
  160. if (err) {
  161. return callback(new Error('error loading object "' + id[0] + '/' + id[1] + '": ' + err))
  162. }
  163. if (!map._popup || map._popup !== data.popup) {
  164. data.feature.openPopup()
  165. }
  166. if (options.showDetails) {
  167. showDetails(data, category)
  168. }
  169. callback(err)
  170. }
  171. )
  172. category.open()
  173. })
  174. }
  175. window.showDetails = function (data, category) {
  176. var div, h, dt, dd
  177. var k
  178. var dom = document.getElementById('contentDetails')
  179. dom.innerHTML = ''
  180. div = document.createElement('h1')
  181. div.className = 'title'
  182. div.innerHTML = data.data.title
  183. dom.appendChild(div)
  184. div = document.createElement('div')
  185. div.className = 'description'
  186. div.innerHTML = data.data.description
  187. dom.appendChild(div)
  188. div = document.createElement('div')
  189. div.className = 'body'
  190. div.innerHTML = data.data.body
  191. dom.appendChild(div)
  192. div = document.createElement('div')
  193. div.className = 'body'
  194. dom.appendChild(div)
  195. category.renderTemplate(data, 'detailsBody', function (div, err, result) {
  196. div.innerHTML = result
  197. }.bind(this, div))
  198. call_hooks_callback('show-details', data, category, dom,
  199. function (err) {
  200. if (err.length) {
  201. console.log('show-details produced errors:', err)
  202. }
  203. }
  204. )
  205. h = document.createElement('h3')
  206. h.innerHTML = 'Attributes'
  207. dom.appendChild(h)
  208. div = document.createElement('dl')
  209. div.className = 'tags'
  210. for (k in data.object.tags) {
  211. dt = document.createElement('dt')
  212. dt.appendChild(document.createTextNode(k))
  213. div.appendChild(dt)
  214. dd = document.createElement('dd')
  215. dd.appendChild(document.createTextNode(data.object.tags[k]))
  216. div.appendChild(dd)
  217. }
  218. dom.appendChild(div)
  219. h = document.createElement('h3')
  220. h.innerHTML = 'OSM Meta'
  221. dom.appendChild(h)
  222. div = document.createElement('dl')
  223. div.className = 'meta'
  224. dt = document.createElement('dt')
  225. dt.appendChild(document.createTextNode('id'))
  226. div.appendChild(dt)
  227. dd = document.createElement('dd')
  228. var a = document.createElement('a')
  229. a.appendChild(document.createTextNode(data.object.type + '/' + data.object.osm_id))
  230. a.href = 'https://openstreetmap.org/' + data.object.type + '/' + data.object.osm_id
  231. a.target = '_blank'
  232. dd.appendChild(a)
  233. div.appendChild(dd)
  234. for (k in data.object.meta) {
  235. dt = document.createElement('dt')
  236. dt.appendChild(document.createTextNode(k))
  237. div.appendChild(dt)
  238. dd = document.createElement('dd')
  239. dd.appendChild(document.createTextNode(data.object.meta[k]))
  240. div.appendChild(dd)
  241. }
  242. dom.appendChild(div)
  243. }
  244. function hide () {
  245. call_hooks('hide-' + document.getElementById('content').className)
  246. document.getElementById('content').className = 'list'
  247. }
  248. window.showRootContent = hide