Cell.vue 2.43 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",
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 {
Anton's avatar
Anton committed
24
    props: ['cell', 'display', 'preview', 'annotation'],
Anton's avatar
Intial  
Anton committed
25
26
    data () {
      return {
27
28
        selected: false,
        inViewPort: false
Anton's avatar
Intial  
Anton committed
29
30
31
32
33
      }
    },
    computed: {
      cellTypeName () {
        return this.cell.type ? this.cellTypeToClassName(this.cell.type) : ''
34
35
      },
      cellClasses () {
36
        let classes = ['cell-content']
37
38
        if (this.cell) {
          if (this.cell.type) classes.push(`cell-type-${this.cell.type.toLowerCase()}`)
39
40
41
          if (this.cell.styleClass) {
            classes = classes.concat(this.cell.styleClass.split(' '))
          }
42
43
        }
        return classes
Anton's avatar
Intial  
Anton committed
44
45
46
      }
    },
    mounted () {
47
48
      this.onScroll()
      this.$parent.$el.addEventListener('scroll', this.onScroll, false)
Anton's avatar
Intial  
Anton committed
49
50
51
52
      if (this.cellTypeName && cellTypes.hasOwnProperty(this.cellTypeName) === false) {
        throw new Error('This cell type is missing: ' + this.cell.type)
      }
    },
53
54
55
    beforeDestroy () {
      this.$parent.$el.removeEventListener('scroll', this.onScroll)
    },
Anton's avatar
Intial  
Anton committed
56
    methods: {
57
58
59
60
61
62
63
      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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
      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">

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

Anton's avatar
Intial  
Anton committed
83
84
85
86
87
88
89
90
91
92
93
94
95
  .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>