Skip to content

Commit

Permalink
better restock js
Browse files Browse the repository at this point in the history
does magic with reading barcodes

fixes #104
  • Loading branch information
bradjc committed Aug 10, 2014
1 parent ffbe2d0 commit 057913a
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions chezbetty/static/js/chezbetty-admin-item.js
Original file line number Diff line number Diff line change
@@ -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});

0 comments on commit 057913a

Please sign in to comment.