|
13 | 13 | <script> |
14 | 14 | import theme from '../../core/components/mdTheme/mixin'; |
15 | 15 |
|
| 16 | + let registeredIcons = {}; |
| 17 | +
|
16 | 18 | export default { |
17 | 19 | props: { |
18 | 20 | mdSrc: String |
|
36 | 38 | isSVG(mimetype) { |
37 | 39 | return mimetype.indexOf('svg') >= 0; |
38 | 40 | }, |
| 41 | + setSVGContent(value) { |
| 42 | + this.svgContent = value; |
| 43 | +
|
| 44 | + this.$nextTick(() => { |
| 45 | + this.$el.children[0].removeAttribute('fill'); |
| 46 | + }); |
| 47 | + }, |
39 | 48 | loadSVG() { |
40 | | - const request = new XMLHttpRequest(); |
41 | | - const self = this; |
| 49 | + if (!registeredIcons[this.mdSrc]) { |
| 50 | + const request = new XMLHttpRequest(); |
| 51 | + const self = this; |
42 | 52 |
|
43 | | - request.open('GET', this.mdSrc, true); |
| 53 | + request.open('GET', this.mdSrc, true); |
44 | 54 |
|
45 | | - request.onload = function() { |
46 | | - const mimetype = this.getResponseHeader('content-type'); |
| 55 | + request.onload = function() { |
| 56 | + const mimetype = this.getResponseHeader('content-type'); |
47 | 57 |
|
48 | | - if (this.status >= 200 && this.status < 400 && self.isImage(mimetype)) { |
49 | | - if (self.isSVG(mimetype)) { |
50 | | - self.svgContent = this.response; |
51 | | - window.setTimeout(() => { |
52 | | - self.$el.children[0].removeAttribute('fill'); |
53 | | - }, 0); |
| 58 | + if (this.status >= 200 && this.status < 400 && self.isImage(mimetype)) { |
| 59 | + if (self.isSVG(mimetype)) { |
| 60 | + registeredIcons[self.mdSrc] = this.response; |
| 61 | + self.setSVGContent(this.response); |
| 62 | + } else { |
| 63 | + self.loadImage(); |
| 64 | + } |
54 | 65 | } else { |
55 | | - this.loadImage(); |
| 66 | + console.warn(`The file ${self.mdSrc} is not a valid image.`); |
56 | 67 | } |
57 | | - } else { |
58 | | - console.warn(`The file ${this.mdSrc} is not a valid image.`); |
59 | | - } |
60 | | - }; |
| 68 | + }; |
61 | 69 |
|
62 | | - request.send(); |
| 70 | + request.send(); |
| 71 | + } else { |
| 72 | + this.setSVGContent(registeredIcons[this.mdSrc]); |
| 73 | + } |
63 | 74 | }, |
64 | 75 | loadImage() { |
65 | 76 | this.imageSrc = this.mdSrc; |
|
0 commit comments