yammat/static/js/barcode.js

66 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-07-19 01:00:54 +02:00
var barcodeBuf = ""
var barcodeShown = false
function showBarcode(text) {
if (!barcodeShown) {
document.getElementById('barcode').classList.add('shown')
2015-07-21 19:18:00 +02:00
barcodeShown = true
2015-07-19 01:00:54 +02:00
}
document.getElementById('barcodeContent').textContent = text
}
function hideBarcode() {
if (barcodeShown) {
document.getElementById('barcode').classList.remove('shown')
2015-07-21 19:18:00 +02:00
barcodeShown = false
}
2015-07-21 19:18:00 +02:00
return ""
}
function barcodeKeyPress( event ) { // takes input from either keyboard or barcode scanner
var key = String.fromCharCode( event.charCode )
var input = document.getElementById( 'crement' )
var inputcount = document.querySelectorAll( '[type="text"]' ).length + document.querySelectorAll( '[type="number"]' ).length
if ( input && inputcount == 1 ) { // focus in 'crement' when no other input fields only
input.focus()
}
2015-07-21 09:14:38 +02:00
var focused = document.activeElement
2015-07-21 21:22:06 +02:00
if ( !focused || focused == document.body ) {
2015-07-21 09:14:38 +02:00
focused = null
2015-07-21 21:22:06 +02:00
} else if ( document.querySelector ) {
focused = document.querySelector( ":focus" )
2015-07-21 09:14:38 +02:00
}
if (
!event.ctrlKey && !event.altKey // no hotkeys used
&& ( focused == null || focused.tagName != "INPUT" ) // focus not in input fielf for manual input
) {
if ( event.keyCode === 13 ) { // carriage return
var input = document.getElementById( 'barcodeInput' )
if ( input && barcodeBuf.length > 0 ) {
input.setAttribute( 'value', barcodeBuf )
2015-07-21 09:14:38 +02:00
input.parentNode.submit()
return
}
barcodeBuf = ""
event.preventDefault()
} else if ( event.keyCode === 27 ) { // escape
console.log( "escape" )
barcodeBuf=hideBarcode()
event.preventDefault()
} else if ( event.keyCode === 8 ) { // backspace
console.log( "backspace" )
barcodeBuf = barcodeBuf.substring( 0, barcodeBuf.length - 1 )
showBarcode( barcodeBuf )
if ( barcodeBuf.length <= 0 ) {
barcodeBuf = hideBarcode()
}
event.preventDefault()
} else if ( event.keyCode == 0 ) { // e.g. F-Keys are 112 to 123, A-Za-z0-9 all are 0.
console.log( "some input: " + barcodeBuf + "[" + key + "] <= {" + event.keyCode + "}" )
2015-07-21 09:14:38 +02:00
barcodeBuf += key
showBarcode( barcodeBuf )
2015-07-21 09:14:38 +02:00
event.preventDefault()
2015-07-21 01:14:21 +02:00
}
2015-07-19 01:00:54 +02:00
}
}