Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

card-icon.html 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template id="card-icon">
  2. <style>
  3. #icon-container {
  4. height: 100%;
  5. background-color: inherit;
  6. }
  7. svg {
  8. height: 100%;
  9. }
  10. svg path {
  11. fill: rgb(255, 255, 255);
  12. }
  13. </style>
  14. <div id="icon-container">
  15. </div>
  16. </template>
  17. <script>
  18. (function(window, document) {
  19. var elemName = 'card-icon';
  20. var mainDoc = document;
  21. var importDoc = document.currentScript.ownerDocument;
  22. var svgCache = {};
  23. function loadSvgCached(src) {
  24. if (src === undefined || src === null || src === "") {
  25. return Promise.resolve(null);
  26. } else if (svgCache[src]) {
  27. return svgCache[src];
  28. } else {
  29. var promise = loadSvg(src);
  30. svgCache[src] = promise;
  31. return promise;
  32. }
  33. }
  34. function loadSvg(src) {
  35. // console.log("Loading " + src);
  36. return new Promise(function(resolve, reject) {
  37. var httpRequest = new XMLHttpRequest();
  38. httpRequest.onreadystatechange = function () {
  39. if (httpRequest.readyState === 4) {
  40. if (httpRequest.status === 404 || httpRequest.responseXML === null) {
  41. reject(Error("Error loading SVG from " + src));
  42. }
  43. if (httpRequest.status === 200) {
  44. if (httpRequest.responseXML instanceof Document) {
  45. var svg = httpRequest.responseXML.children[0];
  46. cleanSvg(svg);
  47. resolve(svg);
  48. }
  49. }
  50. }
  51. }
  52. httpRequest.open('GET', src);
  53. if (httpRequest.overrideMimeType) httpRequest.overrideMimeType('text/xml');
  54. httpRequest.send();
  55. });
  56. }
  57. function cleanSvg(svg) {
  58. // Remove all whitespace
  59. for (var i = 0; i < svg.childNodes.length; ++i) {
  60. var child = svg.childNodes[i];
  61. if (child.nodeName == "#text") {
  62. svg.removeChild(child);
  63. }
  64. }
  65. // Keep only the last path
  66. while (svg.childNodes.length > 1) {
  67. svg.removeChild(svg.firstChild);
  68. }
  69. // Remove the fill of the remaining paths
  70. var path = svg.firstChild;
  71. if (path) {
  72. path.style.fill = "";
  73. }
  74. return svg;
  75. }
  76. var proto = Object.create(HTMLDivElement.prototype);
  77. proto.setSvg = function (svg) {
  78. var root = this.shadowRoot;
  79. var container = root.getElementById("icon-container");
  80. // Remove the previous SVG
  81. while (container.firstChild) {
  82. container.removeChild(container.firstChild);
  83. }
  84. // Replace it with the new one
  85. if (svg !== null) {
  86. container.appendChild(svg.cloneNode(true));
  87. }
  88. }
  89. Object.defineProperty(proto, "src", {
  90. get: function() {
  91. return "";
  92. },
  93. set: function (value) {
  94. // console.log("Property src=" + value);
  95. this.setAttribute('src', value);
  96. var element = this;
  97. loadSvgCached(value).then(function (svg) {
  98. element.setSvg(svg);
  99. }, function (error) {
  100. // console.log(error);
  101. });
  102. }
  103. });
  104. proto.createdCallback = function () {
  105. var template = importDoc.getElementById(elemName);
  106. var clone = mainDoc.importNode(template.content, true);
  107. var root = this.createShadowRoot();
  108. root.appendChild(clone);
  109. this.src = this.getAttribute('src') || "";
  110. }
  111. proto.attributeChangedCallback = function (name, oldValue, newValue) {
  112. switch (name) {
  113. case "src": this.src = newValue; break;
  114. }
  115. }
  116. mainDoc.registerElement(elemName, { prototype: proto });
  117. })(window, document);
  118. </script>