From 70f6154a88f0dc3ecc9f3d43f26269f5a299c57d Mon Sep 17 00:00:00 2001 From: s72785 Date: Tue, 21 Jul 2015 14:14:47 +0200 Subject: [PATCH] enable backspace and escape, disable overlay when either escape is used or barcode is empty after hitting backspace --- static/js/barcode.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/static/js/barcode.js b/static/js/barcode.js index 0daad22..18e9403 100644 --- a/static/js/barcode.js +++ b/static/js/barcode.js @@ -8,6 +8,13 @@ function showBarcode(text) { document.getElementById('barcodeContent').textContent = text } +function hideBarcode() { + if (barcodeShown) { + document.getElementById('barcode').classList.remove('shown') + } + return document.getElementById('barcodeContent').textContent +} + function barcodeKeyPress(event) { var key = String.fromCharCode(event.charCode) var focused = document.activeElement @@ -16,8 +23,8 @@ function barcodeKeyPress(event) { } else if (document.querySelector) { focused = document.querySelector(":focus") } - if (focused == null || focused.tagName != "INPUT") { - if (event.keyCode === 13) { + if ( focused == null || focused.tagName != "INPUT" ) { + if ( event.keyCode === 13 ) { var input = document.getElementById('barcodeInput') if (input) { input.setAttribute('value', barcodeBuf) @@ -26,6 +33,15 @@ function barcodeKeyPress(event) { } barcodeBuf = "" event.preventDefault() + } else if ( event.keyCode === 27 ){ + barcodeBuf=hideBarcode() + event.preventDefault() + } else if ( event.keyCode === 9 ){ + barcodeBuf = barcodeBuf.substring( 0, barcodeBuf.length - 1 ) + if ( barcodeBuf.length <= 0 ) { + barcodeBuf=hideBarcode() + } + event.preventDefault() } else { barcodeBuf += key showBarcode(barcodeBuf)