Total Complexity | 8 |
Complexity/F | 1 |
Lines of Code | 92 |
Function Count | 8 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | import _ from 'underscore'; |
||
|
|||
2 | /** |
||
3 | Command translates a DOM Event into commands that Backgrid |
||
4 | recognizes. Interested parties can listen on selected Backgrid events that |
||
5 | come with an instance of this class and act on the commands. |
||
6 | |||
7 | It is also possible to globally rebind the keyboard shortcuts by replacing |
||
8 | the methods in this class' prototype. |
||
9 | |||
10 | @class Backgrid.Command |
||
11 | @constructor |
||
12 | */ |
||
13 | var Command = function (evt) { |
||
14 | _.extend(this, { |
||
15 | altKey: !!evt.altKey, |
||
16 | "char": evt["char"], |
||
17 | charCode: evt.charCode, |
||
18 | ctrlKey: !!evt.ctrlKey, |
||
19 | key: evt.key, |
||
20 | keyCode: evt.keyCode, |
||
21 | locale: evt.locale, |
||
22 | location: evt.location, |
||
23 | metaKey: !!evt.metaKey, |
||
24 | repeat: !!evt.repeat, |
||
25 | shiftKey: !!evt.shiftKey, |
||
26 | which: evt.which |
||
27 | }); |
||
28 | }; |
||
29 | |||
30 | _.extend(Command.prototype, { |
||
31 | /** |
||
32 | Up Arrow |
||
33 | |||
34 | @member Backgrid.Command |
||
35 | */ |
||
36 | moveUp: function () { |
||
37 | return this.keyCode == 38; |
||
38 | }, |
||
39 | /** |
||
40 | Down Arrow |
||
41 | |||
42 | @member Backgrid.Command |
||
43 | */ |
||
44 | moveDown: function () { |
||
45 | return this.keyCode === 40; |
||
46 | }, |
||
47 | /** |
||
48 | Shift Tab |
||
49 | |||
50 | @member Backgrid.Command |
||
51 | */ |
||
52 | moveLeft: function () { |
||
53 | return this.shiftKey && this.keyCode === 9; |
||
54 | }, |
||
55 | /** |
||
56 | Tab |
||
57 | |||
58 | @member Backgrid.Command |
||
59 | */ |
||
60 | moveRight: function () { |
||
61 | return !this.shiftKey && this.keyCode === 9; |
||
62 | }, |
||
63 | /** |
||
64 | Enter |
||
65 | |||
66 | @member Backgrid.Command |
||
67 | */ |
||
68 | save: function () { |
||
69 | return this.keyCode === 13; |
||
70 | }, |
||
71 | /** |
||
72 | Esc |
||
73 | |||
74 | @member Backgrid.Command |
||
75 | */ |
||
76 | cancel: function () { |
||
77 | return this.keyCode === 27; |
||
78 | }, |
||
79 | /** |
||
80 | None of the above. |
||
81 | |||
82 | @member Backgrid.Command |
||
83 | */ |
||
84 | passThru: function () { |
||
85 | return !(this.moveUp() || this.moveDown() || this.moveLeft() || |
||
86 | this.moveRight() || this.save() || this.cancel()); |
||
87 | } |
||
88 | }); |
||
89 | |||
90 | export { |
||
91 | Command |
||
92 | }; |
||
93 |