All files / coral-component-masonry/src/scripts MasonryItem.js

92% Statements 92/100
82.22% Branches 37/45
90.91% Functions 20/22
92% Lines 92/100

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                                                        2x     2x                 2x 2x   2x           422x   422x   422x   422x   422x   422x 422x 422x       2x               217x   217x 217x 217x 217x 217x                 90x                 444x   20x 14x   6x   6x   2x         444x 18x 14x 14x     18x 426x 6x   6x                   122x   122x   30x 30x   92x 12x   80x               6x   6x             20x   20x         20x             412x   412x     412x             412x   412x   412x   412x   412x   412x         412x   412x 412x 412x 412x     412x 412x 412x       412x               412x   412x   412x 10x     412x   412x   412x             412x     412x     412x   412x 406x           14x                                           16x     2x            
/**
 * 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 {DragAction} from '../../../coral-dragaction';
import '../../../coral-component-checkbox';
import quickactions from '../templates/quickactions';
import {transform, commons, validate, i18n} from '../../../coral-utils';
import MasonryItemAccessibilityState from './MasonryItemAccessibilityState';
import {Messenger} from '../../../coral-messenger';
import {Decorator} from '../../../coral-decorator';
 
const CLASSNAME = '_coral-Masonry-item';
 
/** @ignore */
const isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(window.navigator.platform);
 
/**
 @class Coral.Masonry.Item
 @classdesc A Masonry Item component
 @htmltag coral-masonry-item
 @extends {HTMLElement}
 @extends {BaseComponent}
 */
