ExamplesPlaygroundReference Source

coral-spectrum/coral-component-table/src/scripts/BaseTableSection.js

  1. /**
  2. * Copyright 2019 Adobe. All rights reserved.
  3. * This file is licensed to you under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License. You may obtain a copy
  5. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. *
  7. * Unless required by applicable law or agreed to in writing, software distributed under
  8. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
  9. * OF ANY KIND, either express or implied. See the License for the specific language
  10. * governing permissions and limitations under the License.
  11. */
  12.  
  13. import {divider} from './TableUtil';
  14. import {transform, validate} from '../../../coral-utils';
  15.  
  16. // Builds a string containing all possible divider classnames. This will be used to remove classnames when the
  17. // divider changes
  18. const ALL_DIVIDER_CLASSES = [];
  19. for (const dividerValue in divider) {
  20. ALL_DIVIDER_CLASSES.push(`_coral-Table-divider--${divider[dividerValue]}`);
  21. }
  22.  
  23. /**
  24. @base BaseTableSection
  25. @classdesc The base element for table sections
  26. */
  27. class BaseTableSection extends superClass {
  28. /** @ignore */
  29. constructor() {
  30. super();
  31.  
  32. this._tagName = this.getAttribute('is').toLowerCase();
  33. }
  34.  
  35. /**
  36. The table section divider. See {@link TableSectionDividerEnum}.
  37.  
  38. @type {String}
  39. @default TableSectionDividerEnum.ROW
  40. @htmlattributereflected
  41. @htmlattribute divider
  42. */
  43. get divider() {
  44. return this._divider || divider.ROW;
  45. }
  46.  
  47. set divider(value) {
  48. value = transform.string(value).toLowerCase();
  49. this._divider = validate.enumeration(divider)(value) && value || divider.ROW;
  50. this._reflectAttribute('divider', this._divider);
  51.  
  52. this.classList.remove(...ALL_DIVIDER_CLASSES);
  53. this.classList.add(`_coral-Table-divider--${this.divider}`);
  54. }
  55.  
  56. _toggleObserver(enable) {
  57. this._observer = this._observer || new MutationObserver(this._handleMutations.bind(this));
  58.  
  59. if (enable) {
  60. // Initialize content MO
  61. this._observer.observe(this, {
  62. childList: true,
  63. subtree: true
  64. });
  65. } else {
  66. this._observer.disconnect();
  67. }
  68. }
  69.  
  70. _handleMutations(mutations) {
  71. mutations.forEach((mutation) => {
  72. this.trigger(`${this._tagName}:_contentchanged`, {
  73. addedNodes: mutation.addedNodes,
  74. removedNodes: mutation.removedNodes
  75. });
  76. });
  77. }
  78.  
  79. /** @ignore */
  80. static get observedAttributes() {
  81. return super.observedAttributes.concat(['divider', '_observe']);
  82. }
  83.  
  84. /** @ignore */
  85. attributeChangedCallback(name, oldValue, value) {
  86. if (name === '_observe') {
  87. this._toggleObserver(value !== 'off');
  88. } else {
  89. super.attributeChangedCallback(name, oldValue, value);
  90. }
  91. }
  92.  
  93. /** @ignore */
  94. render() {
  95. super.render();
  96.  
  97. // Default reflected attributes
  98. if (!this._divider) {
  99. this.divider = divider.ROW;
  100. }
  101. }
  102. };
  103.  
  104. export default BaseTableSection;