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.

198 lines
7.3 KiB

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