Skip to content

Commit

Permalink
Extracted UI logic into controller.coffee
Browse files Browse the repository at this point in the history
  • Loading branch information
Dremora committed Jun 15, 2013
1 parent 99df70a commit 416d63f
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 130 deletions.
3 changes: 2 additions & 1 deletion Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ module.exports = (grunt) ->
files:
'barcode.js': 'barcode.coffee'
'barcodeCanvas.js': 'barcodeCanvas.coffee'
'controller.js': 'controller.coffee'

uglify:
build:
src: ['barcodeCanvas.js', 'barcode.js']
src: ['barcodeCanvas.js', 'barcode.js', 'controller.js']
dest: 'barcode-min.js'
)

Expand Down
2 changes: 1 addition & 1 deletion barcode-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 1 addition & 51 deletions barcode.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -187,54 +187,4 @@ class Barcode
group2 = (bits: @codes[digit][2], digit: digit for digit in @canonical[7..12])
[bits: '101'].concat(group1).concat([bits: '01010']).concat(group2).concat([bits: '101'])


timeout = null
barcode = new Barcode('')

barcode.onChange (value) -> $('#barcode').val(value)

barcode.onChange (value) ->
if !barcode.valid
$('#main').removeClass('barcode-shown')
$('#results').hide()
if barcode.empty
$('#error').hide()
else
$('#error').show()
else
$('#main').addClass('barcode-shown')
$('#error').hide()
canvas = document.getElementById('canvas')
barcodeToCanvas(barcode, canvas)
document.getElementById('image').src = canvas.toDataURL()
$('#results').show()
if barcode.validChecksum()
$('#checksum').text('valid').attr('class', 'valid')
else
$('#checksum').text('invalid').attr('class', 'invalid')
$('#canonical').text barcode.canonical
$('#country').text barcode.country()

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($('#barcode').val())

onHashChange = (hash) ->
clearTimeout timeout if timeout?
value = if hash then hash.split('#')[1] else ''
barcode.set(value)

$ ->
$('#barcode').bind event, onBarcodeChange for event in [
'keyup', 'keydown', 'paste', 'cut', 'change', 'search'
]
window.onhashchange = -> onHashChange(window.location.hash)
onHashChange(window.location.hash)
window.Barcode = Barcode
79 changes: 2 additions & 77 deletions barcode.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions controller.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
timeout = null
barcode = new Barcode('')

barcode.onChange (value) -> $('#barcode').val(value)

barcode.onChange (value) ->
if !barcode.valid
$('#main').removeClass('barcode-shown')
$('#results').hide()
if barcode.empty
$('#error').hide()
else
$('#error').show()
else
$('#main').addClass('barcode-shown')
$('#error').hide()
canvas = document.getElementById('canvas')
barcodeToCanvas(barcode, canvas)
document.getElementById('image').src = canvas.toDataURL()
$('#results').show()
if barcode.validChecksum()
$('#checksum').text('valid').attr('class', 'valid')
else
$('#checksum').text('invalid').attr('class', 'invalid')
$('#canonical').text barcode.canonical
$('#country').text barcode.country()

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($('#barcode').val())

onHashChange = (hash) ->
clearTimeout timeout if timeout?
value = if hash then hash.split('#')[1] else ''
barcode.set(value)

$ ->
$('#barcode').bind event, onBarcodeChange for event in [
'keyup', 'keydown', 'paste', 'cut', 'change', 'search'
]
window.onhashchange = -> onHashChange(window.location.hash)
onHashChange(window.location.hash)
Loading

0 comments on commit 416d63f

Please sign in to comment.