aboutsummaryrefslogtreecommitdiff
path: root/src/modules/media_viewer.js
blob: ebcba01d4cf697a6d831b51a254b604f00fbe030 (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
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 }, attachments) {
      const media = attachments.filter(attachment => {
        const type = fileTypeService.fileType(attachment.mimetype)
        return supportedTypes.has(type)
      })
      commit('setMedia', media)
    },
    setCurrentMedia ({ commit, state }, current) {
      const index = state.media.indexOf(current)
      commit('setCurrentMedia', index || 0)
    },
    closeMediaViewer ({ commit }) {
      commit('close')
    }
  }
}

export default mediaViewer