All files / coral-component-wait/src/scripts Wait.js

100% Statements 89/89
93.75% Branches 45/48
100% Functions 17/17
100% Lines 89/89

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                                                                  2x                                 2x           2x                   2x 2x   2x         2134x   2134x   2134x 2134x 2134x                       2x         2442x   94x     2442x             94x   94x   94x   94x 36x     94x 72x       94x 92x       94x 16x       94x   94x 40x       94x 94x                   8x     120x 120x   120x     120x   120x                           118x     2036x   2036x   2036x                         4x     94x 94x   94x                           6x     118x   118x   118x 116x   116x 116x 116x 116x   2x   2x 2x 2x                             12x     152x   152x 2x 150x 2x     152x   152x   152x 152x   152x     16x 10x 10x 10x 6x 2x 2x 2x   4x 4x       16x 16x 16x   136x 136x     152x                   22x  
/**
 * 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 base from '../templates/base';
import {Decorator} from '../../../coral-decorator';
 
/**
 Enumeration for {@link Wait} variants.
 
 @typedef {Object} WaitVariantEnum
 
 @property {String} DEFAULT
 The default variant.
 @property {String} DOTS
 Not supported. Falls back to DEFAULT.
 */
const variant = {
  DEFAULT: 'default',
  DOTS: 'dots'
};
 
/**
 Enumeration for {@link Wait} sizes.
 
 @typedef {Object} WaitSizeEnum
 
 @property {String} SMALL
 A small wait indicator.
 @property {String} MEDIUM
 A medium wait indicator. This is the default size.
 @property {String} LARGE
 A large wait indicator.
 */
const size = {
  SMALL: 'S',
  MEDIUM: 'M',
  LARGE: 'L'
};
 
// the waits's base classname
const CLASSNAME = '_coral-CircleLoader';
 
/**
 @class Coral.Wait
 @classdesc A Wait component to be used to indicate a process that is in-progress for an indefinite amount of time.
 When the time is known, {@link Progress} should be used instead.
 @htmltag coral-wait
 @extends {HTMLElement}
 @extends {BaseComponent}
 */