const MasonryItem = Decorator(class extends BaseComponent(HTMLElement) {
  /** @ignore */
  constructor() {
    super();
 
    // messenger
    this._messenger = new Messenger(this);
 
    // Represents ownership (necessary when the item is moved which triggers callbacks)
    this._masonry = null;
 
    // Default value
    this._dragAction = null;
 
    // Template
    this._elements = {};
 
    quickactions.call(this._elements);
  }
 
  // @compat
  get content() {
    return this;
  }
 
  set content(value) {
    // Support configs
    if (typeof value === 'object') {
      for (const prop in value) {
        /** @ignore */
        this[prop] = value[prop];
      }
    }
  }
 
  /**
   Specify while disconnecting the item, should it show transition or not.
   This is useful when replacing large items, this result in delay.
 
   @type {Boolean}
   @default true
   @private No need to update in public document.
   */E
  get showRemoveTransition() {
    return !(this._showRemoveTransition === false);
  }
 
  set showRemoveTransition(value) {
    this._showRemoveTransition = transform.boolean(value);
  }
 
 
  /**
   Specify whether the item is in removing state or not.
   @type {Boolean}
   */
  get removing() {
    return this.hasAttribute('_removing');
  }
 
  /**
   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);
 
    if(validate.valueMustChange(this._selected, value)) {
      this._selected = value;
      this._reflectAttribute('selected', value);
      this.trigger('coral-masonry-item:_selecteditemchanged');
 
      this.setAttribute('aria-selected', value);
      this.classList.toggle('is-selected', value);
 
      this._elements.check[value ? 'setAttribute' : 'removeAttribute']('checked', '');
 
      this._messenger.postMessage('coral-masonry-item:_selectedchanged');
    }
  }
 
  /**
   Animates the insertion of the item.
 
   @private
   */
  _insert() {
    if (this.classList.contains('is-beforeInserting')) {
      this.classList.remove('is-beforeInserting');
      this.classList.add('is-inserting');
 
      commons.transitionEnd(this, () => {
        this.classList.remove('is-inserting');
      });
    }
  }
 
  /** @private */
  _setTabbable(tabbable) {
    this.setAttribute('tabindex', tabbable ? 0 : -1);
  }
 
  /** @private */
  _updateDragAction(enabled) {
    let handle;
    if (enabled) {
      // Find handle
      if (this.getAttribute('coral-masonry-draghandle') !== null) {
        handle = this;
      } else {
        handle = this.querySelector('[coral-masonry-draghandle]');
        if (!handle) {
          // Disable drag&drop if handle wasn't found
          enabled = false;
        }
      }
    }
 
    if (enabled) {
      if (!this._dragAction) {
        this._dragAction = new DragAction(this);
        this._dragAction.dropZone = this.parentNode;
      }
      this._dragAction.handle = handle;
    } else if (this._dragAction) {
      this._dragAction.destroy();
      this._dragAction = null;
    }
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat(['selected', '_removing', '_orderable']);
  }
 
  /** @ignore */
  attributeChangedCallback(name, oldValue, value) {
    if (name === '_removing') {
      // Do it in the next frame so that the removing animation is visible
      window.requestAnimationFrame(() => {
        this.classList.toggle('is-removing', value !== null);
      });
    } else if (name === '_orderable') {
      this._updateDragAction(value !== null);
    } else {
      super.attributeChangedCallback(name, oldValue, value);
    }
  }
 
  /** @ignore */
  _suspendCallback() {
    super._suspendCallback();
    this._messenger.disconnect();
  }I

  /** @ignore */
  _resumeCallback() {
    this._messenger.connect();
    super._resumeCallback();
    // In case an already connected element is switched to new parent,
    // we would be ignoring the connected callback,
    // as the item will be connected to new parent, the new parent should be informed immediately
    this._messenger.postMessage('coral-masonry-item:_connected');
  }
 
  /** @ignore */
  connectedCallback() {
    thisE._messenger.connect();
    super.connectedCallback();
 
    // Inform masonry immediately
    this._messenger.postMessage('coral-masonry-item:_connected');
  }I

  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    // @a11y
    this.setAttribute('tabindex', '-1');
 
    // @a11y Add live region element to ensure announcement of selected state
    const accessibilityState = this._elements.accessibilityState || this.querySelector('coral-masonry-item-accessibilitystate') || new MasonryItemAccessibilityState();
 
    // @a11y Style to be visually hidden yet accessible to screen readers
    if (!accessibilityState.classList.contains('u-coral-screenReaderOnly')) {
      accessibilityState.classList.add('u-coral-screenReaderOnly');
    }
 
    // @a11y accessibility state string should announce in document lang, rather than item lang.
    accessibilityState.setAttribute('lang', i18n.locale);
 
    // @a11y accessibility state has role="status" to announce as a live region
    accessibilityState.setAttribute('role', 'status');
    accessibilityState.setAttribute('aria-live', 'off');
    accessibilityState.hidden = true;
 
    accessibilityState.id = accessibilityState.id || commons.getUID();
 
    // @a11y Wait a frame and append live region content element so that it is the last child within item.
    // wait for next macrotask to avoid appending the accessibility state element before quickactions.
    setTimeout(() => {
      if (!accessibilityState.parentNode) {
        this.appendChild(accessibilityState);
      }
 
      // @a11y Item should be labelled by accessibility state.
      if (isMacLike) {
        const ariaLabelledby = this.getAttribute('aria-labelledby');
        if (ariaLabelledby) {
          this.setAttribute('aria-labelledby', ariaLabelledby + ' ' + accessibilityState.id);
        }
      }
    });

    this._elements.accessibilityState = accessibilityState;

    // Support cloneNode
    const template = this.querySelector('._coral-Masonry-item-quickActions');
    if (template) {
      template.remove();
    }
    this.insertBefore(this._elements.quickactions, this.firstChild);
    // todo workaround to not give user possibility to tab into checkbox
    this._elements.check._labellableElement.tabIndex = -1;
    this._elements.check.setAttribute('aria-hidden', 'true');
  }
 
  /** @ignore */
  disconnectedCallback() {
    super.disconnectedCallback();
    // disconnect messenger before calling masonry._onItemDisconnected
    this._messenger.disconnect();
 
    // Handle it in masonry immediately
    const masonry = this._masonry;
    if (masonry) {
      masonry._onItemDisconnected(this);
    }
  }
});
 
export default MasonryItem;