logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

media_modal.js (4487B)


  1. import StillImage from '../still-image/still-image.vue'
  2. import VideoAttachment from '../video_attachment/video_attachment.vue'
  3. import Modal from '../modal/modal.vue'
  4. import PinchZoom from '../pinch_zoom/pinch_zoom.vue'
  5. import SwipeClick from '../swipe_click/swipe_click.vue'
  6. import GestureService from '../../services/gesture_service/gesture_service'
  7. import Flash from 'src/components/flash/flash.vue'
  8. import fileTypeService from '../../services/file_type/file_type.service.js'
  9. import { library } from '@fortawesome/fontawesome-svg-core'
  10. import {
  11. faChevronLeft,
  12. faChevronRight,
  13. faCircleNotch,
  14. faTimes
  15. } from '@fortawesome/free-solid-svg-icons'
  16. library.add(
  17. faChevronLeft,
  18. faChevronRight,
  19. faCircleNotch,
  20. faTimes
  21. )
  22. const MediaModal = {
  23. components: {
  24. StillImage,
  25. VideoAttachment,
  26. PinchZoom,
  27. SwipeClick,
  28. Modal,
  29. Flash
  30. },
  31. data () {
  32. return {
  33. loading: false,
  34. swipeDirection: GestureService.DIRECTION_LEFT,
  35. swipeThreshold: () => {
  36. const considerableMoveRatio = 1 / 4
  37. return window.innerWidth * considerableMoveRatio
  38. },
  39. pinchZoomMinScale: 1,
  40. pinchZoomScaleResetLimit: 1.2
  41. }
  42. },
  43. computed: {
  44. showing () {
  45. return this.$store.state.mediaViewer.activated
  46. },
  47. media () {
  48. return this.$store.state.mediaViewer.media
  49. },
  50. description () {
  51. return this.currentMedia.description
  52. },
  53. currentIndex () {
  54. return this.$store.state.mediaViewer.currentIndex
  55. },
  56. currentMedia () {
  57. return this.media[this.currentIndex]
  58. },
  59. canNavigate () {
  60. return this.media.length > 1
  61. },
  62. type () {
  63. return this.currentMedia ? this.getType(this.currentMedia) : null
  64. },
  65. swipeDisableClickThreshold () {
  66. // If there is only one media, allow more mouse movements to close the modal
  67. // because there is less chance that the user wants to switch to another image
  68. return () => this.canNavigate ? 1 : 30
  69. }
  70. },
  71. methods: {
  72. getType (media) {
  73. return fileTypeService.fileType(media.mimetype)
  74. },
  75. hide () {
  76. // HACK: Closing immediately via a touch will cause the click
  77. // to be processed on the content below the overlay
  78. const transitionTime = 100 // ms
  79. setTimeout(() => {
  80. this.$store.dispatch('closeMediaViewer')
  81. }, transitionTime)
  82. },
  83. hideIfNotSwiped (event) {
  84. // If we have swiped over SwipeClick, do not trigger hide
  85. const comp = this.$refs.swipeClick
  86. if (!comp) {
  87. this.hide()
  88. } else {
  89. comp.$gesture.click(event)
  90. }
  91. },
  92. goPrev () {
  93. if (this.canNavigate) {
  94. const prevIndex = this.currentIndex === 0 ? this.media.length - 1 : (this.currentIndex - 1)
  95. const newMedia = this.media[prevIndex]
  96. if (this.getType(newMedia) === 'image') {
  97. this.loading = true
  98. }
  99. this.$store.dispatch('setCurrentMedia', newMedia)
  100. }
  101. },
  102. goNext () {
  103. if (this.canNavigate) {
  104. const nextIndex = this.currentIndex === this.media.length - 1 ? 0 : (this.currentIndex + 1)
  105. const newMedia = this.media[nextIndex]
  106. if (this.getType(newMedia) === 'image') {
  107. this.loading = true
  108. }
  109. this.$store.dispatch('setCurrentMedia', newMedia)
  110. }
  111. },
  112. onImageLoaded () {
  113. this.loading = false
  114. },
  115. handleSwipePreview (offsets) {
  116. this.$refs.pinchZoom.setTransform({ scale: 1, x: offsets[0], y: 0 })
  117. },
  118. handleSwipeEnd (sign) {
  119. this.$refs.pinchZoom.setTransform({ scale: 1, x: 0, y: 0 })
  120. if (sign > 0) {
  121. this.goNext()
  122. } else if (sign < 0) {
  123. this.goPrev()
  124. }
  125. },
  126. handleKeyupEvent (e) {
  127. if (this.showing && e.keyCode === 27) { // escape
  128. this.hide()
  129. }
  130. },
  131. handleKeydownEvent (e) {
  132. if (!this.showing) {
  133. return
  134. }
  135. if (e.keyCode === 39) { // arrow right
  136. this.goNext()
  137. } else if (e.keyCode === 37) { // arrow left
  138. this.goPrev()
  139. }
  140. }
  141. },
  142. mounted () {
  143. window.addEventListener('popstate', this.hide)
  144. document.addEventListener('keyup', this.handleKeyupEvent)
  145. document.addEventListener('keydown', this.handleKeydownEvent)
  146. },
  147. unmounted () {
  148. window.removeEventListener('popstate', this.hide)
  149. document.removeEventListener('keyup', this.handleKeyupEvent)
  150. document.removeEventListener('keydown', this.handleKeydownEvent)
  151. }
  152. }
  153. export default MediaModal