forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQIcon.js
More file actions
219 lines (187 loc) · 5.01 KB
/
QIcon.js
File metadata and controls
219 lines (187 loc) · 5.01 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import Vue from 'vue'
import SizeMixin from '../../mixins/size.js'
import TagMixin from '../../mixins/tag.js'
import ListenersMixin from '../../mixins/listeners.js'
import { slot, mergeSlot } from '../../utils/slot.js'
const defaultViewBox = '0 0 24 24'
const sameFn = i => i
const ionFn = i => `ionicons ${i}`
const libMap = {
'icon-': sameFn, // fontawesome equiv
'bt-': i => `bt ${i}`,
'eva-': i => `eva ${i}`,
'ion-md': ionFn,
'ion-ios': ionFn,
'ion-logo': ionFn,
'mdi-': i => `mdi ${i}`,
'iconfont ': sameFn,
'ti-': i => `themify-icon ${i}`,
'bi-': i => `bootstrap-icons ${i}`
}
const matMap = {
o_: '-outlined',
r_: '-round',
s_: '-sharp'
}
const libRE = new RegExp('^(' + Object.keys(libMap).join('|') + ')')
const matRE = new RegExp('^(' + Object.keys(matMap).join('|') + ')')
const mRE = /^[Mm]\s?[-+]?\.?\d/
const imgRE = /^img:/
const svgUseRE = /^svguse:/
const ionRE = /^ion-/
const faLaRE = /^[lf]a[srlbdk]? /
const fa6RE = /^fa-(brand|regular|solid)/
export default Vue.extend({
name: 'QIcon',
mixins: [ ListenersMixin, SizeMixin, TagMixin ],
props: {
tag: {
type: String,
default: 'i'
},
name: String,
color: String,
left: Boolean,
right: Boolean
},
computed: {
classes () {
return 'q-icon' +
(this.left === true ? ' on-left' : '') +
(this.right === true ? ' on-right' : '') +
(this.color !== void 0 ? ` text-${this.color}` : '')
},
type () {
let cls
let icon = this.name
if (icon === 'none' || !icon) {
return { none: true }
}
if (this.$q.iconMapFn !== void 0) {
const res = this.$q.iconMapFn(icon)
if (res !== void 0) {
if (res.icon !== void 0) {
icon = res.icon
if (icon === 'none' || !icon) {
return { none: true }
}
}
else {
return {
cls: res.cls,
content: res.content !== void 0
? res.content
: ' '
}
}
}
}
if (mRE.test(icon) === true) {
const [ def, viewBox = defaultViewBox ] = icon.split('|')
return {
svg: true,
viewBox,
nodes: def.split('&&').map(path => {
const [ d, style, transform ] = path.split('@@')
return this.$createElement('path', {
attrs: { d, transform },
style
})
})
}
}
if (imgRE.test(icon) === true) {
return {
img: true,
src: icon.substring(4)
}
}
if (svgUseRE.test(icon) === true) {
const [ def, viewBox = defaultViewBox ] = icon.split('|')
return {
svguse: true,
src: def.substring(7),
viewBox
}
}
let content = ' '
const matches = icon.match(libRE)
if (matches !== null) {
cls = libMap[ matches[ 1 ] ](icon)
}
else if (faLaRE.test(icon) === true || fa6RE.test(icon) === true) {
cls = icon
}
else if (ionRE.test(icon) === true) {
cls = `ionicons ion-${this.$q.platform.is.ios === true ? 'ios' : 'md'}${icon.substr(3)}`
}
else {
// "notranslate" class is for Google Translate
// to avoid tampering with Material Icons ligature font
//
// Caution: To be able to add suffix to the class name,
// keep the 'material-icons' at the end of the string.
cls = 'notranslate material-icons'
const matches = icon.match(matRE)
if (matches !== null) {
icon = icon.substring(2)
cls += matMap[ matches[ 1 ] ]
}
content = icon
}
return {
cls,
content
}
}
},
render (h) {
const data = {
class: this.classes,
style: this.sizeStyle,
on: { ...this.qListeners },
attrs: {
'aria-hidden': 'true',
role: 'presentation'
}
}
if (this.type.none === true) {
return h(this.tag, data, slot(this, 'default'))
}
if (this.type.img === true) {
return h('span', data, mergeSlot([
h('img', {
attrs: { src: this.type.src }
})
], this, 'default'))
}
if (this.type.svg === true) {
return h('span', data, mergeSlot([
h('svg', {
attrs: {
viewBox: this.type.viewBox,
focusable: 'false' /* needed for IE11 */
}
}, this.type.nodes)
], this, 'default'))
}
if (this.type.svguse === true) {
return h('span', data, mergeSlot([
h('svg', {
attrs: {
viewBox: this.type.viewBox,
focusable: 'false' /* needed for IE11 */
}
}, [
h('use', { attrs: { 'xlink:href': this.type.src } })
])
], this, 'default'))
}
if (this.type.cls !== void 0) {
data.class += ' ' + this.type.cls
}
return h(this.tag, data, mergeSlot([
this.type.content
], this, 'default'))
}
})