-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
226 lines (172 loc) · 4.55 KB
/
app.js
File metadata and controls
226 lines (172 loc) · 4.55 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
220
221
222
223
224
225
226
// Path to colorscheme file
COLORS_PATH = 'colorscheme.json'
// Main
window.onload = function() {
// Store DOM Object
window.dom = {}
// DOM Access
dom.render = document.querySelector('#render')
dom.copy = document.querySelector('#copy')
// Factory object
var factory = new Factory()
// Get Input
dom.subject = document.querySelector('#subject')
dom.value = document.querySelector('#value')
dom.styles = document.querySelector('#stylenames')
dom.colors = document.querySelector('#colors')
dom.custom = document.querySelector('#custom')
dom.c_custom = document.querySelector('#c_custom')
// Link to function
dom.subject.oninput = newInput
dom.value.oninput = newInput
// Auto select for copying
dom.copy.onclick = function() {
this.select()
}
// Download all styles
factory.downloadAll(afterDownload)
// Download colorscheme
downloadColorscheme(buildColorsForm)
/*
* Functions
*/
function sanitize(s) {
return s.replace(/-/g, '').replace(/ /g, '_')
}
function getSubject() {
return sanitize(dom.subject.value || 'subject')
}
function getValue() {
return sanitize(dom.value.value || 'value')
}
function getStyle() {
var el = dom.styles.querySelector(':checked')
if (el) {
return el.value
} else {
return 'flat'
}
}
function getColor() {
var el = dom.colors.querySelector(':checked')
if (el) {
if (el.value == 'custom') {
// Custom color
if (dom.custom.value[0] == '#') {
return dom.custom.value
} else {
return '#' + dom.custom.value
}
} else {
// Colorscheme
return el.value
}
} else {
// No color
return null
}
}
function getColorName() {
var el = dom.colors.querySelector(':checked')
if (el) {
return el.dataset.colorname
} else {
return null
}
}
// Compile badge and show it to user
function updateBadge() {
if (!factory.ready()) return
// Get SVG
var style = getStyle()
var subject = getSubject()
var value = getValue()
var color = getColor()
var colorname = getColorName() || '0'
var html = factory.compileBadge(style, subject, value, color)
dom.render.innerHTML = html
// Update link
var link = ''
link = ' {
// Not default style
link += '?style=' + style
}
link += ')' // Don't forget to close tag
dom.copy.value = link
}
// After download
function afterDownload() {
buildStylesForm()
// Show badge style input
for (var i in STYLENAMES) {
var svg = factory.compileBadge(STYLENAMES[i], 'subject', 'value', '#4c1')
// Show style
document.querySelector('#b_' + STYLENAMES[i]).src = 'data:image/svg+xml;charset=utf-8,' + svg
}
// Update badge
updateBadge()
}
// Input event handler
function newInput() {
updateBadge()
}
// Download color scheme
function downloadColorscheme(callback) {
var r = new XMLHttpRequest()
r.open('GET', COLORS_PATH, true)
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return
// Parse colorscheme data
var colors = JSON.parse(r.responseText)
// Delete synonyme and basic gray
delete colors.grey
delete colors.gray
delete colors.lightgrey
// Callback with data
callback(colors)
}
r.send()
}
// Build styles radio input
function buildStylesForm() {
// Get template
var node = document.querySelector('#t_style')
// Compile template
var template = doT.template(node.innerHTML)
// Generate html from template
var html = template(STYLENAMES)
// Replace template script with html
node.parentNode.innerHTML = html
// Link click event
var children = dom.styles.childNodes
for (var i = 0 ; i < children.length ; ++i) {
children[i].onclick = updateBadge
}
updateBadge()
}
// Build color radio input
function buildColorsForm(colors) {
var node = document.querySelector('#t_color')
var template = doT.template(node.innerHTML)
var html = template(colors)
node.parentNode.innerHTML = html
var children = dom.colors.childNodes
for (var i = 0 ; i < children.length ; ++i) {
children[i].onclick = updateBadge
}
var cb = function() {
dom.c_custom.checked = true
if (dom.custom.value[0] == '#') {
dom.c_custom.dataset.colorname = dom.custom.value.substr(1)
} else {
dom.c_custom.dataset.colorname = dom.custom.value
}
updateBadge()
}
// Link custom input
dom.custom.oninput = cb
dom.custom.onfocus = cb
updateBadge()
}
}