aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/components/loader.vue
blob: e28fabb8185438b75f112fe1a30201b6810a5241 (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
<script>
import Modal from './modal.vue'
import Spinner from './spinner.vue'

export default {
  name: 'Loader',
  components: { Modal, Spinner },
  emits: ['loaded'],
  props: {
    load: {
      type: Function,
      required: true
    },
    delay: {
      type: Number,
      default: 200
    },
    inline: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loaded: false,
      delayed: false
    }
  },
  mounted() {
    setTimeout(() => {
      if (!this.loaded) {
        this.delayed = true
      }
    }, this.delay)

    this.load().then(() => {
      this.$emit('loaded')
      this.loaded = true
    })
  }
}
</script>

<template>
  <div :style="{ display: inline ? 'inline' : 'block' }">
    <slot v-if="loaded" />
    <slot name="loading" v-else-if="delayed">
      <modal>
        <spinner style="color: white">
          <p>Waiting for API...</p>
        </spinner>
      </modal>
    </slot>
  </div>
</template>