-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (48 loc) · 1.38 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
var hash = location.hash.slice(1)
if (hash) {
var params = hash.split('&').sort().map(function(param) {
return decodeURIComponent(param.replace(/\w*=/, ''))
})
showSection('message')
showMessage(params[1], params[0])
} else {
showSection('form')
}
// Show section
function showSection(section) {
$('[data-section=' + section + ']').hidden = false
$('[data-section]:not([data-section=' + section + '])').hidden = true
}
// Handle form
$('form').addEventListener('submit', function(e) {
e.preventDefault()
var msg = $('textarea').value
var flags = $('select').value
showMessage(msg, flags)
})
// Show message
function showMessage(msg, flags) {
$('.message').textContent = msg
var $flags = $('.flag', true)
var i = $flags.length
while (i--) $flags[i].remove()
i = 0
while (i++ < flags) addFlag(i / flags <= .5 ? 'left' : 'right')
// Change the hash for sharing
location.hash = 'msg=' + encodeURIComponent(msg) + '&flags=' + flags
showSection('message')
}
function addFlag(side) {
flag = document.createElement('div')
flag.className = 'flag'
$('[data-side=' + side + ']').appendChild(flag)
}
// Reset form
$('.reset').addEventListener('click', function(e) {
showSection('form')
})
// jQuery
function $(sel, all) {
var method = all ? 'querySelectorAll' : 'querySelector'
return document[method](sel)
}