You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
7.1 KiB

8 years ago
  1. function pos_keyboard_widgets(instance, module){
  2. module.PosWidget.include({
  3. start: function() {
  4. self = this;
  5. resSuper = this._super();
  6. res = resSuper.done(function(e){
  7. self.pos.keypad.connect();
  8. self.pos.keypad.set_action_callback(function(data){
  9. self.keypad_action(data, self.pos.keypad.type);
  10. });
  11. });
  12. return res;
  13. },
  14. close: function() {
  15. this._super();
  16. this.pos.keypad.disconnect();
  17. },
  18. keypad_action: function(data, type){
  19. var numpad = this.pos_widget.numpad;
  20. if (data.type === type.numchar){
  21. numpad.state.appendNewChar(data.val);
  22. }
  23. else if (data.type === type.bmode) {
  24. numpad.state.changeMode(data.val);
  25. }
  26. else if (data.type === type.sign){
  27. numpad.clickSwitchSign();
  28. }
  29. else if (data.type === type.backspace){
  30. numpad.clickDeleteLastChar();
  31. }
  32. },
  33. });
  34. var PosModelSuper = module.PosModel;
  35. module.PosModel = module.PosModel.extend({
  36. initialize: function(session, attributes) {
  37. this.keypad = new module.Keypad({'pos': this});
  38. PosModelSuper.prototype.initialize.call(this, session, attributes);
  39. },
  40. });
  41. // this module mimics a keypad-only cash register. Use connect() and
  42. // disconnect() to activate and deactivate it.
  43. module.Keypad = instance.web.Class.extend({
  44. init: function(attributes){
  45. this.pos = attributes.pos;
  46. this.pos_widget = this.pos.pos_widget;
  47. this.type = {
  48. numchar: 'number, dot',
  49. bmode: 'qty, disc, price',
  50. sign: '+, -',
  51. backspace: 'backspace'
  52. }
  53. this.data = {
  54. type: undefined,
  55. val: undefined
  56. }
  57. this.action_callback = undefined;
  58. },
  59. save_callback: function(){
  60. this.saved_callback_stack.push(this.action_callback);
  61. },
  62. restore_callback: function(){
  63. if (this.saved_callback_stack.length > 0) {
  64. this.action_callback = this.saved_callback_stack.pop();
  65. }
  66. },
  67. set_action_callback: function(callback){
  68. this.action_callback = callback
  69. },
  70. //remove action callback
  71. reset_action_callback: function(){
  72. this.action_callback = undefined;
  73. },
  74. // starts catching keyboard events and tries to interpret keystrokes,
  75. // calling the callback when needed.
  76. connect: function(){
  77. var self = this;
  78. // --- additional keyboard ---//
  79. var KC_PLU = 107; // KeyCode: + or - (Keypad '+')
  80. var KC_QTY = 111; // KeyCode: Quantity (Keypad '/')
  81. var KC_AMT = 106; // KeyCode: Price (Keypad '*')
  82. var KC_DISC = 109; // KeyCode: Discount Percentage [0..100] (Keypad '-')
  83. // --- basic keyboard --- //
  84. var KC_PLU_1 = 83; // KeyCode: sign + or - (Keypad 's')
  85. var KC_QTY_1 = 81; // KeyCode: Quantity (Keypad 'q')
  86. var KC_AMT_1 = 80; // KeyCode: Price (Keypad 'p')
  87. var KC_DISC_1 = 68; // KeyCode: Discount Percentage [0..100] (Keypad 'd')
  88. var KC_BACKSPACE = 8; // KeyCode: Backspace (Keypad 'backspace')
  89. var kc_lookup = {
  90. 48: '0', 49: '1', 50: '2', 51: '3', 52: '4',
  91. 53: '5', 54: '6', 55: '7', 56: '8', 57: '9',
  92. 80: 'p', 83: 's', 68: 'd', 190: '.', 81: 'q',
  93. 96: '0', 97: '1', 98: '2', 99: '3', 100: '4',
  94. 101: '5', 102: '6', 103: '7', 104: '8', 105: '9',
  95. 106: '*', 107: '+', 109: '-', 110: '.', 111: '/',
  96. };
  97. //cancel return to the previous page when press backspace
  98. var rx = /INPUT|SELECT|TEXTAREA/i;
  99. $(document).on("keydown keypress", function(e){
  100. if( e.which == 8 ){ // 8 == backspace
  101. if(!rx.test(e.target.tagName) || e.target.disabled || e.target.readOnly ){
  102. e.preventDefault();
  103. }
  104. }
  105. });
  106. //usb keyboard keyup event
  107. var ok = false;
  108. var timeStamp = 0;
  109. $('body').on('keyup', '', function (e){
  110. var statusHandler = !rx.test(e.target.tagName) ||
  111. e.target.disabled || e.target.readOnly;
  112. if (statusHandler){
  113. var is_number = false;
  114. var type = self.type;
  115. var buttonMode = {
  116. qty: 'quantity',
  117. disc: 'discount',
  118. price: 'price'
  119. };
  120. var token = e.keyCode;
  121. if ((token >= 96 && token <= 105 || token == 110) ||
  122. (token >= 48 && token <= 57 || token == 190)) {
  123. self.data.type = type.numchar;
  124. self.data.val = kc_lookup[token];
  125. is_number = true;
  126. ok = true;
  127. }
  128. else if (token == KC_PLU || token == KC_PLU_1) {
  129. self.data.type = type.sign;
  130. ok = true;
  131. }
  132. else if (token == KC_QTY || token == KC_QTY_1) {
  133. self.data.type = type.bmode;
  134. self.data.val = buttonMode.qty;
  135. ok = true;
  136. }
  137. else if (token == KC_AMT || token == KC_AMT_1) {
  138. self.data.type = type.bmode;
  139. self.data.val = buttonMode.price;
  140. ok = true;
  141. }
  142. else if (token == KC_DISC || token == KC_DISC_1) {
  143. self.data.type = type.bmode;
  144. self.data.val = buttonMode.disc;
  145. ok = true;
  146. }
  147. else if (token == KC_BACKSPACE) {
  148. self.data.type = type.backspace;
  149. ok = true;
  150. }
  151. if (is_number) {
  152. if (timeStamp + 50 > new Date().getTime()) {
  153. ok = false;
  154. }
  155. }
  156. timeStamp = new Date().getTime();
  157. setTimeout(function(){
  158. if (ok) {self.action_callback(self.data);}
  159. }, 50);
  160. }
  161. });
  162. },
  163. // stops catching keyboard events
  164. disconnect: function(){
  165. $('body').off('keyup', '')
  166. }
  167. });
  168. }
  169. (function(){
  170. var _super = window.openerp.point_of_sale;
  171. window.openerp.point_of_sale = function(instance){
  172. _super(instance);
  173. var module = instance.point_of_sale;
  174. pos_keyboard_widgets(instance, module);
  175. }
  176. })();