summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/viewer/viewer.actions.js
blob: ed92bb6e6e8f63d7d0e8a5deb0b0c90e863a3764 (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 * as types from 'app/types'
import { store, history, dispatch } from 'app/store'
import {
  MEDIA_ANNOTATION_TYPES, MEDIA_LABEL_TYPES,
  TEXT_ANNOTATION_TYPES, UTILITY_ANNOTATION_TYPES,
} from 'app/constants'
import { buildParagraphs } from 'app/utils/transcript.utils'

const newSection = (annotation, index, mediaIndex) => ({
  start_ts: annotation.start_ts,
  title: annotation.text,
  media: [],
  index,
  mediaIndex,
})

export const loadSections = () => dispatch => {
  let sections = []
  let currentSection
  let mediaIndex = 0
  let mediaLabels = {}
  let sectionAnnotationOrder = []

  const state = store.getState()
  const { order: annotationOrder, lookup: annotationLookup } = state.annotation.index
  const { lookup: mediaLookup } = state.media.index

  // loop over the annotations in time order. group the media found in each section.
  annotationOrder.forEach((annotation_id, i) => {

    const annotation = annotationLookup[annotation_id]
    if (annotation.type === 'section_heading') {
      if (currentSection) {
        currentSection.mediaLabels = Object.keys(mediaLabels).join(', ')
        currentSection.paragraphs = buildParagraphs(sectionAnnotationOrder)
        mediaLabels = {}
        sectionAnnotationOrder = []
      }
      currentSection = newSection(annotation, sections.length, mediaIndex)
      sections.push(currentSection)
    }

    if (MEDIA_ANNOTATION_TYPES.has(annotation.type)) {
      if (currentSection) {
        const media = mediaLookup[annotation.settings.media_id]
        currentSection.media.push({
          start_ts: annotation.start_ts,
          media
        })
        if (media.type in MEDIA_LABEL_TYPES) {
          mediaLabels[MEDIA_LABEL_TYPES[media.type]] = true
        }
        mediaIndex += 1
        // non-fullscreen media should be displayed inline in the transcript
        if (!annotation.settings.fullscreen && !annotation.settings.inline) {
          sectionAnnotationOrder.push(annotation.id)
        }
      } else {
        console.error("media found before first section")
      }
    }

    // group media into sections
    // group annotations by section, group into paragraphs
    if (UTILITY_ANNOTATION_TYPES.has(annotation.type) || annotation.settings.fullscreen) {
    }

    if (TEXT_ANNOTATION_TYPES.has(annotation.type)) {
      sectionAnnotationOrder.push(annotation.id)
    }

  })

  if (currentSection && Object.keys(mediaLabels).length) {
    currentSection.mediaLabels = Object.keys(mediaLabels).join(', ')
    currentSection.paragraphs = buildParagraphs(sectionAnnotationOrder)
  }

  // console.log(sections)

  dispatch({ type: types.viewer.load_sections, data: sections })
}

export const showSection = section => dispatch => {
  dispatch({ type: types.viewer.toggle_section, key: section, value: true })
}

export const hideSection = section => dispatch => {
  dispatch({ type: types.viewer.toggle_section, key: section, value: false })
}

export const toggleSection = section => dispatch => {
  dispatch({ type: types.viewer.toggle_section, key: section, value: !store.getState().viewer[section] })
}