All files / coral-component-quickactions/src/scripts QuickActionsItem.js

90.38% Statements 47/52
75% Branches 18/24
85% Functions 17/20
90.38% Lines 47/52

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                                                                        2x                       2x 2x   2x           378x   378x   378x   378x 378x   378x           378x       2x                 12x                                                           386x   386x             386x   386x             378x     378x 350x                                                           10x       6x 6x   6x                               110x     10x   10x 10x   10x   10x                               4x     302x   302x 302x   302x   302x                             656x     388x 388x   388x 386x   386x   386x
/**
 * 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 {Messenger} from '../../../coral-messenger';
import {Decorator} from '../../../coral-decorator';
 
/**
 Enumeration for {@link QuickActionsItem} type values.
 
 @typedef {Object} QuickActionsItemTypeEnum
 
 @property {String} BUTTON
 Default button type
 @property {String} ANCHOR
 Anchor button type
 */
const type = {
  BUTTON: 'button',
  ANCHOR: 'anchor'
};
 
/**
 @class Coral.QuickActions.Item
 @classdesc A QuickActions item component
 @htmltag coral-quickactions-item
 @extends {HTMLElement}
 @extends {BaseComponent}
 */
const QuickActionsItem = Decorator(class extends BaseComponent(HTMLElement) {
  /** @ignore */
  constructor() {
    super();
 
    // QuickActions will add button/anchorbutton references to it
    this._elements = {};
    // messenger
    this._messenger = new Messenger(this);
 
    this._observer = new MutationObserver(this._onMutation.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];
      }
    }
  }
 
  /**
   When <code>type</code> is {@link Coral.QuickActions.Item.type.ANCHOR}, the href will be used for the anchor.
 
   @type {String}
   @default ""
   @htmlattribute href
   @htmlattributereflected
   @emits {coral-quickactions-item:_hrefchanged}
   */
  get href() {
    return this._href || '';
  }
 
  set href(value) {
    value = transform.string(value);
 
    if(validate.valueMustChange(this._href, value)) {
      this._href = value;
      this._reflectAttribute('href', value);
      this._messenger.postMessage('coral-quickactions-item:_hrefchanged');
    }
  }
 
  /**
   Specifies the name of the icon to be shown in the QuickActions Item. See {@link Icon} for valid icon
   names.
 
   @type {String}
   @default ""
   @htmlattribute icon
   @htmlattributereflected
   @emits {coral-quickactions-item:_iconchanged}
   */
  get icon() {
    return this._icon || '';
  }

  set icon(value) {
    value = transform.string(value);
 
    if(validate.valueMustChange(this._icon, value)) {
      this._icon = value;
      this._reflectAttribute('icon', value);
 
      this._messenger.postMessage('coral-quickactions-item:_iconchanged');
    }
  }
 
  /**
   The type of item that will be used. Setting {@link QuickActionsItemTypeEnum}.ANCHOR will allow users to
   navigate using the quickactions proving the correct hypermedia to the users.
 
   @type {String}
   @default QuickActionsItemTypeEnum.BUTTON
   @htmlattribute type
   @htmlattributereflected
   */
  get type() {
    return this._type || type.BUTTON;
  }
 
  set type(value) {
    value = transform.string(value).toLowerCase();
    value = validate.enumeration(type)(value) && value || type.BUTTON;
 
    if(validate.valueMustChange(this._type, value)) {
      this._type = value;
      this._reflectAttribute('type', value);
 
      this._messenger.postMessage('coral-quickactions-item:_typechanged');
    }
  }
 
  /**
   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
      (this.textContent && this.textContent.replace(/\s{2,}/g, ' ').trim() || this.icon) :
      this._trackingElement;
  }
 
  set trackingElement(value) {
    super.trackingElement = value;
  }
 
  /**
   Handles mutations on the Item.
 
   @emits {coral-quickactions-item:_contentchanged}
 
   @private
   */
  _onMutation() {
    this._messenger.postMessage('coral-quickactions-item:_contentchanged');
  }
 
  /**
   RetEurns {@link QuickActionsItem} type options.
 
   @return {QuickActionsItemTypeEnum}
   */
  static get type() {
    return type;
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat(['href', 'icon', 'type']);
  }
 
  /** @ignore */
  _suspendCallback() {
    super._suspendCallback();
    this._messenger.disconnect();
  }
 
  /** @ignore */
  _resumeCallback() {
    this._messenger.connect();
    super._resumeCallback();
  }
E
  /** @ignore */
  connectedCallback() {
    this._messenger.connect();
    super.connectedCallback();
  }
 
  /** @ignore */
  disconnectedCallback() {
    super.disconnectedCallback();
    this._messenger.disconnect();
  }
 
  /** @ignore */
  render() {
    super.render();
 
    // Default reflected attributes
    if (!this._type) {
      this.type = type.BUTTON;
    }
  }
 
  /**
   Triggered when an icon of a {@link QuickActionsItem} was changed.
 
   @tyEpedef {CustomEvent} coral-quickactions-item:_iconchanged
 
   @private
   */
 
  /**
   Triggered when the content of a {@link QuickActionsItem} was changed.
 
   @typedef {CustomEvent} coral-quickactions-item:_contentchanged
 
   @private
   */
 
  /**
   Triggered when the href of a {@link QuickActionsItem} was changed.
 
   @typedef {CustomEvent} coral-quickactions-item:_hrefchanged
 
   @private
   */

  /**
   Triggered when the type of a {@link QuickActionsItem} was changed.
 
   @typedef {CustomEvent} coral-quickactions-item:_typechanged
 
   @private
   */
});
 
export default QuickActionsItem;