-
Notifications
You must be signed in to change notification settings - Fork 85
/
progress-polyfill.js
224 lines (179 loc) · 4.99 KB
/
progress-polyfill.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
/*
* <progress> polyfill
* Don't forget to also include progress-polyfill.css!
* @author Lea Verou http://leaverou.me
*/
(function(){
// Test browser support first
if('position' in document.createElement('progress')) {
return;
}
/**
* Private functions
*/
// Smoothen out differences between Object.defineProperty
// and __defineGetter__/__defineSetter__
var defineProperty, supportsEtters = true;
if(Object.defineProperty) {
// Changed to fix issue #3 https://github.com/LeaVerou/HTML5-Progress-polyfill/issues/3
defineProperty = function(o, property, etters) {
etters.enumerable = true;
etters.configurable = true;
try {
Object.defineProperty(o, property, etters);
} catch(e) {
if(e.number === -0x7FF5EC54) {
etters.enumerable = false;
Object.defineProperty(o, property, etters);
}
}
}
}
else {
if ('__defineSetter__' in document.body) {
defineProperty = function(o, property, etters) {
o.__defineGetter__(property, etters.get);
if(etters.set) {
o.__defineSetter__(property, etters.set);
}
};
}
else {
// Fallback to regular properties if getters/setters are not supported
defineProperty = function(o, property, etters) {
o[property] = etters.get.call(o);
},
supportsEtters = false;
}
}
try {
[].slice.apply(document.images)
var arr = function(collection) {
return [].slice.apply(collection);
}
} catch(e) {
var arr = function(collection) {
var ret = [], len = collection.length;
for(var i=0; i<len; i++) {
ret[i] = collection[i];
}
return ret;
}
}
// Does the browser use attributes as properties? (IE8- bug)
var attrsAsProps = (function(){
var e = document.createElement('div');
e.foo = 'bar';
return e.getAttribute('foo') === 'bar';
})();
var self = window.ProgressPolyfill = {
DOMInterface: {
max: {
get: function(){
return parseFloat(this.getAttribute('aria-valuemax')) || 1;
},
set: function(value) {
this.setAttribute('aria-valuemax', value);
if(!attrsAsProps) {
this.setAttribute('max', value);
}
self.redraw(this);
}
},
value: {
get: function(){
return parseFloat(this.getAttribute('aria-valuenow')) || 0;
},
set: function(value) {
value = Math.min(value, this.max);
this.setAttribute('aria-valuenow', value);
if(!attrsAsProps) {
this.setAttribute('value', value);
}
self.redraw(this);
}
},
position: {
get: function(){
return this.hasAttribute('aria-valuenow')? this.value/this.max : -1;
}
},
labels: {
get: function(){
var label = this.parentNode;
while(label && !/^label$/i.test(label.nodeName)) {
label = label.parentNode;
}
var labels = label? [label] : [];
if(this.id && document.querySelectorAll) {
var forLabels = arr(document.querySelectorAll('label[for="' + this.id + '"]'));
if(forLabels.length) {
labels = labels.concat(forLabels);
}
}
return labels;
}
}
},
redraw: function redraw(progress) {
if(!self.isInited(progress)) {
self.init(progress);
}
else if(!attrsAsProps) {
progress.setAttribute('aria-valuemax', parseFloat(progress.getAttribute('max')) || 1);
if(progress.hasAttribute('value')) {
progress.setAttribute('aria-valuenow', parseFloat(progress.getAttribute('value')) || 0);
}
else {
progress.removeAttribute('aria-valuenow');
}
}
if(progress.position !== -1) {
progress.style.paddingRight = progress.offsetWidth * (1-progress.position) + 'px';
}
},
isInited: function(progress) {
return progress.getAttribute('role') === 'progressbar';
},
init: function (progress) {
if(self.isInited(progress)) {
return; // Already init-ed
}
// Add ARIA
progress.setAttribute('role', 'progressbar');
progress.setAttribute('aria-valuemin', '0');
progress.setAttribute('aria-valuemax', parseFloat(progress.getAttribute('max')) || 1);
if(progress.hasAttribute('value')) {
progress.setAttribute('aria-valuenow', parseFloat(progress.getAttribute('value')) || 0);
}
// We can't add them on a prototype, as it's the same for all unknown elements
for(var attribute in self.DOMInterface) {
defineProperty(progress, attribute, {
get: self.DOMInterface[attribute].get,
set: self.DOMInterface[attribute].set
});
}
self.redraw(progress);
},
// Live NodeList, will update automatically
progresses: document.getElementsByTagName('progress')
};
for(var i=self.progresses.length-1; i>=0; i--) {
self.init(self.progresses[i]);
}
// Take care of future ones too, if supported
if(document.addEventListener) {
document.addEventListener('DOMAttrModified', function(evt) {
var node = evt.target, attribute = evt.attrName;
if(/^progress$/i.test(node.nodeName) && (attribute === 'max' || attribute === 'value')) {
self.redraw(node);
}
}, false);
document.addEventListener('DOMNodeInserted', function(evt) {
var node = evt.target;
if(/^progress$/i.test(node.nodeName)) {
self.init(node);
}
}, false);
}
})();