From 057913a94a4b42ef58ba39aeb320bee642b7e9b7 Mon Sep 17 00:00:00 2001 From: Brad Campbell Date: Sun, 10 Aug 2014 00:28:59 -0400 Subject: [PATCH] better restock js does magic with reading barcodes fixes #104 --- chezbetty/static/js/chezbetty-admin-item.js | 23 ++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/chezbetty/static/js/chezbetty-admin-item.js b/chezbetty/static/js/chezbetty-admin-item.js index a09e6dc..b5acb50 100644 --- a/chezbetty/static/js/chezbetty-admin-item.js +++ b/chezbetty/static/js/chezbetty-admin-item.js @@ -1,8 +1,25 @@ // Callback when scanner input is detected and read -$("#scanner").bind("scannerDetectionComplete", function (e, data) { +$("body").bind("scannerDetectionComplete", function (e, data) { add_item(data.string); -}) +}); + +// "Errors" are really just things that didn't turn out to be barcodes +// Add them to currently selected thing +$("body").bind("scannerDetectionError", function (e, data) { + var focused = $(':focus'); + var cursor_pos = document.getElementById(focused.attr("id")).selectionStart; + var cursor_end = document.getElementById(focused.attr("id")).selectionEnd; + var start = focused.val(); + focused.val(start.substring(0, cursor_pos) + data.string + start.substring(cursor_end)); + + // Put the cursor back where it was. + document.getElementById(focused.attr("id")).selectionStart = cursor_pos + data.string.length; + document.getElementById(focused.attr("id")).selectionEnd = cursor_pos + data.string.length; + + // Trigger the input change + focused.trigger("input"); +}); // Register the scanner detector -$("#scanner").scannerDetection(); +$("body").scannerDetection({preventDefault:true});