KeyWatcher.js
1.5 KB
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
/**
* KeyWatcher
* provides keyboard support
*/
// var KeyWatcher = function(core){{{
var KeyWatcher = function(core){
this.core = core;
this.init();
};
// }}}
$.extend(KeyWatcher,{
// defaults: {{{
defaults: {
eventName: 'keydown.jcrop',
passthru: [ 9 ],
debug: false
},
// }}}
prototype: {
// init: function(){{{
init: function(){
$.extend(this,KeyWatcher.defaults);
this.enable();
},
// }}}
// disable: function(){{{
disable: function(){
this.core.container.off(this.eventName);
},
// }}}
// enable: function(){{{
enable: function(){
var t = this, m = t.core;
m.container.on(t.eventName,function(e){
var nudge = e.shiftKey? 16: 2;
if ($.inArray(e.keyCode,t.passthru) >= 0)
return true;
switch(e.keyCode){
case 37: m.nudge(-nudge,0); break;
case 38: m.nudge(0,-nudge); break;
case 39: m.nudge(nudge,0); break;
case 40: m.nudge(0,nudge); break;
case 46:
case 8:
m.requestDelete();
return false;
break;
default:
if (t.debug) console.log('keycode: ' + e.keyCode);
break;
}
if (!e.metaKey && !e.ctrlKey)
e.preventDefault();
});
}
// }}}
}
});
Jcrop.registerComponent('Keyboard',KeyWatcher);