GridDisplay.vue 6.11 KB
Newer Older
Anton's avatar
Intial  
Anton committed
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
<template lang="pug">

  div.cell-grid-container
    div.cell-grid(:style="gridStyle")

      template(v-for="(cell, index) in cells")
        .cell-item(
          :style="getCellStyle(cell)",
          :title="cell.title")
            cell(:cell="cell", display="display", :messenger="messenger")

      q-page-sticky(position="top-right", :offset="[18, 18]", v-if="$store.state.auth.user")
        q-btn(round, color="primary", small, @click="$router.push(`/mosys/grids/${$route.params.id}/annotate`)")
          q-icon(name="edit")

</template>

<script>
  import Vue from 'vue'
  import Cell from './Cell'

  const MessengerComponent = Vue.component('grid-editor-messenger', {})

  export default {
    components: {
      Cell
    },
    props: ['gridUuid'],
    data () {
      return {
        grid: undefined,
        annotations: [],
        cells: [],
        gridMetadata: {},
Anton's avatar
Anton committed
35
        gridDimensions: { gridWidth: 0, gridHeight: 0, cellWidth: 0, cellHeight: 0 },
Anton's avatar
Intial  
Anton committed
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        gridStyle: {},
        messenger: new MessengerComponent()
      }
    },
    async mounted () {
      const _this = this
      window.addEventListener('resize', this.updateGridDimensions)
      // this.messenger.$on('video-loaded', (/* origin */) => {
      //   // console.log('video loaded', origin.origin)
      // })
      // this.messenger.$on('video-time-changed', (/* time, globalTime, origin */) => {
      //   // console.log(videoUuid, time)
      // })
      this.grid = await this.$store.dispatch('maps/get', this.$route.params.id)
      await this.fetchMetadataAnnotations()
      this.updateGridDimensions()
      _this.fetchCellAnnotations()
    },
    methods: {
      fetchCellAnnotations () {
        const _this = this,
          query = { 'body.type': '2DCell', 'target.id': this.grid.id }
        this.$store.dispatch('annotations/find', query)
          .then(annotations => {
            _this.annotations = annotations.items
            _this.cells = _this.annotations.map(annotation => {
              let cell = JSON.parse(annotation.body.value)
              if (cell) {
                cell.uuid = annotation.uuid
                return cell
              }
              return null
            }).filter(cell => cell)
          })
      },
      async fetchMetadataAnnotations () {
        const _this = this
        const query = { 'body.type': '2DGridMetadata', 'target.id': this.grid.id }
        const annotations = await this.$store.dispatch('annotations/find', query)
        let annotation = annotations.items.shift()
        if (annotation) {
          let metadata = JSON.parse(annotation.body.value)
          metadata.uuid = annotation.uuid
          if (metadata) {
            _this.gridMetadata = metadata
          }
        }
        else {
          _this.gridMetadata = {
            columns: 10,
            rows: 6,
            ratio: 16 / 9.0
          }
          _this.updateGridMetadataStore()
        }
      },
      async updateCellStore (cell) {
        const _this = this
        let annotation = this.getGridCellAnnotation(cell)
        if (cell.uuid) {
          await _this.$store.dispatch('annotations/patch', [cell.uuid, annotation])
        }
        else {
          await _this.$store.dispatch('annotations/post', annotation)
        }
      },
      async updateGridMetadataStore () {
        const _this = this
        let mapAnnotation = this.getGridMetadataAnnotation(this.grid.id, this.gridMetadata)

        if (_this.gridMetadata.uuid) {
          await _this.$store.dispatch('annotations/patch', [_this.gridMetadata.uuid, mapAnnotation])
        }
        await _this.$store.dispatch('annotations/post', mapAnnotation)
        _this.updateGridDimensions()
      },
      updateGridDimensions () {
        let elWidth = this.$el.offsetWidth
        let elHeight = this.$el.offsetHeight
        let cellSizeRatio = this.gridMetadata.ratio
        let gridHeight = elHeight
        let cellHeight = gridHeight / this.gridMetadata.rows
        let cellWidth = elWidth / Math.round(elWidth / (cellHeight * cellSizeRatio))
        let gridWidth = cellWidth * this.gridMetadata.columns
        let cellsPerWidth = elWidth / cellWidth
        let cellWidthMini = elWidth / this.gridMetadata.columns
        let gridHeightMini = cellWidthMini / cellSizeRatio
        this.gridDimensions = {
          full: {
            width: gridWidth,
            height: gridHeight,
            cell: {
              width: cellWidth,
              height: cellHeight
            },
            cells_per_width: cellsPerWidth
          },
          mini: {
            width: elWidth,
            height: gridHeightMini * this.gridMetadata.rows,
            cell: {
              width: cellWidthMini,
              height: gridHeightMini
            }
          }
        }
        if (elWidth > 800) {
          this.gridStyle = {
            width: this.gridDimensions.full.width + 'px',
            height: '100%',
            'grid-auto-columns': this.gridDimensions.full.cell.width + 'px',
            'grid-auto-rows': this.gridDimensions.full.cell.height + 'px'
          }
        }
        else {
          this.gridStyle = {
            width: '100%',
            height: this.gridDimensions.mini.height + 'px',
            'grid-auto-columns': this.gridDimensions.mini.cell.width + 'px',
            'grid-auto-rows': this.gridDimensions.mini.cell.height + 'px'
          }
        }
      },
      getGridMetadataAnnotation (id, metadata) {
        return {
          body: {
            type: '2DGridMetadata',
            purpose: 'linking',
            value: JSON.stringify(metadata)
          },
          target: {
            id,
            type: 'Map'
          }
        }
      },
      getCellStyle (cell) {
Anton's avatar
Anton committed
173
        return { 'grid-column-start': cell.x, 'grid-column-end': `span ${cell.width}`, 'grid-row-start': cell.y, 'grid-row-end': `span ${cell.height}` }
Anton's avatar
Intial  
Anton committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
      }
    }
  }
</script>

<style scoped lang="stylus">

  .cell-grid
    display grid

    .cell-item
      position relative
      overflow: hidden
      grid-column-start: 1
      grid-column-end: span 1
      grid-row-start: 1
      grid-row-end: span 1

      .cell-item-inner
        width 100%
        height 100%

</style>