-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
79 lines (74 loc) · 1.89 KB
/
index.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
import { observe } from 'selector-observer'
import next from './Arrow-right.svg'
import prev from './Arrow-left.svg'
import calendar from './Calendar.svg'
import check from './Check.svg'
import chevron from './Chevron.svg'
import close from './Close.svg'
import document from './Document.svg'
import alert from './Alert.svg'
import pencil from './Pencil.svg'
import plus from './Plus.svg'
import square from './Square.svg'
const icons = {
active: pencil,
add: plus,
calendar,
check,
chevron,
close,
delete: close,
document,
error: alert,
alert,
next,
pencil,
plus,
prev,
square
}
export default icons
export { getIcon, observeIcons }
function getIcon (name) {
return icons[name]
}
function observeIcons () {
observe('i.fa', {
add (el) {
const match = el.className.match(/\bfa-([-\w]+)\b/)
if (match) {
const name = match[1]
if (name in icons) {
el.classList.remove('fa', `fa-${name}`)
if (el.parentNode.classList.contains('input-group-text')) {
el.classList.add('d-flex')
}
el.setAttribute('data-icon', name)
} else {
console.warn('no such icon: "%s"', name, el)
}
}
}
})
observe('[data-icon]', {
add (el) {
const icon = getIcon(el.getAttribute('data-icon'))
if (icon) {
el.classList.add('notranslate')
el.innerHTML = icon
const svg = el.getElementsByTagName('svg')[0]
const viewBox = svg.getAttribute('viewBox') || ''
const defaultHeight = viewBox.split(' ')[3] || 20
const desired = {
width: el.getAttribute('data-width'),
height: el.getAttribute('data-height') || defaultHeight
}
if (desired.width) {
svg.setAttribute('width', desired.width)
} else if (desired.height) {
svg.setAttribute('height', desired.height)
}
}
}
})
}