-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.coffee
More file actions
80 lines (70 loc) · 2.63 KB
/
code.coffee
File metadata and controls
80 lines (70 loc) · 2.63 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
@VK_LEFT or= 37
@VK_RIGHT or= 39
@VK_UP or= 38
@VK_DOWN or= 40
$(document).keydown (e) ->
if handleNavigationKey e.target, e.keyCode
e.preventDefault() # Prevent Philips NetTV's default navigation.
# A simple navigation handler. Reads the data-nav-* properties of an element to
# see where to navigate to. For example, the HTML:
#
# <button data-nav-left="#other-element">Some Button</button>
#
# declares that when that button is focused and the left key is pressed, the
# element that corresponds to the selector "#other-element" will be focused.
#
# If no navigation data that correspònds to the given key is declared in the
# element, the function will "bubble" to the element's parent and so on.
#
# The possible data-nav-* values are "left", "right", "up" and "down". The value
# can be either a selector or an element, and can be set programmatically:
#
# $(someElement).data('navLeft', otherElement)
#
# Returns true if the keyCode was a navigation key (and therefore was handled)
# or false otherwise.
handleNavigationKey = (el, keyCode) ->
direction = directionForKeys keyCode
return false unless el and direction
nextElSelector = $(el).data(direction)
if nextElSelector
navigateTo $(nextElSelector)
else
handleNavigationKey el.parentNode, keyCode
true
directionForKeys = (keyCode) ->
switch keyCode
when VK_LEFT then 'navLeft'
when VK_RIGHT then 'navRight'
when VK_UP then 'navUp'
when VK_DOWN then 'navDown'
navigateTo = ($el) ->
if isFocusable $el
$el.focus()
else
# On some brosers (Samsung) the focus event cannot be triggered on
# non-focusable elements, so the attached handlers are called directly.
$el.triggerHandler 'focus'
isFocusable = ($el) ->
$el.is 'a, input, button, select'
# The menu remembers which element was last focused.
lastFocused = null
$('.menu .option').focus ->
lastFocused = @
$('.menu').focus ->
$(lastFocused or '.menu .option:first').focus()
# The content panel just focuses the first item.
$('.content').focus ->
$(@).find('.item:first').focus()
# Adds navLeft/navRight or navUp/navDown data to a sequence of elements.
# `direction` must be "horizontal" or "vertical"
addNavigationData = (elements, direction) ->
prevDir = if direction is 'horizontal' then 'navLeft' else 'navUp'
nextDir = if direction is 'horizontal' then 'navRight' else 'navDown'
for el, i in elements
$(el).data(prevDir, elements[i - 1]) if i > 0
$(el).data(nextDir, elements[i + 1]) if i < elements.length - 1
addNavigationData $('.menu .option'), 'vertical'
addNavigationData $('.content .item'), 'horizontal'
# Menu is focused at the start.
navigateTo $('.menu')