Blame view

src/components/loading-bar/index.js 2.31 KB
9dde24b6   梁灏   add LoadingBar co...
1
2
3
4
  import LoadingBar from './loading-bar';
  
  let loadingBarInstance;
  let color = 'primary';
c82c4bb8   pyyzcwg2833   feat(loading-bar)...
5
  let duration = 800;
9dde24b6   梁灏   add LoadingBar co...
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
  let failedColor = 'error';
  let height = 2;
  let timer;
  
  function getLoadingBarInstance () {
      loadingBarInstance = loadingBarInstance || LoadingBar.newInstance({
          color: color,
          failedColor: failedColor,
          height: height
      });
  
      return loadingBarInstance;
  }
  
  function update(options) {
      let instance  = getLoadingBarInstance();
  
      instance.update(options);
  }
  
  function hide() {
      setTimeout(() => {
          update({
              show: false
          });
          setTimeout(() => {
              update({
                  percent: 0
              });
b0893113   jingsam   :art: add eslint
35
          }, 200);
c82c4bb8   pyyzcwg2833   feat(loading-bar)...
36
      }, duration);
9dde24b6   梁灏   add LoadingBar co...
37
38
39
40
41
42
43
44
45
46
47
  }
  
  function clearTimer() {
      if (timer) {
          clearInterval(timer);
          timer = null;
      }
  }
  
  export default {
      start () {
6e572d44   梁灏   fixed #133
48
49
          if (timer) return;
  
9dde24b6   梁灏   add LoadingBar co...
50
51
52
53
54
55
56
57
58
          let percent = 0;
  
          update({
              percent: percent,
              status: 'success',
              show: true
          });
  
          timer = setInterval(() => {
a35fb299   梁灏   update LoadingBar...
59
              percent += Math.floor(Math.random () * 3 + 1);
9dde24b6   梁灏   add LoadingBar co...
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
              if (percent > 95) {
                  clearTimer();
              }
              update({
                  percent: percent,
                  status: 'success',
                  show: true
              });
          }, 200);
      },
      update (percent) {
          clearTimer();
          update({
              percent: percent,
              status: 'success',
              show: true
          });
      },
      finish () {
          clearTimer();
          update({
              percent: 100,
              status: 'success',
              show: true
          });
          hide();
      },
      error () {
          clearTimer();
          update({
              percent: 100,
              status: 'error',
              show: true
          });
          hide();
      },
      config (options) {
          if (options.color) {
              color = options.color;
          }
c82c4bb8   pyyzcwg2833   feat(loading-bar)...
100
101
102
          if (options.duration) {
              duration = options.duration;
          }
9dde24b6   梁灏   add LoadingBar co...
103
104
105
106
107
108
109
110
111
112
113
114
115
          if (options.failedColor) {
              failedColor = options.failedColor;
          }
          if (options.height) {
              height = options.height;
          }
      },
      destroy () {
          clearTimer();
          let instance = getLoadingBarInstance();
          loadingBarInstance = null;
          instance.destroy();
      }
b0893113   jingsam   :art: add eslint
116
  };