This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.treevue.js
397 lines (339 loc) · 13.9 KB
/
jquery.treevue.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
387
388
389
390
391
392
393
394
395
396
397
/*jslint white: true */
/**
* Treevue is a jQuery plugin for accessible tree views. It uses wai-aria
* for full feature support with modern screenreaders.
*
*
*/
(function($) {
'use strict';
var treeFallback, branchFallback,
// ARIA-properties
ariaExp = 'aria-expanded',
ariaSel = 'aria-selected',
ariaHide = 'aria-hidden',
ariaDisable = 'aria-disabled',
role = 'role',
// className values
focusClass = 'treevue-focus',
expandedCls = 'treevue-expanded',
collapseCls = 'treevue-collapsed',
selectedCls = 'treevue-selected',
selectableCls = 'treevue-selectable',
disableCls = 'treevue-disabled',
useAriaCls = 'treevue-aria-on',
// Text for l10n
textExpanded = 'Collapse node',
textCollapsed = 'Expand node',
textTree = 'Tree view',
// Setup properties of the fallback
fallbackCss = {
'position': 'absolute',
'width': '0',
'overflow': 'hidden'
},
fallbackAria = {};
fallbackAria[role] = 'document';
fallbackAria[ariaHide] = true;
// Set up nodes that work as fallbacks for AT that don't
// support ARIA
treeFallback = $('<span class="treevue_fallback">' +
textTree + ', </span>').css(fallbackCss);
branchFallback = $('<span class="treevue_fallback_branch">' +
'<button>' + textExpanded +
'</button></span>').css(fallbackCss);
/**
* Return the value of the checkbox. If none is given the label is returned
*/
function getCheckboxValue(i, node) {
var label,
box = $(node),
val = box.attr('value'),
type = box.closest('.treevue li').attr("data-treevue-type");
if (!val) {
label = $('label[for=' + box.context.id + ']');
if (label.length === 0) {
label = box.closest('label');
}
if (label) {
val = $.trim(label.text());
}
}
return {
type: type,
value: val,
id: box.prop('id')
};
}
/**
* Add treeview fallbacks for screen readers
*/
function addFallbacks(trees, options) {
var nodeFbClone = branchFallback.clone(),
treeFbClone = treeFallback.clone(),
// define tree nodes
treeitems = trees.find('li'),
// define branches
branches = trees.find('ul, ol');
if (options.useAria) {
nodeFbClone.attr(fallbackAria)
.find('button').attr('tabindex', -1);
treeFbClone.attr(fallbackAria);
}
treeitems.first().prepend(treeFbClone);
branches.closest('li').addClass(expandedCls)
.prepend(nodeFbClone);
}
function addAriaRoles(trees) {
trees.attr(role, 'tree');
trees.find('li').attr(role, 'treeitem');
trees.find('ul, ol').attr(role, 'group')
.find('.'+expandedCls).attr(ariaExp, true);
trees.find('.'+collapseCls).attr(ariaExp, 'false')
.find('ul, ol').first().attr(ariaHide, true);
trees.find('.'+disableCls).attr(ariaDisable, true);
}
// Collapse nodes nested within a list with aria-hidden
function collapseHiddenBranches(trees, options) {
var hiddenBranches = trees.find('ul['+ariaHide+'=true], ' +
'ol['+ariaHide+'=true]');
hiddenBranches.hide()
.closest('li').find('.' + expandedCls).andSelf()
.removeClass(expandedCls).addClass(collapseCls);
trees.find(':disabled').closest('li').addClass(disableCls);
if (!options.useAria) {
hiddenBranches.removeAttr(ariaHide);
}
}
function addArrowKeyFeatures(trees) {
trees.find(':checkbox').attr('tabindex', -1);
trees.find('li').attr('tabindex', -1);
trees.find('> :first-child').
attr('tabindex', 0).addClass(focusClass);
}
/**
* Where checkboxes are included allow selection
*/
function addSelectStates(trees, options) {
trees = trees.find(':checkbox').each(function () {
var boxes,
$this = $(this),
treeitem = $this.closest('li')
.addClass(selectableCls);
if ($this.prop('checked')) {
$this.addClass(selectedCls);
}
if (options.useAria) {
treeitem.attr(ariaSel, !!$this.prop('checked'));
}
if ($this.is('[data-type="subselector"]')) {
boxes = treeitem.find(':checkbox:not(:first())');
if (boxes.length !== 0) {
$this.prop('checked',
boxes.length === boxes.filter(':checked').length);
}
}
});
if (options.useAria) {
trees.closest('.treevue').attr('aria-multiselectable', true);
}
}
/**
* TreeVue jQuery plugin; accessible treeview
*/
$.fn.treevue = function(options) {
if (typeof options === 'undefined') {
options = {};
}
if (typeof options.useAria === 'undefined') {
options.useAria = true;
}
this.addClass('treevue');
// Add WAI-ARIA role and state
addFallbacks(this, options);
collapseHiddenBranches(this, options);
addSelectStates(this, options);
if (options.useAria) {
this.addClass(useAriaCls);
addAriaRoles(this);
addArrowKeyFeatures(this);
}
return this;
};
// When the document is loaded, attach event handlers for all vuetrees
$(function () {
/**
* Toggle the visibility of the branch
*/
function toggleBranch(branch) {
var eventProps = {target: branch.context},
fallbackButton = branch.find('.treevue_fallback_branch button').first(),
isExpanded = branch.hasClass(expandedCls),
subtree = $();
branch.each(function () {
subtree = subtree.add($('ul, ol', this).first());
});
if (branch.closest('.treevue').hasClass(useAriaCls)) {
branch.attr(ariaExp, !isExpanded);
subtree.attr(ariaHide, isExpanded);
}
if (isExpanded) {
branch.addClass(collapseCls).removeClass(expandedCls)
.trigger($.Event('treevue:collapse', eventProps));
fallbackButton.text(textCollapsed);
subtree.hide(200);
} else {
branch.addClass(expandedCls).removeClass(collapseCls)
.trigger($.Event('treevue:expand', eventProps));
fallbackButton.text(textExpanded);
subtree.show(200);
}
}
/**
* Move the focus to an item in the tree.
*/
function focusItem(elm) {
var tree = elm.closest('.treevue');
if (tree.length === 1 && tree.hasClass(useAriaCls)) {
tree.find('.' + focusClass).removeClass(focusClass).attr(
'tabindex', -1);
elm.focus().attr('tabindex', 0).
addClass(focusClass);
// Move the tree fallback
tree.find('.treevue_fallback').detach().prependTo(elm);
}
}
/**
* When a checkbox is changed, update the aria-selected state.
*/
function checkboxChange(box) {
var item = box.closest('.'+ selectedCls),
checked = box.prop('checked'),
tree = box.closest('.treevue'),
node = box.closest('li');
// update the selected state
if (item.closest('.treevue').hasClass(useAriaCls)) {
item.attr(ariaSel, checked);
}
// select / unselect all children when the node is a subselector
if (box.attr('data-type') === 'subselector') {
node.find('ul :checkbox, ol :checkbox').
not(':disabled').prop('checked', checked);
}
// Locate any parent nodes
node.parentsUntil(tree, 'li').each(function () {
var boxes,
$this = $(this),
checkbox = $(':checkbox', this).first();
// check if the parents have a subselector
if (checkbox.closest('li').is($this) &&
checkbox.attr('data-type') === 'subselector') {
// All boxes are checked to check the subselector
boxes = $this.find('ul :checkbox, ol :checkbox').
not(':disabled');
if (boxes.length === boxes.filter(':checked').length) {
checkbox.prop('checked', true);
// Subselector is unchecked
} else if (checked === false) {
checkbox.prop('checked', false);
}
}
});
// Fire a new event
tree.trigger($.Event('treevue:change', {
target: tree.context,
// Get all the values of selected items
values: tree.find(':checked').map(getCheckboxValue).get()
}));
}
/**
* pointer input
*/
$('body').on('click', '.treevue li.' + expandedCls +
', .treevue li.' + collapseCls, function (event) {
if (event.target === this) {
var $this = $(this);
event.preventDefault();
toggleBranch($this);
focusItem($this);
}
/**
* Keep aria-selected state in sync with the checkbox
*/
}).on('change', '.treevue :checkbox', function () {
var $this = $(this);
checkboxChange($this);
focusItem($this.closest('li'));
/**
* keyboard input
*/
}).on('keydown',
'.treevue.'+ useAriaCls + ' li',
function (event) {
var expanded, checkbox,
keyCode = event.keyCode,
$this = $(this);
if (event.target !== this) {
return;
}
expanded = $this.hasClass(expandedCls);
// Press RETURN
if (keyCode === 13 && $this.hasClass(selectableCls)) {
//locate the checkbox and invert it and the select value
checkbox = $this.find(':checkbox').first();
if (checkbox.is(':not(:disabled)')) {
checkbox.prop('checked', !checkbox.prop('checked'));
checkboxChange(checkbox);
}
} else if (keyCode === 40) { // press DOWN
if (expanded) { // enter a branch
focusItem($this.find('ul li, ol li').first());
} else if ($this.next().length === 0) { // exit a branch
focusItem($this.parentsUntil('.treevue', 'li').
next().first());
} else { // next sibling
focusItem($this.next());
}
} else if (keyCode === 38) { // press UP
if ($this.prev().hasClass(expandedCls)) { // enter a branch
focusItem($this.prev().find('ul li, ol li').last());
} else if ($this.prev().length === 0) { // exit a branch
focusItem($this.parent().closest('li'));
} else { // prev sibling
focusItem($this.prev());
}
} else if (keyCode === 37) { // press LEFT
if (expanded) {
toggleBranch($this);
} else { // exit the current branch
focusItem($this.parent().closest('li'));
}
} else if (keyCode === 39) { // press RIGHT
if ($this.hasClass(collapseCls)) {
toggleBranch($this);
} else if (expanded) { // enter a branch
focusItem($this.find('ul li, ol li').first());
}
} else if (keyCode === 36) { // press HOME
focusItem($this.closest('.treevue').find('li:visible').first());
} else if (keyCode === 35) { // press END
focusItem($this.closest('.treevue').find('li:visible').last());
} else if (keyCode === 106) { // press keypad asterisk
toggleBranch($this.closest('.treevue').
find('li.' + collapseCls));
} else { // no known keys activated, so nothing has to be prevented
return;
}
event.preventDefault();
event.stopPropagation();
return false;
// Toggle the branch when clicking the fallback button
}).on('click', '.treevue_fallback_branch button', function () {
toggleBranch($(this).closest('li'));
}).on('focus', '.treevue_fallback_branch', function () {
$(this).parent().addClass('treeitem_focus');
}).on('blur', '.treevue_fallback_branch', function () {
$(this).parent().removeClass('treeitem_focus');
});
});
}(jQuery));