Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($) {
- jQuery.fn.barcode_reader = function(options, callback) {
- // -----------
- // Set options
- // -----------
- var defaults = {
- valid: {},
- delay: 1200, // how long to keep messages on screen
- trans: 250, // fade transition
- trigger: 17, // Ctrl key
- message_standby: "Press the 'Ctrl' key to scan or manually enter barcodes.",
- message_ready: "Ready for barcode...",
- message_match: "Match found.",
- message_nomatch: "Unrecognized barcode.",
- message_nomatch_verb: "This barcode was not found in the company library."
- }
- var o = $.extend(defaults, options);
- // ----------
- // Initialize
- // ----------
- var dummy =
- "<div style=\"visibility: hidden; height: 0; width: 0; overflow: hidden;\">" +
- " <form id=\"barcode_scanner_form\">" +
- " <input type=\"text\" name=\"barcode_scanner_input\" id=\"barcode_scanner_input\" maxlength=\"30\" />" +
- " </form>" +
- "</div>" +
- "<div id=\"barcode_reader_result\" style=\"heigh:14px;\"></div>";
- var container = $(this);
- container.prepend(dummy);
- var form = $("#barcode_scanner_form");
- var field = $("#barcode_scanner_input");
- var status = $("#barcode_reader_result");
- // ---------------
- // Info box states
- // ---------------
- function info_standby() {
- status.fadeOut(o.trans, function(){
- status.removeClass()
- .addClass('info_box info_box_fyi')
- .text(o.message_standby)
- .fadeIn(o.trans);
- });
- }
- function info_ready(delay) {
- setTimeout( function(){
- status.fadeOut(o.trans, function() {
- status.removeClass()
- .addClass('info_box info_box_barcode')
- .text(o.message_ready)
- .fadeIn(o.trans);
- });
- }, delay);
- }
- function info_success() {
- status.fadeOut(o.trans, function(){
- status.removeClass()
- .addClass('info_box info_box_success')
- .text(o.message_match)
- .fadeIn(o.trans);
- });
- }
- function info_warning() {
- status.fadeOut(o.trans, function(){
- status.removeClass()
- .addClass('info_box info_box_warning')
- .text(o.message_nomatch)
- .fadeIn(o.trans);
- });
- }
- info_standby();
- field.blur(function(){ info_standby(); });
- field.keyup(function(){
- if (field.val()) {
- status.text('BARCODE: ' + field.val());
- }
- });
- // ---------
- // Trigger
- // ---------
- $(document).keydown(function(e) {
- if (e.which == o.trigger) {
- field.val("").focus();
- info_ready(0);
- }
- });
- // -------------
- // Process input
- // -------------
- form.submit(function() {
- var barcode = field.val();
- field.val("").focus();
- // compare input with array of valid barcodes
- var match = 0;
- if ( $.inArray(barcode, o.valid) != -1 ) { match = 1; }
- if (match){
- info_success();
- info_ready(o.delay);
- } else {
- info_warning();
- info_ready(o.delay);
- $.jGrowl('<b>' + barcode + '</b> - ' + o.message_nomatch_verb,
- { life: 5000, theme: 'error' }
- );
- }
- if ( typeof(callback) == "function" ) { callback(barcode, match); }
- return false;
- });
- }
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement