-
Notifications
You must be signed in to change notification settings - Fork 5
/
jquery.keynav.js
206 lines (195 loc) · 5.19 KB
/
jquery.keynav.js
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
/*
* jQuery Keyboard Navigation Plugin - Current
* http://mike-hostetler.com/jquery-keyboard-navigation-plugin
*
* To use, download this file to your server, save as keynav.js,
* and add this HTML into the <head>...</head> of your web page:
* <script type="text/javascript" src="jquery.keynav.js"></script>
*
* Copyright (c) 2006-2010 Mike Hostetler <http://www.mike-hostetler.com/>
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*/
$.keynav = new Object();
$.fn.keynav = function (onClass,offClass) {
//Initialization
var kn = $.keynav;
if(!kn.init) {
kn.el = new Array();
$(document).keydown(function(e) {
var key = 0;
if (e == null) {
key = event.keyCode;
} else { // mozilla
key = e.which;
}
switch(key) {
case 37:
$.keynav.goLeft();
break;
case 38:
$.keynav.goUp();
break;
case 39:
$.keynav.goRight();
break;
case 40:
$.keynav.goDown();
break;
case 13:
$.keynav.activate();
break;
}
});
kn.init = true;
}
return this.each(function() {
$.keynav.reg(this,onClass,offClass);
});
}
$.fn.keynav_sethover = function(onClass,offClass) {
return this.each(function() {
this.onClass = onClass;
this.offClass = offClass;
});
}
$.keynav.reset = function() {
var kn = $.keynav;
kn.el = new Array();
}
$.keynav.reg = function(e,onClass,offClass) {
var kn = $.keynav;
e.pos = $.keynav.getPos(e);
e.onClass = onClass;
e.offClass = offClass;
e.onmouseover = function (e) { $.keynav.setActive(this); };
kn.el.push(e);
}
$.keynav.setActive = function(e, fromKeyb) {
var kn = $.keynav;
var cur = $.keynav.getCurrent();
$(cur).trigger('blur');
for(var i=0;i<kn.el.length;i++) {
var tmp = kn.el[i];
$(tmp).removeClass(tmp.onClass).addClass(tmp.offClass);
}
$(e).removeClass(e.offClass).addClass(e.onClass);
$(e).trigger('focus');
if (fromKeyb) $(e).trigger('keynav:focus');
kn.currentEl = e;
}
$.keynav.getCurrent = function () {
var kn = $.keynav;
if(kn.currentEl) {
var cur = kn.currentEl;
}
else {
var cur = kn.el[0];
}
return cur;
}
$.keynav.quad = function(cur,fQuad) {
var kn = $.keynav;
var quad = Array();
for(i=0;i<kn.el.length;i++) {
var el = kn.el[i];
if(cur == el) continue;
if(fQuad((cur.pos.cx - el.pos.cx),(cur.pos.cy - el.pos.cy)))
quad.push(el);
}
return quad;
}
$.keynav.activateClosest = function(cur,quad) {
var closest;
var od = 1000000;
var nd = 0;
var found = false;
for(i=0;i<quad.length;i++) {
var e = quad[i];
nd = Math.sqrt(Math.pow(cur.pos.cx-e.pos.cx,2)+Math.pow(cur.pos.cy-e.pos.cy,2));
if(nd < od) {
closest = e;
od = nd;
found = true;
}
}
if(found)
$.keynav.setActive(closest, true);
}
$.keynav.goLeft = function () {
var cur = $.keynav.getCurrent();
var quad = $.keynav.quad(cur,function (dx,dy) {
if((dy >= 0) && (Math.abs(dx) - dy) <= 0)
return true;
else
return false;
});
$.keynav.activateClosest(cur,quad);
}
$.keynav.goRight = function () {
var cur = $.keynav.getCurrent();
var quad = $.keynav.quad(cur,function (dx,dy) {
if((dy <= 0) && (Math.abs(dx) + dy) <= 0)
return true;
else
return false;
});
$.keynav.activateClosest(cur,quad);
}
$.keynav.goUp = function () {
var cur = $.keynav.getCurrent();
var quad = $.keynav.quad(cur,function (dx,dy) {
if((dx >= 0) && (Math.abs(dy) - dx) <= 0)
return true;
else
return false;
});
$.keynav.activateClosest(cur,quad);
}
$.keynav.goDown = function () {
var cur = $.keynav.getCurrent();
var quad = $.keynav.quad(cur,function (dx,dy) {
if((dx <= 0) && (Math.abs(dy) + dx) <= 0)
return true;
else
return false;
});
$.keynav.activateClosest(cur,quad);
}
$.keynav.activate = function () {
var kn = $.keynav;
$(kn.currentEl).trigger('click');
}
/**
* This function was taken from Stefan's exellent interface plugin
* http://www.eyecon.ro/interface/
*
* I included it in this library's namespace because the functions aren't
* quite the same.
*/
$.keynav.getPos = function (e)
{
var l = 0;
var t = 0;
var w = $.intval($.css(e,'width'));
var h = $.intval($.css(e,'height'));
while (e.offsetParent){
l += e.offsetLeft + (e.currentStyle?$.intval(e.currentStyle.borderLeftWidth):0);
t += e.offsetTop + (e.currentStyle?$.intval(e.currentStyle.borderTopWidth):0);
e = e.offsetParent;
}
l += e.offsetLeft + (e.currentStyle?$.intval(e.currentStyle.borderLeftWidth):0);
t += e.offsetTop + (e.currentStyle?$.intval(e.currentStyle.borderTopWidth):0);
var cx = Math.round(t+(h/2));
var cy = Math.round(l+(w/2));
return {x:l, y:t, w:w, h:h, cx:cx, cy:cy};
};
/**
* This function was taken from Stefan's exellent interface plugin
* http://www.eyecon.ro/interface/
*/
$.intval = function (v)
{
v = parseInt(v);
return isNaN(v) ? 0 : v;
};