-
Notifications
You must be signed in to change notification settings - Fork 0
/
Progress.js
82 lines (65 loc) · 1.96 KB
/
Progress.js
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
/*
Copyright 2017 Marcel Greter
https://www.github.com/mgreter
*/
// private scope
(function (jQuery, THREE, THREEAPP)
{
"use strict";
var Progress = THREEAPP.Class.create('Progress', null, ['Plugin'])
.ctor(function (app) {
// create the main container
this.el = jQuery('<div id="progressbar"/>');
this.wrapper = jQuery('<div class="indicator"/>').appendTo(this.el);
this.indicator = jQuery('<div class="bg"/>').appendTo(this.wrapper);
this.text = jQuery('<div class="text"/>').appendTo(this.el);
// append it to the viewport
this.el.appendTo(app.viewport);
// threshold for new loads
this.thresholdFiles = 0;
this.thresholdBytes = 0;
// initialize stopped
this.stopped = true;
// hide main element
this.el.hide();
// just a sample how it's done
this.indicator.css('width', 0 + '%');
var _this = this;
app.listen('fetch.progress', function (stats) {
if (_this.stopped) {
if (_this.stopped !== true) {
clearTimeout(_this.stopped);
}
_this.el.fadeIn(200);
_this.stopped = false;
}
var txt = sprintf("%d/%d (%S/%S)",
stats.filesLoaded, stats.filesTotal,
stats.bytesLoaded - _this.thresholdBytes,
stats.bytesTotal - _this.thresholdBytes
);
var p = (stats.bytesLoaded - _this.thresholdBytes) /
(stats.bytesTotal - _this.thresholdBytes);
_this.indicator.css('width', (p * 100) + '%');
_this.text.html(txt);
})
app.listen('fetch.complete', function (stats) {
_this.thresholdBytes = stats.bytesLoaded;
_this.thresholdFiles = stats.filesLoaded;
// schedule the timeout for the closer
_this.stopped = setTimeout(function () {
_this.el.fadeOut(600, function () {
_this.stopped = true;
});
}, 1000);
})
})
.init(function () {
})
.ready(function () {
})
;
// assign class to global namespace
THREEAPP('Plugin.Progress', Progress);
// EO private scope
})(jQuery, THREE, THREEAPP);