-
Notifications
You must be signed in to change notification settings - Fork 23
/
plugin.js
159 lines (147 loc) · 4.03 KB
/
plugin.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const CSSOM = require('cssom')
const SimpleSVG = {
render (createElement) {
return createElement('svg')
},
name: 'simple-svg',
props: {
src: {
type: String,
required: true
},
fillClassName: {
type: String,
default: ''
},
fill: {
type: String,
default: ''
},
strokeClassName: {
type: String,
default: ''
},
stroke: {
type: String,
default: ''
},
width: {
type: String,
default: 'auto'
},
height: {
type: String,
default: 'auto'
},
customId: {
type: String,
default: ''
},
customClassName: {
type: String,
default: ''
}
},
mounted () {
this.generateInlineSVG()
},
watch: {
src (val) {
// Re-generate inline SVG if src is updated
this.generateInlineSVG()
},
fill (val) {
this.updateFillColor(val)
},
stroke (val) {
this.updateStrokeColor(val)
},
width (val) {
this.$el.style.width = val
},
height (val) {
this.$el.style.width = val
}
},
methods: {
/* Load an SVG file with XHR and generate an inline SVG code */
generateInlineSVG () {
const context = this
// Reset first. Remove all the code of the existing inline SVG
this.resetInlineSVG()
// Get the content of the SVG file
const request = new XMLHttpRequest()
request.open('GET', this.src, true)
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
// Setup a DOM parser to convert the response to text/xml
const domParser = new DOMParser()
const result = domParser.parseFromString(request.responseText, 'text/xml')
const loadedSVG = result.getElementsByTagName('svg')[0]
if (!loadedSVG) {
console.error('No SVG element found in the given file: ' + context.filepath)
return
}
// add attributes to the inline SVG
const attributeNames = loadedSVG.getAttributeNames()
for (const name of attributeNames) {
const value = loadedSVG.getAttribute(name)
context.$el.setAttribute(name, value)
}
if (context.customId) context.$el.id = context.customId
if (context.customClassName) context.$el.setAttribute('class', context.customClassName)
context.$el.style.width = context.width
context.$el.style.height = context.height
// add child nodes to the inline SVG
const domN = loadedSVG.children.length;
for (let i = domN - 1; i >= 0; i--) {
const node = loadedSVG.children.item(i)
context.$el.appendChild(node)
}
// set colors
context.updateFillColor(context.fill)
context.updateStrokeColor(context.stroke)
// now the inline SVG is generated
context.$emit('load')
} else {
console.error('There was an error retrieving the source of the SVG.')
}
}
request.onerror = function () {
console.error('There was on XML Http Request')
}
request.send()
},
resetInlineSVG () {
while (this.$el.firstChild) {
this.$el.removeChild(this.$el.firstChild)
}
const attributeNames = this.$el.getAttributeNames()
for (const name of attributeNames) {
this.$el.removeAttribute(name)
}
},
updateFillColor (fill) {
if (this.fillClassName) {
const matches = this.$el.getElementsByClassName(this.fillClassName)
for (const element of matches) {
element.style.fill = fill
}
}
},
updateStrokeColor (stroke) {
if (this.strokeClassName) {
const matches = this.$el.getElementsByClassName(this.strokeClassName)
for (const element of matches) {
element.style.stroke = stroke
}
}
}
}
}
const plugin = {
install (Vue, options) {
Vue.component('simple-svg', SimpleSVG)
}
}
export {plugin as default, SimpleSVG}