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.

270 lines
9.8 KiB

8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
  1. /* Copyright 2015 igallyamov <https://github.com/igallyamov>
  2. Copyright 2016 ufaks <https://github.com/ufaks>
  3. Copyright 2016 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
  4. Copyright 2019 Kolushov Alexandr <https://it-projects.info/team/kolushovalexandr>
  5. License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). */
  6. odoo.define('pos_keyboard.pos', function (require) {
  7. "use strict";
  8. var core = require('web.core');
  9. var gui = require('point_of_sale.gui');
  10. var models = require('point_of_sale.models');
  11. var screens = require('point_of_sale.screens');
  12. var PopupWidget = require('point_of_sale.popups');
  13. var _super_posmodel = models.PosModel.prototype;
  14. models.PosModel = models.PosModel.extend({
  15. initialize: function (session, attributes) {
  16. var self = this;
  17. this.keypad = new Keypad({'pos': this});
  18. _super_posmodel.initialize.call(this, session, attributes);
  19. this.ready.then(function(){
  20. self.keypad.set_action_callback(function(data){
  21. var current_screen = self.gui.current_screen;
  22. var current_popup = self.gui.current_popup;
  23. if (current_popup) {
  24. current_popup.keypad_action(data);
  25. } else if (current_screen.numpad && current_screen.numpad.keypad_action) {
  26. current_screen.numpad.keypad_action(data);
  27. }
  28. });
  29. });
  30. }
  31. });
  32. gui.Gui.prototype.popup_classes.filter(function(c){
  33. return c.name === 'password';
  34. })[0].widget.include({
  35. init: function(parent, args) {
  36. this._super(parent, args);
  37. this.popup_type = 'password';
  38. },
  39. });
  40. PopupWidget.include({
  41. keypad_action: function(data){
  42. var type = this.pos.keypad.type;
  43. if (data.type === type.numchar){
  44. this.click_keyboard(data.val);
  45. } else if (data.type === type.backspace){
  46. this.click_keyboard('BACKSPACE');
  47. } else if (data.type === type.enter){
  48. if (this.click_confirm.length) {
  49. return this.click_confirm();
  50. }
  51. return;
  52. } else if (data.type === type.escape){
  53. this.click_cancel();
  54. }
  55. },
  56. click_keyboard: function(value){
  57. if (typeof this.inputbuffer === 'undefined') {
  58. return;
  59. }
  60. var newbuf = this.gui.numpad_input(
  61. this.inputbuffer,
  62. value,
  63. {'firstinput': this.firstinput});
  64. this.firstinput = (newbuf.length === 0);
  65. var $value = this.$('.value');
  66. if (newbuf !== this.inputbuffer) {
  67. this.inputbuffer = newbuf;
  68. $value.text(this.inputbuffer);
  69. }
  70. if (this.popup_type === 'password') {
  71. $value.text($value.text().replace(/./g, '•'));
  72. }
  73. },
  74. show: function(options){
  75. this._super(options);
  76. this.$('input,textarea').focus();
  77. },
  78. });
  79. screens.NumpadWidget.include({
  80. keypad_action: function(data){
  81. var type = this.pos.keypad.type;
  82. if (data.type === type.numchar){
  83. this.state.appendNewChar(data.val);
  84. }
  85. else if (data.type === type.bmode) {
  86. this.state.changeMode(data.val);
  87. }
  88. else if (data.type === type.sign){
  89. this.clickSwitchSign();
  90. }
  91. else if (data.type === type.backspace){
  92. this.clickDeleteLastChar();
  93. }
  94. }
  95. });
  96. screens.PaymentScreenWidget.include({
  97. show: function(){
  98. this._super();
  99. this.pos.keypad.disconnect();
  100. },
  101. hide: function(){
  102. this._super();
  103. this.pos.keypad.connect();
  104. }
  105. });
  106. // this module mimics a keypad-only cash register. Use connect() and
  107. // disconnect() to activate and deactivate it.
  108. var Keypad = core.Class.extend({
  109. init: function(attributes){
  110. this.pos = attributes.pos;
  111. /*this.pos_widget = this.pos.pos_widget;*/
  112. this.type = {
  113. numchar: 'number, dot',
  114. bmode: 'quantity, discount, price',
  115. sign: '+, -',
  116. backspace: 'backspace',
  117. enter: 'enter',
  118. escape: 'escape',
  119. };
  120. this.data = {
  121. type: undefined,
  122. val: undefined
  123. };
  124. this.action_callback = undefined;
  125. },
  126. save_callback: function(){
  127. this.saved_callback_stack.push(this.action_callback);
  128. },
  129. restore_callback: function(){
  130. if (this.saved_callback_stack.length > 0) {
  131. this.action_callback = this.saved_callback_stack.pop();
  132. }
  133. },
  134. set_action_callback: function(callback){
  135. this.action_callback = callback;
  136. },
  137. //remove action callback
  138. reset_action_callback: function(){
  139. this.action_callback = undefined;
  140. },
  141. // starts catching keyboard events and tries to interpret keystrokes,
  142. // calling the callback when needed.
  143. connect: function(){
  144. var self = this;
  145. // --- additional keyboard ---//
  146. // KeyCode: + or - (Keypad '+')
  147. var KC_PLU = 107;
  148. // KeyCode: Quantity (Keypad '/')
  149. var KC_QTY = 111;
  150. // KeyCode: Price (Keypad '*')
  151. var KC_AMT = 106;
  152. // KeyCode: Discount Percentage [0..100] (Keypad '-')
  153. var KC_DISC = 109;
  154. // --- basic keyboard --- //
  155. // KeyCode: sign + or - (Keypad 's')
  156. var KC_PLU_1 = 83;
  157. // KeyCode: Quantity (Keypad 'q')
  158. var KC_QTY_1 = 81;
  159. // KeyCode: Price (Keypad 'p')
  160. var KC_AMT_1 = 80;
  161. // KeyCode: Discount Percentage [0..100] (Keypad 'd')
  162. var KC_DISC_1 = 68;
  163. // KeyCode: Backspace (Keypad 'backspace')
  164. var KC_BACKSPACE = 8;
  165. // KeyCode: Enter (Keypad 'enter')
  166. var KC_ENTER = 13;
  167. // KeyCode: Escape (Keypad 'esc')
  168. var KC_ESCAPE = 27;
  169. var kc_lookup = {
  170. 48: '0', 49: '1', 50: '2', 51: '3', 52: '4',
  171. 53: '5', 54: '6', 55: '7', 56: '8', 57: '9',
  172. 80: 'p', 83: 's', 68: 'd', 190: '.', 81: 'q',
  173. 96: '0', 97: '1', 98: '2', 99: '3', 100: '4',
  174. 101: '5', 102: '6', 103: '7', 104: '8', 105: '9',
  175. 106: '*', 107: '+', 109: '-', 110: '.', 111: '/'
  176. };
  177. //usb keyboard keyup event
  178. var rx = /INPUT|SELECT|TEXTAREA/i;
  179. var ok = false;
  180. var timeStamp = 0;
  181. $('body').on('keyup', '', function (e){
  182. var statusHandler = !rx.test(e.target.tagName) ||
  183. e.target.disabled || e.target.readOnly;
  184. if (statusHandler){
  185. var is_number = false;
  186. var type = self.type;
  187. var buttonMode = {
  188. qty: 'quantity',
  189. disc: 'discount',
  190. price: 'price'
  191. };
  192. var token = e.keyCode;
  193. if (((token >= 96 && token <= 105) || token === 110) ||
  194. ((token >= 48 && token <= 57) || token === 190)) {
  195. self.data.type = type.numchar;
  196. self.data.val = kc_lookup[token];
  197. is_number = true;
  198. ok = true;
  199. } else if (token === KC_PLU || token === KC_PLU_1) {
  200. self.data.type = type.sign;
  201. ok = true;
  202. } else if (token === KC_QTY || token === KC_QTY_1) {
  203. self.data.type = type.bmode;
  204. self.data.val = buttonMode.qty;
  205. ok = true;
  206. } else if (token === KC_AMT || token === KC_AMT_1) {
  207. self.data.type = type.bmode;
  208. self.data.val = buttonMode.price;
  209. ok = true;
  210. } else if (token === KC_DISC || token === KC_DISC_1) {
  211. self.data.type = type.bmode;
  212. self.data.val = buttonMode.disc;
  213. ok = true;
  214. } else if (token === KC_BACKSPACE) {
  215. self.data.type = type.backspace;
  216. ok = true;
  217. } else if (token === KC_ENTER) {
  218. self.data.type = type.enter;
  219. ok = true;
  220. } else if (token === KC_ESCAPE) {
  221. self.data.type = type.escape;
  222. ok = true;
  223. } else {
  224. self.data.type = undefined;
  225. self.data.val = undefined;
  226. ok = false;
  227. }
  228. if (is_number) {
  229. if (timeStamp + 50 > new Date().getTime()) {
  230. ok = false;
  231. }
  232. }
  233. timeStamp = new Date().getTime();
  234. setTimeout(function(){
  235. if (ok) {self.action_callback(self.data);}
  236. }, 50);
  237. }
  238. });
  239. },
  240. // stops catching keyboard events
  241. disconnect: function(){
  242. $('body').off('keyup', '');
  243. }
  244. });
  245. return {
  246. Keypad: Keypad
  247. };
  248. });