Library.vue 2.1 KB

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