forked from olton/metroui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.js
More file actions
206 lines (163 loc) · 5.82 KB
/
Copy pathprogress.js
File metadata and controls
206 lines (163 loc) · 5.82 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* global Metro */
(function(Metro, $) {
'use strict';
var ProgressDefaultConfig = {
progressDeferred: 0,
showValue: false,
valuePosition: "free", // center, free
showLabel: false,
labelPosition: "before", // before, after
labelTemplate: "",
value: 0,
buffer: 0,
type: "bar",
small: false,
clsBack: "",
clsBar: "",
clsBuffer: "",
clsValue: "",
clsLabel: "",
onValueChange: Metro.noop,
onBufferChange: Metro.noop,
onComplete: Metro.noop,
onBuffered: Metro.noop,
onProgressCreate: Metro.noop
};
Metro.progressSetup = function (options) {
ProgressDefaultConfig = $.extend({}, ProgressDefaultConfig, options);
};
if (typeof globalThis["metroProgressSetup"] !== undefined) {
Metro.progressSetup(globalThis["metroProgressSetup"]);
}
Metro.Component('progress', {
init: function( options, elem ) {
this._super(elem, options, ProgressDefaultConfig, {
value: 0,
buffer: 0
});
return this;
},
_create: function(){
var element = this.element, elem = this.elem, o = this.options;
var value;
if (typeof o.type === "string") o.type = o.type.toLowerCase();
element
.html("")
.addClass("progress");
function _progress(){
elem.innerHTML = `<div class="bar"></div>`
}
function _buffer(){
elem.innerHTML = `
<div class="bar"></div>
<div class="buffer"></div>
`
}
function _load(){
element.addClass("with-load");
elem.innerHTML = `
<div class="bar"></div>
<div class="buffer"></div>
<div class="load"></div>
`
}
function _line(){
element.addClass("line");
}
switch (o.type) {
case "buffer": _buffer(); break;
case "load": _load(); break;
case "line": _line(); break;
default: _progress();
}
if (o.type !== 'line') {
value = $("<span>").addClass("value").addClass(o.clsValue).appendTo(element);
if (o.valuePosition === "center") value.addClass("centered");
if (o.showValue === false) value.hide();
}
if (o.small === true) element.addClass("small");
element.addClass(o.clsBack);
element.find(".bar").addClass(o.clsBar);
element.find(".buffer").addClass(o.clsBuffer);
if (o.showLabel === true) {
var label = $("<span>").addClass("progress-label").addClass(o.clsLabel).html(o.labelTemplate === "" ? o.value+"%" : o.labelTemplate.replace("%VAL%", o.value));
if (o.labelPosition === 'before') {
label.insertBefore(element);
} else {
label.insertAfter(element);
}
}
this.val(o.value);
this.buff(o.buffer);
this._fireEvent("progress-create", {
element: element
});
},
val: function(v){
var that = this, element = this.element, o = this.options;
var value = element.find(".value");
if (v === undefined) {
return that.value;
}
var bar = element.find(".bar");
if (bar.length === 0) {
return false;
}
this.value = parseInt(v, 10);
bar.css("width", this.value + "%");
value.html(this.value+"%");
var diff = element.width() - bar.width();
var valuePosition = value.width() > diff ? {left: "auto", right: diff + 'px'} : {left: v + '%'};
if (o.valuePosition === "free") value.css(valuePosition);
if (o.showLabel === true) {
var label = element[o.labelPosition === "before" ? "prev" : "next"](".progress-label");
if (label.length) {
label.html(o.labelTemplate === "" ? o.value+"%" : o.labelTemplate.replace("%VAL%", o.value));
}
}
this._fireEvent("value-change", {
val: this.value
});
if (this.value === 100) {
this._fireEvent("complete", {
val: this.value
});
}
},
buff: function(v){
var that = this, element = this.element;
if (v === undefined) {
return that.buffer;
}
var bar = element.find(".buffer");
if (bar.length === 0) {
return false;
}
this.buffer = parseInt(v, 10);
bar.css("width", this.buffer + "%");
this._fireEvent("buffer-change", {
val: this.buffer
});
if (this.buffer === 100) {
this._fireEvent("buffered", {
val: this.buffer
});
}
},
changeValue: function(){
this.val(this.element.attr('data-value'));
},
changeBuffer: function(){
this.buff(this.element.attr('data-buffer'));
},
changeAttribute: function(attributeName){
switch (attributeName) {
case 'data-value': this.changeValue(); break;
case 'data-buffer': this.changeBuffer(); break;
}
},
destroy: function(){
return this.element;
}
});
}(Metro, m4q));