Cell.vue 2.31 KB
Newer Older
Anton's avatar
Intial  
Anton committed
1
2
3
<template lang="pug">

  .cell-item-inner(:class="{'display-preview': preview, 'display-full': display}")
4
    template(v-if="cell")
Anton's avatar
Intial  
Anton committed
5
      component(
6
        :is="cell.component",
Anton's avatar
Intial  
Anton committed
7
        :cell="cell",
8
        :class="cellClasses",
Anton's avatar
Intial  
Anton committed
9
        :display="display",
10
11
        :preview="preview",
        :visible="inViewPort")
Anton's avatar
Intial  
Anton committed
12
13
14
15
16
17
18
19
20
21
22
23

    template(v-else)
      div Empty cell

    slot

</template>

<script>
  import cellTypes from './cells'

  export default {
24
    props: ['annotation', 'display', 'preview'],
Anton's avatar
Intial  
Anton committed
25
26
    data () {
      return {
27
        cell: undefined,
28
29
        selected: false,
        inViewPort: false
Anton's avatar
Intial  
Anton committed
30
31
32
      }
    },
    computed: {
33
      cellClasses () {
34
        let classes = ['cell-content']
35
        if (this.cell) {
36
          if (this.cell.component) classes.push(`cell-type-${this.cell.component.toLowerCase()}`)
37
38
39
          if (this.cell.styleClass) {
            classes = classes.concat(this.cell.styleClass.split(' '))
          }
40
41
        }
        return classes
Anton's avatar
Intial  
Anton committed
42
43
      }
    },
44
45
46
47
48
49
    watch: {
      async annotation (val) {
        if (val) await this.getCell()
      }
    },
    async mounted () {
50
51
      this.onScroll()
      this.$parent.$el.addEventListener('scroll', this.onScroll, false)
52
      if (this.annotation) await this.getCell()
Anton's avatar
Intial  
Anton committed
53
    },
54
55
56
    beforeDestroy () {
      this.$parent.$el.removeEventListener('scroll', this.onScroll)
    },
Anton's avatar
Intial  
Anton committed
57
    methods: {
58
59
60
      async getCell () {
        this.cell = await this.$store.dispatch('cells/get', this.annotation.body.source.id)
      },
61
62
63
64
65
66
67
      onScroll () {
        const
          bounds = this.$el.getBoundingClientRect(),
          boundsParent = this.$parent.$el.getBoundingClientRect()
        this.inViewPort = (bounds.left >= 0 && bounds.left <= boundsParent.width) ||
          (bounds.right >= 0 && bounds.right <= boundsParent.width)
      },
Anton's avatar
Intial  
Anton committed
68
69
70
71
72
73
74
75
76
77
78
79
80
      handleClick () {
        this.selected = !this.selected
      },
      handleDragStart (event) {
        event.dataTransfer.setData('text/plain', JSON.stringify(this.cell))
      }
    },
    components: cellTypes
  }
</script>

<style scoped lang="stylus">

Mathias Bär's avatar
Mathias Bär committed
81
  .cell-item-inner
82
    padding 0 1px 1px 0
Mathias Bär's avatar
Mathias Bär committed
83

Anton's avatar
Intial  
Anton committed
84
85
86
87
88
89
90
91
92
93
94
95
96
  .display-preview .cell-content
      pointer-events none

  .cell-content
      h1, h2, h3, h4, h5, h6
        font-size: 1em
        font-weight: normal

      a, a:hover, a:visited
        color: black
        text-decoration: underline

</style>