Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    
  2. (function($) {
  3.     jQuery.fn.barcode_reader = function(options, callback) {
  4.        
  5.         // -----------
  6.         // Set options
  7.         // -----------
  8.         var defaults = {
  9.             delay:                  1200,   // how long to keep messages on screen
  10.             trans:                  250,    // fade transition
  11.            
  12.             message_standby:        "Press the 'Ctrl' key to scan or manually enter barcodes.",
  13.             message_ready:          "Ready for barcode...",
  14.             message_match:          "Match found.",
  15.             message_nomatch:        "Unrecognized barcode.",
  16.             message_nomatch_verb:   "This barcode was not found in the company library."
  17.         }
  18.            
  19.         var o =  $.extend(defaults, options);
  20.        
  21.         // ----------
  22.         // Initialize
  23.         // ----------
  24.         var ghost =
  25.             "<div style=\"height: 0; width: 0; overflow: hidden;\">" +
  26.             "   <form id=\"barcode_scanner_form\">" +
  27.             "   <input type=\"text\" name=\"barcode_scanner_input\" id=\"barcode_scanner_input\" maxlength=\"30\" />" +
  28.             "   </form>" +
  29.             "</div>" +
  30.             "<div id=\"barcode_reader_result\" style=\"heigh:14px;\"></div>";
  31.  
  32.            
  33.         var container = $(this);
  34.         container.prepend(status).prepend(ghost);
  35.        
  36.         var form    = $("#barcode_scanner_form");
  37.         var field   = $("#barcode_scanner_input");
  38.         var status  = $("#barcode_reader_result");
  39.        
  40.         // ---------------
  41.         // Info box states
  42.         // ---------------
  43.         function info_standby() {          
  44.             status.fadeOut(o.trans, function(){
  45.                 status.removeClass()
  46.                     .addClass('info_box info_box_fyi')
  47.                     .text(o.message_standby)
  48.                     .fadeIn(o.trans);
  49.                     $.colorbox.resize();
  50.             });            
  51.            
  52.         }
  53.        
  54.         function info_ready(delay) {   
  55.             setTimeout( function(){
  56.                 status.fadeOut(o.trans, function() {
  57.                     status.removeClass()
  58.                         .addClass('info_box info_box_barcode')
  59.                         .text(o.message_ready)
  60.                         .fadeIn(o.trans);
  61.                 });
  62.             }, delay);
  63.         }
  64.        
  65.         function info_success() {          
  66.             status.fadeOut(o.trans, function(){
  67.                 status.removeClass()
  68.                     .addClass('info_box info_box_success')
  69.                     .text(o.message_match)
  70.                     .fadeIn(o.trans);
  71.             });
  72.         }
  73.                
  74.         function info_warning() {          
  75.             status.fadeOut(o.trans, function(){
  76.                 status.removeClass()
  77.                     .addClass('info_box info_box_warning')
  78.                     .text(o.message_nomatch)
  79.                     .fadeIn(o.trans);
  80.             });
  81.         }
  82.  
  83.         info_standby();
  84.        
  85.         field.blur(function(){ info_standby(); });
  86.         field.keyup(function(){
  87.             if (field.val()) {
  88.                 status.text('BARCODE: ' + field.val());
  89.             }
  90.         });
  91.        
  92.         // ---------
  93.         // Trigger
  94.         // ---------
  95.         $(document).keydown(function(e) {
  96.             if (e.which == 17) { // ctrl key code
  97.                 field.val("").focus();
  98.                 info_ready(0);
  99.             }
  100.         });
  101.            
  102.         // -------------
  103.         // Process input
  104.         // -------------
  105.         form.submit(function() {
  106.             var barcode = field.val();
  107.             field.val("").focus();
  108.  
  109.             // compare input with array of valid barcodes
  110.             var match   = 0;
  111.             if ( $.inArray(barcode, o.valid) != -1 )    { match = 1; }
  112.            
  113.             if (match){
  114.                 info_success();
  115.                 info_ready(o.delay);
  116.             } else {
  117.                 info_warning();
  118.                 info_ready(o.delay);
  119.                 $.jGrowl('<b>' + barcode + '</b> - ' + o.message_nomatch_verb,
  120.                     { life: 5000, theme:    'error' }
  121.                 );
  122.             }
  123.            
  124.             if ( typeof(callback) == "function" )   { callback(barcode, match); }                      
  125.             return false;
  126.         });
  127.     }
  128. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement