Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
74
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.             trigger:    17,     // Ctrl key
  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 dummy =
  25.             "<div style=\"visibility: hidden; 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.        
  35.         container.prepend(dummy);
  36.        
  37.         var form    = $("#barcode_scanner_form");
  38.         var field   = $("#barcode_scanner_input");
  39.         var status  = $("#barcode_reader_result");
  40.        
  41.        
  42.        
  43.         // ---------------
  44.         // Info box states
  45.         // ---------------
  46.         function info_standby() {          
  47.             status.fadeOut(o.trans, function(){
  48.                 status.removeClass()
  49.                     .addClass('info_box info_box_fyi')
  50.                     .text(o.message_standby)
  51.                     .fadeIn(o.trans);
  52.             });            
  53.            
  54.         }
  55.        
  56.         function info_ready(delay) {   
  57.             setTimeout( function(){
  58.                 status.fadeOut(o.trans, function() {
  59.                     status.removeClass()
  60.                         .addClass('info_box info_box_barcode')
  61.                         .text(o.message_ready)
  62.                         .fadeIn(o.trans);
  63.                 });
  64.             }, delay);
  65.         }
  66.        
  67.         function info_success() {          
  68.             status.fadeOut(o.trans, function(){
  69.                 status.removeClass()
  70.                     .addClass('info_box info_box_success')
  71.                     .text(o.message_match)
  72.                     .fadeIn(o.trans);
  73.             });
  74.         }
  75.                
  76.         function info_warning() {          
  77.             status.fadeOut(o.trans, function(){
  78.                 status.removeClass()
  79.                     .addClass('info_box info_box_warning')
  80.                     .text(o.message_nomatch)
  81.                     .fadeIn(o.trans);
  82.             });
  83.         }
  84.  
  85.         info_standby();
  86.        
  87.         field.blur(function(){ info_standby(); });
  88.         field.keyup(function(){
  89.             if (field.val()) {
  90.                 status.text('BARCODE: ' + field.val());
  91.             }
  92.         });
  93.        
  94.         // ---------
  95.         // Trigger
  96.         // ---------
  97.         $(document).keydown(function(e) {
  98.             if (e.which == o.trigger) {
  99.                 field.val("").focus();
  100.                 info_ready(0);
  101.             }
  102.         });
  103.            
  104.         // -------------
  105.         // Process input
  106.         // -------------
  107.         form.submit(function() {
  108.             var barcode = field.val();
  109.             field.val("").focus();
  110.  
  111.             // compare input with array of valid barcodes
  112.             var match   = 0;
  113.             if ( $.inArray(barcode, o.valid) != -1 )    { match = 1; }
  114.            
  115.             if (match){
  116.                 info_success();
  117.                 info_ready(o.delay);
  118.             } else {
  119.                 info_warning();
  120.                 info_ready(o.delay);
  121.                 $.jGrowl('<b>' + barcode + '</b> - ' + o.message_nomatch_verb,
  122.                     { life: 5000, theme:    'error' }
  123.                 );
  124.             }
  125.            
  126.             if ( typeof(callback) == "function" )   { callback(barcode, match); }                      
  127.             return false;
  128.         });
  129.     }
  130. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement