summaryrefslogtreecommitdiff
path: root/addons/point_of_sale/static/src/js/keyboard.js
blob: a9ea1f555535f99f1bf6ba3c3212dfa0b5606675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
odoo.define('point_of_sale.keyboard', function (require) {
"use strict";

var Widget = require('web.Widget');

// ---------- OnScreen Keyboard Widget ----------
// A Widget that displays an onscreen keyboard.
// There are two options when creating the widget :
// 
// * 'keyboard_model' : 'simple' (default) | 'full' 
//   The 'full' emulates a PC keyboard, while 'simple' emulates an 'android' one.
//
// * 'input_selector  : (default: '.searchbox input') 
//   defines the dom element that the keyboard will write to.
// 
// The widget is initially hidden. It can be shown with this.show(), and is 
// automatically shown when the input_selector gets focused.

var OnscreenKeyboardWidget = Widget.extend({
    template: 'OnscreenKeyboardSimple', 
    init: function(parent, options){
        this._super(parent,options);
        options = options || {};

        this.keyboard_model = options.keyboard_model || 'simple';
        if(this.keyboard_model === 'full'){
            this.template = 'OnscreenKeyboardFull';
        }

        this.input_selector = options.input_selector || '.searchbox input';
        this.$target = null;

        //Keyboard state
        this.capslock = false;
        this.shift    = false;
        this.numlock  = false;
    },
    
    connect : function(target){
        var self = this;
        this.$target = $(target);
        this.$target.focus(function(){self.show();});
    },
    generateEvent: function(type,key){
        var event = document.createEvent("KeyboardEvent");
        var initMethod =  event.initKeyboardEvent ? 'initKeyboardEvent' : 'initKeyEvent';
        event[initMethod](  type,
                            true, //bubbles
                            true, //cancelable
                            window, //viewArg
                            false, //ctrl
                            false, //alt
                            false, //shift
                            false, //meta
                            ((typeof key.code === 'undefined') ? key.char.charCodeAt(0) : key.code),
                            ((typeof key.char === 'undefined') ? String.fromCharCode(key.code) : key.char)
                        );
        return event;

    },

    // Write a character to the input zone
    writeCharacter: function(character){
        var input = this.$target[0];
        input.dispatchEvent(this.generateEvent('keypress',{char: character}));
        if(character !== '\n'){
            input.value += character;
        }
        input.dispatchEvent(this.generateEvent('keyup',{char: character}));
    },
    
    // Removes the last character from the input zone.
    deleteCharacter: function(){
        var input = this.$target[0];
        input.dispatchEvent(this.generateEvent('keypress',{code: 8}));
        input.value = input.value.substr(0, input.value.length -1);
        input.dispatchEvent(this.generateEvent('keyup',{code: 8}));
    },
    
    // Clears the content of the input zone.
    deleteAllCharacters: function(){
        var input = this.$target[0];
        if(input.value){
            input.dispatchEvent(this.generateEvent('keypress',{code: 8}));
            input.value = "";
            input.dispatchEvent(this.generateEvent('keyup',{code: 8}));
        }
    },

    // Makes the keyboard show and slide from the bottom of the screen.
    show:  function(){
        $('.keyboard_frame').show().css({'height':'235px'});
    },
    
    // Makes the keyboard hide by sliding to the bottom of the screen.
    hide:  function(){
        $('.keyboard_frame')
            .css({'height':'0'})
            .hide();
        this.reset();
    },
    
    //What happens when the shift key is pressed : toggle case, remove capslock
    toggleShift: function(){
        $('.letter').toggleClass('uppercase');
        $('.symbol span').toggle();
        
        this.shift = (this.shift === true) ? false : true;
        this.capslock = false;
    },
    
    //what happens when capslock is pressed : toggle case, set capslock
    toggleCapsLock: function(){
        $('.letter').toggleClass('uppercase');
        this.capslock = true;
    },
    
    //What happens when numlock is pressed : toggle symbols and numlock label 
    toggleNumLock: function(){
        $('.symbol span').toggle();
        $('.numlock span').toggle();
        this.numlock = (this.numlock === true ) ? false : true;
    },

    //After a key is pressed, shift is disabled. 
    removeShift: function(){
        if (this.shift === true) {
            $('.symbol span').toggle();
            if (this.capslock === false) $('.letter').toggleClass('uppercase');
            
            this.shift = false;
        }
    },

    // Resets the keyboard to its original state; capslock: false, shift: false, numlock: false
    reset: function(){
        if(this.shift){
            this.toggleShift();
        }
        if(this.capslock){
            this.toggleCapsLock();
        }
        if(this.numlock){
            this.toggleNumLock();
        }
    },

    //called after the keyboard is in the DOM, sets up the key bindings.
    start: function(){
        var self = this;

        //this.show();


        $('.close_button').click(function(){ 
            self.deleteAllCharacters();
            self.hide(); 
        });

        // Keyboard key click handling
        $('.keyboard li').click(function(){
            
            var $this = $(this),
                character = $this.html(); // If it's a lowercase letter, nothing happens to this variable
            
            if ($this.hasClass('left-shift') || $this.hasClass('right-shift')) {
                self.toggleShift();
                return false;
            }
            
            if ($this.hasClass('capslock')) {
                self.toggleCapsLock();
                return false;
            }
            
            if ($this.hasClass('delete')) {
                self.deleteCharacter();
                return false;
            }

            if ($this.hasClass('numlock')){
                self.toggleNumLock();
                return false;
            }
            
            // Special characters
            if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
            if ($this.hasClass('space')) character = ' ';
            if ($this.hasClass('tab')) character = "\t";
            if ($this.hasClass('return')) character = "\n";
            
            // Uppercase letter
            if ($this.hasClass('uppercase')) character = character.toUpperCase();
            
            // Remove shift once a key is clicked.
            self.removeShift();

            self.writeCharacter(character);
        });
    },
});

return {
    OnscreenKeyboardWidget: OnscreenKeyboardWidget,
};

});