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.

186 lines
7.0 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).bind("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. $('body').delegate('','keyup', function (e){
  118. var statusHandler = !rx.test(e.target.tagName) ||
  119. e.target.disabled || e.target.readOnly;
  120. if (statusHandler){
  121. var ok =false;
  122. var type = self.type;
  123. var buttonMode = self.pos.pos_widget.numpad.modeButton
  124. token = e.keyCode;
  125. if ((token >= 96 && token <= 105 || token == 110) ||
  126. (token >= 48 && token <= 57 || token == 190)) {
  127. self.data.type = type.numchar;
  128. self.data.val = kc_lookup[token];
  129. ok = true;
  130. }
  131. else if (token == KC_PLU || token == KC_PLU_1) {
  132. self.data.type = type.sign;
  133. ok = true;
  134. }
  135. else if (token == KC_QTY || token == KC_QTY_1) {
  136. self.data.type = type.bmode;
  137. self.data.val = buttonMode.qty
  138. ok = true;
  139. }
  140. else if (token == KC_AMT || token == KC_AMT_1) {
  141. self.data.type = type.bmode;
  142. self.data.val = buttonMode.price;
  143. ok = true;
  144. }
  145. else if (token == KC_DISC || token == KC_DISC_1) {
  146. self.data.type = type.bmode;
  147. self.data.val = buttonMode.disc;
  148. ok = true;
  149. }
  150. else if (token == KC_BACKSPACE) {
  151. self.data.type = type.backspace;
  152. self.action_callback(self.data);
  153. ok = true;
  154. }
  155. if (ok) {self.action_callback(self.data);}
  156. }
  157. });
  158. },
  159. // stops catching keyboard events
  160. disconnect: function(){
  161. $('body').undelegate('', 'keyup')
  162. },
  163. });
  164. }
  165. (function(){
  166. var _super = window.openerp.point_of_sale;
  167. window.openerp.point_of_sale = function(instance){
  168. _super(instance);
  169. var module = instance.point_of_sale;
  170. pos_keyboard_widgets(instance, module);
  171. }
  172. })()