summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diffs/components/pre_renderer.vue
blob: e4320c40d2c8a59bb0a906a8bd464aa92fa138c8 (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
<script>
export default {
  inject: ['vscrollParent'],
  props: {
    maxLength: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      nextIndex: -1,
      nextItem: null,
      startedRender: false,
      width: 0,
    };
  },
  mounted() {
    this.width = this.$el.parentNode.offsetWidth;

    this.$_itemsWithSizeWatcher = this.$watch('vscrollParent.itemsWithSize', async () => {
      await this.$nextTick();

      const nextItem = this.findNextToRender();

      if (nextItem) {
        this.startedRender = true;
        requestIdleCallback(() => {
          this.nextItem = nextItem;

          if (this.nextIndex === this.maxLength - 1) {
            this.$nextTick(() => {
              if (this.vscrollParent.itemsWithSize[this.maxLength - 1].size !== 0) {
                this.clearRendering();
              }
            });
          }
        });
      } else if (this.startedRender) {
        this.clearRendering();
      }
    });
  },
  beforeDestroy() {
    this.$_itemsWithSizeWatcher();
  },
  methods: {
    clearRendering() {
      this.nextItem = null;

      if (this.maxLength === this.vscrollParent.itemsWithSize.length) {
        this.$_itemsWithSizeWatcher();
      }
    },
    findNextToRender() {
      return this.vscrollParent.itemsWithSize.find(({ size }, index) => {
        const isNext = size === 0;

        if (isNext) {
          this.nextIndex = index;
        }

        return isNext;
      });
    },
  },
};
</script>

<template>
  <div v-if="nextItem" :style="{ width: `${width}px` }" class="gl-absolute diff-file-offscreen">
    <slot
      v-bind="{ item: nextItem.item, index: nextIndex, active: true, itemWithSize: nextItem }"
    ></slot>
  </div>
</template>

<style scoped>
.diff-file-offscreen {
  top: -200%;
  left: -200%;
}
</style>