-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.freefocus.js
386 lines (299 loc) · 10.4 KB
/
jquery.freefocus.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
jQuery.Freefocus 0.11.2
Copyright (c) 2013-2016 Ilia Ablamonov. Licensed under the MIT license.
*/
(function ($) {
'use strict';
var freefocus = window.freefocus;
/*
### $.freefocus({options...}, {moveOptions...})
Set up keyboard navigation.
Options:
- `focusedSelector` - selector for currently focused (or active) element. default: `':focus'`
- `hoverFocus` - focus target elements on mouse enter. default: `false`
- `throttle` - throttle key input for specified time (in milliseconds).
You'll need underscore.js included to use this feature. default: `false`
Move options are passed to `$.fn.freefocus`
Usage:
```
$.freefocus({hoverFocus: true})
```
### $.freefocus('remove')
Remove previously set keyboard navigation.
### $.freefocus('cache', options)
Compute and cache dimension information for focusable elements. Options: `focusablesSelector`, `focusablesFilter`
*/
$.freefocus = function (setupOptions, moveOptions) {
if (setupOptions === 'remove') {
removeHandlers();
return;
}
if (setupOptions === 'cache') {
var cacheOptions = $.extend({}, $.freefocus.cacheOptions, moveOptions);
var targets = cacheOptions.targets;
if ($.isFunction(targets)) {
targets = targets(cacheOptions);
}
for (var i = 0, len = targets.length; i < len; i++) {
freefocus.populateDimensionsCache(targets[i]);
}
return;
}
setupOptions = $.extend({}, $.freefocus.setupOptions, setupOptions);
var keyHandler = function (move) {
var options = $.extend({}, moveOptions, { move: move });
$(setupOptions.focusedSelector).freefocus(options);
};
if (typeof setupOptions.throttle === 'number') {
keyHandler = _.throttle(keyHandler, setupOptions.throttle);
}
addHandler('keydown', function (event) {
var move = $.freefocus.keys[event.which];
if (!move)
return;
event.preventDefault();
keyHandler(move);
});
if (setupOptions.hoverFocus) {
if (!moveOptions.focusablesSelector) {
console.error('focusablesSelector is required for hoverFocus');
moveOptions.focusablesSelector = 'zzz';
}
addHandler('mouseenter', moveOptions.focusablesSelector, function () {
var trigger = (moveOptions || {}).trigger || $.freefocus.moveOptions.trigger;
var target = $(this);
if (setupOptions.focusablesFilter) {
target = target.filter(setupOptions.focusablesFilter);
}
return target.trigger(trigger);
});
}
};
/*
### .freefocus({moveOptions...})
Move 'focus' from active element to one of the targets by triggering specified event.
Options:
- `move` - move direction: `left` | `right` | `up` | `down`. no default
- `focusablesSelector` - selector for navigation targets. default: a long selector describing all focusable options in web browsers.
You may want to provide something shorter to improve performance or use `:focusable` from jQuery UI.
- `focusablesFilter` — selector that filters targets after they were selected using `focusablesSelector`.
Separated for performance reasons. default: `':visible'`
- `focusablesContext` — element or selector, conext for navigation targets search. default: `undefined`
- `targets` - jQuery object containing 'focusable' elements. no default
You should supply either focusablesSelector/Filter (preferred if you use nav-*) or explicit targets.
- `debug` - print weighting information over targets. default: `false`
- `trigger` - event to trigger on selected target. default: `'focus'`
- `preTrigger` - event to trigger on selected target before the `trigger` one. default: `false` (don't trigger)
Useful if `trigger` is `focus` to move the next focused element into view to avoid native behavior.
- `maxDistance` - maximum distance to element to still consider moving to it. default: `Infinity`
- `cache` - cache dimension information for element. default: `false`.
You'll need to manually reset cache for moved elements by using `$(element).freefocus('moved')`
Usage:
```
$(':focus').freefocus({move: 'right', targets: $('[tabindex]')})
```
### .freefocus('dimensions')
Get element dimensions `{top, left, width, height}`. Uses cache, if it's enabled.
### .freefocus('moved')
Clear cached dimension info for element. Should be triggered for every element that is moved, if using `cache`.
### `$.fn.freefocus('nav', hints)`
Set hints (see next chapter for details).
Hints is either:
- opbject: `{ left: 'none', right: '#someId' }`
- special string value `clear`, which would clear all nav hints from the element
*/
$.fn.freefocus = function (options, navHints) {
if (options === 'dimensions') {
if (!this.length) {
console.error('Can\'t get freefocus dimensions for empty jQuery object');
return { left: 0, top: 0, width: 0, height: 0 };
}
if (this.length > 1) {
console.error('Can\'t use freefocus on multiple element jQuery object');
// First element will be used
}
return freefocus.getDimensions(this[0]);
}
if (options === 'moved') {
this.each(function () {
freefocus.invalidateDimensionsCache(this);
});
return;
}
if (options === 'nav') {
if (navHints === 'clear') {
this.each(function () {
freefocus.clearHint(this);
});
} else {
this.each(function () {
freefocus.setHint(this, navHints);
});
}
return;
}
if (!this.length) {
if (options.debug) {
console.warn('freefocus called on empty jQuery object');
}
// It's useful to be silent in this case
return this;
}
if (this.length > 1) {
console.error('Can\'t use freefocus on multiple element jQuery object');
// First element will be used
}
var direction = options.move;
options = $.extend({}, $.freefocus.moveOptions, options);
freefocus.configuration.cache = options.cache;
freefocus.configuration.maxDistance = options.maxDistance;
if (!freefocus.configuration.directions[direction]) {
console.error('Unknown move direction "' + direction + '"');
return this;
}
if (!($.isFunction(options.targets) || $.isArray(options.targets) || options.targets.jquery)) {
console.warn('Argument targets should be a function, array, or jQuery object');
}
var candidatesFn = function (hintSelector) {
if (hintSelector) {
var targetFn = $.isFunction(options.targets) ? options.targets : defaultTargets;
var targetFnOptions = $.extend({}, options, { hintSelector: hintSelector });
return targetFn(targetFnOptions);
} else {
var targets = options.targets;
if ($.isFunction(targets)) {
targets = targets(options);
}
return targets;
}
};
var toEl = freefocus.move(this[0], direction, candidatesFn);
if (!toEl) {
// It's useful to be silent in this case
return this;
}
if (options.preTrigger) {
triggerEvent(toEl, options.preTrigger);
}
if (options.trigger) {
triggerEvent(toEl, options.trigger);
}
return $(toEl);
};
/*
Defaults:
*/
function defaultTargets(options) {
var result;
if (!options.focusablesSelector) {
console.error('Options should contain either focusablesSelector or targets');
return [];
}
if (options.hintSelector) {
var context = options.ignoreContextForHints ? undefined : options.focusablesContext;
result = $(options.hintSelector, context).filter(options.focusablesSelector);
} else {
result = $(options.focusablesSelector, options.focusablesContext);
}
if (options.focusablesFilter) {
result = result.filter(options.focusablesFilter);
}
return result.toArray();
}
$.freefocus.moveOptions = {
targets: defaultTargets,
focusablesSelector: [
'a[href]',
'area[href]',
'input:enabled',
'select:enabled',
'textarea:enabled',
'button:enabled',
'iframe',
'object',
'embed',
'*[tabindex]',
'*[contenteditable]'
].join(', '),
focusablesFilter: ':visible',
focusablesContext: undefined,
ignoreContextForHints: true,
trigger: 'focus',
preTrigger: false,
debug: false,
maxDistance: Infinity
};
$.freefocus.setupOptions = {
focusedSelector: ':focus',
hoverFocus: false,
throttle: false
};
$.freefocus.cacheOptions = {
targets: defaultTargets
};
$.freefocus.keys = {
37: 'left',
38: 'up',
39: 'right',
40: 'down'
};
freefocus.configuration.hintSources.push(
function (element, direction) {
var propName = 'nav' + (direction.charAt(0).toUpperCase()) + (direction.slice(1));
var hint = element.style[propName];
return fixToshiba(hint);
},
function (element, direction) {
var style = $.trim(element.getAttribute('style'));
var hint = style && parseStyleString(style)['nav-' + direction];
return fixToshiba(hint);
}
);
/*
Private:
*/
function fixToshiba(hint) {
// Toshiba removes "#" from the beginning and adds " ''" to the end
return hint && hint.replace(/^([^#].*) ''$/, '#$1');
}
function parseStyleString(style) {
var result = {};
style.split(';').forEach(function (rule) {
var kv = rule.split(':');
if (kv[1])
result[kv[0].trim()] = kv[1].trim();
});
return result;
}
/*
Event handlers:
*/
// Rewrite using jQuery's event namespaces instead
var eventHandlers = [];
var $document = $(document);
function addHandler() {
$document.on.apply($document, arguments);
eventHandlers.push(arguments);
}
function removeHandlers() {
eventHandlers.forEach(function (args) {
$document.off.apply($document, args);
});
eventHandlers = [];
}
/*
Trigger event optimization:
*/
function triggerEvent(target, eventName) {
if (document.createEvent && target.dispatchEvent && eventName !== 'focus') {
// Native dispatchEvent is way faster than $.fn.trigger
// Focus is a special event, handled natively by jQuery itself
var event = document.createEvent('Event');
event.initEvent(eventName, true, true);
target.dispatchEvent(event);
} else {
// Safe fallback
$(target).trigger($.Event(eventName));
}
}
})(jQuery);