-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprogressbar.js
367 lines (321 loc) · 10.2 KB
/
progressbar.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
People working in large organizations need this sort of statement to be able to include it in their applications, so here it goes:
Copyright (c) 2008, Daniel Barreiro (a.k.a. Satyam). All rights reserved.
satyam at satyam dot com dot ar (yes, it ends with ar)
It is the intention of the author to make this component freely available for use along the YAHOO User Interface Library
so it is licensed with a BSD License: http://developer.yahoo.net/yui/license.txt
developped along version: 3.0.0PR2 of YUI
*/
YUI.add('progressbar', function(Y) {
var PB = 'progressbar',
MASK = 'mask',
BAR = 'bar',
TL = 'tl',
TR = 'tr',
BL = 'bl',
BR = 'br',
ANIM = 'anim',
CONTENT_BOX = 'contentBox',
MASK_EL = 'maskEl',
BAR_EL = 'barEl',
getCN = Y.ClassNameManager.getClassName,
IMAGE = 'image',
C_MASK = getCN(PB,MASK),
C_BAR = getCN(PB,BAR),
C_TL = getCN(PB,TL),
C_TR = getCN(PB,TR),
C_BL = getCN(PB,BL),
C_BR = getCN(PB,BR),
C_ANIM = getCN(PB,ANIM),
BAR_MARKUP = '<div class="' + C_BAR + '"></div>',
MASK_MARKUP = '<table class="' +
C_MASK + '"><tr><td class="' + C_TL + '"></td><td class="' +
C_TR + '"></td></tr><tr><td class="' + C_BL + '"></td><td class="' +
C_BR + '"></td></tr></table>',
L = Y.Lang,
isArray = L.isArray,
isBoolean = L.isBoolean,
isString = L.isString,
isNumber = L.isNumber,
isNull = L.isNull;
var ProgressBar = function() {
// Y.log('creating new instance');
ProgressBar.superclass.constructor.apply(this, arguments);
};
Y.mix(ProgressBar,{
NAME:'progressbar',
ATTRS : {
barEl: {
//writeOnce: true,
value : null
},
maskEl: {
//writeOnce: true,
value : null
},
direction: {
//writeOnce: true,
value:'lr',
set: function (value) {
return this._setDirectionAtt(value);
},
validator: function (value) {
return this._validateDirectionAtt(value);
}
},
maxValue: {
value: 100,
validator: function (value) {
return this._validateMaxValueAtt(value);
}
},
minValue: {
value: 0,
validator: function (value) {
return this._validateMinValueAtt(value);
}
},
width: {
value: null,
set: function (value) {
return this._setWidthAtt(value);
}
},
height: {
value: null,
set: function (value) {
return this._setHeightAtt(value);
}
},
barColor: {
},
backColor: {
},
border: {
},
ariaText: {
value:'|'
},
value: {
value: 50,
validator: function (value) {
return this._validateValueAtt(value);
}
},
anim: {
//writeOnce:true,
value: {}
}
}
});
Y.extend(ProgressBar, Y.Widget, {
_previousValue: 50,
_tweenFactor: 1,
_barFactor:1,
_barSpace:0,
_rendered:false,
_anim:null,
initializer: function (config) {
// Y.log('initializer');
this.publish('startEvent', { emitFacade: false });
// If animation is loaded, this one will trigger for each frame of the animation providing partial values
this.publish('changingEvent', { emitFacade: false });
// This will fire at the end of the animation or immediately upon changing values if animation is not loaded
this.publish('completeEvent', { emitFacade: false });
// I'm listening to AttributeProvider's own attribute change events to adjust the bar according to the new sizes
this.after('valueChange',this._redraw);
this.after('minValueChange', this.redraw );
this.after('maxValueChange', this.redraw );
this.after('widthChange', this._onWidthChange);
this.after('heightChange',this._onHeightChange);
this.after('barColorChange', this._onBarColorChange);
this.after('backColorChange', this._onBackColorChange);
this.after('borderChange', this._onBorderChange);
},
renderer : function() {
// Y.log('renderer');
ProgressBar.superclass.renderer.apply(this, arguments);
},
renderUI: function () {
// Y.log('renderUI');
var cb = this.get(CONTENT_BOX);
this.set(BAR_EL, cb.appendChild(Y.Node.create(BAR_MARKUP)));
this.set(MASK_EL, cb.appendChild(Y.Node.create(MASK_MARKUP)));
if (isNull(this.get('width'))) {
this.set('width',cb.getComputedStyle('width'));
}
if (isNull(this.get('height'))) {
this.set('height',cb.getComputedStyle('height'));
}
this._rendered = true;
this._onWidthChange();
this._onHeightChange();
this._onBarColorChange();
this._onBackColorChange();
this._onBorderChange();
// If there is an instance of Animator available, I fire the events
var barEl =this.get(BAR_EL);
if (Y.Anim) {
barEl.plug(Y.plugin.NodeFX);
this._anim = barEl.fx;
this._anim.setAtts(this.get('anim'));
this._anim.after('tween',function (ev) {
this.fire('changingEvent', Math.floor(this._tweenFactor * this._anim.get('elapsedTime') + this._previousValue));
},this,true);
this._anim.after('end',function(ev) {
this.fire('completeEvent',this._previousValue = this.get('value'));
this.get(BAR_EL).removeClass(C_ANIM);
},this,true);
//this._anim.run();
}
},
/* The user does not interact with this control
bindUI: function () {
},
*/
syncUI: function() {
// Y.log('syncUI');
this.get(CONTENT_BOX).setAttribute('tabIndex',1).setAttribute('role','progressbar').setAttribute('aria-valuemin',this.get('minValue')).setAttribute('aria-valuemax',this.get('maxValue')).setAttribute('aria-valuenow',this.get('value')).setAttribute('aria-valuetext',this.get('ariaText').replace('|',this.get('value')));
this.redraw();
},
redraw: function () {
this._recalculateConstants();
this._redraw();
},
_redraw: function () {
var value = this.get('value');
// Y.log('set value: ' + value);
var pixelValue = (value - this._mn) * this._barFactor;
this.get(CONTENT_BOX).setAttribute('aria-valuenow',value).setAttribute('aria-valuetext',this.get('ariaText').replace('|',value));
this.fire('startEvent',this._previousValue);
var barEl = this.get(BAR_EL);
if (this._anim) {
barEl.addClass(C_ANIM);
this._tweenFactor = (value - this._previousValue) / (this._anim.get('duration') * 1000);
switch (this.get('direction')) {
case 'lr':
this._anim.set('to',{ width: pixelValue });
break;
case 'rl':
this._anim.set('to',{
width: pixelValue,
left: this._barSpace - pixelValue
}
);
break;
case 'tb':
this._anim.set('to',{
height: pixelValue
});
break;
case 'bt':
this._anim.set('to',{
height: pixelValue,
top: this._barSpace - pixelValue
}
);
break;
}
this._anim.run();
} else {
switch (this.get('direction')) {
case 'lr':
barEl.setStyle('width', pixelValue + 'px');
break;
case 'rl':
barEl.setStyle('width', pixelValue + 'px');
barEl.setStyle('left',(this._barSpace - pixelValue) + 'px');
break;
case 'tb':
barEl.setStyle('height', (pixelValue) + 'px');
break;
case 'bt':
barEl.setStyle('height', pixelValue + 'px');
barEl.setStyle('top', (this._barSpace - pixelValue) + 'px');
break;
}
// If the animation utility has not been loaded then changing the value will always complete immediately.
this.fire('completeEvent',value);
}
},
destructor: function() {
},
_recalculateConstants: function() {
this._mn = this.get('minValue');
var barEl = this.get(BAR_EL);
switch (this.get('direction')) {
case 'lr':
case 'rl':
this._barSpace = parseInt(this.get('width'),10) - parseInt(barEl.getComputedStyle('marginLeft'),10) - parseInt(barEl.getComputedStyle('marginRight'),10);
break;
case 'tb':
case 'bt':
this._barSpace = parseInt(this.get('height'),10) - parseInt(barEl.getComputedStyle('marginTop'),10) - parseInt(barEl.getComputedStyle('marginBottom'),10);
break;
}
this._barFactor = this._barSpace / (this.get('maxValue') - this._mn);
},
_onWidthChange: function() {
if (!this._rendered) {return;}
var value = this.get('width');
this.get(CONTENT_BOX).setStyle('width',value);
this.get(MASK_EL).setStyle('width', value);
this.get(BAR_EL).setStyle('width', value);
this.redraw();
},
_onHeightChange: function(ev) {
if (!this._rendered) {return;}
var value = this.get('height');
this.get(CONTENT_BOX).setStyle('height',value);
this.get(MASK_EL).setStyle('height', value);
this.get(BAR_EL).setStyle('height', value);
this.redraw();
},
_onBarColorChange: function() {
if (!this._rendered) {return;}
this.get(BAR_EL).setStyle('backgroundColor', this.get('barColor'));
},
_onBackColorChange:function(ev) {
if (!this._rendered) {return;}
this.get(CONTENT_BOX).setStyle('backgroundColor', this.get('backColor'));
},
_onBorderChange:function(ev) {
if (!this._rendered) {return;}
this.get(CONTENT_BOX).setStyle('border', this.get('border'));
},
_setDirectionAtt:function(value) {
return Y.Lang.trim(value.toLowerCase());
},
_validateDirectionAtt:function(value) {
switch (value) {
case 'lr':
case 'rl':
case 'tb':
case 'bt':
return true;
default:
return false;
}
},
_validateMaxValueAtt:isNumber,
_validateMinValueAtt:isNumber,
_setWidthAtt:function(value) {
if (isNumber(value)) {
value += 'px';
}
// Y.log('Setting width: ' + value);
return value;
},
_setHeightAtt:function(value) {
if (isNumber(value)) {
value += 'px';
}
// Y.log('Setting height: ' + value);
return value;
},
_validateValueAtt:function(value) {
return isNumber(value) && value >= this.get('minValue') && value <= this.get('maxValue');
}
});
Y.ProgressBar = ProgressBar;
}, '3.0.0pr2' ,{requires:['widget']});