2 function openerp_pos_keyboard(instance, module){ //module is instance.point_of_sale
5 // ---------- OnScreen Keyboard Widget ----------
6 // A Widget that displays an onscreen keyboard.
7 // There are two options when creating the widget :
9 // * 'keyboard_model' : 'simple' | 'full' (default)
10 // The 'full' emulates a PC keyboard, while 'simple' emulates an 'android' one.
12 // * 'input_selector : (default: '.searchbox input')
13 // defines the dom element that the keyboard will write to.
15 // The widget is initially hidden. It can be shown with this.show(), and is
16 // automatically shown when the input_selector gets focused.
18 module.OnscreenKeyboardWidget = instance.web.Widget.extend({
19 template: 'OnscreenKeyboardSimple',
20 init: function(parent, options){
22 this._super(parent,options);
23 options = options || {};
25 this.keyboard_model = options.keyboard_model || 'full';
26 if(this.keyboard_model === 'full'){
27 this.template = 'OnscreenKeyboardFull';
30 this.input_selector = options.input_selector || '.searchbox input';
34 this.capslock = false;
39 connect : function(target){
41 this.$target = $(target);
42 this.$target.focus(function(){self.show();});
44 generateEvent: function(type,key){
45 var event = document.createEvent("KeyboardEvent");
46 var initMethod = event.initKeyboardEvent ? 'initKeyboardEvent' : 'initKeyEvent';
47 event[initMethod]( type,
55 ((typeof key.code === 'undefined') ? key.char.charCodeAt(0) : key.code),
56 ((typeof key.char === 'undefined') ? String.fromCharCode(key.code) : key.char)
62 // Write a character to the input zone
63 writeCharacter: function(character){
64 var input = this.$target[0];
65 input.dispatchEvent(this.generateEvent('keydown',{char: character}));
66 if(character !== '\n'){
67 input.value += character;
69 input.dispatchEvent(this.generateEvent('keyup',{char: character}));
72 // Removes the last character from the input zone.
73 deleteCharacter: function(){
74 var input = this.$target[0];
75 input.dispatchEvent(this.generateEvent('keydown',{code: 8}));
76 input.value = input.value.substr(0, input.value.length -1);
77 input.dispatchEvent(this.generateEvent('keyup',{code: 8}));
80 // Clears the content of the input zone.
81 deleteAllCharacters: function(){
82 var input = this.$target[0];
84 input.dispatchEvent(this.generateEvent('keydown',{code: 8}));
86 input.dispatchEvent(this.generateEvent('keyup',{code: 8}));
90 // Makes the keyboard show and slide from the bottom of the screen.
92 $('.keyboard_frame').show().css({'height':'235px'});
95 // Makes the keyboard hide by sliding to the bottom of the screen.
103 //What happens when the shift key is pressed : toggle case, remove capslock
104 toggleShift: function(){
105 $('.letter').toggleClass('uppercase');
106 $('.symbol span').toggle();
108 self.shift = (self.shift === true) ? false : true;
109 self.capslock = false;
112 //what happens when capslock is pressed : toggle case, set capslock
113 toggleCapsLock: function(){
114 $('.letter').toggleClass('uppercase');
115 self.capslock = true;
118 //What happens when numlock is pressed : toggle symbols and numlock label
119 toggleNumLock: function(){
120 $('.symbol span').toggle();
121 $('.numlock span').toggle();
122 self.numlock = (self.numlock === true ) ? false : true;
125 //After a key is pressed, shift is disabled.
126 removeShift: function(){
127 if (self.shift === true) {
128 $('.symbol span').toggle();
129 if (this.capslock === false) $('.letter').toggleClass('uppercase');
135 // Resets the keyboard to its original state; capslock: false, shift: false, numlock: false
141 this.toggleCapsLock();
144 this.toggleNumLock();
148 //called after the keyboard is in the DOM, sets up the key bindings.
155 $('.close_button').click(function(){
156 self.deleteAllCharacters();
160 // Keyboard key click handling
161 $('.keyboard li').click(function(){
164 character = $this.html(); // If it's a lowercase letter, nothing happens to this variable
166 if ($this.hasClass('left-shift') || $this.hasClass('right-shift')) {
171 if ($this.hasClass('capslock')) {
172 self.toggleCapsLock();
176 if ($this.hasClass('delete')) {
177 self.deleteCharacter();
181 if ($this.hasClass('numlock')){
182 self.toggleNumLock();
186 // Special characters
187 if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
188 if ($this.hasClass('space')) character = ' ';
189 if ($this.hasClass('tab')) character = "\t";
190 if ($this.hasClass('return')) character = "\n";
193 if ($this.hasClass('uppercase')) character = character.toUpperCase();
195 // Remove shift once a key is clicked.
198 self.writeCharacter(character);