All files / coral-component-columnview/src/scripts ColumnViewItem.js

95.65% Statements 132/138
79.21% Branches 80/101
100% Functions 22/22
95.65% Lines 132/138

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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445                                                  2x                       2x               2x                 2x 2x   2x           1492x   1492x   1492x           1492x   1492x         1492x                 2x         6436x   1446x 20x     1426x 1426x   1426x 1280x 1280x   1280x       1280x 1280x   1280x       4990x                         1486x   1486x   1486x 1486x   1486x 1226x       1486x 750x     1486x 1486x 1486x   1486x   86x           1486x 1486x   1486x   1486x 166x       1486x 1486x   1486x   1486x   1486x   1486x         4522x     1486x       1486x   1486x                         10x     1486x       1486x   1486x                             326x     1486x 1486x   1486x   1486x 1486x   1486x   656x   656x 596x     656x         656x     656x     830x 830x                               1170x     1164x   1164x   1164x 1164x   1164x   1164x 1164x   1164x     1164x 1164x   1164x   1164x     1164x                           2865x     196x   196x   196x   196x 196x 196x   196x 196x   196x           196x               196x   196x   140x   140x     140x   140x 140x         56x 56x       56x       196x   196x     196x   196x 186x                               2164x     324x   324x   324x 322x 322x 322x         322x 300x 26x   274x       322x   80x 80x     322x           38328x               12x          
/**
 * 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 accessibilityState from '../templates/accessibilityState';
import {BaseComponent} from '../../../coral-base-component';
import {BaseLabellable} from '../../../coral-base-labellable';
import {Icon} from '../../../coral-component-icon';
import {Checkbox} from '../../../coral-component-checkbox';
import {commons, i18n, transform, validate} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
 
const CLASSNAME = '_coral-AssetList-item';
 
/**
 Enumeration for {@link ColumnViewItem} variants.
 
 @typedef {Object} ColumnViewItemVariantEnum
 
 @property {String} DEFAULT
 Default item variant. Contains no special decorations.
 @property {String} DRILLDOWN
 An item with a right arrow indicating that the navigation will go one level down.
 */
const variant = {
  DEFAULT: 'default',
  DRILLDOWN: 'drilldown'
};
 
/**
 Utility that identifies Chrome on macOS, which announces drilldown items as "row 1 expanded" or "row 1 collapsed" when navigating between items.
 */
const isChromeMacOS = !!window && !!window.chrome && /Mac/i.test(window.navigator.platform);
 
/**
 @class Coral.ColumnView.Item
 @classdesc A ColumnView Item component
 @htmltag coral-columnview-item
 @extends {HTMLElement}
 @extends {BaseComponent}
 */
