|
@@ -26,50 +26,99 @@
|
26
|
26
|
var mainDoc = document;
|
27
|
27
|
var importDoc = document.currentScript.ownerDocument;
|
28
|
28
|
|
29
|
|
- var proto = Object.create(HTMLDivElement.prototype);
|
30
|
|
- proto.loadSvg = function (src, callback) {
|
31
|
|
- var isLocal = window.location.protocol === 'file:';
|
32
|
|
- var httpRequest = new XMLHttpRequest();
|
|
29
|
+ var svgCache = {};
|
|
30
|
+
|
|
31
|
+ function loadSvgCached(src) {
|
|
32
|
+ if (src === undefined || src === null || src === "") {
|
|
33
|
+ return Promise.resolve(null);
|
|
34
|
+ } else if (svgCache[src]) {
|
|
35
|
+ return svgCache[src];
|
|
36
|
+ } else {
|
|
37
|
+ var promise = loadSvg(src);
|
|
38
|
+ svgCache[src] = promise;
|
|
39
|
+ return promise;
|
|
40
|
+ }
|
|
41
|
+ }
|
|
42
|
+ function loadSvg(src) {
|
|
43
|
+ console.log("Loading " + src);
|
|
44
|
+ return new Promise(function(resolve, reject) {
|
33
|
45
|
|
34
|
|
- httpRequest.onreadystatechange = function () {
|
35
|
|
- if (httpRequest.readyState === 4) {
|
|
46
|
+ var httpRequest = new XMLHttpRequest();
|
36
|
47
|
|
37
|
|
- if (httpRequest.status === 404 || httpRequest.responseXML === null) {
|
38
|
|
- throw new Error("Error loading SVG");
|
39
|
|
- }
|
|
48
|
+ httpRequest.onreadystatechange = function () {
|
|
49
|
+ if (httpRequest.readyState === 4) {
|
|
50
|
+
|
|
51
|
+ if (httpRequest.status === 404 || httpRequest.responseXML === null) {
|
|
52
|
+ reject(Error("Error loading SVG from " + src));
|
|
53
|
+ }
|
40
|
54
|
|
41
|
|
- if (httpRequest.status === 200 || (isLocal && httpRequest.status === 0)) {
|
42
|
|
- if (httpRequest.responseXML instanceof Document) {
|
43
|
|
- callback(httpRequest.responseXML.children[0]);
|
|
55
|
+ if (httpRequest.status === 200) {
|
|
56
|
+ if (httpRequest.responseXML instanceof Document) {
|
|
57
|
+ var svg = httpRequest.responseXML.children[0];
|
|
58
|
+ cleanSvg(svg);
|
|
59
|
+ resolve(svg);
|
|
60
|
+ }
|
44
|
61
|
}
|
45
|
62
|
}
|
46
|
63
|
}
|
|
64
|
+
|
|
65
|
+ httpRequest.open('GET', src);
|
|
66
|
+ if (httpRequest.overrideMimeType) httpRequest.overrideMimeType('text/xml');
|
|
67
|
+ httpRequest.send();
|
|
68
|
+ });
|
|
69
|
+ }
|
|
70
|
+ function cleanSvg(svg) {
|
|
71
|
+ // Remove all whitespace
|
|
72
|
+ for (var i = 0; i < svg.childNodes.length; ++i) {
|
|
73
|
+ var child = svg.childNodes[i];
|
|
74
|
+ if (child.nodeName == "#text") {
|
|
75
|
+ svg.removeChild(child);
|
|
76
|
+ }
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ // Keep only the last path
|
|
80
|
+ while (svg.childNodes.length > 1) {
|
|
81
|
+ svg.removeChild(svg.firstChild);
|
47
|
82
|
}
|
48
|
83
|
|
49
|
|
- httpRequest.open('GET', src);
|
50
|
|
- if (httpRequest.overrideMimeType) httpRequest.overrideMimeType('text/xml');
|
51
|
|
- httpRequest.send();
|
|
84
|
+ // Remove the fill of the remaining paths
|
|
85
|
+ var path = svg.firstChild;
|
|
86
|
+ if (path) {
|
|
87
|
+ path.style.fill = "";
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ return svg;
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+ var proto = Object.create(HTMLDivElement.prototype);
|
|
95
|
+ proto.setSvg = function (svg) {
|
|
96
|
+ var root = this.shadowRoot;
|
|
97
|
+ var container = root.getElementById("icon-container");
|
|
98
|
+
|
|
99
|
+ // Remove the previous SVG
|
|
100
|
+ while (container.firstChild) {
|
|
101
|
+ container.removeChild(container.firstChild);
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ // Replace it with the new one
|
|
105
|
+ if (svg !== null) {
|
|
106
|
+ container.appendChild(svg.cloneNode(true));
|
|
107
|
+ }
|
52
|
108
|
}
|
53
|
109
|
Object.defineProperty(proto, "src", {
|
54
|
110
|
get: function() {
|
55
|
111
|
return "";
|
56
|
112
|
},
|
57
|
113
|
set: function (value) {
|
|
114
|
+ console.log("Property src=" + value);
|
58
|
115
|
this.setAttribute('src', value);
|
59
|
116
|
|
60
|
|
- var root = this.shadowRoot;
|
61
|
|
- var container = root.getElementById("icon-container");
|
62
|
|
- while (container.firstChild) {
|
63
|
|
- container.removeChild(container.firstChild);
|
64
|
|
- }
|
65
|
|
- this.loadSvg(value, function (svg) {
|
66
|
|
- container.appendChild(svg);
|
67
|
|
-
|
68
|
|
- // game-icons.net specific modifications
|
69
|
|
- var firstPath = root.querySelector("svg path");
|
70
|
|
- svg.removeChild(firstPath);
|
71
|
|
- var secondPath = root.querySelector("svg path");
|
72
|
|
- secondPath.style.fill = "";
|
|
117
|
+ var element = this;
|
|
118
|
+ loadSvgCached(value).then(function (svg) {
|
|
119
|
+ element.setSvg(svg);
|
|
120
|
+ }, function (error) {
|
|
121
|
+ // console.log(error);
|
73
|
122
|
});
|
74
|
123
|
}
|
75
|
124
|
});
|