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.

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