const ColumnViewItem = Decorator(class extends BaseLabellable(BaseComponent(HTMLElement)) {
  /** @ignore */
  constructor() {
    super();
 
    // Content zone
    this._elements = {
      content: this.querySelector('coral-columnview-item-content') || document.createElement('coral-columnview-item-content'),
      thumbnail: this.querySelector('coral-columnview-item-thumbnail') || document.createElement('coral-columnview-item-thumbnail'),
      accessibilityState: this.querySelector('span[handle="accessibilityState"]')
    };
 
    if (!this._elements.accessibilityState) {
      // Templates
      accessibilityState.call(this._elements, {commons});
    }
  }
 
  /**
   The content of the item.
 
   @type {ColumnViewItemContent}
   @contentzone
   */
  geEt content() {
    return this._getContentZone(this._elements.content);
  }
 
  set content(value) {
    this._setContentZone('content', value, {
      handle: 'content',
      tagName: 'coral-columnview-item-content',
      insert: function (content) {
        content.classList.add(`${CLASSNAME}Label`);
        // Insert before chevron
        this.insertBefore(content, this.querySelector('._coral-AssetList-itemChildIndicator'));
      }
    });
  }
 
  /**
   The thumbnail of the item. It is used to hold an icon or an image.
 
   @type {ColumnViewItemThumbnail}
   @contentzone
   */
  get thumbnail() {
    return this._getContentZone(this._elements.thumbnail);
  }
 
  set thumbnail(value) {
    this._setContentZone('thumbnail', value, {
      handle: 'thumbnail',
      tagName: 'coral-columnview-item-thumbnail',
      insert: function (thumbnail) {
        thumbnail.classList.add(`${CLASSNAME}Thumbnail`);
        // Insert before content
        this.insertBefore(thumbnail, this.content || null);
      }I
    });
  }
 
  /**
   The item's variant. See {@link ColumnViewItemVariantEnum}.
 
   @type {String}
   @default ColumnViewItemVariantEnum.DEFAULT
   @htmlattribute variant
   @htmlattributereflected
   */
  get variant() {
    return this._variant || variant.DEFAULT;
  }
 
  set variant(value) {
    value = transform.string(value).toLowerCase();
    value = validate.enumeration(variant)(value) && value || variant.DEFAULT;
 
    this._reflectAttribute('variant', value);
 
    if(validate.valueMustChange(this._variant, value)) {
      this._variant = value;
 
      if (value === variant.DRILLDOWN) {
        // Render chevron on demand
        const childIndicator = this.querySelector('._coral-AssetList-itemChildIndicator');
        if (!childIndicator) {
          this.insertAdjacentHTML('beforeend', Icon._renderSVG('spectrum-css-icon-ChevronRightMedium', ['_coral-AssetList-itemChildIndicator', '_coral-UIIcon-ChevronRightMedium']));
        }
  
        this.classList.add('is-branch');
  
        // @a11y Update aria-expanded. Active drilldowns should be expanded.
        // Note: Omit aria-expanded on Chrome for macOS, because with VoiceOver tends
        // to announce drilldown items as "row 1 expanded" or "row 1 collapsed" when
        // navigating between items.
        if (this.selected || (isChromeMacOS && this.getAttribute('aria-level') === '1')) {
          this.removeAttribute('aria-expanded');
        } else {
          this.setAttribute('aria-expanded', this.active);
        }
      } else {
        this.classList.remove('is-branch');
        this.removeAttribute('aria-expanded');
      }
    }
  }
 
  /**
   Specifies the icon that will be placed inside the thumbnail. The size of the icon is always controlled by the
   component.
 
   @type {String}
   @default ""
   @htmlattribute icon
   @htmlattributereflected
   */
  get icon() {
    return this._icon || '';
  }
 
  set icon(value) {
    value = transform.string(value);
    
    this._reflectAttribute('icon', value);
 
    if(validate.valueMustChange(this._icon, value)) {
      this._icon = value;
 
      // ignored if it is an empty string
      if (value) {
        // creates a new icon element
        if (!this._elements.icon) {
          this._elements.icon = new Icon();
          // register observer only if there present an icon field.
          super._observeLabel();
        }
  
        this._elements.icon.icon = this.icon;
        this._elements.icon.size = Icon.size.SMALL;
  
        // removes all the items, since the icon attribute has precedence
        this._elements.thumbnail.innerHTML = '';
  
        // adds the newly created icon
        this._elements.thumbnail.appendChild(this._elements.icon);
      }
  
      super._toggleIconAriaHidden();
    }
  }
 
  /**
   Whether the item is selected.
 
   @type {Boolean}
   @default false
   @htmlattribute selected
   @htmlattributereflected
   */
  get selected() {
    return this._selected || false;
  }
 
  set selected(value) {
    value = transform.booleanAttr(value);
    this._reflectAttribute('selected', value);
 
    if(validate.valueMustChange(this._selected, value)) {
      this._selected = value;
      this.trigger('coral-columnview-item:_selectedchanged');
  
      // wait a frame before updating attributes
      commons.nextFrame(() => {
        this.classList.toggle('is-selected', value);
        this.setAttribute('aria-selected', value);
  
        // @a11y Update aria-expanded. Active drilldowns should be expanded.
        // Note: Omit aria-expanded on Chrome for macOS, because with VoiceOver tends
        // to announce drilldown items as "row 1 expanded" or "row 1 collapsed" when
        // navigating between items.
        if (value === variant.DRILLDOWN) {
          if (this._selected || (isChromeMacOS && this.getAttribute('aria-level') === '1')) {
            this.removeAttribute('aria-expanded');
          } else {
            this.setAttribute('aria-expanded', this.active);
          }
        }
  E
        let accessibilityState = this._elements.accessibilityState;
  
        if (value) {
  
          // @a11y Panels to right of selected item are removed, so remove aria-owns and aria-describedby attributes.
          this.removeAttribute('aria-owns');
          this.removeAttribute('aria-describedby');
  
          // @a11y Update content to ensure that checked state is announced by assistive technology when the item receives focus
          accessibilityState.innerHTML = i18n.get(', checked');
  
          // @a11y append live region content element
          if (!this.contains(accessibilityState)) {
            this.appendChild(accessibilityState);
          }
        }I
        // @a11y If deselecting from checked state,
        else {
  
          // @a11y remove, but retain reference to accessibilityState state
          if (accessibilityState.parentNode) {
            this._elements.accessibilityState = accessibilityState.parentNode.removeChild(accessibilityState);
          }
  
          // @a11y Update content to remove checked state
          this._elements.accessibilityState.innerHTML = '';
        }
  
        // @a11y Item should be labelled by thumbnail, content, and if appropriate accessibility state.
        let ariaLabelledby = this._elements.thumbnail.id + ' ' + this._elements.content.id;
        this.setAttribute('aria-labelledby', this.selected ? `${ariaLabelledby} ${accessibilityState.id}` : ariaLabelledby);
  
        // Sync checkbox item selector
        const itemSelector = this.querySelector('coral-checkbox[coral-columnview-itemselect]');
        if (itemSelector) {
          itemSelector[value ? 'setAttribute' : 'removeAttribute']('checked', '');
        }
      });
    }
  }
 
  /**
   Whether the item is active.
 
   @type {Boolean}
   @deEfault false
   @htmlattribut active
   @htmlattributereflected
   */E
  get active() {
    returnE this._active || false;
  }
 
  set active(value) {
    value = transform.booleanAttr(value);
    this._reflectAttribute('active', value);
 
    if(validate.valueMustChange(this._active, value)) {
      this._active = value;
 
      this.classList.toggle('is-navigated', value);
      this.setAttribute('aria-selected', this.hasAttribute('_selectable') ? this.selected : value);
  
      // @a11y Update aria-expanded. Active drilldowns should be expanded.
      // Note: Omit aria-expanded on Chrome for macOS, because with VoiceOver tends
      // to announce drilldown items as "row 1 expanded" or "row 1 collapsed" when
      // navigating between items.
      if (this.variant === variant.DRILLDOWN) {
        if (this.selected || (isChromeMacOS && this.getAttribute('aria-level') === '1')) {
          this.removeAttribute('aria-expanded');
        } else {
          this.setAttribute('aria-expanded', this.active);
        }
      }
  
      if (!value) {
        // @a11y Inactive items are not expanded, so remove aria-owns and aria-describedby attributes.
        this.removeAttribute('aria-owns');
        this.removeAttribute('aria-describedby');
      }
  
      this.trigger('coral-columnview-item:_activechanged');
    }
  }
 
  get _contentZones() {
    return {
      E'coral-columnview-item-content': 'content',
      'coral-columnview-item-thumbnail': 'thumbnail'
    };
  }
 
  /** @ignore */
  attributeChangedCallback(name, oldValue, value) {
    if (name === '_selectable') {
      // Disable selection
      if (value === null) {
        this.classList.remove('is-selectable');
      }
      // Enable selection
      elseI {
        this.classList.add('is-selectable');
        let itemSelector = this.querySelector('[coral-columnview-itemselect]');
 
        // Render checkbox on demand
        if (!itemSelector) {
          itemSelector = new Checkbox();
          itemSelector.setAttribute('coral-columnview-itemselect', '');
          if (this.classList.contains('is-selected')) {
            itemSelector.setAttribute('checked', '');
          }
          itemSelector._elements.input.tabIndex = -1;
          itemSelector.setAttribute('labelledby', this._elements.content.id);
 
          // Add the item selector as first child
          this.insertBefore(itemSelector, this.firstChild);
        }
      }
    } else {
      super.EattributeChangedCallback(name, oldValue, value);
    }
  }
 
  /**
   Returns {@link ColumnViewItem} variants.
E
   @return {ColumnViewItemVariantEnum}
   */
  static get variant() {
    return variant;
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat([
      'variant',
      'icon',
      'selected',
      'active',
      '_selectable'
    ]);
  }
 
  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    // @a11y
    this.setAttribute('role', 'treeitem');
 
    this.id = this.id || commons.getUID();
 
    // only set tabIndex if it is not already set
    if (!this.hasAttribute('tabindex')) {
      this.tabIndex = this.active || this.selected ? 0 : -1;
    }
 
    // Default reflected attributes
    if (!this._variant) {
      this.variant = variant.DEFAULT;
    }
 
    const thumbnail = this._elements.thumbnail;
    const content = this._elements.content;
 
    const contentZoneProvided = content.parentNode || thumbnail.parentNode;
 
    if (!contentZoneProvided) {
      // move the contents of the item into the content zone
      while (this.firstChild) {
        content.appendChild(this.firstChild);
      }
    }
 
    // Assign content zones
    this.content = content;
    this.thumbnail = thumbnail;
 
    // @a11y thumbnail img element should have alt attribute
    const thumbnailImg = thumbnail.querySelector('img:not([alt])');
    if (thumbnailImg) {
      thumbnailImg.setAttribute('alt', '');
    }
 
    // @ally add aria-labelledby so that JAWS/IE announces item correctly
    thumbnail.id = thumbnail.id || commons.getUID();
 
    content.id = content.id || commons.getUID();
 
    // @a11y Add live region element to ensure announcement of selected state
    const accessibilityState = this._elements.accessibilityState;
 
    // @a11y accessibility state string should announce in document lang, rather than item lang.
    accessibilityState.setAttribute('lang', i18n.locale);
 
    // @a11y Item should be labelled by thumbnail, content, and accessibility state.
    this.setAttribute('aria-labelledby', thumbnail.id + ' ' + content.id);
 
    //adding html title, on hovering over textcontent title will be visible
    this.setAttribute('title', this.content.textContent.trim());
  }
});
 
export default ColumnViewItem;