[WIP] stock.ui: numpad support
authorFrédéric van der Essen <fva@openerp.com>
Fri, 28 Jun 2013 14:40:19 +0000 (16:40 +0200)
committerFrédéric van der Essen <fva@openerp.com>
Fri, 28 Jun 2013 14:40:19 +0000 (16:40 +0200)
bzr revid: fva@openerp.com-20130628144019-v1hiix8ox3qbh0uw

addons/stock/static/src/js/widgets.js

index 10c16e3..204f67c 100644 (file)
@@ -108,6 +108,7 @@ function openerp_picking_widgets(instance){
             this.operations = null;
             this.packages = null;
             this.scan_timestamp = 0;
+            this.numpad_buffer  = [];
             
             window.pickwidget = this;
             
@@ -162,7 +163,7 @@ function openerp_picking_widgets(instance){
         start: function(){
             var self = this;
             instance.webclient.set_content_full_screen(true);
-            this.connect_barcode_scanner();
+            this.connect_barcode_scanner_and_numpad();
 
             this.$('.js_pick_quit').click(function(){ self.quit(); });
             this.$('.js_pick_pack').click(function(){ self.pack(); });
@@ -267,44 +268,67 @@ function openerp_picking_widgets(instance){
 
             return current_package;
         },
-        connect_barcode_scanner: function(){
+        set_operation_quantity: function(quantity){
+            if(quantity === '++'){
+                console.log('Increase quantity!');
+            }else if(quantity === '--'){
+                console.log('Decrease quantity :(');
+            }else if(typeof quantity === 'number'){
+                console.log('Set quantity: ',quantity);
+            }
+        },
+        connect_barcode_scanner_and_numpad: function(){
             var self =this;
             var numbers = [];
             var timestamp = 0;
-            var nocode_timeout_id = 0;
-            var nocode_delay = 50;
-            function nocode(){
-                //console.log('NoCode:',numbers);
-                numbers = [];
-            }
+            var numpad = [];
+            var numpad_timestamp;
             // it is important to catch the keypress event and not keyup/keydown as keypress normalizes the input codes :) 
-            $('body').delegate('','keypress',function(e){ 
-                //console.log('Key:',e);
+            $('body').delegate('','keyup',function(e){ 
+                //console.log('Key:',e.keyCode);
                 if (e.keyCode >= 48 && e.keyCode < 58){
-                    clearTimeout(nocode_timeout_id);
-                    nocode_timeout_id = setTimeout(nocode,nocode_delay);
                     if(timestamp + 30 < new Date().getTime()){
-                        clearTimeout(nocode_timeout_id);
-                        nocode();
+                        numbers = [];
                     }
                     numbers.push(e.keyCode - 48);
                     timestamp = new Date().getTime();
                     if(numbers.length === 13){
                         self.scan(numbers.join(''));
                         numbers = [];
-                        clearTimeout(nocode_timeout_id);
                     }
                 }else{
-                    nocode();
+                    numbers = [];
+                    if(numpad_timestamp + 1500 < new Date().getTime()){
+                        numpad = [];
+                    }
+                    if(e.keyCode === 27 || e.keyCode === 8){ // ESC or BACKSPACE
+                        numpad = [];
+                    }else if(e.keyCode >= 96 && e.keyCode <= 105){ // NUMPAD NUMBERS
+                        numpad.push(e.keyCode - 96);
+                    }else if(e.keyCode === 13){ // ENTER
+                        if(numpad.length > 0){
+                            self.set_operation_quantity(parseInt(numpad.join('')));
+                        }
+                        numpad = [];
+                    }else if(e.keyCode === 107){ // NUMPAD +
+                        self.set_operation_quantity('++');
+                        numpad = [];
+                    }else if(e.keyCode === 109){ // NUMPAD -
+                        self.set_operation_quantity('--');
+                        numpad = [];
+                    }else{
+                        numpad = [];
+                    }
+                    numpad_timestamp = new Date().getTime();
                 }
             });
         },
-        disconnect_barcode_scanner: function(){
+        disconnect_barcode_scanner_and_numpad: function(){
             $('body').undelegate('', 'keyup')
         },
         quit: function(){
             console.log('Quit');
-            disconnect_barcode_scanner();
+            this.disconnect_barcode_scanner_and_numpad();
             instance.webclient.set_content_full_screen(false);
             window.location = '/'; // FIXME THIS IS SHIT NIV WILL KILL YOU (BY MULTIPLE FACE-STABBING) IF YOU MERGE THIS IN TRUNK
         },