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

100% Statements 33/33
84.62% Branches 11/13
100% Functions 11/11
100% Lines 33/33

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                                            2x   2x 8x               2x 6x 6x   6x           1370x   1370x 1370x 1370x                     6x     2678x   2678x   1814x         864x           920x   920x 3701x                         2684x 1758x   926x               876x  
/**
 * 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 {divider} from './TableUtil';
import {transform, validate} from '../../../coral-utils';
 
// Builds a string containing all possible divider classnames. This will be used to remove classnames when the
// divider changes
const ALL_DIVIDER_CLASSES = [];
for (const dividerValue in divider) {
  ALL_DIVIDER_CLASSES.push(`_coral-Table-divider--${divider[dividerValue]}`);
}
 
/**
 @base BaseTableSection
 @classdesc The base element for table sections
 */
const BaseTableSection = (superClass) => class extends superClass {
  /** @ignore */
  constructor() {
    super();
 
    this._tagName = this.getAttribute('is').toLowerCase();
  }
 
  /**
   The table section divider. See {@link TableSectionDividerEnum}.
 
   @type {String}
   @default TableSectionDividerEnum.ROW
   @htmlattributereflected
   @htmlattribute divider
   */
  get divider() {
    return this._divider || divider.ROW;
  }
 
  set divider(value) {
    value = transform.string(value).toLowerCase();
    this._divider = validate.enumeration(divider)(value) && value || divider.ROW;
    this._reflectAttribute('divider', this._divider);
 
    this.classList.remove(...ALL_DIVIDER_CLASSES);
    this.classList.add(`_coral-Table-divider--${this.divider}`);
  }
 
  _toggleObserver(enable) {
    this._observer = this._observer || new MutationObserver(this._handleMutations.bind(this));
 
    if (enable) {
      // Initialize content MO
      this._observer.observe(this, {
        childList: true,
        subtree: true
      });
    } else {
      this._observer.disconnect();
    }
  }
 
  _handleMutations(mutations) {
    mutations.forEach((mutation) => {
      this.trigger(`${this._tagName}:_contentchanged`, {
        addedNodes: mutation.addedNodes,
        removedNodes: mutation.removedNodes
      });
    });
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat(['divider', '_observe']);
  }
 
  /** @ignore */
  attributeChangedCallback(name, oldValue, value) {
    if (name === '_observe') {
      this._toggleObserver(value !== 'off');
    } else {
      super.attributeChangedCallback(name, oldValue, value);
    }
  }
 
  /** @ignore */
  render() {
    super.render();
 
    // Default reflected attributes
    if (!this._divider) {
      this.divider = divider.ROW;
    }
  }
};
 
export default BaseTableSection;