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

100% Statements 77/77
87.8% Branches 36/41
100% Functions 30/30
100% Lines 77/77

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                                            2x                           2x                                       2x                             2x 2x   2x     504x   504x     2x             30x 18x 12x 8x 4x 4x     30x                 564x                               486x   486x   486x 268x     486x 464x     486x 462x                                                                                                                           22x     488x   488x 488x 488x   488x     488x 6x 6x                             6x     8x   8x   8x   8x 8x                           8x     6x   6x   6x   6x 6x                             268x     70x   70x   70x   70x 70x                           560x     208x   208x   208x   208x 208x                                 504x     488x 488x   488x                           4008x     694x   694x 694x   694x     694x 562x   562x 562x             2552x                   6x  
/**
 * 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 {alignment} from './TableUtil';
import {commons, transform, validate} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
 
const CLASSNAME = '_coral-Table-column';
 
/**
 Enumeration for {@link TableColumn} sortable direction options.
 
 @typedef {Object} TableColumnSortableDirectionEnum
 
 @property {String} DEFAULT
 Default. No sorting applied.
 @property {String} ASCENDING
 Ascending sort.
 @property {String} DESCENDING
 Descending sort.
 */
const sortableDirection = {
  DEFAULT: 'default',
  ASCENDING: 'ascending',
  DESCENDING: 'descending'
};
 
/**
 Enumeration for {@link TableColumn} sortable type options.
 
 @typedef {Object} TableColumnSortableTypeEnum
 
 @property {String} ALPHANUMERIC
 Alphanumeric type. If sorting is based on {@link TableCell#value}, use {String}.
 @property {String} NUMBER
 Number type. If sorting is based on {@link TableCell#value}, use {Number}.
 @property {String} DATE
 Date type. If sorting is based on {@link TableCell#value}, use {Date} in milliseconds.
 @property {String} CUSTOM
 Custom type. Sorting is based on user defined sorting.
 */
const sortableType = {
  ALPHANUMERIC: 'alphanumeric',
  NUMBER: 'number',
  DATE: 'date',
  CUSTOM: 'custom'
};
 
/**
 @class Coral.Table.Column
 @classdesc A Table column component
 @htmltag coral-table-column
 @htmlbasetag col
 @extends {HTMLTableColElement}
 @extends {BaseComponent}
 */
