All files / coral-component-select/src/scripts SelectItem.js

98% Statements 49/50
80% Branches 16/20
100% Functions 18/18
98% Lines 49/50

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                                                                  2x 2x   2x           5516x   5516x   5516x 5516x   5516x           5516x       2x         2376x                     16x   16x             44x   44x             4720x   4720x             4720x   4720x         630x       372x 372x   372x                               9732x     70x   70x 70x   70x   70x                           16571x     4285x   4285x 2842x   2842x   2842x                           26033x   26033x 1830x
/**
 * 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 {transform, validate} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
import {Messenger} from '../../../coral-messenger';
 
/**
 @class Coral.Select.Item
 @classdesc A Select item component
 @htmltag coral-select-item
 @extends {HTMLElement}
 @extends {BaseComponent}
 */
const SelectItem = Decorator(class extends BaseComponent(HTMLElement) {
  /** @ignore */
  constructor() {
    super();
 
    // messenger
    this._messenger = new Messenger(this);
    this._observer = new MutationObserver(this._handleMutation.bind(this));
    this._observer.observe(this, {
      characterData: true,
      childList: true,
      subtree: true
    });
  }
 
  // @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 this item is disabled. When set to <code>true</code>, this will prevent every user interaction with the
   item. If disabled is set to <code>true</code> for a selected item it will be deselected.
 
   @type {Boolean}
   @default false
   @htmlattribute disabled
   @htmlattributereflected
   */
  get disabled() {
    return this._disabled || false;
  }
 
  set disabled(value) {
    value = transform.booleanAttr(value);
 
    if(validate.valueMustChange(this._disabled, value)) {
      this._disabled = value;
      this._reflectAttribute('disabled', value);
      this._messenger.postMessage('coral-select-item:_disabledchanged');
    }
  }
 
  /**
   Whether the item is selected. Selected cannot be set to <code>true</code> if the item is disabled.
 
   @type {Boolean}
   @default false
   @htmlattribute selected
   @htmlattributereflected
   */
  get selected() {
    return this._selected || false;
  }
 
  set selected(value) {
    value = transform.booleanAttr(value);
 
    if(validate.valueMustChange(this._selected, value)) {
      this._selected = value;
      this._reflectAttribute('selected', value);
      this._messenger.postMessage('coral-select-item:_selectedchanged');
    }
  }
 
  /**
   Value of the item. If not explicitly set, the value of <code>Node.textContent</code> is returned.
 
   @type {String}
   @default ""
   @htmlattribute value
   @htmlattributereflected
   */
  get value() {
    let val = this._value;
    if (typeof this._value === 'undefined') {
      if (this.getAttribute('value') === null) {
        // keep spaces to only 1 max and trim to mimic native select option behavior
        val = this.textContent.replace(/\s{2,}/g, ' ').trim();
      E} else {
        val = this.getAttribute('value');
      }
    }
 
    return val;
  }
 
  set value(value) {
    value = transform.string(value);
 
    if(validate.valueMustChange(this._value, value)) {
      this._value = value;
      this._reflectAttribute('value', value);
      this._messenger.postMessage('coral-select-item:_valuechanged');
    }
  }
 
  /**
   Inherited from {@link BaseComponent#trackingElement}.
   */
  get trackingElement() {
    return typeof this._trackingElement === 'undefined' ?
      // keep spaces to only 1 max and trim. this mimics native html behaviors
      Ethis.value || this.textContent.replace(/\s{2,}/g, ' ').trim() :
      this._trackingElement;
  }
 
  set trackingElement(value) {
    super.trackingElement = value;
  }
 
  /** @private */
  _handleMutation() {
    this._messenger.postMessage('coral-select-item:_contentchanged', {
      content: this.textContent
    });
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat(['selected', 'disabled', 'value']);
  }
 
  /** @ignore */
  _suspendCallback() {
    super._suspendCallback();
    this._messenger.disconnect();
  }
 
  /** @ignore */
  _resumeCallback() {
    this._messenger.connect();
    super._resumeCallback();
  }
 
  /** @ignore */
  connectedCallback() {
    this._messenger.connect();
    super.connectedCallback();
  }
 
  /** @ignore */
  disconnectedCallback() {
    super.disconnectedCallback();
    this._messenger.disconnect();
  }
});
 
export default SelectItem;
E