-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
does magic with reading barcodes fixes #104
- Loading branch information
Showing
1 changed file
with
20 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); |