| 1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Implement an accessible aria tree widget, from a nested unordered list.
|
|
|
18 |
* Based on http://oaa-accessibility.org/example/41/
|
|
|
19 |
*
|
|
|
20 |
* To respond to selection changed events - use tree.on("selectionchanged", handler).
|
|
|
21 |
* The handler will receive an array of nodes, which are the list items that are currently
|
|
|
22 |
* selected. (Or a single node if multiselect is disabled).
|
|
|
23 |
*
|
|
|
24 |
* @module tool_lp/tree
|
|
|
25 |
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
define(['jquery', 'core/url', 'core/log'], function($, url, log) {
|
|
|
29 |
// Private variables and functions.
|
|
|
30 |
/** @var {String} expandedImage The html for an expanded tree node twistie. */
|
| 1441 |
ariadna |
31 |
var expandedImage = $('<img alt="" class="icon" src="' + url.imageUrl('t/expanded') + '"/>');
|
| 1 |
efrain |
32 |
/** @var {String} collapsedImage The html for a collapsed tree node twistie. */
|
| 1441 |
ariadna |
33 |
var collapsedImage = $('<img alt="" class="icon" src="' + url.imageUrl('t/collapsed') + '"/>');
|
| 1 |
efrain |
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Constructor
|
|
|
37 |
*
|
|
|
38 |
* @param {String} selector
|
|
|
39 |
* @param {Boolean} multiSelect
|
|
|
40 |
*/
|
|
|
41 |
var Tree = function(selector, multiSelect) {
|
|
|
42 |
this.treeRoot = $(selector);
|
|
|
43 |
this.multiSelect = (typeof multiSelect === 'undefined' || multiSelect === true);
|
|
|
44 |
|
|
|
45 |
this.items = this.treeRoot.find('li');
|
|
|
46 |
this.expandAll = this.items.length < 20;
|
|
|
47 |
this.parents = this.treeRoot.find('li:has(ul)');
|
|
|
48 |
|
|
|
49 |
if (multiSelect) {
|
|
|
50 |
this.treeRoot.attr('aria-multiselectable', 'true');
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
this.items.attr('aria-selected', 'false');
|
|
|
54 |
|
|
|
55 |
this.visibleItems = null;
|
|
|
56 |
this.activeItem = null;
|
|
|
57 |
this.lastActiveItem = null;
|
|
|
58 |
|
|
|
59 |
this.keys = {
|
|
|
60 |
tab: 9,
|
|
|
61 |
enter: 13,
|
|
|
62 |
space: 32,
|
|
|
63 |
pageup: 33,
|
|
|
64 |
pagedown: 34,
|
|
|
65 |
end: 35,
|
|
|
66 |
home: 36,
|
|
|
67 |
left: 37,
|
|
|
68 |
up: 38,
|
|
|
69 |
right: 39,
|
|
|
70 |
down: 40,
|
|
|
71 |
eight: 56,
|
|
|
72 |
asterisk: 106
|
|
|
73 |
};
|
|
|
74 |
|
|
|
75 |
this.init();
|
|
|
76 |
|
|
|
77 |
this.bindEventHandlers();
|
|
|
78 |
};
|
|
|
79 |
// Public variables and functions.
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Init this tree
|
|
|
83 |
* @method init
|
|
|
84 |
*/
|
|
|
85 |
Tree.prototype.init = function() {
|
|
|
86 |
this.parents.attr('aria-expanded', 'true');
|
|
|
87 |
this.parents.prepend(expandedImage.clone());
|
|
|
88 |
|
|
|
89 |
this.items.attr('role', 'tree-item');
|
|
|
90 |
this.items.attr('tabindex', '-1');
|
|
|
91 |
this.parents.attr('role', 'group');
|
|
|
92 |
this.treeRoot.attr('role', 'tree');
|
|
|
93 |
|
|
|
94 |
this.visibleItems = this.treeRoot.find('li');
|
|
|
95 |
|
|
|
96 |
var thisObj = this;
|
|
|
97 |
if (!this.expandAll) {
|
|
|
98 |
this.parents.each(function() {
|
|
|
99 |
thisObj.collapseGroup($(this));
|
|
|
100 |
});
|
|
|
101 |
this.expandGroup(this.parents.first());
|
|
|
102 |
}
|
|
|
103 |
};
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Expand a collapsed group.
|
|
|
107 |
*
|
|
|
108 |
* @method expandGroup
|
|
|
109 |
* @param {Object} item is the jquery id of the parent item of the group
|
|
|
110 |
*/
|
|
|
111 |
Tree.prototype.expandGroup = function(item) {
|
|
|
112 |
// Find the first child ul node.
|
|
|
113 |
var group = item.children('ul');
|
|
|
114 |
|
|
|
115 |
// Expand the group.
|
|
|
116 |
group.show().attr('aria-hidden', 'false');
|
|
|
117 |
|
|
|
118 |
item.attr('aria-expanded', 'true');
|
|
|
119 |
|
|
|
120 |
item.children('img').attr('src', expandedImage.attr('src'));
|
|
|
121 |
|
|
|
122 |
// Update the list of visible items.
|
|
|
123 |
this.visibleItems = this.treeRoot.find('li:visible');
|
|
|
124 |
};
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Collapse an expanded group.
|
|
|
128 |
*
|
|
|
129 |
* @method collapseGroup
|
|
|
130 |
* @param {Object} item is the jquery id of the parent item of the group
|
|
|
131 |
*/
|
|
|
132 |
Tree.prototype.collapseGroup = function(item) {
|
|
|
133 |
var group = item.children('ul');
|
|
|
134 |
|
|
|
135 |
// Collapse the group.
|
|
|
136 |
group.hide().attr('aria-hidden', 'true');
|
|
|
137 |
|
|
|
138 |
item.attr('aria-expanded', 'false');
|
|
|
139 |
|
|
|
140 |
item.children('img').attr('src', collapsedImage.attr('src'));
|
|
|
141 |
|
|
|
142 |
// Update the list of visible items.
|
|
|
143 |
this.visibleItems = this.treeRoot.find('li:visible');
|
|
|
144 |
};
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Expand or collapse a group.
|
|
|
148 |
*
|
|
|
149 |
* @method toggleGroup
|
|
|
150 |
* @param {Object} item is the jquery id of the parent item of the group
|
|
|
151 |
*/
|
|
|
152 |
Tree.prototype.toggleGroup = function(item) {
|
|
|
153 |
if (item.attr('aria-expanded') == 'true') {
|
|
|
154 |
this.collapseGroup(item);
|
|
|
155 |
} else {
|
|
|
156 |
this.expandGroup(item);
|
|
|
157 |
}
|
|
|
158 |
};
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Whenever the currently selected node has changed, trigger an event using this function.
|
|
|
162 |
*
|
|
|
163 |
* @method triggerChange
|
|
|
164 |
*/
|
|
|
165 |
Tree.prototype.triggerChange = function() {
|
|
|
166 |
var allSelected = this.items.filter('[aria-selected=true]');
|
|
|
167 |
if (!this.multiSelect) {
|
|
|
168 |
allSelected = allSelected.first();
|
|
|
169 |
}
|
|
|
170 |
this.treeRoot.trigger('selectionchanged', {selected: allSelected});
|
|
|
171 |
};
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Select all the items between the last focused item and this currently focused item.
|
|
|
175 |
*
|
|
|
176 |
* @method multiSelectItem
|
|
|
177 |
* @param {Object} item is the jquery id of the newly selected item.
|
|
|
178 |
*/
|
|
|
179 |
Tree.prototype.multiSelectItem = function(item) {
|
|
|
180 |
if (!this.multiSelect) {
|
|
|
181 |
this.items.attr('aria-selected', 'false');
|
|
|
182 |
} else if (this.lastActiveItem !== null) {
|
|
|
183 |
var lastIndex = this.visibleItems.index(this.lastActiveItem);
|
|
|
184 |
var currentIndex = this.visibleItems.index(this.activeItem);
|
|
|
185 |
var oneItem = null;
|
|
|
186 |
|
|
|
187 |
while (lastIndex < currentIndex) {
|
|
|
188 |
oneItem = $(this.visibleItems.get(lastIndex));
|
|
|
189 |
oneItem.attr('aria-selected', 'true');
|
|
|
190 |
lastIndex++;
|
|
|
191 |
}
|
|
|
192 |
while (lastIndex > currentIndex) {
|
|
|
193 |
oneItem = $(this.visibleItems.get(lastIndex));
|
|
|
194 |
oneItem.attr('aria-selected', 'true');
|
|
|
195 |
lastIndex--;
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
item.attr('aria-selected', 'true');
|
|
|
200 |
this.triggerChange();
|
|
|
201 |
};
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Select a single item. Make sure all the parents are expanded. De-select all other items.
|
|
|
205 |
*
|
|
|
206 |
* @method selectItem
|
|
|
207 |
* @param {Object} item is the jquery id of the newly selected item.
|
|
|
208 |
*/
|
|
|
209 |
Tree.prototype.selectItem = function(item) {
|
|
|
210 |
// Expand all nodes up the tree.
|
|
|
211 |
var walk = item.parent();
|
|
|
212 |
while (walk.attr('role') != 'tree') {
|
|
|
213 |
walk = walk.parent();
|
|
|
214 |
if (walk.attr('aria-expanded') == 'false') {
|
|
|
215 |
this.expandGroup(walk);
|
|
|
216 |
}
|
|
|
217 |
walk = walk.parent();
|
|
|
218 |
}
|
|
|
219 |
this.items.attr('aria-selected', 'false');
|
|
|
220 |
item.attr('aria-selected', 'true');
|
|
|
221 |
this.triggerChange();
|
|
|
222 |
};
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* Toggle the selected state for an item back and forth.
|
|
|
226 |
*
|
|
|
227 |
* @method toggleItem
|
|
|
228 |
* @param {Object} item is the jquery id of the item to toggle.
|
|
|
229 |
*/
|
|
|
230 |
Tree.prototype.toggleItem = function(item) {
|
|
|
231 |
if (!this.multiSelect) {
|
|
|
232 |
this.selectItem(item);
|
|
|
233 |
return;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
var current = item.attr('aria-selected');
|
|
|
237 |
if (current === 'true') {
|
|
|
238 |
current = 'false';
|
|
|
239 |
} else {
|
|
|
240 |
current = 'true';
|
|
|
241 |
}
|
|
|
242 |
item.attr('aria-selected', current);
|
|
|
243 |
this.triggerChange();
|
|
|
244 |
};
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
* Set the focus to this item.
|
|
|
248 |
*
|
|
|
249 |
* @method updateFocus
|
|
|
250 |
* @param {Object} item is the jquery id of the parent item of the group
|
|
|
251 |
*/
|
|
|
252 |
Tree.prototype.updateFocus = function(item) {
|
|
|
253 |
this.lastActiveItem = this.activeItem;
|
|
|
254 |
this.activeItem = item;
|
|
|
255 |
// Expand all nodes up the tree.
|
|
|
256 |
var walk = item.parent();
|
|
|
257 |
while (walk.attr('role') != 'tree') {
|
|
|
258 |
walk = walk.parent();
|
|
|
259 |
if (walk.attr('aria-expanded') == 'false') {
|
|
|
260 |
this.expandGroup(walk);
|
|
|
261 |
}
|
|
|
262 |
walk = walk.parent();
|
|
|
263 |
}
|
|
|
264 |
this.items.attr('tabindex', '-1');
|
|
|
265 |
item.attr('tabindex', 0);
|
|
|
266 |
};
|
|
|
267 |
|
|
|
268 |
/**
|
|
|
269 |
* Handle a key down event - ie navigate the tree.
|
|
|
270 |
*
|
|
|
271 |
* @method handleKeyDown
|
|
|
272 |
* @param {Object} item is the jquery id of the parent item of the group
|
|
|
273 |
* @param {Event} e The event.
|
|
|
274 |
* @return {Boolean}
|
|
|
275 |
*/
|
|
|
276 |
// This function should be simplified. In the meantime..
|
|
|
277 |
// eslint-disable-next-line complexity
|
|
|
278 |
Tree.prototype.handleKeyDown = function(item, e) {
|
|
|
279 |
var currentIndex = this.visibleItems.index(item);
|
|
|
280 |
var newItem = null;
|
|
|
281 |
var hasKeyModifier = e.shiftKey || e.ctrlKey || e.metaKey || e.altKey;
|
|
|
282 |
var thisObj = this;
|
|
|
283 |
|
|
|
284 |
switch (e.keyCode) {
|
|
|
285 |
case this.keys.home: {
|
|
|
286 |
// Jump to first item in tree.
|
|
|
287 |
newItem = this.parents.first();
|
|
|
288 |
newItem.focus();
|
|
|
289 |
if (e.shiftKey) {
|
|
|
290 |
this.multiSelectItem(newItem);
|
|
|
291 |
} else if (!hasKeyModifier) {
|
|
|
292 |
this.selectItem(newItem);
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
e.stopPropagation();
|
|
|
296 |
return false;
|
|
|
297 |
}
|
|
|
298 |
case this.keys.end: {
|
|
|
299 |
// Jump to last visible item.
|
|
|
300 |
newItem = this.visibleItems.last();
|
|
|
301 |
newItem.focus();
|
|
|
302 |
if (e.shiftKey) {
|
|
|
303 |
this.multiSelectItem(newItem);
|
|
|
304 |
} else if (!hasKeyModifier) {
|
|
|
305 |
this.selectItem(newItem);
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
e.stopPropagation();
|
|
|
309 |
return false;
|
|
|
310 |
}
|
|
|
311 |
case this.keys.enter:
|
|
|
312 |
case this.keys.space: {
|
|
|
313 |
|
|
|
314 |
if (e.shiftKey) {
|
|
|
315 |
this.multiSelectItem(item);
|
|
|
316 |
} else if (e.metaKey || e.ctrlKey) {
|
|
|
317 |
this.toggleItem(item);
|
|
|
318 |
} else {
|
|
|
319 |
this.selectItem(item);
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
e.stopPropagation();
|
|
|
323 |
return false;
|
|
|
324 |
}
|
|
|
325 |
case this.keys.left: {
|
|
|
326 |
if (item.has('ul') && item.attr('aria-expanded') == 'true') {
|
|
|
327 |
this.collapseGroup(item);
|
|
|
328 |
} else {
|
|
|
329 |
// Move up to the parent.
|
|
|
330 |
var itemUL = item.parent();
|
|
|
331 |
var itemParent = itemUL.parent();
|
|
|
332 |
if (itemParent.is('li')) {
|
|
|
333 |
itemParent.focus();
|
|
|
334 |
if (e.shiftKey) {
|
|
|
335 |
this.multiSelectItem(itemParent);
|
|
|
336 |
} else if (!hasKeyModifier) {
|
|
|
337 |
this.selectItem(itemParent);
|
|
|
338 |
}
|
|
|
339 |
}
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
e.stopPropagation();
|
|
|
343 |
return false;
|
|
|
344 |
}
|
|
|
345 |
case this.keys.right: {
|
|
|
346 |
if (item.has('ul') && item.attr('aria-expanded') == 'false') {
|
|
|
347 |
this.expandGroup(item);
|
|
|
348 |
} else {
|
|
|
349 |
// Move to the first item in the child group.
|
|
|
350 |
newItem = item.children('ul').children('li').first();
|
|
|
351 |
if (newItem.length > 0) {
|
|
|
352 |
newItem.focus();
|
|
|
353 |
if (e.shiftKey) {
|
|
|
354 |
this.multiSelectItem(newItem);
|
|
|
355 |
} else if (!hasKeyModifier) {
|
|
|
356 |
this.selectItem(newItem);
|
|
|
357 |
}
|
|
|
358 |
}
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
e.stopPropagation();
|
|
|
362 |
return false;
|
|
|
363 |
}
|
|
|
364 |
case this.keys.up: {
|
|
|
365 |
|
|
|
366 |
if (currentIndex > 0) {
|
|
|
367 |
var prev = this.visibleItems.eq(currentIndex - 1);
|
|
|
368 |
prev.focus();
|
|
|
369 |
if (e.shiftKey) {
|
|
|
370 |
this.multiSelectItem(prev);
|
|
|
371 |
} else if (!hasKeyModifier) {
|
|
|
372 |
this.selectItem(prev);
|
|
|
373 |
}
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
e.stopPropagation();
|
|
|
377 |
return false;
|
|
|
378 |
}
|
|
|
379 |
case this.keys.down: {
|
|
|
380 |
|
|
|
381 |
if (currentIndex < this.visibleItems.length - 1) {
|
|
|
382 |
var next = this.visibleItems.eq(currentIndex + 1);
|
|
|
383 |
next.focus();
|
|
|
384 |
if (e.shiftKey) {
|
|
|
385 |
this.multiSelectItem(next);
|
|
|
386 |
} else if (!hasKeyModifier) {
|
|
|
387 |
this.selectItem(next);
|
|
|
388 |
}
|
|
|
389 |
}
|
|
|
390 |
e.stopPropagation();
|
|
|
391 |
return false;
|
|
|
392 |
}
|
|
|
393 |
case this.keys.asterisk: {
|
|
|
394 |
// Expand all groups.
|
|
|
395 |
this.parents.each(function() {
|
|
|
396 |
thisObj.expandGroup($(this));
|
|
|
397 |
});
|
|
|
398 |
|
|
|
399 |
e.stopPropagation();
|
|
|
400 |
return false;
|
|
|
401 |
}
|
|
|
402 |
case this.keys.eight: {
|
|
|
403 |
if (e.shiftKey) {
|
|
|
404 |
// Expand all groups.
|
|
|
405 |
this.parents.each(function() {
|
|
|
406 |
thisObj.expandGroup($(this));
|
|
|
407 |
});
|
|
|
408 |
|
|
|
409 |
e.stopPropagation();
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
return false;
|
|
|
413 |
}
|
|
|
414 |
}
|
|
|
415 |
|
|
|
416 |
return true;
|
|
|
417 |
};
|
|
|
418 |
|
|
|
419 |
/**
|
|
|
420 |
* Handle a key press event - ie navigate the tree.
|
|
|
421 |
*
|
|
|
422 |
* @method handleKeyPress
|
|
|
423 |
* @param {Object} item is the jquery id of the parent item of the group
|
|
|
424 |
* @param {Event} e The event.
|
|
|
425 |
* @return {Boolean}
|
|
|
426 |
*/
|
|
|
427 |
Tree.prototype.handleKeyPress = function(item, e) {
|
|
|
428 |
if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) {
|
|
|
429 |
// Do nothing.
|
|
|
430 |
return true;
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
switch (e.keyCode) {
|
|
|
434 |
case this.keys.tab: {
|
|
|
435 |
return true;
|
|
|
436 |
}
|
|
|
437 |
case this.keys.enter:
|
|
|
438 |
case this.keys.home:
|
|
|
439 |
case this.keys.end:
|
|
|
440 |
case this.keys.left:
|
|
|
441 |
case this.keys.right:
|
|
|
442 |
case this.keys.up:
|
|
|
443 |
case this.keys.down: {
|
|
|
444 |
e.stopPropagation();
|
|
|
445 |
return false;
|
|
|
446 |
}
|
|
|
447 |
default : {
|
|
|
448 |
var chr = String.fromCharCode(e.which);
|
|
|
449 |
var match = false;
|
|
|
450 |
var itemIndex = this.visibleItems.index(item);
|
|
|
451 |
var itemCount = this.visibleItems.length;
|
|
|
452 |
var currentIndex = itemIndex + 1;
|
|
|
453 |
|
|
|
454 |
// Check if the active item was the last one on the list.
|
|
|
455 |
if (currentIndex == itemCount) {
|
|
|
456 |
currentIndex = 0;
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
// Iterate through the menu items (starting from the current item and wrapping) until a match is found
|
|
|
460 |
// or the loop returns to the current menu item.
|
|
|
461 |
while (currentIndex != itemIndex) {
|
|
|
462 |
|
|
|
463 |
var currentItem = this.visibleItems.eq(currentIndex);
|
|
|
464 |
var titleChr = currentItem.text().charAt(0);
|
|
|
465 |
|
|
|
466 |
if (currentItem.has('ul')) {
|
|
|
467 |
titleChr = currentItem.find('span').text().charAt(0);
|
|
|
468 |
}
|
|
|
469 |
|
|
|
470 |
if (titleChr.toLowerCase() == chr) {
|
|
|
471 |
match = true;
|
|
|
472 |
break;
|
|
|
473 |
}
|
|
|
474 |
|
|
|
475 |
currentIndex = currentIndex + 1;
|
|
|
476 |
if (currentIndex == itemCount) {
|
|
|
477 |
// Reached the end of the list, start again at the beginning.
|
|
|
478 |
currentIndex = 0;
|
|
|
479 |
}
|
|
|
480 |
}
|
|
|
481 |
|
|
|
482 |
if (match === true) {
|
|
|
483 |
this.updateFocus(this.visibleItems.eq(currentIndex));
|
|
|
484 |
}
|
|
|
485 |
e.stopPropagation();
|
|
|
486 |
return false;
|
|
|
487 |
}
|
|
|
488 |
}
|
|
|
489 |
|
|
|
490 |
// eslint-disable-next-line no-unreachable
|
|
|
491 |
return true;
|
|
|
492 |
};
|
|
|
493 |
|
|
|
494 |
/**
|
|
|
495 |
* Attach an event listener to the tree.
|
|
|
496 |
*
|
|
|
497 |
* @method on
|
|
|
498 |
* @param {String} eventname This is the name of the event to listen for. Only 'selectionchanged' is supported for now.
|
|
|
499 |
* @param {Function} handler The function to call when the event is triggered.
|
|
|
500 |
*/
|
|
|
501 |
Tree.prototype.on = function(eventname, handler) {
|
|
|
502 |
if (eventname !== 'selectionchanged') {
|
|
|
503 |
log.warning('Invalid custom event name for tree. Only "selectionchanged" is supported.');
|
|
|
504 |
} else {
|
|
|
505 |
this.treeRoot.on(eventname, handler);
|
|
|
506 |
}
|
|
|
507 |
};
|
|
|
508 |
|
|
|
509 |
/**
|
|
|
510 |
* Handle a double click (expand/collapse).
|
|
|
511 |
*
|
|
|
512 |
* @method handleDblClick
|
|
|
513 |
* @param {Object} item is the jquery id of the parent item of the group
|
|
|
514 |
* @param {Event} e The event.
|
|
|
515 |
* @return {Boolean}
|
|
|
516 |
*/
|
|
|
517 |
Tree.prototype.handleDblClick = function(item, e) {
|
|
|
518 |
|
|
|
519 |
if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) {
|
|
|
520 |
// Do nothing.
|
|
|
521 |
return true;
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
// Apply the focus markup.
|
|
|
525 |
this.updateFocus(item);
|
|
|
526 |
|
|
|
527 |
// Expand or collapse the group.
|
|
|
528 |
this.toggleGroup(item);
|
|
|
529 |
|
|
|
530 |
e.stopPropagation();
|
|
|
531 |
return false;
|
|
|
532 |
};
|
|
|
533 |
|
|
|
534 |
/**
|
|
|
535 |
* Handle a click (select).
|
|
|
536 |
*
|
|
|
537 |
* @method handleExpandCollapseClick
|
|
|
538 |
* @param {Object} item is the jquery id of the parent item of the group
|
|
|
539 |
* @param {Event} e The event.
|
|
|
540 |
* @return {Boolean}
|
|
|
541 |
*/
|
|
|
542 |
Tree.prototype.handleExpandCollapseClick = function(item, e) {
|
|
|
543 |
|
|
|
544 |
// Do not shift the focus.
|
|
|
545 |
this.toggleGroup(item);
|
|
|
546 |
e.stopPropagation();
|
|
|
547 |
return false;
|
|
|
548 |
};
|
|
|
549 |
|
|
|
550 |
|
|
|
551 |
/**
|
|
|
552 |
* Handle a click (select).
|
|
|
553 |
*
|
|
|
554 |
* @method handleClick
|
|
|
555 |
* @param {Object} item is the jquery id of the parent item of the group
|
|
|
556 |
* @param {Event} e The event.
|
|
|
557 |
* @return {Boolean}
|
|
|
558 |
*/
|
|
|
559 |
Tree.prototype.handleClick = function(item, e) {
|
|
|
560 |
|
|
|
561 |
if (e.shiftKey) {
|
|
|
562 |
this.multiSelectItem(item);
|
|
|
563 |
} else if (e.metaKey || e.ctrlKey) {
|
|
|
564 |
this.toggleItem(item);
|
|
|
565 |
} else {
|
|
|
566 |
this.selectItem(item);
|
|
|
567 |
}
|
|
|
568 |
this.updateFocus(item);
|
|
|
569 |
e.stopPropagation();
|
|
|
570 |
return false;
|
|
|
571 |
};
|
|
|
572 |
|
|
|
573 |
/**
|
|
|
574 |
* Handle a blur event
|
|
|
575 |
*
|
|
|
576 |
* @method handleBlur
|
|
|
577 |
* @return {Boolean}
|
|
|
578 |
*/
|
|
|
579 |
Tree.prototype.handleBlur = function() {
|
|
|
580 |
return true;
|
|
|
581 |
};
|
|
|
582 |
|
|
|
583 |
/**
|
|
|
584 |
* Handle a focus event
|
|
|
585 |
*
|
|
|
586 |
* @method handleFocus
|
|
|
587 |
* @param {Object} item item is the jquery id of the parent item of the group
|
|
|
588 |
* @return {Boolean}
|
|
|
589 |
*/
|
|
|
590 |
Tree.prototype.handleFocus = function(item) {
|
|
|
591 |
|
|
|
592 |
this.updateFocus(item);
|
|
|
593 |
|
|
|
594 |
return true;
|
|
|
595 |
};
|
|
|
596 |
|
|
|
597 |
/**
|
|
|
598 |
* Bind the event listeners we require.
|
|
|
599 |
*
|
|
|
600 |
* @method bindEventHandlers
|
|
|
601 |
*/
|
|
|
602 |
Tree.prototype.bindEventHandlers = function() {
|
|
|
603 |
var thisObj = this;
|
|
|
604 |
|
|
|
605 |
// Bind a dblclick handler to the parent items.
|
|
|
606 |
this.parents.dblclick(function(e) {
|
|
|
607 |
return thisObj.handleDblClick($(this), e);
|
|
|
608 |
});
|
|
|
609 |
|
|
|
610 |
// Bind a click handler.
|
|
|
611 |
this.items.click(function(e) {
|
|
|
612 |
return thisObj.handleClick($(this), e);
|
|
|
613 |
});
|
|
|
614 |
|
|
|
615 |
// Bind a toggle handler to the expand/collapse icons.
|
|
|
616 |
this.items.children('img').click(function(e) {
|
|
|
617 |
return thisObj.handleExpandCollapseClick($(this).parent(), e);
|
|
|
618 |
});
|
|
|
619 |
|
|
|
620 |
// Bind a keydown handler.
|
|
|
621 |
this.items.keydown(function(e) {
|
|
|
622 |
return thisObj.handleKeyDown($(this), e);
|
|
|
623 |
});
|
|
|
624 |
|
|
|
625 |
// Bind a keypress handler.
|
|
|
626 |
this.items.keypress(function(e) {
|
|
|
627 |
return thisObj.handleKeyPress($(this), e);
|
|
|
628 |
});
|
|
|
629 |
|
|
|
630 |
// Bind a focus handler.
|
|
|
631 |
this.items.focus(function(e) {
|
|
|
632 |
return thisObj.handleFocus($(this), e);
|
|
|
633 |
});
|
|
|
634 |
|
|
|
635 |
// Bind a blur handler.
|
|
|
636 |
this.items.blur(function(e) {
|
|
|
637 |
return thisObj.handleBlur($(this), e);
|
|
|
638 |
});
|
|
|
639 |
|
|
|
640 |
};
|
|
|
641 |
|
|
|
642 |
return /** @alias module:tool_lp/tree */ Tree;
|
|
|
643 |
});
|