Skip to content

Commit 9332f3c

Browse files
committed
Add polyfills for older browsers.
requestAnimationFrame and btoa.
1 parent dd71b40 commit 9332f3c

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

docs/assets/js/vendor/base64.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
;(function () {
2+
3+
var object =
4+
typeof exports != 'undefined' ? exports :
5+
typeof self != 'undefined' ? self : // #8: web workers
6+
$.global; // #31: ExtendScript
7+
8+
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
9+
10+
function InvalidCharacterError(message) {
11+
this.message = message;
12+
}
13+
InvalidCharacterError.prototype = new Error;
14+
InvalidCharacterError.prototype.name = 'InvalidCharacterError';
15+
16+
// encoder
17+
// [https://gist.github.com/999166] by [https://github.com/nignag]
18+
object.btoa || (
19+
object.btoa = function (input) {
20+
var str = String(input);
21+
for (
22+
// initialize result and counter
23+
var block, charCode, idx = 0, map = chars, output = '';
24+
// if the next str index does not exist:
25+
// change the mapping table to "="
26+
// check if d has no fractional digits
27+
str.charAt(idx | 0) || (map = '=', idx % 1);
28+
// "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
29+
output += map.charAt(63 & block >> 8 - idx % 1 * 8)
30+
) {
31+
charCode = str.charCodeAt(idx += 3/4);
32+
if (charCode > 0xFF) {
33+
throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
34+
}
35+
block = block << 8 | charCode;
36+
}
37+
return output;
38+
});
39+
40+
// decoder
41+
// [https://gist.github.com/1020396] by [https://github.com/atk]
42+
object.atob || (
43+
object.atob = function (input) {
44+
var str = String(input).replace(/[=]+$/, ''); // #31: ExtendScript bad parse of /=
45+
if (str.length % 4 == 1) {
46+
throw new InvalidCharacterError("'atob' failed: The string to be decoded is not correctly encoded.");
47+
}
48+
for (
49+
// initialize result and counters
50+
var bc = 0, bs, buffer, idx = 0, output = '';
51+
// get next character
52+
buffer = str.charAt(idx++);
53+
// character found in table? initialize bit storage and add its ascii value;
54+
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
55+
// and if not first of each 4 characters,
56+
// convert the first 8 bits to one ascii character
57+
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
58+
) {
59+
// try to find character in table (0-63, not found => -1)
60+
buffer = chars.indexOf(buffer);
61+
}
62+
return output;
63+
});
64+
65+
}());
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://gist.github.com/paulirish/1579671
2+
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
3+
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
4+
5+
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
6+
7+
// MIT license
8+
9+
(function() {
10+
var lastTime = 0;
11+
var vendors = ['ms', 'moz', 'webkit', 'o'];
12+
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
13+
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
14+
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
15+
|| window[vendors[x]+'CancelRequestAnimationFrame'];
16+
}
17+
18+
if (!window.requestAnimationFrame)
19+
window.requestAnimationFrame = function(callback, element) {
20+
var currTime = new Date().getTime();
21+
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
22+
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
23+
timeToCall);
24+
lastTime = currTime + timeToCall;
25+
return id;
26+
};
27+
28+
if (!window.cancelAnimationFrame)
29+
window.cancelAnimationFrame = function(id) {
30+
clearTimeout(id);
31+
};
32+
}());

grunt/configBridge.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"../assets/js/src/customizer.js"
1212
],
1313
"docsJs": [
14+
"../assets/js/vendor/requestAnimationFrame-polyfill.js",
15+
"../assets/js/vendor/base64.js",
1416
"../assets/js/vendor/holder.min.js",
1517
"../assets/js/vendor/clipboard.min.js",
1618
"../assets/js/vendor/anchor.min.js",

0 commit comments

Comments
 (0)