Library.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div class="h-100 position-relative">
  3. <!-- BACKGROUND (mode) -->
  4. <component :is="mode" @open="openText" />
  5. <!-- FOREGROUND (texts) -->
  6. <section v-for="parent in parents" :key="parent.id" class="split-screen">
  7. <text-card :id="parent.id" @open="openText" @close="closeText" />
  8. <div>
  9. <text-card
  10. v-for="id in parent.children" :key="id"
  11. :id="id"
  12. @close="closeText(parent.id, $event)"
  13. />
  14. </div>
  15. </section>
  16. </div>
  17. </template>
  18. <script>
  19. import { CardList, CardMap, TreeMap } from './library'
  20. import TextCard from '@/components/text/TextCard'
  21. export default {
  22. name: 'Library',
  23. components: {
  24. CardList,
  25. CardMap,
  26. TreeMap,
  27. TextCard
  28. },
  29. props: {
  30. mode: { type: String, required: true },
  31. texts: { type: Array, required: true }
  32. },
  33. computed: {
  34. parents () {
  35. return this.texts.map(text => {
  36. const [id, ...children] = text
  37. return { id, children }
  38. })
  39. }
  40. },
  41. methods: {
  42. openText (id, childId) {
  43. const parent = this.parents.find(p => p.id === id)
  44. if (parent && (childId === undefined || parent.children.includes(childId))) return
  45. if (!parent) {
  46. this.parents.push({ id, children: childId ? [childId] : [] })
  47. } else if (childId) {
  48. parent.children.push(childId)
  49. }
  50. this.updateQuery(this.parents)
  51. },
  52. closeText (id, childId) {
  53. const parent = this.parents.find(p => p.id === id)
  54. if (!childId) {
  55. this.updateQuery(this.parents.filter(p => p !== parent))
  56. } else {
  57. parent.children = parent.children.filter(childId_ => childId_ !== childId)
  58. this.updateQuery(this.parents)
  59. }
  60. },
  61. updateQuery (parents) {
  62. // Update the route's query (will not reload the page) and let vue determine what changed.
  63. this.$router.push({
  64. query: {
  65. mode: this.mode,
  66. texts: parents.map(parent => [parent.id, ...parent.children])
  67. }
  68. })
  69. }
  70. }
  71. }
  72. </script>
  73. <style lang="scss" scoped>
  74. </style>