All files / coral-component-table/src/scripts TableCell.js

93.88% Statements 46/49
85.19% Branches 23/27
94.12% Functions 16/17
93.88% Lines 46/49

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                                            2x                   2x 2x   2x     2396x   2396x     2x         188x   188x   188x 188x     188x   188x               678x             288x 188x     100x   100x     288x             678x   678x 218x   460x                 390x   390x 10x                   284x 184x   100x               2378x   2378x 2378x                                   2x                                           1170x       206x 4x     202x 202x   202x
/**
 * 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 {commons, transform} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
 
const CLASSNAME = '_coral-Table-cell';
 
/**
 @class Coral.Table.Cell
 @classdesc A Table cell component
 @htmltag coral-table-cell
 @htmlbasetag td
 @extends {HTMLTableCellElement}
 @extends {BaseComponent}
 */
const TableCell = Decorator(class extends BaseComponent(HTMLTableCellElement) {
  // @compat
  get content() {
    return this;
  }
 
  set content(value) {
    // Support configs
    if (typeof value === 'object') {
      for (const prop in value) {
        /** @ignore */
        this[prop] = value[prop];
      }
    }
  }
 
  /**
   Whether the table cell is selected.
 
   @type {Boolean}
   @default false
   @htmlattribute selected
   @htmlattributereflected
   */
  get selected() {
    return this._selected || false;
  }E
 
  set selected(value) {
    // Prevent selection if disabled
    if (this.hasAttribute('coral-table-cellselect') && this.hasAttribute('disabled') ||
      this.querySelector('[coral-table-cellselect][disabled]')) {
      return;
    }
 
    this.trigger('coral-table-cell:_beforeselectedchanged');
 
    this._selected = transform.booleanAttr(value);
    this._reflectAttribute('selected', this._selected);
 
    this.trigger('coral-table-cell:_selectedchanged');
 
    this._syncAriaSelectedState();
    this._syncSelectHandle();
  }
 
  /**
   The cell's value. It is used to compare cells during a column sort. If not set, the sorting will be performed on the
   cell content. The content will be parse accordingly based on the column's <code>sortabletype</code> property.
 
   @type {String}
   @default ""
   @htmlattribute value
   @htmlattributereflected
   */
  get value() {
    return this.getAttribute('value') || '';
  }
 
  set value(value) {
    this.setAttribute('value', transform.string(value));
  }
 
  /** @private */
  _setHandle(handle) {
    requestAnimationFrame(() => {
      // Specify handle directly on the cell if none found
      if (!this.querySelector(`[${handle}]`)) {
        this.setAttribute(handle, '');
      }
      this._syncAriaSelectedState();
      this._syncSelectHandle();
    });
  }
 
  /** @private */
  _getHandle(handle) {
    return this.hasAttribute(handle) ? this : this.querySelector(`[${handle}]`);
  }
 
  /** @private */
  _toggleSelectable(selectable) {
    if (selectable) {
      this._setHandle('coral-table-cellselect');
    } else {
      // Remove the handle
      this.removeAttribute('coral-table-cellselect');
 
      // Clear selection
      this.selected = false;
    }
 
    this._syncAriaSelectedState();
  }
 
  /** @private */
  _syncAriaSelectedState() {
    this.classList.toggle('is-selected', this.selected);
    if (this._getHandle('coral-table-cellselect')) {
      this.setAttribute('aria-selected', this.selected);
    } else {
      this.removeAttribute('aria-selected');
    }
  }
 
  /** @private */
  _syncSelectHandle() {
    // Check/uncheck the select handle
    const selectHandle = this.querySelector('coral-checkbox');
    if (selectHandle) {
      selectHandle[this.selected ? 'setAttribute' : 'removeAttribute']('checked', '');
    }
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat(['selected', '_selectable']);
  }
 
  /** @ignore */
  attributeChangedCallback(name, oldValue, value) {
    if (name === '_selectable') {
      this._toggleSelectable(value !== null);
    } else {
      super.attributeChangedCallback(name, oldValue, value);
    }
  }

  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    this.id = this.id || commons.getUID();
  }
 
  /**
   Triggered before {@link TableCell#selected} is changed.
 
   @typedef {CustomEvent} coral-table-cell:_beforeselectedchanged
 
   @private
   */
 
  /**
   Triggered when {@link TableCell#selected} changed.
 
   @typedef {CustomEvent} coral-table-cell:_selectedchanged
 
   @private
   */
});
 
export default TableCell;