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.

279 lines
10 KiB

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