const Wait = Decorator(class extends BaseComponent(HTMLElement) {
  constructor() {
    super();
 
    // Prepare templates
    this._elements = {};
    base.call(this._elements);
  }
 
  /**
   The size of the wait indicator. Currently 'S' (the default), 'M' and 'L' are available.
   See {@link WaitSizeEnum}.
 
   @type {String}
   @default WaitSizeEnum.MEDIUM
   @htmlattribute size
   @htmlattributereflected
   */
  get size() {
    return this._size || size.MEDIUM;
  }
 
  set size(value) {
    value = transform.string(value).toUpperCase();
    this._size = validate.enumeration(size)(value) && value || size.MEDIUM;
    this._reflectAttribute('size', this._size);
 
    // large css change
    this.classList.toggle(`${CLASSNAME}--large`, this._size === size.LARGE);
 
    // small css change
    this.classList.toggle(`${CLASSNAME}--small`, this._size === size.SMALL);
  }
 
  /**
   Whether the component is centered or not. The container needs to have the style <code>position: relative</code>
   for the centering to work correctly.
   @type {Boolean}
   @default false
   @htmlattribute centered
   @htmlattributereflected
   */
  get centered() {
    return this._centered || false;
  }
 
  set centered(value) {
    this._centered = transform.booleanAttr(value);
    this._reflectAttribute('centered', this._centered);
 
    this.classList.toggle(`${CLASSNAME}--centered`, this._centered);
  }
 
  /**
   The wait's variant. See {@link WaitVariantEnum}.
 
   @type {String}
   @default WaitVariantEnum.DEFAULT
   @htmlattribute variant
   @htmlattributereflected
   */
  get variant() {
    return this._variant || variant.DEFAULT;
  }
 
  set variant(value) {
    value = transform.string(value).toLowerCase();
    this._variant = validate.enumeration(variant)(value) && value || variant.DEFAULT;
    this._reflectAttribute('variant', this._variant);
  }
 
  /**
   Whether to hide the current value and show an animation. Set to true for operations whose progress cannot be
   determined.
 
   @type {Boolean}
   @default false
   @htmlattribute indeterminate
   @htmlattributereflected
   */
  get indeterminate() {
    return this._indeterminate || false;
  }
 
  set indeterminate(value) {
    this._indeterminate = transform.booleanAttr(value);
    this._reflectAttribute('indeterminate', this._indeterminate);
 
    if (this._indeterminate) {
      this.classList.add(`${CLASSNAME}--indeterminate`);
 
      // ARIA: Remove attributes
      this.removeAttribute('aria-valuenow');
      this.removeAttribute('aria-valuemin');
      this.removeAttribute('aria-valuemax');

      this.value = 0;
    } else {
      this.classList.remove(`${CLASSNAME}--indeterminate`);
 
      // ARIA: Add attributes
      this.setAttribute('aria-valuemin', '0');
      this.setAttribute('aria-valuemax', '100');
 
      this.value = this._oldValue;
    }
  }
 
  /**
   The current progress in percent. If no value is set on initialization, wait is forced into indeterminate state.
 
   @type {Number}
   @default 0
   @emits {coral-wait:change}
   @htmlattribute value
   @htmlattributereflected
   */
  get value() {
    return this.hasAttribute('indeterminate') ? 0 : this._value || 0;
  }
 
  set value(value) {
    value = transform.number(value) || 0;
 
    // Stay within bounds
    if (value > 100) {
      value = 100;
    } else if (value < 0) {
      value = 0;
    }
 
    this._value = value;
    this._reflectAttribute('value', this._value);
 
    const subMask1 = this._elements.subMask1;
    const subMask2 = this._elements.subMask2;

    if (!this.hasAttribute('indeterminate')) {
      let angle;
 
      if (value > 0 && value <= 50) {
        angle = -180 + value / 50 * 180;
        subMask1.style.transform = `rotate(${angle}deg)`;
        subMask2.style.transform = 'rotate(-180deg)';
      } else if (value > 50) {
        angle = -180 + (value - 50) / 50 * 180;
        subMask1.style.transform = 'rotate(0deg)';
        subMask2.style.transform = `rotate(${angle}deg)`;
      } else {
        subMask1.style.transform = '';
        subMask2.style.transform = '';
      }
 
      // ARIA: Reflect value for screenreaders
      this.setAttribute('aria-valuenow', this._value);
      this.setAttribute('aria-valuemin', '0');
      this.setAttribute('aria-valuemax', '100');
    } else {
      subMask1.style.transform = '';
      subMask2.style.transform = '';
    }
 
    this.trigger('coral-wait:change');
  }
 
  /**
   Returns {@link Wait} sizes.
 
   @return {WaitSizeEnum}
   */
  static get size() {
    return size;
  }
 
  /**
   Returns {@link Wait} variants.
 
   @return {WaitVariantEnum}
   */
  static get variant() {
    return variant;
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat(['size', 'centered', 'variant', 'value', 'indeterminate']);
  }
 
  /** @ignore */
  attributeChangedCallback(name, oldValue, value) {
    if (name === 'indeterminate' && transform.booleanAttr(value)) {
      // Remember current value in case indeterminate is toggled back
      this._oldValue = this._value || 0;
    }
 
    super.attributeChangedCallback(name, oldValue, value);
  }
 
  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    // ARIA
    this.setAttribute('role', 'progressbar');
 
    // Default reflected attributes
    if (!this._size) {
      this.size = size.MEDIUM;
    }
    if (!this._variant) {
      this.variant = variant.DEFAULT;
    }
 
    // If no value is specified, indeterminate is set
    if (!this._value) {
      this.indeterminate = true;
    }
 
    // Centering reads the size
    if (this.centered) {
      this.centered = this.centered;
    }
 
    // Support cloneNode
    const template = this.querySelectorAll('._coral-CircleLoader-track, ._coral-CircleLoader-fills');
    for (let i = 0 ; i < template.length ; i++) {
      template[i].remove();
    }
 
    // Render template
    this.appendChild(this._elements.track);
    this.appendChild(this._elements.fills);
  }
 
  /**
   Triggered when the {@link Wait} value is changed.
 
   @typedef {CustomEvent} coral-wait:change
   */
});
 
export default Wait;