forked from frexy/svg-sprite-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolyfill.js
103 lines (93 loc) · 4.04 KB
/
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
(function (requestAnimationFrame, active) {
// create a sprite svg element and hide it somewhere
var createSpriteElement = function (url, svgText) {
var wrap = document.createElement('span');
wrap.innerHTML = svgText;
wrap.style.display = 'block';
wrap.style.height = 0;
wrap.style.width = 0;
// append
document.body.insertBefore(wrap, document.body.firstChild);
return;
};
// load sprite svg using AJAX
var loadSprite = function (file, cb) {
var x = new (XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
x.open('GET', file, 1);
//
x.onreadystatechange = function () {
if (x.readyState > 3) {
createSpriteElement(file, x.responseText);
cb();
}
return;
};
x.send();
return file;
};
var modifyElement = function (e, uri) {
return (function (symbol) {
if (!symbol) {
return void(0);
} else {
e.parentNode.setAttribute('viewBox', symbol.getAttribute('viewBox'));
e.parentNode.replaceChild((function (nodes, frag) {
Array.prototype.forEach.call(nodes, function (n) {
frag.appendChild(n.cloneNode(true));
});
return frag;
})(symbol.childNodes, document.createDocumentFragment()), e);
return void(0);
}
})(document.getElementById(uri));
};
var createScanner = function (loaded) {
return function () {
(function scanEle(n, l, counter) {
return counter >= n.length ?
(function () {
// setup next scanner
requestAnimationFrame(createScanner(l));
return void(0);
})() :
(function (href) {
return (href[0] == '#') ?
modifyElement(n[counter], href.substring(1)) :
(function (parts) {
return (function (uri, hash) {
// if the file hasn't been loaded before,
// load it into DOM
scanEle(n ,
l.indexOf(uri) > -1 ?
(function () {
modifyElement(n[counter], hash);
return l;
})() :
(function (e) {
return l.concat([
loadSprite(uri, function () {
// now modify the e element
return modifyElement(e, hash);
})
]);
})(n[counter]) ,
(counter + 1)
);
return void(0);
})(parts[0], parts[1]);
})(href.split('#'));
})(n[counter].getAttribute('xlink:href'));
})(document.getElementsByTagName('use'), loaded, 0);
};
};
return active ? createScanner([])() : void(0);
})(
(window.requestAnimationFrame || function (fn) {
window.setTimeout(fn, 300);
}),
(
/MSIE\b/.test(navigator.userAgent) ||
/Trident\b/.test(navigator.userAgent) ||
/AppleWebKit\/(\d+)/.test(navigator.userAgent)
)
);