-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
98 lines (80 loc) · 2.32 KB
/
code.js
File metadata and controls
98 lines (80 loc) · 2.32 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
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
// Generated by CoffeeScript 1.3.3
(function() {
var addNavigationData, directionForKeys, handleNavigationKey, isFocusable, lastFocused, navigateTo;
this.VK_LEFT || (this.VK_LEFT = 37);
this.VK_RIGHT || (this.VK_RIGHT = 39);
this.VK_UP || (this.VK_UP = 38);
this.VK_DOWN || (this.VK_DOWN = 40);
$(document).keydown(function(e) {
if (handleNavigationKey(e.target, e.keyCode)) {
return e.preventDefault();
}
});
handleNavigationKey = function(el, keyCode) {
var direction, nextElSelector;
direction = directionForKeys(keyCode);
if (!(el && direction)) {
return false;
}
nextElSelector = $(el).data(direction);
if (nextElSelector) {
navigateTo($(nextElSelector));
} else {
handleNavigationKey(el.parentNode, keyCode);
}
return true;
};
directionForKeys = function(keyCode) {
switch (keyCode) {
case VK_LEFT:
return 'navLeft';
case VK_RIGHT:
return 'navRight';
case VK_UP:
return 'navUp';
case VK_DOWN:
return 'navDown';
}
};
navigateTo = function($el) {
if (isFocusable($el)) {
return $el.focus();
} else {
return $el.triggerHandler('focus');
}
};
isFocusable = function($el) {
return $el.is('a, input, button, select');
};
lastFocused = null;
$('.menu .option').focus(function() {
return lastFocused = this;
});
$('.menu').focus(function() {
return $(lastFocused || '.menu .option:first').focus();
});
$('.content').focus(function() {
return $(this).find('.item:first').focus();
});
addNavigationData = function(elements, direction) {
var el, i, nextDir, prevDir, _i, _len, _results;
prevDir = direction === 'horizontal' ? 'navLeft' : 'navUp';
nextDir = direction === 'horizontal' ? 'navRight' : 'navDown';
_results = [];
for (i = _i = 0, _len = elements.length; _i < _len; i = ++_i) {
el = elements[i];
if (i > 0) {
$(el).data(prevDir, elements[i - 1]);
}
if (i < elements.length - 1) {
_results.push($(el).data(nextDir, elements[i + 1]));
} else {
_results.push(void 0);
}
}
return _results;
};
addNavigationData($('.menu .option'), 'vertical');
addNavigationData($('.content .item'), 'horizontal');
navigateTo($('.menu'));
}).call(this);