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 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | 2x 2x 2x 2x 1376x 1376x 1376x 1376x 1376x 1376x 1376x 1376x 1376x 2x 194x 194x 194x 186x 186x 186x 186x 186x 6x 1372x 1372x 1372x 1372x 884x 884x 488x 488x 488x 488x 488x 490x 490x 490x 490x 114x 490x 490x 490x 490x 490x 490x 242x 488x 488x 164x 164x 488x 488x 488x 330x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 488x 480x 3734x 3734x 3112x 1300x 1300x 3112x 622x 958x 622x 804x 622x 804x 804x 116x 116x 116x 58x 58x 688x 520x 622x 332x 622x 448x 622x 586x 622x 1372x 1372x 1372x 118x 112x 118x 118x 114x 64x 114x 112x 112x 234x 112x 742x 342x 400x 742x 508x 178x 330x 454x 32x 552x 460x 552x 552x 552x 552x 552x 178x 178x 374x 374x 142x 142x 262x 262x 982x 316x 666x 82x 584x 28x 556x 1358x 1358x 1358x 302x 20x 20x 20x 4964x 824x 4x 820x 820x 820x 820x 820x 820x 820x 530x 62x 62x 62x 62x 180x 222x 22x 22x 22x 330x | /**
* 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 accessibilityState from '../templates/accessibilityState';
import {BaseComponent} from '../../../coral-base-component';
import {SelectableCollection} from '../../../coral-collection';
import {transform, commons, i18n} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
const CLASSNAME = '_coral-Table-row';
/**
@class Coral.Table.Row
@classdesc A Table row component
@htmltag coral-table-row
@htmlbasetag tr
@extends {HTMLTableRowElement}
@extends {BaseComponent}
*/
const TableRow = Decorator(class extends BaseComponent(HTMLTableRowElement) {
/** @ignore */
constructor() {
super();
// Templates
this._elements = {};
accessibilityState.call(this._elements, {commons});
// Required for coral-table-row:change event
this._oldSelection = [];
// Events
this._delegateEvents({
// Private
'coral-table-cell:_beforeselectedchanged': '_onBeforeCellSelectionChanged',
'coral-table-cell:_selectedchanged': '_onCellSelectionChanged'
});
// Initialize content MO
this._observer = new MutationObserver(this._handleMutations.bind(this));
this._observer.observe(this, {
childList: true
});
}
/**
Whether the table row is locked.
@type {Boolean}
@default false
@htmlattribute locked
@htmlattributereflected
*/
get locked() {
return this._locked || false;
}
set locked(value) {
this._locked = transform.booleanAttr(value);
this._reflectAttribute('locked', this._locked);
this.trigger('coral-table-row:_lockedchanged');
}
/**
Whether the table row is selected.
@type {Boolean}
@default false
@htmlattribute selected
@htmlattributereflected
*/
get selected() {
return this._selected || false;
}
set selected(value) {
// Prevent selection if disabled
if (this.hasAttribute('coral-table-rowselect') && this.hasAttribute('disabled') ||
this.querySelector('[coral-table-rowselect][disabled]')) {
return;
}
this.trigger('coral-table-row:_beforeselectedchanged');
this._selected = transform.booleanAttr(value);
this._reflectAttribute('selected', this._selected);
this.trigger('coral-table-row:_selectedchanged');
this._syncSelectHandle();
this._syncAriaLabelledby();
this._syncAriaSelectedState();
}
/**
Whether the items are selectable.
@type {Boolean}
@default false
@htmlattribute selectable
@htmlattributereflected
*/
get selectable() {
return this._selectable || false;
}
set selectable(value) {
this._selectable = transform.booleanAttr(value);
this._reflectAttribute('selectable', this._selectable);
this.items.getAll().forEach((cell) => {
cell[this._selectable ? 'setAttribute' : 'removeAttribute']('_selectable', '');
});
}
/**
Whether multiple items can be selected.
@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.trigger('coral-table-row:_multiplechanged');
}
/**
Returns an Array containing the selected items.
@typeI {Array.<HTMLElement>}
@readonly
*/
get selectedItems() {
return this.items._getAllSelected();
}
I
/**
Returns the first selected item of the row. The value <code>null</code> is returned if no element is
selected.
@type {HTMLElement}
@readIonly
*/
get selectedItem() {
return this.items._getFirstSelected();
}E
/**
The Collection Interface that allows interacting with the items that the component contains.
@type {SelectableCollection}
@readonly
*/
get items() {
// Construct the collection on first request
if (!this._items) {
this._items = new SelectableCollection({
host: this,
itemBaseTagName: 'td',
itemTagName: 'coral-table-cell'
});
}
return this._items;
}
E
/**
* The row header element for the row.
* @type {HTMLElement}
* @readonly
*/
get rowHeaIder () {
return this.items.getAll().filter(cell => {
return (cell.getAttribute('role') === 'rowheader' ||
(cell.tagName === 'TH' && cell.getAttribute('scope') === 'row'));
})[0];
}
I
_triggerChangeEvent() {
const selectedItems = this.selectedItems;
this.trigger('coral-table-row:_change', {
oldSelection: this._oldSelection,
selection: selectedItems
});I
this._oldSelection = selectedItems;
}
/** @private */
_onCellSelectionChanged(event) {
event.stIopImmediatePropagation();
this._triggerChangeEvent();
}
/** @private */
_onBeforeCEellSelectionChanged(event) {
event.stopImmediatePropagation();
// In single selection, if the added item is selected, the rest should be deselected
const selectedItem = this.selectedItem;
if (!this.multiple && selectedItem && !event.target.selected) {
selectedItem.set('selected', false, true);
}
}
/** @private */
_syncAriaSelectedState() {
this.classList.toggle('is-selected', this.selected);
const selectHandle = this.querySelector('[coral-table-rowselect]');
// @a11y Only update aria-selected if the table row can be selected
if (!(this.hasAttribute('coral-table-rowselect') || selectHandle)) {
this.removeAttribute('aria-selected');
return;
}
const rowOrderHandle = this.querySelector('[coral-table-roworder]');
const rowLockHandle = this.querySelector('[coral-table-rowlock]');
const rowRemoveHandle = this.querySelector('[coral-row-remove]');
const accessibilityState = this._elements.accessibilityState;
const resetAccessibilityState = () => {
// @a11y remove aria-live
this.removeAttribute('aria-live');
this.removeAttribute('aria-atomic');
this.removeAttribute('aria-relevant');
// @a11y Unhide the selectHandle, so that it will be resume being announced by assistive
// technology
if (selectHandle && selectHandle.tagName === 'CORAL-CHECKBOX') {
selectHandle.removeAttribute('aria-hidden');
}
// @a11y Unhide the coral-table-roworder handle, so that it will be resume being announced by
// assistive technology
if (rowOrderHandle) {
rowOrderHandle.removeAttribute('aria-hidden');
}
// @a11y Unhide the coral-table-rowlock handle, so that it will be resume being announced by
// assistive technology
if (rowLockHandle) {
rowLockHandle.removeAttribute('aria-hidden');
}
// @a11y Unhide the coral-row-remove handle, so that it will be resume being announced by
// assistive technology
if (rowRemoveHandle) {
rowRemoveHandle.removeAttribute('aria-hidden');
}
if (accessibilityState) {
//E @a11y Hide the _accessibilityState from assistive technology, so that it can not be read
// using a screen reader separately from the row it helps label
accessibilityState.setAttribute('aria-hidden', 'true');
// @a11y If the item is not selected, remove ', unchecked' to decrease verbosity.
if (!this.selected) {
accessibilityState.innerHTML = '';
}
}
};
// @a11y set aria-selected
this.setAttribute('aria-selected', this.selected);
if (this._ariaLiveOnTimeout || this._ariaLiveOffTimeout) {
clearTimeout(this._ariaLiveOnTimeout);
clearTimeout(this._ariaLiveOffTimeout);
}
// @ally If _accessibilityState has been added to a cell within the row,
if (accessibilityState) {
resetAccessibilityState();
this._ariaLiveOnTimeout = setTimeout(() => {
// @a11y and the row or one of its descendants has focus,
if (this === document.activeElement || this.contains(document.activeElement)) {
// @a11y Hide the "Select" checkbox so that it does not get announced with the state change.
if (selectHandle && selectHandle.tagName === 'CORAL-CHECKBOX') {
selectHandle.setAttribute('aria-hidden', 'true');
}
// @a11y Hide the coral-table-roworder handle so that it does not get announced with the
// state change.
if (rowOrderHandle) {
rowOrderHandle.setAttribute('aria-hidden', 'true');
}
// @a11y Hide the coral-table-rowlock handle so that it does not get announced with the state
// change.
if (rowLockHandle) {
rowLockHandle.setAttribute('aria-hidden', 'true');
}
// @a11y Hide the coral-row-remove handle so that it does not get announced with the state
// change.
if (rowRemoveHandle) {
rowRemoveHandle.setAttribute('aria-hidden', 'true');
}
// @a11y The ChromeVox screenreader, used on Chromebook, announces the state change and
// should not need aria-live, otherwise it double-voices the row.
if (!window.cvox) {
// @a11y Unhide the _accessibilityState so that it will get announced with the state change.
accessibilityState.removeAttribute('aria-hidden');
// @ally use aria-live to announce the state change
this.setAttribute('aria-live', 'assertive');
// @ally use aria-atomic="true" to announce the entire row
this.setAttribute('aria-atomic', 'true');
}
this._ariaLiveOnTimeout = setTimeout(() => {
// @ally Set the _accessibilityState text to read either ", checked" or ", unchecked",
// which should trigger a live region announcement.
accessibilityState.innerHTML = i18n.get(this.selected ? ', checked' : ', unchecked');
// @ally wait 250ms for row to announce
this._ariaLiveOffTimeout = setTimeout(resetAccessibilityState, 250);
}, 20);
}
}, 20);
if (!(this === document.activeElement || this.contains(document.activeElement))) {
accessibilityState.innerHTML = i18n.get(this.selected ? ', checked' : '');
}
}
}
/** @private */
_syncAriaLabelledby() {
// @a11y if the row is not selectable, remove accessibilityState
if (!(this.hasAttribute('coral-table-rowselect') || this.querySelector('[coral-table-rowselect]'))) {
if (this._elements.accessibilityState.parentNode) {
this.removeAttribute('aria-labelledby');
this._elements.accessibilityState = this._elements.accessibilityState.parentNode.removeChild(this._elements.accessibilityState);
}
return;
}
// @a11y get a list of ids for cells
const cells = this.items.getAll().filter(cell => {
// @a11y exclude cells for coral-table-roworder, coral-table-rowlock or coral-row-remove
return (
cell.id &&
!(I
cell.hasAttribute('coral-table-roworder') || cell.querySelector('[coral-table-roworder]') ||
cell.hasAttribute('coral-table-rowlock') || cell.querySelector('[coral-table-rowlock]') ||
cell.hasAttribute('coral-row-remove') || cell.querySelector('[coral-table-remove]')
)
);
});
const rowHeaders = cells.filter(cell => {
return (cell.getAttribute('role') === 'rowheader' ||
(cell.tagName === 'TH' && cell.getAttribute('scope') === 'row'));
});
let cellForAccessibilityState;
const ids = cells.map(cell => {
const handle = cell.querySelector('[coral-table-rowselect]');
if (handle) {
cellForAccessibilityState = cell;
// @a11y otherwise, if the selectHandle is a coral-checkbox,
if (handle && handle.tagName === 'CORAL-CHECKBOX' && handle._elements) {
// @a11y if the row is selected, don't add the coral-table-rowselect to accessibility name
if (this.selected) {
return;
}
// otherwise, include the checkbox input labelled "Select" in the accessibility name
return handle._elements.input && handle._elements.input.id;
}
}
// @a11y include row headers, or if no row header is defined,
// all other cells in the row, in the accessibility name
if (rowHeaders.length === 0 || rowHeaders.indexOf(cell) !== -1) {
return cell.id;
}
});
// @a11y If an _accessibilityState has not been defined within one of the cells, add to the last
// cell
if (!cellForAccessibilityState && cells.length) {
cellForAccessibilityState = cells[cells.length - 1];
}
if (cellForAccessibilityState) {
cellForAccessibilityState.appendChild(this._elements.accessibilityState);
}
// @a11y Once defined,
if (this._elements.accessibilityState.parentNode) {
// @a11y add the _accessibilityState ", checked" or ", unchecked" as the last item in the
// accessibility name
ids.push(this._elements.accessibilityState.id);
}
// @a11y Update the aria-labelledby attribute for the row.
this.setAttribute('aria-labelledby', ids.join(' '));
}
/** @private */
_syncSelectHandle() {
// Check/uncheck the select handle
const selectHandle = this.querySelector('[coral-table-rowselect]');
if (selectHandle) {
if (typeof selectHandle.indeterminate !== 'undefined') {
selectHandle.indeterminate = false;
}
selectHandle[this.selected ? 'setAttribute' : 'removeAttribute']('checked', '');
// @a11y If the handle is a checkbox but lacks a label, label it with "Select".
if (selectHandle.tagName === 'CORAL-CHECKBOX') {
if (!selectHandle.labelled) {
selectHandle.labelled = i18n.get('Select');
}
// @a11y provide a more explicit label for the checkbox than just "Select"
if (this.hasAttribute('aria-labelledby')) {
// Wait for the next frame to ensure the selectHandle has initialized _elements object.
window.requestAnimationFrame(() => {
let ids = this.getAttribute('aria-labelledby')
.split(/\s+/g)
.filter(id => selectHandle._elements.id !== id && this._elements.accessibilityState.id !== id)
.join(' ');
selectHandle.labelledBy = selectHandle._elements.id + ' ' + ids;
});
}
}
}
}
/** @private */
_toggleSelectable(selectable) {
if (selectable) {
this._setHandle('coral-table-rowselect');
} else {
// Clear selection but leave the handle if any
this.set('selected', false, true);
}
// Sync the aria-labelledby attribute to include the _accessibilityState
this._syncAriaLabelledby();
}
/** @private */
_toggleOrderable(orderable) {
if (orderable) {
this._setHandle('coral-table-roworder', 0);
}
// Remove DragAction instance
else if (this.dragAction) {
this.dragAction.destroy();
}
}
/** @private */
_toggleLockable(lockable) {
if (lockable) {
this._setHandle('coral-table-rowlock');
}
}
_setHandleAndSync(handle) {
// Specify handle directly on the row if none found
if (!this.querySelector(`[${handle}]`)) {
this.setAttribute(handle, '');
}
this._syncSelectHandle();
this._syncAriaLabelledby();
this._syncAriaSelectedState();
}
/** @private */
_setHandle(handle, timeout) {
if(typeof timeout === "number") {
setTimeout(() => {
this._setHandleAndSync(handle);
}, timeout);
} else {
requestAnimationFrame(() => {
this._setHandleAndSync(handle);
});
}
}
/** @private */
_handleMutations(mutations) {
mutations.forEach((mutation) => {
// Sync added nodes
this.trigger('coral-table-row:_contentchanged', {
addedNodes: mutation.addedNodes,
removedNodes: mutation.removedNodes
});
this._syncAriaLabelledby();
});
}
/** @ignore */
static get observedAttributes() {
return super.observedAttributes.concat(['locked', 'selected', 'multiple', 'selectable', '_selectable', '_orderable', '_lockable']);
}
/** @ignore */
attributeChangedCallback(name, oldValue, value) {
if (name === '_selectable') {
this._toggleSelectable(value !== null);
} else if (name === '_orderable') {
this._toggleOrderable(value !== null);
} else if (name === '_lockable') {
this._toggleLockable(value !== null);
} else {
super.attributeChangedCallback(name, oldValue, value);
}
}
/** @ignore */
render() {
super.render();
this.classList.add(CLASSNAME);
this._syncAriaLabelledby();
}
/**
Triggered before {@link TableRow#selected} is changed.
@typedef {CustomEvent} coral-table-row:_beforeselectedchanged
@private
*/
/**
Triggered when {@link TableRow#selected} changed.
@typedef {CustomEvent} coral-table-row:_selectedchanged
@private
*/
/**
Triggered when {@link TableRow#locked} changed.
@typedef {CustomEvent} coral-table-row:_lockedchanged
@private
*/
/**
Triggered when {@link TableRow#multiple} changed.
@typedef {CustomEvent} coral-table-row:_multiplechanged
@private
*/
/**
Triggered when the {@link TableRow} selection changed.
@typedef {CustomEvent} coral-table-row:_change
@property {Array.<TableCell>} detail.oldSelection
The old item selection. When {@link TableRow#multiple}, it includes an Array.
@property {Array.<TableCell>} event.detail.selection
The item selection. When {@link TableRow#multiple}, it includes an Array.
@private
*/
});
export default TableRow;
|