yammat/static/js/barcode.js

66 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-07-18 23:00:54 +00:00
var barcodeBuf = ""
var barcodeShown = false
function showBarcode(text) {
if (!barcodeShown) {
document.getElementById('barcode').classList.add('shown')
2015-07-21 17:18:00 +00:00
barcodeShown = true
2015-07-18 23:00:54 +00:00
}
document.getElementById('barcodeContent').textContent = text
}
function hideBarcode() {
if (barcodeShown) {
document.getElementById('barcode').classList.remove('shown')
2015-07-21 17:18:00 +00:00
barcodeShown = false
}
2015-07-21 17:18:00 +00: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 07:14:38 +00:00
var focused = document.activeElement
2015-07-21 19:22:06 +00:00
if ( !focused || focused == document.body ) {
2015-07-21 07:14:38 +00:00
focused = null
2015-07-21 19:22:06 +00:00
} else if ( document.querySelector ) {
focused = document.querySelector( ":focus" )
2015-07-21 07:14:38 +00: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 07:14:38 +00: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()
2017-11-07 19:53:10 +00:00
} else if ( key && key != " " ) {
console.log( "some input: " + barcodeBuf + "[" + key + "] <= {" + event.keyCode + "}" )
2015-07-21 07:14:38 +00:00
barcodeBuf += key
showBarcode( barcodeBuf )
2015-07-21 07:14:38 +00:00
event.preventDefault()
2015-07-20 23:14:21 +00:00
}
2015-07-18 23:00:54 +00:00
}
}