aboutsummaryrefslogtreecommitdiff
path: root/src/modules/media_viewer.js
blob: d62aa7c1322792b88c30faf886eb2d121c107ec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import fileTypeService from '../services/file_type/file_type.service.js'
const supportedTypes = new Set(['image', 'video', 'audio', 'flash'])

const mediaViewer = {
  state: {
    media: [],
    currentIndex: 0,
    activated: false
  },
  mutations: {
    setMedia (state, media) {
      state.media = media
    },
    setCurrentMedia (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 supportedTypes.has(type)
      })
      commit('setMedia', media)
      dispatch('swipeScaler/reset')
    },
    setCurrentMedia ({ commit, state }, current) {
      const index = state.media.indexOf(current)
      commit('setCurrentMedia', 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
        },
        finish (state) {
          state.origOffsets = [...state.offsets]
          state.origScaling = state.scaling
        },
        revert (state) {
          state.offsets = [...state.origOffsets]
          state.scaling = state.origScaling
        }
      },

      actions: {
        reset ({ commit }) {
          commit('reset')
        },
        apply ({ commit }, { offsets, scaling = 1 }) {
          commit('applyOffsets', { offsets })
          commit('applyScaling', { scaling })
        },
        finish ({ commit }) {
          commit('finish')
        },
        revert ({ commit }) {
          commit('revert')
        }
      }
    }
  }
}

export default mediaViewer