const TableColumn = Decorator(class extends BaseComponent(HTMLTableColElement) {
  /**
   The column cells alignment. The alignment should take the {@link i18n} configuration into account.
 
   @type {String}
   @default TableColumnAlignmentEnum.LEFT
   @htmlattribute alignment
   @htmlattributereflected
   */
  get alignment() {
    return this._alignment || alignment.LEFT;
  }
 
  set alignment(value) {
    const oldValue = this._alignment;
 
    value = transform.string(value).toLowerCase();
    this._alignment = validate.enumeration(alignment)(value) && value || alignment.LEFT;
    this._reflectAttribute('alignment', this._alignment);
 
    // Don't trigger on initialization if alignment is LEFT to improve performance
    if (!(typeof oldValue === 'undefined' && this._alignment === alignment.LEFT)) {
      window.requestAnimationFrame(() => {
        this.trigger('coral-table-column:_alignmentchanged');
      });
    }
  }
E
  /**
   Whether the column has a fixed width.
 
   @type {Boolean}
   @default false
   @htmlattribute fixedwidth
   @htmlattributereflected
   */
  get fixedWidth() {
    return this._fixedWidth || false;
  }
 
  set fixedWidth(value) {
    this._fixedWidth = transform.booleanAttr(value);
    this._reflectAttribute('fixedwidth', this._fixedWidth);
 
    window.requestAnimationFrame(() => {
      this.trigger('coral-table-column:_fixedwidthchanged');
    });
  }
 
  /**
   Whether the column is hidden.
 
   @type {Boolean}
   @default false
   @htmlattribute hidden
   @htmlattributereflected
   */
  get hidden() {
    return this._hidden || false;
  }
 
  set hidden(value) {
    this._hidden = transform.booleanAttr(value);
    this._reflectAttribute('hidden', this._hidden);
 
    window.requestAnimationFrame(() => {
      this.trigger('coral-table-column:_hiddenchanged');
    });
  }
 
  /**
   Whether the table column is orderable.
   Note that this does not affect the underlying data, only presentation.
 
   @type {Boolean}
   @default false
   @htmlattribute orderable
   @htmlattributereflected
   */
  get orderable() {
    return this._orderable || false;
  }
 
  set orderable(value) {
    this._orderable = transform.booleanAttr(value);
    this._reflectAttribute('orderable', this._orderable);
 
    window.requestAnimationFrame(() => {
      this.trigger('coral-table-column:_orderablechanged');
    });
  }
 
  /**
   Whether the column is sortable by user interaction.
 
   @type {Boolean}
   @default false
   @htmlattribute sortable
   @htmlattributereflected
   */
  get sortable() {
    return this._sortable || false;
  }
 
  set sortable(value) {
    this._sortable = transform.booleanAttr(value);
    this._reflectAttribute('sortable', this._sortable);
 
    window.requestAnimationFrame(() => {
      this.trigger('coral-table-column:_sortablechanged');
    });
  }
 
  /**
   The sorting type. See {@link TableColumnSortableTypeEnum}. If setting to <code>custom</code>, columns won't sort
   based on the default table sorting.
   Instead, a custom sorting can be performed when triggered by user interaction. This can be defined by listening to
   the {@link coral-table:beforecolumnsort} event.
 
   @type {String}
   @default TableColumnSortableTypeEnum.ALPHANUMERIC
   @htmlattribute sortabletype
   @htmlattributereflected
   */
  get sortableType() {
    return this._sortableType || sortableType.ALPHANUMERIC;
  }
 
  set sortableType(value) {
    value = transform.string(value).toLowerCase();
    this._sortableType = validate.enumeration(sortableType)(value) && value || sortableType.ALPHANUMERIC;
    this._reflectAttribute('sortabletype', this._sortableType);
  }
 
  /**
   The sorting direction. Sorts the column cells based on {@link TableCell#value}.
   If not present, the sort is based on the cell text content. See {@link TableColumnSortableDirectionEnum}.
 
   @type {String}
   @default TableColumnSortableDirectionEnum.DEFAULT
   @htmlattribute sortabledirection
   @htmlattributereflected
   */
  get sortableDirection() {
    return this._sortableDirection || sortableDirection.DEFAULT;
  }
 
  set sortableDirection(value) {
    value = transform.string(value).toLowerCase();
    this._sortableDirection = validate.enumeration(sortableDirection)(value) && value || sortableDirection.DEFAULT;
    this._reflectAttribute('sortabledirection', this._sortableDirection);
 
    // Prevent sorting if unnecessary
    if (!this._preventSort) {
      this._doSort();
 
      window.requestAnimationFrame(() => {
        this.trigger('coral-table-column:_sortabledirectionchanged');
      });
    }
  }
 
  /** @private */
  _sort() {
    let newSortableDirection;
    if (this.sortableDirection === sortableDirection.DEFAULT) {
      newSortableDirection = sortableDirection.ASCENDING;
    } else if (this.sortableDirection === sortableDirection.ASCENDING) {
      newSortableDirection = sortableDirection.DESCENDING;
    } else if (this.sortableDirection === sortableDirection.DESCENDING) {
      newSortableDirection = sortableDirection.DEFAULT;
    }
 
    this.trigger('coral-table-column:_beforecolumnsort', {newSortableDirection});
  }
 
  /** @private */
  _doSort(onInitialization) {
    this.trigger('coral-table-column:_sort', {onInitialization, sortableDirection, sortableType});
  }
 
  /**
   Returns {@link TableColumn} sortable direction options.
 
   @return {TableColumnSortableDirectionEnum}
   */
  static get sortableDirection() {
    return sortableDirection;
  }
 
  /**
   Returns {@link TableColumn} sortable type options.
 
   @return {TableColumnSortableTypeEnum}
   */
  static get sortableType() {
    return sortableType;
  }
 
  /**
   Returns {@link TableColumn} alignment options.
 
   @return {TableColumnAlignmentEnum}
   */
  static get alignment() {
    return alignment;
  }
 
  static get _attributePropertyMap() {
    return commons.extend(super._attributePropertyMap, {
      fixedwidth: 'fixedWidth',
      sortabletype: 'sortableType',
      sortabledirection: 'sortableDirection'
    });
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat([
      'fixedwidth',
      'hidden',
      'alignment',
      'orderable',
      'sortable',
      'sortabletype',
      'sortabledirection',
    ]);
  }
 
  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    // Default reflected attributes
    if (!this._sortableType) {
      this.sortableType = sortableType.ALPHANUMERIC;
    }
    if (!this._sortableDirection) {
      this.sortableDirection = sortableDirection.DEFAULT;
    }
    if (!this._alignment) {
      this.alignment = alignment.LEFT;
    }
  }
 
  /**
   Triggered when {@link TableColumn#alignment} changed.
 
   @typedef {CustomEvent} coral-table-column:_alignmentchanged
 
   @private
   */
 
  /**
   Triggered when {@link TableColumn#fixedWidth} changed.
 
   @typedef {CustomEvent} coral-table-column:_fixedwidthchanged
 
   @private
   */
 
  /**
   Triggered when {@link TableColumn#orderable} changed.
 
   @typedef {CustomEvent} coral-table-column:_orderablechanged
 
   @private
   */
 
  /**
   Triggered when {@link TableColumn#sortable} changed.
 
   @typedef {CustomEvent} coral-table-column:_sortablechanged
 
   @private
   */
 
  /**
   Triggered when {@link TableColumn#sortableDirection} changed.
 
   @typedef {CustomEvent} coral-table-column:_sortabledirectionchanged
 
   @private
   */

  /**
   Triggered when {@link TableColumn#hidden} changed.
 
   @typedef {CustomEvent} coral-table-column:_hiddenchanged
 
   @private
   */
 
  /**
   Triggered before {@link TableColumn#sortableDirection} changed.
 
   @typedef {CustomEvent} coral-table-column:_beforecolumnsort
 
   @private
   */
 
  /**
   Triggered when {@link TableColumn#sortableDirection} changed.
 
   @typedef {CustomEvent} coral-table-column:_sort
 
   @private
   */
});
 
export default TableColumn;