Advertisement
Guest User

Untitled

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