All files / coral-component-multifield/src/scripts MultifieldItem.js

100% Statements 69/69
100% Branches 23/23
100% Functions 16/16
100% Lines 69/69

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                                                2x                   2x 2x   2x           306x   306x   306x       306x   306x   306x   306x       306x                 2x         304x   304x   304x   304x 304x   304x 304x 304x 304x 304x   304x 40x       304x 472x   472x   312x     160x         304x   304x   304x 304x 304x 304x 304x         324x     304x         304x                           34x     314x 314x   314x 292x                       79x     68x   68x       48x     20x       68x   68x   68x                       314x     50x 50x   50x     50x 50x 574x 28x     50x 50x 50x 50x         12158x
/**
 * 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 '../../../coral-component-button';
import item from '../templates/item';
import {DragAction} from '../../../coral-dragaction';
import {i18n, transform, commons} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
 
const CLASSNAME = '_coral-Multifield-item';
 
/**
 @class Coral.Multifield.Item
 @classdesc A Multifield item component. It can have a pre-filled content different from the Multifield template but
 added items will always be rendered based on the template.
 @htmltag coral-multifield-item
 @extends {HTMLElement}
 @extends {BaseComponent}
 */
const MultifieldItem = Decorator(class extends BaseComponent(HTMLElement) {
  /** @ignore */
  constructor() {
    super();
 
    // Prepare templates
    this._elements = {
      // Create or fetch the content zones
      content: this.querySelector('coral-multifield-item-content') || document.createElement('coral-multifield-item-content')
    };
 
    const uid = this.id || commons.getUID();
    this.setAttribute('id', uid);
    this._elements.content.setAttribute('id', `${uid}-content`);
    item.call(this._elements, {i18n, uid});
  }
 
  /**
   The item content.
 
   @type {MultifieldItemContent}
   @contentzone
   */
  get content() {
    return this._getContentZone(this._elements.content);
  }
 
  set content(value) {
    this._setContentZone('content', value, {
      handle: 'content',
      tagName: 'coral-multifield-item-content',
      insert: function (content) {
        // Insert the content zone before the move and remove buttons
        this.insertBefore(content, this.firstChild);
      }
    });
  }
 
  /**
    Specify whether the remove button is in disabled state or not.
 
    @type {Boolean}
    @default false
    @private
    */
  get _deletable() {
    return typeof this.__deletable === 'boolean' ? this.__deletable : true;
  }
 
  set _deletable(value) {
    value = transform.boolean(value);
    this.__deletable = value;
 
    if(!this._readOnly) {
      this._elements.remove.disabled = !value;
    }
  }
 
  /**
   Whether the item is set to be reorder using the keyboard
 
   @type {boolean}
   @private
   */
  get _dragging() {
    return this.__dragging || false;
  }
 
  set _dragging(value) {
    this.__dragging = transform.boolean(value);
    if (this.__dragging) {
      // Setting role="application" to the move button forces
      // NVDA and JAWS screen readers into forms mode,
      // so arrow keys can be used to reorder.
      this._elements.move.setAttribute('role', 'application');
    } else {
      // when reordering stops, restore the default role for the move button
      this._elements.move.removeAttribute('role');
    }
    // aria-grabbed, may be deprecated in WAI-ARIA 1.1, but it is still reported by NVDA as "draggable" or "dragging"
    this._elements.move.setAttribute('aria-grabbed', this.__dragging);
    this._elements.move.setAttribute('aria-pressed', this.__dragging);
    this._elements.move.selected = this.__dragging;
  }
 
  /**
   Whether this multifieldItem is readOnly or not. Indicating that the user cannot modify the value of the multifieldItem fields.
   @type {Boolean}
   @default false
   @private
   */
  get _readOnly() {
    return this.__readOnly || false;
  }
 
  set _readOnly(value) {
    value = transform.booleanAttr(value);
    this.__readOnly = value;
    this._reflectAttribute('_readonly', value);
 
    // get all fields and set readonly to those whose has this property
    let allFields = this.querySelectorAll("*");
    Array.prototype.forEach.call(allFields, (field) => {
      if(typeof field.readOnly === "boolean") {
        field.readOnly = value;
      }
    });
 
    this._elements.move.disabled = value;
    this._elements.remove.disabled = value;
    this._elements.reorderup.disabled = value;
    this._elements.reorderdown.disabled = value;
  }
 
  get _contentZones() {
    return {'coral-multifield-item-content': 'content'};
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat([
      '_readonly'
    ]);
  }
 
  /** @ignore */
  static get _attributePropertyMap() {
    return commons.extend(super._attributePropertyMap, {
      _readonly: '_readOnly',
    });
  }
 
  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    // a11y
    this.setAttribute('role', 'listitem');
 
    // Create a fragment
    const fragment = document.createDocumentFragment();
 
    const templateHandleNames = ['move', 'remove', 'reorderup', 'reorderdown'];
 
    // Render the main template
    fragment.appendChild(this._elements.remove);
    fragment.appendChild(this._elements.move);
    fragment.appendChild(this._elements.reorderup);
    fragment.appendChild(this._elements.reorderdown);
 
    const content = this._elements.content;
 
    // Remove it so we can process children
    if (content.parentNode) {
      this.removeChild(content);
    }
 
    // Process remaining elements as necessary
    while (this.firstChild) {
      const child = this.firstChild;
      if (child.nodeType === Node.TEXT_NODE ||
        child.nodeType === Node.ELEMENT_NODE && templateHandleNames.indexOf(child.getAttribute('handle')) === -1) {
        // Add non-template elements to the label
        content.appendChild(child);
      } else {
        // Remove anything else
        this.removeChild(child);
      }
    }
 
    // Add the frag to the component
    this.appendChild(fragment);
 
    // Assign the content zones, moving them into place in the process
    this.content = content;
 
    // Attach drag events
    const dragAction = new DragAction(this);
    dragAction.axis = 'vertical';
    dragAction.handle = this._elements.move;
    dragAction.scroll = true;
    dragAction.useScrollParent = true;
  }
});
 
export default MultifieldItem;