2019-01-14 09:23:13 -08:00
|
|
|
import StillImage from '../still-image/still-image.vue'
|
2019-01-26 07:45:03 -08:00
|
|
|
import VideoAttachment from '../video_attachment/video_attachment.vue'
|
2019-10-18 04:05:01 -07:00
|
|
|
import Modal from '../modal/modal.vue'
|
2021-08-02 14:42:10 -07:00
|
|
|
import PinchZoom from '../pinch_zoom/pinch_zoom.vue'
|
2019-01-14 09:23:13 -08:00
|
|
|
import fileTypeService from '../../services/file_type/file_type.service.js'
|
2019-11-07 22:16:26 -08:00
|
|
|
import GestureService from '../../services/gesture_service/gesture_service'
|
2021-06-17 16:01:37 -07:00
|
|
|
import Flash from 'src/components/flash/flash.vue'
|
2021-08-01 16:46:27 -07:00
|
|
|
import Vuex from 'vuex'
|
2020-10-20 12:13:19 -07:00
|
|
|
import { library } from '@fortawesome/fontawesome-svg-core'
|
|
|
|
import {
|
|
|
|
faChevronLeft,
|
2021-08-15 15:02:56 -07:00
|
|
|
faChevronRight,
|
|
|
|
faCircleNotch
|
2020-10-20 12:13:19 -07:00
|
|
|
} from '@fortawesome/free-solid-svg-icons'
|
|
|
|
|
|
|
|
library.add(
|
|
|
|
faChevronLeft,
|
2021-08-15 15:02:56 -07:00
|
|
|
faChevronRight,
|
|
|
|
faCircleNotch
|
2020-10-20 12:13:19 -07:00
|
|
|
)
|
2019-01-14 09:23:13 -08:00
|
|
|
|
2021-08-01 16:46:27 -07:00
|
|
|
const onlyXAxis = ([x, y]) => [x, 0]
|
2021-08-01 20:42:34 -07:00
|
|
|
const SCALING_RESET_MIN = 1.1
|
|
|
|
const SCALING_ENABLE_MOVE_THRESHOLD = 1
|
2021-08-01 16:46:27 -07:00
|
|
|
|
2019-01-14 09:23:13 -08:00
|
|
|
const MediaModal = {
|
|
|
|
components: {
|
2019-01-26 07:45:03 -08:00
|
|
|
StillImage,
|
2019-10-18 04:05:01 -07:00
|
|
|
VideoAttachment,
|
2021-08-02 14:42:10 -07:00
|
|
|
PinchZoom,
|
2021-06-17 16:01:37 -07:00
|
|
|
Modal,
|
|
|
|
Flash
|
2019-01-14 09:23:13 -08:00
|
|
|
},
|
2021-08-15 15:02:56 -07:00
|
|
|
data () {
|
|
|
|
return {
|
2021-08-02 14:42:10 -07:00
|
|
|
loading: false,
|
|
|
|
pinchZoomOptions: {}
|
2021-08-15 15:02:56 -07:00
|
|
|
}
|
|
|
|
},
|
2019-01-14 09:23:13 -08:00
|
|
|
computed: {
|
|
|
|
showing () {
|
|
|
|
return this.$store.state.mediaViewer.activated
|
|
|
|
},
|
2019-02-10 10:23:01 -08:00
|
|
|
media () {
|
|
|
|
return this.$store.state.mediaViewer.media
|
|
|
|
},
|
2021-08-15 09:45:48 -07:00
|
|
|
description () {
|
|
|
|
return this.currentMedia.description
|
|
|
|
},
|
2019-01-14 09:23:13 -08:00
|
|
|
currentIndex () {
|
|
|
|
return this.$store.state.mediaViewer.currentIndex
|
|
|
|
},
|
|
|
|
currentMedia () {
|
2019-02-10 10:23:01 -08:00
|
|
|
return this.media[this.currentIndex]
|
|
|
|
},
|
|
|
|
canNavigate () {
|
|
|
|
return this.media.length > 1
|
2019-01-14 09:23:13 -08:00
|
|
|
},
|
|
|
|
type () {
|
2021-08-15 15:02:56 -07:00
|
|
|
return this.currentMedia ? this.getType(this.currentMedia) : null
|
2021-08-01 16:46:27 -07:00
|
|
|
},
|
|
|
|
scaling () {
|
|
|
|
return this.$store.state.mediaViewer.swipeScaler.scaling
|
|
|
|
},
|
|
|
|
offsets () {
|
|
|
|
return this.$store.state.mediaViewer.swipeScaler.offsets
|
|
|
|
},
|
|
|
|
transform () {
|
2021-08-01 18:39:07 -07:00
|
|
|
return `translate(${this.offsets[0]}px, ${this.offsets[1]}px) scale(${this.scaling}, ${this.scaling})`
|
2019-01-14 09:23:13 -08:00
|
|
|
}
|
|
|
|
},
|
2019-11-07 22:16:26 -08:00
|
|
|
created () {
|
2021-08-01 10:39:56 -07:00
|
|
|
this.mediaGesture = new GestureService.SwipeAndScaleGesture({
|
|
|
|
direction: GestureService.DIRECTION_LEFT,
|
|
|
|
callbackPositive: this.goNext,
|
|
|
|
callbackNegative: this.goPrev,
|
2021-08-01 16:46:27 -07:00
|
|
|
swipePreviewCallback: this.handleSwipePreview,
|
|
|
|
swipeEndCallback: this.handleSwipeEnd,
|
2021-08-01 18:39:07 -07:00
|
|
|
pinchPreviewCallback: this.handlePinchPreview,
|
|
|
|
pinchEndCallback: this.handlePinchEnd,
|
2021-08-01 10:39:56 -07:00
|
|
|
threshold: 50
|
|
|
|
})
|
2019-11-07 22:16:26 -08:00
|
|
|
},
|
2019-01-14 09:23:13 -08:00
|
|
|
methods: {
|
2021-08-15 15:02:56 -07:00
|
|
|
getType (media) {
|
|
|
|
return fileTypeService.fileType(media.mimetype)
|
|
|
|
},
|
2019-11-07 22:16:26 -08:00
|
|
|
mediaTouchStart (e) {
|
2021-08-01 10:39:56 -07:00
|
|
|
this.mediaGesture.start(e)
|
2019-11-07 22:16:26 -08:00
|
|
|
},
|
|
|
|
mediaTouchMove (e) {
|
2021-08-01 10:39:56 -07:00
|
|
|
this.mediaGesture.move(e)
|
|
|
|
},
|
|
|
|
mediaTouchEnd (e) {
|
|
|
|
this.mediaGesture.end(e)
|
2019-11-07 22:16:26 -08:00
|
|
|
},
|
2019-01-14 09:23:13 -08:00
|
|
|
hide () {
|
|
|
|
this.$store.dispatch('closeMediaViewer')
|
2019-02-10 10:23:01 -08:00
|
|
|
},
|
|
|
|
goPrev () {
|
|
|
|
if (this.canNavigate) {
|
|
|
|
const prevIndex = this.currentIndex === 0 ? this.media.length - 1 : (this.currentIndex - 1)
|
2021-08-15 15:02:56 -07:00
|
|
|
const newMedia = this.media[prevIndex]
|
|
|
|
if (this.getType(newMedia) === 'image') {
|
|
|
|
this.loading = true
|
|
|
|
}
|
|
|
|
this.$store.dispatch('setCurrentMedia', newMedia)
|
2019-02-10 10:23:01 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
goNext () {
|
|
|
|
if (this.canNavigate) {
|
|
|
|
const nextIndex = this.currentIndex === this.media.length - 1 ? 0 : (this.currentIndex + 1)
|
2021-08-15 15:02:56 -07:00
|
|
|
const newMedia = this.media[nextIndex]
|
|
|
|
if (this.getType(newMedia) === 'image') {
|
|
|
|
this.loading = true
|
|
|
|
}
|
|
|
|
this.$store.dispatch('setCurrentMedia', newMedia)
|
2019-02-10 10:23:01 -08:00
|
|
|
}
|
|
|
|
},
|
2021-08-15 15:02:56 -07:00
|
|
|
onImageLoaded () {
|
|
|
|
this.loading = false
|
|
|
|
},
|
2021-08-01 16:46:27 -07:00
|
|
|
handleSwipePreview (offsets) {
|
2021-08-01 20:42:34 -07:00
|
|
|
this.$store.dispatch('swipeScaler/apply', {
|
|
|
|
offsets: this.scaling > SCALING_ENABLE_MOVE_THRESHOLD ? offsets : onlyXAxis(offsets)
|
|
|
|
})
|
2021-08-01 16:46:27 -07:00
|
|
|
},
|
|
|
|
handleSwipeEnd (sign) {
|
2021-08-01 20:42:34 -07:00
|
|
|
if (this.scaling > SCALING_ENABLE_MOVE_THRESHOLD) {
|
|
|
|
this.$store.dispatch('swipeScaler/finish')
|
|
|
|
return
|
|
|
|
}
|
2021-08-01 16:46:27 -07:00
|
|
|
if (sign === 0) {
|
2021-08-01 20:42:34 -07:00
|
|
|
this.$store.dispatch('swipeScaler/reset')
|
2021-08-01 16:46:27 -07:00
|
|
|
} else if (sign > 0) {
|
|
|
|
this.goNext()
|
|
|
|
} else {
|
|
|
|
this.goPrev()
|
|
|
|
}
|
|
|
|
},
|
2021-08-01 18:39:07 -07:00
|
|
|
handlePinchPreview (offsets, scaling) {
|
|
|
|
console.log('handle pinch preview:', offsets, scaling)
|
|
|
|
this.$store.dispatch('swipeScaler/apply', { offsets, scaling })
|
|
|
|
},
|
|
|
|
handlePinchEnd () {
|
2021-08-01 20:42:34 -07:00
|
|
|
if (this.scaling > SCALING_RESET_MIN) {
|
|
|
|
this.$store.dispatch('swipeScaler/finish')
|
|
|
|
} else {
|
|
|
|
this.$store.dispatch('swipeScaler/reset')
|
|
|
|
}
|
2021-08-01 18:39:07 -07:00
|
|
|
},
|
2019-02-10 10:23:01 -08:00
|
|
|
handleKeyupEvent (e) {
|
|
|
|
if (this.showing && e.keyCode === 27) { // escape
|
|
|
|
this.hide()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleKeydownEvent (e) {
|
|
|
|
if (!this.showing) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.keyCode === 39) { // arrow right
|
|
|
|
this.goNext()
|
|
|
|
} else if (e.keyCode === 37) { // arrow left
|
|
|
|
this.goPrev()
|
|
|
|
}
|
2019-01-14 09:23:13 -08:00
|
|
|
}
|
2019-02-10 10:23:01 -08:00
|
|
|
},
|
|
|
|
mounted () {
|
2020-06-04 07:30:28 -07:00
|
|
|
window.addEventListener('popstate', this.hide)
|
2019-02-10 10:23:01 -08:00
|
|
|
document.addEventListener('keyup', this.handleKeyupEvent)
|
|
|
|
document.addEventListener('keydown', this.handleKeydownEvent)
|
|
|
|
},
|
|
|
|
destroyed () {
|
2020-06-04 07:30:28 -07:00
|
|
|
window.removeEventListener('popstate', this.hide)
|
2019-02-10 10:23:01 -08:00
|
|
|
document.removeEventListener('keyup', this.handleKeyupEvent)
|
|
|
|
document.removeEventListener('keydown', this.handleKeydownEvent)
|
2019-01-14 09:23:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MediaModal
|