tree.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template lang="pug">
  2. .has-collapsable-nav
  3. ul.collapsable-nav(v-for='treeItem in tree', :class='{ "has-children": treeItem.hasChildren }', v-cloak)
  4. li(v-for='page in treeItem.pages', :class='{ "is-active": page.isActive }')
  5. a(v-on:click='mainAction(page)')
  6. template(v-if='page._id !== "home"')
  7. i(:class='{ "icon-folder2": page.isDirectory, "icon-file-text-o": !page.isDirectory }')
  8. span {{ page.title }}
  9. template(v-else)
  10. i.icon-home
  11. span {{ $t('nav.home') }}
  12. a.is-pagelink(v-if='page.isDirectory && page.isEntry', v-on:click='goto(page._id)')
  13. i.icon-file-text-o
  14. i.icon-arrow-right2
  15. </template>
  16. <script>
  17. /* global socket, siteRoot */
  18. export default {
  19. name: 'tree',
  20. data () {
  21. return {
  22. tree: []
  23. }
  24. },
  25. methods: {
  26. fetch (basePath) {
  27. let self = this
  28. self.$store.dispatch('startLoading')
  29. self.$nextTick(() => {
  30. socket.emit('treeFetch', { basePath }, (data) => {
  31. if (self.tree.length > 0) {
  32. let branch = self._.last(self.tree)
  33. branch.hasChildren = true
  34. self._.find(branch.pages, { _id: basePath }).isActive = true
  35. }
  36. self.tree.push({
  37. hasChildren: false,
  38. pages: data
  39. })
  40. self.$store.dispatch('stopLoading')
  41. })
  42. })
  43. },
  44. goto (entryPath) {
  45. window.location.assign(siteRoot + '/' + entryPath)
  46. },
  47. unfold (entryPath) {
  48. let self = this
  49. let lastIndex = 0
  50. self._.forEach(self.tree, branch => {
  51. lastIndex++
  52. if (self._.find(branch.pages, { _id: entryPath }) !== undefined) {
  53. return false
  54. }
  55. })
  56. self.tree = self._.slice(self.tree, 0, lastIndex)
  57. let branch = self._.last(self.tree)
  58. branch.hasChildren = false
  59. branch.pages.forEach(page => {
  60. page.isActive = false
  61. })
  62. },
  63. mainAction (page) {
  64. let self = this
  65. if (page.isActive) {
  66. self.unfold(page._id)
  67. } else if (page.isDirectory) {
  68. self.fetch(page._id)
  69. } else {
  70. self.goto(page._id)
  71. }
  72. }
  73. },
  74. mounted () {
  75. let basePath = window.location.pathname.slice(0, -4)
  76. if (basePath.length > 1) {
  77. basePath = basePath.slice(1)
  78. }
  79. this.fetch(basePath)
  80. }
  81. }
  82. </script>