svgstore.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. (function(){
  2. /*! svg4everybody v2.1.9 | github.com/jonathantneal/svg4everybody */
  3. function embed(parent, svg, target) {
  4. // if the target exists
  5. if (target) {
  6. // create a document fragment to hold the contents of the target
  7. var fragment = document.createDocumentFragment(), viewBox = !svg.hasAttribute("viewBox") && target.getAttribute("viewBox");
  8. // conditionally set the viewBox on the svg
  9. viewBox && svg.setAttribute("viewBox", viewBox);
  10. // copy the contents of the clone into the fragment
  11. for (// clone the target
  12. var clone = target.cloneNode(!0); clone.childNodes.length; ) {
  13. fragment.appendChild(clone.firstChild);
  14. }
  15. // append the fragment into the svg
  16. parent.appendChild(fragment);
  17. }
  18. }
  19. function loadreadystatechange(xhr) {
  20. // listen to changes in the request
  21. xhr.onreadystatechange = function() {
  22. // if the request is ready
  23. if (4 === xhr.readyState) {
  24. // get the cached html document
  25. var cachedDocument = xhr._cachedDocument;
  26. // ensure the cached html document based on the xhr response
  27. cachedDocument || (cachedDocument = xhr._cachedDocument = document.implementation.createHTMLDocument(""),
  28. cachedDocument.body.innerHTML = xhr.responseText, xhr._cachedTarget = {}), // clear the xhr embeds list and embed each item
  29. xhr._embeds.splice(0).map(function(item) {
  30. // get the cached target
  31. var target = xhr._cachedTarget[item.id];
  32. // ensure the cached target
  33. target || (target = xhr._cachedTarget[item.id] = cachedDocument.getElementById(item.id)),
  34. // embed the target into the svg
  35. embed(item.parent, item.svg, target);
  36. });
  37. }
  38. }, // test the ready state change immediately
  39. xhr.onreadystatechange();
  40. }
  41. function svg4everybody(rawopts) {
  42. function oninterval() {
  43. // while the index exists in the live <use> collection
  44. for (// get the cached <use> index
  45. var index = 0; index < uses.length; ) {
  46. // get the current <use>
  47. var use = uses[index], parent = use.parentNode, svg = getSVGAncestor(parent), src = use.getAttribute("xlink:href") || use.getAttribute("href");
  48. if (!src && opts.attributeName && (src = use.getAttribute(opts.attributeName)),
  49. svg && src) {
  50. if (polyfill) {
  51. if (!opts.validate || opts.validate(src, svg, use)) {
  52. // remove the <use> element
  53. parent.removeChild(use);
  54. // parse the src and get the url and id
  55. var srcSplit = src.split("#"), url = srcSplit.shift(), id = srcSplit.join("#");
  56. // if the link is external
  57. if (url.length) {
  58. // get the cached xhr request
  59. var xhr = requests[url];
  60. // ensure the xhr request exists
  61. xhr || (xhr = requests[url] = new XMLHttpRequest(), xhr.open("GET", url), xhr.send(),
  62. xhr._embeds = []), // add the svg and id as an item to the xhr embeds list
  63. xhr._embeds.push({
  64. parent: parent,
  65. svg: svg,
  66. id: id
  67. }), // prepare the xhr ready state change event
  68. loadreadystatechange(xhr);
  69. } else {
  70. // embed the local id into the svg
  71. embed(parent, svg, document.getElementById(id));
  72. }
  73. } else {
  74. // increase the index when the previous value was not "valid"
  75. ++index, ++numberOfSvgUseElementsToBypass;
  76. }
  77. }
  78. } else {
  79. // increase the index when the previous value was not "valid"
  80. ++index;
  81. }
  82. }
  83. // continue the interval
  84. (!uses.length || uses.length - numberOfSvgUseElementsToBypass > 0) && requestAnimationFrame(oninterval, 67);
  85. }
  86. var polyfill, opts = Object(rawopts), newerIEUA = /\bTrident\/[567]\b|\bMSIE (?:9|10)\.0\b/, webkitUA = /\bAppleWebKit\/(\d+)\b/, olderEdgeUA = /\bEdge\/12\.(\d+)\b/, edgeUA = /\bEdge\/.(\d+)\b/, inIframe = window.top !== window.self;
  87. polyfill = "polyfill" in opts ? opts.polyfill : newerIEUA.test(navigator.userAgent) || (navigator.userAgent.match(olderEdgeUA) || [])[1] < 10547 || (navigator.userAgent.match(webkitUA) || [])[1] < 537 || edgeUA.test(navigator.userAgent) && inIframe;
  88. // create xhr requests object
  89. var requests = {}, requestAnimationFrame = window.requestAnimationFrame || setTimeout, uses = document.getElementsByTagName("use"), numberOfSvgUseElementsToBypass = 0;
  90. // conditionally start the interval if the polyfill is active
  91. polyfill && oninterval();
  92. }
  93. function getSVGAncestor(node) {
  94. for (var svg = node; "svg" !== svg.nodeName.toLowerCase() && (svg = svg.parentNode); ) {}
  95. return svg;
  96. }
  97. svg4everybody();
  98. }());