summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/LoadingBar.vue
blob: 342b638b51415a865ed24a754063ad038a93c87f (plain) (blame)
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
<template>
  <div v-if="loadingBar" class="loadingbar-wrapper">
    <div :class="{ 'loadingbar-background-wrapper': !transparent }">
      <div class="loadingbar-background" :class="color" :style="{ height: height + 'px' }"></div>
    </div>
    <div class="loadingbar" :class="color" :style="{ height: height + 'px', width: 'calc(100% / 100 * ' + loadingValue + ')'}"></div>
  </div>
</template>

<script>

export default {
  name: 'LoadingBar',
  props: {
    loading: {
      type: Boolean,
      default: false
    },
    height: {
      type: Number,
      default: 2
    },
    color: {
      type: String,
      default: 'primary'
    },
    transparent: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      loadingValue: 0,
      loadingBar: false,
      loadingTimeout: null
    }
  },
  watch: {
    loading: {
      immediate: true,
      handler (value) {
        clearTimeout(this.loadingTimeout)
        if (value) {
          this.loadingValue = 0
          this.loadingBar = true
          this.loadingTimeout = setTimeout(this.increaseLoadingValue, 10)
        } else {
          this.loadingValue = 100
          this.loadingTimeout = setTimeout(() => { this.loadingBar = false }, 500)
        }
      }
    }
  },
  methods: {
    increaseLoadingValue () {
      if (this.loading && this.loadingValue <= 85) {
        this.loadingValue += 10
        this.loadingTimeout = setTimeout(this.increaseLoadingValue, 200)
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.loadingbar-wrapper {
  width: 100%;
  height: 0;
  z-index: 1;
  position: relative;
}

.loadingbar-background {
  width: 100%;
  height: 100%;
  opacity: 0.3;
}

.loadingbar {
  top: 0;
  left: 0;
  position: absolute;
  transition: width .2s linear ;
}

.theme--dark .loadingbar-background-wrapper {
  background-color: #424242;
}

.theme--light .loadingbar-background-wrapper {
  background-color: #fff;
}
</style>