Cell.vue 2.36 KB
Newer Older
Anton's avatar
Intial  
Anton committed
1
2
3
4
5
6
7
<template lang="pug">

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

    template(v-else)
      div Empty cell

    slot

</template>

<script>
  import cellTypes from './cells'

  export default {
Anton's avatar
Anton committed
23
    props: ['cell', 'display', 'preview', 'annotation'],
Anton's avatar
Intial  
Anton committed
24
25
    data () {
      return {
26
27
        selected: false,
        inViewPort: false
Anton's avatar
Intial  
Anton committed
28
29
30
31
32
      }
    },
    computed: {
      cellTypeName () {
        return this.cell.type ? this.cellTypeToClassName(this.cell.type) : ''
33
34
      },
      cellClasses () {
35
        let classes = ['cell-content']
36
37
        if (this.cell) {
          if (this.cell.type) classes.push(`cell-type-${this.cell.type.toLowerCase()}`)
38
39
40
          if (this.cell.styleClass) {
            classes = classes.concat(this.cell.styleClass.split(' '))
          }
41
42
        }
        return classes
Anton's avatar
Intial  
Anton committed
43
44
45
      }
    },
    mounted () {
46
47
      this.onScroll()
      this.$parent.$el.addEventListener('scroll', this.onScroll, false)
Anton's avatar
Intial  
Anton committed
48
49
50
51
      if (this.cellTypeName && cellTypes.hasOwnProperty(this.cellTypeName) === false) {
        throw new Error('This cell type is missing: ' + this.cell.type)
      }
    },
52
53
54
    beforeDestroy () {
      this.$parent.$el.removeEventListener('scroll', this.onScroll)
    },
Anton's avatar
Intial  
Anton committed
55
    methods: {
56
57
58
59
60
61
62
      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
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
      cellTypeToClassName (type) {
        return 'Cell' + type.slice(0, 1).toUpperCase() + type.slice(1).replace(/[^a-z0-9]/ig, '')
      },
      handleClick () {
        this.selected = !this.selected
      },
      handleDragStart (event) {
        event.dataTransfer.setData('text/plain', JSON.stringify(this.cell))
      }
    },
    components: cellTypes
  }
</script>

<style scoped lang="stylus">

  .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>