This repository has been archived by the owner on Sep 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TvNavigationController.js
366 lines (333 loc) · 12.1 KB
/
TvNavigationController.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* TvNavigationController class.
* Manages key navigation on a Webpage. Best for Navigation on TVs. Based on jQuery.
* Inspired by googleTV KeyController https://developers.google.com/tv/web/lib/jquery/keycontroller
*
* @author axten
* @version 1.0.0
*
* Licensed under the terms of MIT License. For the full copyright and license
* information, please see the LICENSE file in the root folder.
*
* @fileoverview Class for TvNavigationController.
* A page can use the TvNavigationController to manage the selection of elements with mouse or
* arrow keys and make sure that only one element is selected at a time. The controller can
* manage selection between multiple logical areas of the page, the "zones".
*
* A Zone can by a single row, a single column or a grid of navigation items. In it, a zone
* navigates logical an wrap around (e.g. select the left item when there is no item to
* select when the right arrow key is pressed) at zone borders if no transition is defined.
*
* To use the controller, just create an instance, add zones and call start method.
* If the enter key is pressed, the controller calls the click event.
* In grid-zones, every row need a wrapper with a css class (item_class suffixed with '_row').
*
* @constructor
* @param {string} item_class The css class to select navigation items in a navigation zone,
* default is 'item'.
* @param {string} active_class The css class that navigation items get if selected, default
* is 'selected'.
* @param {object(keyname:keycode)} custom_key_codes The normal browser mapping is extended with
* these custom keycodes, e.g. { KEY_ENTER: 13 }.
*/
TvNavigationController = function(item_class, active_class, custom_key_codes)
{
this.zones = [];
this.active_zone = null;
this.active_item = jQuery();
this.active_item_index = 0;
this.active_item_is_first = false;
this.active_item_is_last = false;
this.active_row_index = 0;
this.active_row_is_first = false;
this.active_row_is_last = false;
this.active_item_is_first_in_row = false;
this.active_item_is_last_in_row = false;
this.started = false;
this.item_class = item_class || 'item';
this.active_class = active_class || 'selected';
this.keycodes = {
KEY_ENTER: 13,
KEY_RIGHT: 39,
KEY_LEFT: 37,
KEY_UP: 38,
KEY_DOWN: 40
};
if (custom_key_codes)
{
this.keycodes = jQuery.extend({}, this.keycodes, custom_key_codes);
}
};
TvNavigationController.prototype = {
/**
* Starts the navigation controller. Zones can be added to the controller both before
* and after it is started.
* @param {jQuery.Element} initial_item Optional initial item to select. If not
* supplied, the first item of first zone is selected.
* @return {boolean} True if the controller starts successfully, else false.
*/
start: function(initial_item)
{
if (this.started || this.zones.length === 0)
{
return false;
}
if (initial_item && initial_item.jquery && initial_item.length > 0)
{
this.setSelectedItem(initial_item, this.getZoneByItem(initial_item));
}
else
{
this.setSelectedItem(this.zones[0].items.first(), this.zones[0]);
}
var that = this;
jQuery(document).on('keyup.navigation_controller', function(event)
{
that.handleKeyPress(event.keyCode);
event.preventDefault();
});
this.started = true;
return true;
},
/**
* Stops all navigation controller activity.
*/
stop: function()
{
if (this.started)
{
jQuery(document).off('.navigation_controller');
this.started = false;
}
},
/**
* Selects a given navigation item.
*
* @param {jQuery.Element} item The item to select.
*/
selectItem: function(item)
{
if (this.started)
{
this.setSelectedItem(item, this.getZoneByItem(item));
}
},
/**
* Adds a new navigation zone to the navigation controller.
*
* @param {string} selector The selector of the zone to add.
* @param {string(row||col||grid)} The type of the zone. Allowed values are 'row' for a single
* row, 'col' for single column and grid for a multi row grid. default is 'row'.
* @param {object(direction:transition)} transitions The Transitons to apply to change between
* zones, e.g. { left: {} }. Allowed values for direction are 'left', 'right', 'up' and
* 'down'. Allowed values for the transiton are a jQeury.Element, a function that returns a
* jQuery.Element or an empty object to abort a transiton.
* @param {function} inner_transitions A function that returns a jQuery.Element to manipulate
* the inner navigation of a zone.
*/
addZone: function(selector, type, transitions, inner_transitions)
{
var zone = {};
zone.element = jQuery(selector);
if (zone.element.length > 1)
{
zone.element = zone.element.first();
}
zone.items = jQuery('.' + this.item_class, zone.element);
if (zone.items.length === 0)
{
return false;
}
if (type === 'grid')
{
var rows = jQuery('.' + this.item_class + '_row', zone.element);
if (rows.length === 0)
{
return false;
}
zone.rows = [];
for (var i = 0; i < rows.length; i++)
{
zone.rows.push(jQuery('.' + this.item_class, rows[i]));
}
}
var that = this;
zone.items.on('mouseenter.navigation_controller', function(event)
{
that.handleMouseEnter(jQuery(event.currentTarget));
});
zone.type = (type === 'row' || type === 'col' || type === 'grid') ? type : 'row';
zone.transitions = transitions || {};
zone.inner_transitions = inner_transitions || null;
this.zones.push(zone);
return true;
},
/***** PRIVATE METHODS ***********************************************************************/
selectNext: function()
{
var next_index = (this.active_item_is_last) ? 0 : this.active_item_index + 1;
this.setSelectedItem(this.active_zone.items.eq(next_index));
},
selectPrevious: function()
{
var previous_index = (this.active_item_is_first) ? -1 : this.active_item_index - 1;
this.setSelectedItem(this.active_zone.items.eq(previous_index));
},
selectUpper: function()
{
var upper_row_index = (this.active_row_is_first) ? this.active_zone.rows.length - 1 : this.active_row_index - 1;
var upper_item_row_index = (this.active_zone.rows[upper_row_index].length < this.active_item_row_index + 1)
? -1 : this.active_item_row_index;
this.setSelectedItem(this.active_zone.rows[upper_row_index].eq(upper_item_row_index));
},
selectLower: function()
{
var lower_row_index = (this.active_row_is_last) ? 0 : this.active_row_index + 1;
var lower_item_row_index = (this.active_zone.rows[lower_row_index].length < this.active_item_row_index + 1)
? -1 : this.active_item_row_index;
this.setSelectedItem(this.active_zone.rows[lower_row_index].eq(lower_item_row_index));
},
processTransition: function(transition_item)
{
if (typeof transition_item === 'function')
{
transition_item = transition_item(this.active_item);
}
if (transition_item && transition_item.jquery && transition_item.length > 0)
{
this.setSelectedItem(transition_item, this.getZoneByItem(transition_item));
}
},
setSelectedItem: function(item, zone)
{
if (item.length > 1)
{
item = item.first();
}
if (zone)
{
this.active_zone = zone;
}
else
{
if (typeof this.active_zone.inner_transitions === 'function')
{
item = this.active_zone.inner_transitions(this.active_item, item);
}
}
if (!this.active_item || item[0] != this.active_item[0])
{
this.active_item.removeClass(this.active_class);
item.addClass(this.active_class);
item.focus();
this.active_item = item;
this.active_item_index = this.active_zone.items.index(item);
this.active_item_is_first = (this.active_item_index === 0);
this.active_item_is_last = (this.active_zone.items.length === (this.active_item_index + 1));
if (this.active_zone.type === 'grid')
{
this.active_row_index = this.getRowIndexByItem(item);
this.active_row_is_first = (this.active_row_index === 0);
this.active_row_is_last = (this.active_zone.rows.length === (this.active_row_index + 1));
this.active_item_row_index = this.active_zone.rows[this.active_row_index].index(item);
this.active_item_is_first_in_row = (this.active_item_row_index === 0);
this.active_item_is_last_in_row = (this.active_zone.rows[this.active_row_index].length === (this.active_item_row_index + 1));
}
}
else
{
/* workaround for opera spatial navigation */
item.focus();
}
},
getZoneByItem: function(item)
{
for (var i = 0; i < this.zones.length; i++)
{
if (this.zones[i].items.is(item))
{
return this.zones[i];
}
}
},
getRowIndexByItem: function(item)
{
for (var i = 0; i < this.active_zone.rows.length; i++)
{
if (this.active_zone.rows[i].is(item))
{
return i;
}
}
},
handleKeyPress: function(keycode)
{
var transitions = this.active_zone.transitions;
var is_row = (this.active_zone.type === 'row');
var is_col = (this.active_zone.type === 'col');
var is_grid = (this.active_zone.type === 'grid');
if (keycode === this.keycodes.KEY_ENTER)
{
this.active_item.click();
}
else if (keycode === this.keycodes.KEY_LEFT)
{
if (transitions.left && (is_col || (is_row && this.active_item_is_first) || (is_grid && this.active_item_is_first_in_row)))
{
this.processTransition(transitions.left);
}
else
{
this.selectPrevious();
}
}
else if (keycode === this.keycodes.KEY_RIGHT)
{
if (transitions.right && (is_col || (is_row && this.active_item_is_last) || (is_grid && this.active_item_is_last_in_row)))
{
this.processTransition(transitions.right);
}
else
{
this.selectNext();
}
}
else if (keycode === this.keycodes.KEY_UP)
{
if (transitions.up && (is_row || (is_col && this.active_item_is_first) || (is_grid && this.active_row_is_first)))
{
this.processTransition(transitions.up);
}
else if (is_grid)
{
this.selectUpper();
}
else
{
this.selectPrevious();
}
}
else if (keycode === this.keycodes.KEY_DOWN)
{
if (transitions.down && (is_row || (is_col && this.active_item_is_last) || (is_grid && this.active_row_is_last)))
{
this.processTransition(transitions.down);
}
else if (is_grid)
{
this.selectLower();
}
else
{
this.selectNext();
}
}
},
handleMouseEnter: function(item)
{
if (this.started)
{
this.setSelectedItem(item, this.getZoneByItem(item));
}
}
};