SharingLinkList.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <!--
  2. - @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
  3. -
  4. - @author John Molakvoæ <skjnldsv@protonmail.com>
  5. -
  6. - @license GNU AGPL version 3 or any later version
  7. -
  8. - This program is free software: you can redistribute it and/or modify
  9. - it under the terms of the GNU Affero General Public License as
  10. - published by the Free Software Foundation, either version 3 of the
  11. - License, or (at your option) any later version.
  12. -
  13. - This program is distributed in the hope that it will be useful,
  14. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. - GNU Affero General Public License for more details.
  17. -
  18. - You should have received a copy of the GNU Affero General Public License
  19. - along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. -
  21. -->
  22. <template>
  23. <ul class="sharing-link-list">
  24. <!-- If no link shares, show the add link default entry -->
  25. <SharingEntryLink v-if="!hasLinkShares && canReshare"
  26. :can-reshare="canReshare"
  27. :file-info="fileInfo"
  28. @add:share="addShare" />
  29. <!-- Else we display the list -->
  30. <template v-if="hasShares">
  31. <!-- using shares[index] to work with .sync -->
  32. <SharingEntryLink v-for="(share, index) in shares"
  33. :key="share.id"
  34. :can-reshare="canReshare"
  35. :share.sync="shares[index]"
  36. :file-info="fileInfo"
  37. @add:share="addShare(...arguments)"
  38. @update:share="awaitForShare(...arguments)"
  39. @remove:share="removeShare" />
  40. </template>
  41. </ul>
  42. </template>
  43. <script>
  44. // eslint-disable-next-line no-unused-vars
  45. import Share from '../models/Share'
  46. import ShareTypes from '../mixins/ShareTypes'
  47. import SharingEntryLink from '../components/SharingEntryLink'
  48. export default {
  49. name: 'SharingLinkList',
  50. components: {
  51. SharingEntryLink
  52. },
  53. mixins: [ShareTypes],
  54. props: {
  55. fileInfo: {
  56. type: Object,
  57. default: () => {},
  58. required: true
  59. },
  60. shares: {
  61. type: Array,
  62. default: () => [],
  63. required: true
  64. },
  65. canReshare: {
  66. type: Boolean,
  67. required: true
  68. }
  69. },
  70. computed: {
  71. /**
  72. * Do we have link shares?
  73. * Using this to still show the `new link share`
  74. * button regardless of mail shares
  75. *
  76. * @returns {Array}
  77. */
  78. hasLinkShares() {
  79. return this.shares.filter(share => share.type === this.SHARE_TYPES.SHARE_TYPE_LINK).length > 0
  80. },
  81. /**
  82. * Do we have any link or email shares?
  83. *
  84. * @returns {boolean}
  85. */
  86. hasShares() {
  87. return this.shares.length > 0
  88. }
  89. },
  90. methods: {
  91. /**
  92. * Add a new share into the link shares list
  93. * and return the newly created share component
  94. *
  95. * @param {Share} share the share to add to the array
  96. * @param {Function} resolve a function to run after the share is added and its component initialized
  97. */
  98. addShare(share, resolve) {
  99. this.shares.unshift(share)
  100. this.awaitForShare(share, resolve)
  101. },
  102. /**
  103. * Await for next tick and render after the list updated
  104. * Then resolve with the matched vue component of the
  105. * provided share object
  106. *
  107. * @param {Share} share newly created share
  108. * @param {Function} resolve a function to execute after
  109. */
  110. awaitForShare(share, resolve) {
  111. this.$nextTick(() => {
  112. const newShare = this.$children.find(component => component.share === share)
  113. if (newShare) {
  114. resolve(newShare)
  115. }
  116. })
  117. },
  118. /**
  119. * Remove a share from the shares list
  120. *
  121. * @param {Share} share the share to remove
  122. */
  123. removeShare(share) {
  124. const index = this.shares.findIndex(item => item === share)
  125. this.shares.splice(index, 1)
  126. }
  127. }
  128. }
  129. </script>