-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.coffee
64 lines (55 loc) · 2.23 KB
/
controller.coffee
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
timeout = null
barcode = new Barcode('')
barcode.onChange (value) ->
if document.getElementById('barcode').value != value
document.getElementById('barcode').value = value
barcode.onChange (value) ->
if !barcode.valid
document.getElementById('main').classList.remove('barcode-shown')
document.getElementById('results').style.display = 'none'
if barcode.empty
document.getElementById('error').style.display = 'none'
else
document.getElementById('error').style.display = 'block'
else
document.getElementById('main').classList.add('barcode-shown')
document.getElementById('error').style.display = 'none'
canvas = document.getElementById('canvas')
options = {}
if !barcode.isChecksumValid()
options.black = 'rgb(180, 102, 96)'
barcodeToCanvas(barcode, canvas, options)
document.getElementById('image').src = canvas.toDataURL()
document.getElementById('results').style.display = 'block'
$checksum = document.getElementById('checksum')
if barcode.isChecksumValid()
$checksum.style.display = 'none'
else
$checksum.textContent = 'fix checksum'
$checksum.href = '#' + barcode.withValidChecksum()
$checksum.style.display = ''
document.getElementById('country').textContent = barcode.country()
if value == barcode.canonical
document.getElementById('show-ean-link').style.display = 'none'
else
document.getElementById('show-ean-link').textContent = 'show EAN'
document.getElementById('show-ean-link').href = '#' + barcode.canonical
document.getElementById('show-ean-link').style.display = ''
barcode.onChange (value) ->
clearTimeout timeout if timeout?
newValue = value
if barcode.valid || barcode.empty
timeout = setTimeout ->
window.location.hash = value
, 300
onBarcodeChange = ->
clearTimeout timeout if timeout?
barcode.set(document.getElementById('barcode').value)
onHashChange = (hash) ->
clearTimeout timeout if timeout?
value = if hash then hash.split('#')[1] else ''
barcode.set(value)
document.getElementById('barcode').addEventListener('input', onBarcodeChange)
window.onhashchange = -> onHashChange(window.location.hash)
onHashChange(window.location.hash)
document.body.classList.remove('preload')