Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | 2x 2x 2x 2x 180x 180x 180x 180x 180x 180x 180x 2564x 1806x 1806x 14x 180x 180x 2x 104x 104x 296x 296x 296x 246x 246x 10x 24x 14x 14x 10x 296x 296x 296x 296x 94x 40x 54x 94x 104x 104x 16x 6x 104x 32x 32x 106x 40x 10x 30x 14x 14x 16x 16x 16x 16x 16x 16x 16x 2x 16x 16x 16x 16x 16x 16x 16x 16x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 12x 12x 12x 12x 12x 2x 10x 2x 12x 14x 14x 6x 6x 6x 12x 12x 12x 12x 12x 12x 4x 4x 4x 4x 4x 4x 4x 4x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 2x 736x 736x 736x 4x 40x 40x 40x 948x 198x 740x 17x 17x 192x 192x 32x 32x 192x 192x 188x 4x 4x 4x 4x 4x 12x 12x 12x 2x 2x 178x 178x 178x 178x 178x 178x 178x 178x | /**
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {BaseComponent} from '../../../coral-base-component';
import {SelectableCollection} from '../../../coral-collection';
import TreeItem from './TreeItem';
import {transform} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
const CLASSNAME = '_coral-TreeView';
/**
@class Coral.Tree
@classdesc A Tree component is a container component to display collapsible content.
Tree items don't expand by default. It's the developer's responsibility to handle it by listening to the
{@link coral-collection:add} and {@link coral-collection:remove} events.
@htmltag coral-tree
@extends {HTMLElement}
@extends {BaseComponent}
*/
const Tree = Decorator(class extends BaseComponent(HTMLElement) {
/** @ignore */
constructor() {
super();
// Attach events
this._delegateEvents({
'click ._coral-TreeView-itemLink': '_onItemClick',
'click ._coral-TreeView-indicator': '_onExpandCollapseClick',
'coral-collection:add coral-tree-item': '_onCollectionChange',
'coral-collection:remove coral-tree-item': '_onCollectionChange',
// a11y
'key:space ._coral-TreeView-itemLink': '_onItemClick',
'key:space ._coral-TreeView-indicator': '_onExpandCollapseClick',
'key:return ._coral-TreeView-itemLink, ._coral-TreeView-indicator': '_onExpandCollapseClick',
'key:pageup ._coral-TreeView-itemLink, ._coral-TreeView-indicator': '_onFocusPreviousItem',
'key:left ._coral-TreeView-itemLink, ._coral-TreeView-indicator': '_onCollapseItem',
'key:up ._coral-TreeView-itemLink, ._coral-TreeView-indicator': '_onFocusPreviousItem',
'key:pagedown ._coral-TreeView-itemLink, ._coral-TreeView-indicator': '_onFocusNextItem',
'key:right ._coral-TreeView-itemLink, ._coral-TreeView-indicator': '_onExpandItem',
'key:down ._coral-TreeView-itemLink, ._coral-TreeView-indicator': '_onFocusNextItem',
'key:home ._coral-TreeView-itemLink, ._coral-TreeView-indicator': '_onFocusFirstItem',
'key:end ._coral-TreeView-itemLink, ._coral-TreeView-indicator': '_onFocusLastItem',
'capture:blur ._coral-TreeView-itemLink[tabindex="0"]': '_onItemBlur',
// private
'coral-tree-item:_selectedchanged': '_onItemSelectedChanged',
'coral-tree-item:_disabledchanged': '_onFocusableChanged',
'coral-tree-item:_expandedchanged': '_onFocusableChanged',
'coral-tree-item:_afterexpandedchanged': '_onExpandedChanged',
'coral-tree-item:_hiddenchanged': '_onFocusableChanged'
});
// Used for eventing
this._oldSelection = [];
// Init the collection mutation observer
this.items._startHandlingItems(true);
// Listen for mutations for Torq compatibility
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
for (let i = 0 ; i < mutation.addedNodes.length ; i++) {
const addedNode = mutation.addedNodes[i];
if (addedNode.tagName === 'CORAL-TREE-ITEM') {
// Move tree items to their container
if (addedNode.parentNode.tagName === addedNode.tagName) {
addedNode.parentNode._elements.subTreeContainer.appendChild(addedNode);
}
}
}
});
});
observer.observe(this, {
childList: true,
subtree: true
});
}
I
/**
The Collection Interface that allows interacting with the items that the component contains.
@type {SelectableCollection}
@readonly
*/
get items() {
// just init on demand
if (!this._items) {
this._items = new SelectableCollection({
host: this,
itemTagName: 'coral-tree-item'
});
}
return this._items;
}
/**
Indicates whether the tree accepts multiple selected items.
@type {Boolean}
@default false
@htmlattribute multiple
@htmlattributereflected
*/
get multiple() {
return this._multiple || false;
}
set multiple(value) {
this._multiple = transform.booleanAttr(value);
this._reflectAttribute('multiple', this._multiple);
this.setAttribute('aria-multiselectable', this._multiple);
this._validateSelection();
}
/**
Returns an Array containing the set selected items.
@type {Array.<HTMLElement>}
@readonly
*/
get selectedItems() {
return this.items._getAllSelected();
}
/**
Returns the first selected item in the Tree. The value <code>null</code> is returned if no element is
selected.
@type {?HTMLElement}
@readonly
*/
get selectedItem() {
return this.items._getAllSelected()[0] || null;
}
/** @private */
_onItemSelectedChanged(event) {
event.stopImmediatePropagation();
this._validateSelection(event.target);
}
/** @private */
_validateSelection(item) {
const selectedItems = this.selectedItems;
if (!this.multiple) {
// Last selected item wins if multiple selection while not allowed
item = item || selectedItems[selectedItems.length - 1];
if (item && item.hasAttribute('selected') && selectedItems.length > 1) {
selectedItems.forEach((selectedItem) => {
if (selectedItem !== item) {
// Don't trigger change events
this._preventTriggeringEvents = true;
selectedItem.removeAttribute('selected');
}
});
// We can trigger change events again
this._preventTriggeringEvents = false;
}
}
this._triggerChangeEvent();
}
/** @private */
_triggerChangeEvent() {
const selectedItems = this.selectedItems;
const oldSelection = this._oldSelection;
if (!this._preventTriggeringEvents && this._arraysAreDifferent(selectedItems, oldSelection)) {
// We differentiate whether multiple is on or off and return an array or HTMLElement respectively
if (this.multiple) {
this.trigger('coral-tree:change', {
oldSelection: oldSelection,
selection: selectedItems
});
} else {
// Return all items if we just switched from multiple=true to multiple=false and we had >1 selected items
this.trigger('coral-tree:change', {
E oldSelection: oldSelection.length > 1 ? oldSelection : oldSelection[0] || null,
selection: selectedItems[0] || null
});
}
this._oldSelection = selectedItems;
}
}
/** @private */
_arraysAreDifferent(selection, oldSelection) {
let diff = [];
if (oldSelection.length === selection.length) {
diff = oldSelection.filter((item) => selection.indexOf(item) === -1);
}
// since we guarantee that they are arrays, we can start by comparing their size
return oldSelection.length !== selection.length || diff.length !== 0;
}
/** @private */
_toggleItemAttribute(item, attributeName) {
if (item) {
item[item.hasAttribute(attributeName) ? 'removeAttribute' : 'setAttribute'](attributeName, '');
}
}
/** @private */
_onCollectionChange(event) {
// Prevent triggering collection event twice. Only coral-tree collection events are propagated.
event.stopImmediatePropagation();
}E
/** @private */
_onItemClick(event) {
// Clickable item inside Tree Item should not trigger selection of item
if (event.target.hasAttribute('coral-interactive') || event.target.closest('[coral-interactive]')) {
return;
}
// If the indicator is clicked, expand/collapse the tree item
if (event.target.closest('._coral-TreeView-indicator')) {
this._onExpandCollapseClick(event);
return;
}
// The click was performed on the header so we select the item (parentNode) the selection is toggled
const item = event.target.closest('coral-tree-item');
if (item && !item.hasAttribute('disabled')) {
event.preventDefault();
event.stopPropagation();
// We ignore the selection if the item is disabled
this._toggleItemAttribute(item, 'selected');
const focusable = this._getFocusable();
if (focusable) {
focusable.setAttribute('tabindex', '-1');
}
Eitem._elements.header.setAttribute('tabindex', '0');
item._elements.header.focus();
}I
}
/** @private */
_onExpandCollapseClick(event) {
event.preventDefault();
event.stopPropagation();
// The click was performed on the icon to expand/collapse the sub tree
const item = event.target.closest('coral-tree-item');
if (item) {
// We ignore the expand/collapse if the item is disabled
if (item.hasAttribute('disabled')) {
return;
}
// Toggle the expanded of the item:
this._toggleItemAttribute(item, 'expanded');
}E
}
I
/** @private */
_onExpandItem(event) {
event.preventDefault();
event.stopPropagation();
// The click was performed on the icon to expand the sub tree
const item = event.target.closest('coral-tree-item');
if (item) {
// We ignEore the expand if the item is disabled
if (item.hasAttribute('disabled')) {
return;
}
if (!item.expanded && item.variant === TreeItem.variant.DRILLDOWN) {
// If the item is not expanded, expand the item
item.expanded = !item.expanded;
item._elements.header.classList.add('focus-ring');
} else if (item.items.length > 0) {
// If the item is expanded, and contains items, focus the next item
this._onFocusNextItem(event);
}
}
}
/** E@private */
_onCollapseItem(event) {
evenIt.preventDefault();
event.stopPropagation();
// The click was performed on the icon to collapse the sub tree
const item = event.target.closest('coral-tree-item');
if (item) {
// We ignore the expand if the item is disabled
if (item.hasAttribute('disabled')) {
return;
}E
if (item.expanded && item.variant === TreeItem.variant.DRILLDOWN) {
// If the item is not expanded, expand the item
item.expanded = !item.expanded;
item._elements.header.classList.add('focus-ring');
} else if (item.parent) {
item._elements.header.setAttribute('tabindex', '-1');
item._elements.header.classList.remove('focus-ring');
item.parent.focus();
item.parent._elements.header.classList.add('focus-ring');
}
}
}
/** @private */
_focusSiblingItem(item, next) {
const focusableItems = this._getFocusableItems();
//I There's not enough items to change focus
if (focusableItems.length < 2) {
return;
}
let index = focusableItems.indexOf(item) + (next ? 1 : -1);
let siblingItem = null;
// If we reached the edge, target the other edge
if (index > focusableItems.length - 1) {
siblingItem = focusableItems[0];
} else if (index < 0) {
siblingItem = focusableItems[focusableItems.length - 1];
}
// Find the sibling item
while (!siblingItem) {
siblingItem = focusableItems[index];
// TIhe item might be hidden because a parent is collapsed
if (siblingItem.parentNode.closest('coral-tree-item.is-collapsed')) {
if (next) {
index++;
siblingItem = index > focusableItems.length - 1 ? item : null;
} else {
index--;
siblingItem = index < 0 ? item : null;
}
}
}
E
// Change focus
if (siblingItem !== item) {
item._elements.header.setAttribute('tabindex', '-1');
item._elements.header.classList.remove('focus-ring');
siblingItem._elements.header.setAttribute('tabindex', '0');
siblingItem._elements.header.classList.add('focus-ring');
siblingItem._elements.header.focus();
}
}
/** @private */
_focusEdgeItem(last) {
// Query the focusable item
const focusable = this._getFocusable();
if (focusable) {
const focusableItems = this._getFocusableItems();
const edgeItem = focusableItems[last ? focusableItems.length - 1 : 0];
E// Change focus
if (edgeItem !== focusable) {
focusable.setAttribute('tabindex', '-1');
edgeItem._elements.header.setAttribute('tabindex', '0');
edgeItem._elements.header.focus();
}E
}
}
/** @private */
_onFocusNextItem(event) {
event.preventDefault();
event.stopPropagation();
const item = event.target.closest('coral-tree-item');
if (item) {
this._focusSiblingItem(item, true);
}
}
/** @private */
_onFocusPreviousItem(event) {
event.preventDefault();
evEent.stopPropagation();
const item = event.target.closest('coral-tree-item');
if (item) {
this._focusSiblingItem(item, false);
}
}
/** @private */
_onFocusFirstItem(event) {
event.preventDefault();
event.stopPropagation();
thEis._focusEdgeItem(false);
}
/** @private */
_onFocusLastItem(event) {
event.preventDefault();
event.stopPropagation();
this._focusEdgeItem(true);
}
/** @private */
_onFocusableChanged(event) {
event.preventDefault();
event.stopPropagation();
if (event.target.contains(this._getFocusable())) {
this._resetFocusableItem();
}
}
/** @private */
_onExpandedChanged(event) {
event.stopImmediatePropagation();
const item = event.target;
this.trigger(`coral-tree:${item.expanded ? 'expand' : 'collapse'}`, {item});
}
/** @private */
_getFocusable() {
return this.querySelector('coral-tree-item > ._coral-TreeView-itemLink[tabindex="0"]');
}
/** @private */
_getFocusableItems() {
return this.items.getAll().filter((item) => !item.closest('coral-tree-item[disabled]') && !item.closest('coral-tree-item[hidden]'));
}
/** @private */
_onItemBlur() {
const focused = this.querySelector('._coral-TreeView-itemLink.focus-ring');
if (focused) {
focused.classList.remove('focus-ring');
}
}
/** @private */
_resetFocusableItem(item) {
// Old focusable becomes unfocusable
const focusable = this._getFocusable();
if (focusable) {
focusable.setAttribute('tabindex', '-1');
focusable.classList.remove('focus-ring');
}
// Defined item or first item by default gets the focus
item = item || this._getFocusableItems()[0];
if (item) {
item._elements.header.setAttribute('tabindex', '0');
}
}
/** @private */
_expandCollapseAll(expand) {
const coralTreeItems = this.querySelectorAll('coral-tree-item');
if (coralTreeItems) {
let item;
const length = coralTreeItems.length;
Iif (length > 0) {
for (let index = 0 ; index < length ; index++) {
item = coralTreeItems[index];
if (item) {
item.expanded = expand;
}
}
}
}
}
/**
Expand all the Tree Items
*/
expandAll() {
this._expandCollapseAll(true);
}
/**
Collapse all the Tree Items
*/
collapseAll() {
this._expandCollapseAll(false);
}
/** @ignore */
static get observedAttributes() {
return super.observedAttributes.concat(['multiple']);
}
/** @ignore */
rendEer() {
super.render();
this.classList.add(CLASSNAME);
E
// a11y
this.setAttribute('role', 'tree');
this.setAttribute('aria-multiselectable', this.multiple);
E
// Enable keyboard interaction
requestAnimationFrame(() => {
this._resetFocusableItem();
});
// Don't trigger events once connected
this._preventTriggeringEvents = true;
this._validateSelection();
this._preventTriggeringEvents = false;
this._oldSelection = this.selectedItems;
}
/**
Triggered when the {@link Tree} selection changed.
@typedef {CustomEvent} coral-tree:change
@property {Array.<TreeItem>} detail.oldSelection
The old selected item.
@property {Array.<TreeItem>} detail.selection
The selected items.
*/
/**
Triggered when a {@link Tree} item expanded.
@typedef {CustomEvent} coral-tree:expand
@property {TreeItem} detail.item
The expanded item.
*/
/**
Triggered when a {@link Tree} item collapsed.
@typedef {CustomEvent} coral-tree:collapse
@property {TreeItem} detail.item
The collapsed item.
*/
});
export default Tree;
|