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.

174 lines
4.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /* global OverpassLayer, repositoriesGitea */
  2. require('./addCategories.css')
  3. const tabs = require('modulekit-tabs')
  4. const weightSort = require('weight-sort')
  5. const OpenStreetBrowserLoader = require('./OpenStreetBrowserLoader')
  6. let tab
  7. function addCategoriesShow (repo) {
  8. let content = tab.content
  9. let repoId
  10. let branchId
  11. if (repo) {
  12. [ repoId, branchId ] = repo.split(/~/)
  13. }
  14. content.innerHTML = '<h3>' + lang('more_categories') + '</h3>' + '<i class="fa fa-spinner fa-pulse fa-fw"></i> ' + lang('loading')
  15. OpenStreetBrowserLoader.getRepo(repo, {}, function (err, repoData) {
  16. if (err) {
  17. alert(err)
  18. }
  19. content.innerHTML = '<h3>' + lang('more_categories') + '</h3>'
  20. var categoryUrl = null
  21. if (repoData.categoryUrl) {
  22. categoryUrl = OverpassLayer.twig.twig({ data: repoData.categoryUrl, autoescape: true })
  23. }
  24. var list = {}
  25. if (repo) {
  26. var backLink = document.createElement('a')
  27. backLink.className = 'back'
  28. backLink.href = '#'
  29. backLink.innerHTML = '<i class="fa fa-chevron-circle-left" aria-hidden="true"></i> '
  30. backLink.appendChild(document.createTextNode(lang('back')))
  31. backLink.onclick = function () {
  32. addCategoriesShow()
  33. return false
  34. }
  35. content.appendChild(backLink)
  36. let h = document.createElement('h2')
  37. h.appendChild(document.createTextNode(repoId))
  38. content.appendChild(h)
  39. list = repoData.categories
  40. } else {
  41. if (typeof repositoriesGitea === 'object' && repositoriesGitea.url) {
  42. let a = document.createElement('a')
  43. a.href = repositoriesGitea.url
  44. a.target = '_blank'
  45. a.innerHTML = lang('more_categories_gitea')
  46. content.appendChild(a)
  47. }
  48. list = weightSort(repoData, {
  49. key: 'timestamp',
  50. reverse: true
  51. })
  52. }
  53. if ('branches' in repoData) {
  54. let text = document.createElement('span')
  55. text.innerHTML = lang('available_branches') + ': '
  56. content.appendChild(text)
  57. let branchSelector = document.createElement('select')
  58. branchSelector.onchange = () => {
  59. let branch = branchSelector.value
  60. addCategoriesShow(repoId + '~' + branch)
  61. }
  62. Object.keys(repoData.branches).forEach(
  63. branch => {
  64. let option = document.createElement('option')
  65. option.value = branch
  66. option.appendChild(document.createTextNode(branch))
  67. if (repoData.branch === branch) {
  68. option.selected = true
  69. }
  70. branchSelector.appendChild(option)
  71. }
  72. )
  73. content.appendChild(branchSelector)
  74. }
  75. var ul = document.createElement('ul')
  76. for (var id in list) {
  77. var data = list[id]
  78. var repositoryUrl = null
  79. if (data.repositoryUrl) {
  80. repositoryUrl = OverpassLayer.twig.twig({ data: data.repositoryUrl, autoescape: true })
  81. }
  82. var li = document.createElement('li')
  83. let a = document.createElement('a')
  84. if (repo) {
  85. a.href = '#categories=' + repo + '/' + id
  86. a.onclick = function () {
  87. addCategoriesHide()
  88. }
  89. } else {
  90. a.href = '#'
  91. a.onclick = function (id) {
  92. addCategoriesShow(id)
  93. return false
  94. }.bind(this, id)
  95. }
  96. li.appendChild(a)
  97. a.appendChild(document.createTextNode('name' in data ? lang(data.name) : id))
  98. var editLink = null
  99. if (repo && categoryUrl) {
  100. editLink = document.createElement('a')
  101. editLink.href = categoryUrl.render({ repositoryId: repo, categoryId: id })
  102. }
  103. if (!repo && repositoryUrl) {
  104. editLink = document.createElement('a')
  105. editLink.href = repositoryUrl.render({ repositoryId: id })
  106. }
  107. if (editLink) {
  108. editLink.className = 'source-code'
  109. editLink.title = 'Show source code'
  110. editLink.target = '_blank'
  111. editLink.innerHTML = '<i class="fa fa-file-code-o" aria-hidden="true"></i>'
  112. li.appendChild(document.createTextNode(' '))
  113. li.appendChild(editLink)
  114. }
  115. ul.appendChild(li)
  116. }
  117. content.appendChild(ul)
  118. })
  119. }
  120. function addCategoriesHide () {
  121. tab.unselect()
  122. }
  123. register_hook('init', function (callback) {
  124. tab = new tabs.Tab({
  125. id: 'addCategories'
  126. })
  127. global.tabs.add(tab)
  128. tab.header.innerHTML = '<i class="fa fa-plus" aria-hidden="true"></i>'
  129. tab.header.title = lang('more_categories')
  130. let initialized = false
  131. tab.on('select', () => {
  132. if (!initialized) {
  133. addCategoriesShow()
  134. initialized = true
  135. }
  136. })
  137. })