forked from AkkomaGang/akkoma-fe
100 lines
2.5 KiB
JavaScript
100 lines
2.5 KiB
JavaScript
import fileTypeService from '../services/file_type/file_type.service.js'
|
|
|
|
const mediaViewer = {
|
|
state: {
|
|
media: [],
|
|
currentIndex: 0,
|
|
activated: false
|
|
},
|
|
mutations: {
|
|
setMedia (state, media) {
|
|
state.media = media
|
|
},
|
|
setCurrent (state, index) {
|
|
state.activated = true
|
|
state.currentIndex = index
|
|
},
|
|
close (state) {
|
|
state.activated = false
|
|
}
|
|
},
|
|
actions: {
|
|
setMedia ({ commit, dispatch }, attachments) {
|
|
const media = attachments.filter(attachment => {
|
|
const type = fileTypeService.fileType(attachment.mimetype)
|
|
return type === 'image' || type === 'video' || type === 'audio'
|
|
})
|
|
commit('setMedia', media)
|
|
dispatch('swipeScaler/reset')
|
|
},
|
|
setCurrent ({ commit, state, dispatch }, current) {
|
|
const index = state.media.indexOf(current)
|
|
commit('setCurrent', index || 0)
|
|
dispatch('swipeScaler/reset')
|
|
},
|
|
closeMediaViewer ({ commit, dispatch }) {
|
|
commit('close')
|
|
dispatch('swipeScaler/reset')
|
|
}
|
|
},
|
|
modules: {
|
|
swipeScaler: {
|
|
namespaced: true,
|
|
|
|
state: {
|
|
origOffsets: [0, 0],
|
|
offsets: [0, 0],
|
|
origScaling: 1,
|
|
scaling: 1
|
|
},
|
|
|
|
mutations: {
|
|
reset (state) {
|
|
state.origOffsets = [0, 0]
|
|
state.offsets = [0, 0]
|
|
state.origScaling = 1
|
|
state.scaling = 1
|
|
},
|
|
applyOffsets (state, { offsets }) {
|
|
state.offsets = state.origOffsets.map((k, n) => k + offsets[n])
|
|
},
|
|
applyScaling (state, { scaling }) {
|
|
state.scaling = state.origScaling * scaling
|
|
},
|
|
finishOffsets (state) {
|
|
state.origOffsets = [...state.offsets]
|
|
},
|
|
finishScaling (state) {
|
|
state.origScaling = state.scaling
|
|
},
|
|
revertOffsets (state) {
|
|
state.offsets = [...state.origOffsets]
|
|
},
|
|
revertScaling (state) {
|
|
state.scaling = state.origScaling
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
reset ({ commit }) {
|
|
commit('reset')
|
|
},
|
|
apply ({ commit }, { offsets, scaling = 1 }) {
|
|
commit('applyOffsets', { offsets })
|
|
commit('applyScaling', { scaling })
|
|
},
|
|
finish ({ commit }) {
|
|
commit('finishOffsets')
|
|
commit('finishScaling')
|
|
},
|
|
revert ({ commit }) {
|
|
commit('revertOffsets')
|
|
commit('revertScaling')
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export default mediaViewer
|