summaryrefslogtreecommitdiff
path: root/vendor/assets/javascripts/vue-virtual-scroller/src/components/DynamicScrollerItem.vue
blob: 3db24018ad03027bd995e9d1748766134b893fde (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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<script>
export default {
  name: 'DynamicScrollerItem',

  inject: [
    'vscrollData',
    'vscrollParent',
    'vscrollResizeObserver',
  ],

  props: {
    // eslint-disable-next-line vue/require-prop-types
    item: {
      required: true,
    },

    watchData: {
      type: Boolean,
      default: false,
    },

    /**
     * Indicates if the view is actively used to display an item.
     */
    active: {
      type: Boolean,
      required: true,
    },

    index: {
      type: Number,
      default: undefined,
    },

    sizeDependencies: {
      type: [Array, Object],
      default: null,
    },

    emitResize: {
      type: Boolean,
      default: false,
    },

    tag: {
      type: String,
      default: 'div',
    },
  },

  computed: {
    id () {
      return this.vscrollData.simpleArray ? this.index : this.item[this.vscrollData.keyField]
    },

    size () {
      return (this.vscrollData.validSizes[this.id] && this.vscrollData.sizes[this.id]) || 0
    },

    finalActive () {
      return this.active && this.vscrollData.active
    },
  },

  watch: {
    watchData: 'updateWatchData',

    id () {
      if (!this.size) {
        this.onDataUpdate()
      }
    },

    finalActive (value) {
      if (!this.size) {
        if (value) {
          if (!this.vscrollParent.$_undefinedMap[this.id]) {
            this.vscrollParent.$_undefinedSizes++
            this.vscrollParent.$_undefinedMap[this.id] = true
          }
        } else {
          if (this.vscrollParent.$_undefinedMap[this.id]) {
            this.vscrollParent.$_undefinedSizes--
            this.vscrollParent.$_undefinedMap[this.id] = false
          }
        }
      }

      if (this.vscrollResizeObserver) {
        if (value) {
          this.observeSize()
        } else {
          this.unobserveSize()
        }
      } else if (value && this.$_pendingVScrollUpdate === this.id) {
        this.updateSize()
      }
    },
  },

  created () {
    if (this.$isServer) return

    this.$_forceNextVScrollUpdate = null
    this.updateWatchData()

    if (!this.vscrollResizeObserver) {
      for (const k in this.sizeDependencies) {
        this.$watch(() => this.sizeDependencies[k], this.onDataUpdate)
      }

      this.vscrollParent.$on('vscroll:update', this.onVscrollUpdate)
      this.vscrollParent.$on('vscroll:update-size', this.onVscrollUpdateSize)
    }
  },

  mounted () {
    if (this.vscrollData.active) {
      this.updateSize()
      this.observeSize()
    }
  },

  beforeDestroy () {
    this.vscrollParent.$off('vscroll:update', this.onVscrollUpdate)
    this.vscrollParent.$off('vscroll:update-size', this.onVscrollUpdateSize)
    this.unobserveSize()
  },

  methods: {
    updateSize () {
      if (this.finalActive) {
        if (this.$_pendingSizeUpdate !== this.id) {
          this.$_pendingSizeUpdate = this.id
          this.$_forceNextVScrollUpdate = null
          this.$_pendingVScrollUpdate = null
          this.computeSize(this.id)
        }
      } else {
        this.$_forceNextVScrollUpdate = this.id
      }
    },

    updateWatchData () {
      if (this.watchData) {
        this.$_watchData = this.$watch('data', () => {
          this.onDataUpdate()
        }, {
          deep: true,
        })
      } else if (this.$_watchData) {
        this.$_watchData()
        this.$_watchData = null
      }
    },

    onVscrollUpdate ({ force }) {
      // If not active, sechedule a size update when it becomes active
      if (!this.finalActive && force) {
        this.$_pendingVScrollUpdate = this.id
      }

      if (this.$_forceNextVScrollUpdate === this.id || force || !this.size) {
        this.updateSize()
      }
    },

    onDataUpdate () {
      this.updateSize()
    },

    computeSize (id) {
      this.$nextTick(() => {
        if (this.id === id) {
          const width = this.$el.offsetWidth
          const height = this.$el.offsetHeight
          this.applySize(width, height)
        }
        this.$_pendingSizeUpdate = null
      })
    },

    applySize (width, height) {
      const size = Math.round(this.vscrollParent.direction === 'vertical' ? height : width)
      if (size && this.size !== size) {
        if (this.vscrollParent.$_undefinedMap[this.id]) {
          this.vscrollParent.$_undefinedSizes--
          this.vscrollParent.$_undefinedMap[this.id] = undefined
        }
        this.$set(this.vscrollData.sizes, this.id, size)
        this.$set(this.vscrollData.validSizes, this.id, true)
        if (this.emitResize) this.$emit('resize', this.id)
      }
    },

    observeSize () {
      if (!this.vscrollResizeObserver) return
      this.vscrollResizeObserver.observe(this.$el.parentNode)
      this.$el.parentNode.addEventListener('resize', this.onResize)
    },

    unobserveSize () {
      if (!this.vscrollResizeObserver) return
      this.vscrollResizeObserver.unobserve(this.$el.parentNode)
      this.$el.parentNode.removeEventListener('resize', this.onResize)
    },

    onResize (event) {
      const { width, height } = event.detail.contentRect
      this.applySize(width, height)
    },
  },

  render (h) {
    return h(this.tag, this.$slots.default)
  },
}
</script>