All files / coral-component-shell/src/scripts ShellOrganization.js

91.01% Statements 81/89
76.47% Branches 26/34
78.95% Functions 15/19
91.01% Lines 81/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                                              2x               2x 2x   2x           216x   216x   216x               216x 216x     216x   216x   216x   216x                 2x           52x   26x   26x                 82x   82x                                 324x   324x   324x 2x 4x   2x 2x         2x     324x             324x 324x   324x 2x       2x               298x   298x 292x 76x                 216x   216x 34x 34x     182x 182x     216x                                         216x   216x   216x   216x   216x   216x 90x     216x   216x 90x       216x 216x 216x 216x   216x   216x   216x 216x   216x                       1414x 216x                 1414x                     572x                               32x   32x   32x 32x 32x  
/**
 * 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 {List} from '../../../coral-component-list';
import {SelectableCollection} from '../../../coral-collection';
import {Icon} from '../../../coral-component-icon';
import organization from '../templates/organization';
import {transform, commons} from '../../../coral-utils';
 
const CLASSNAME = '_coral-Shell-orgSwitcher-item';
 
/**
 @class Coral.Shell.Organization
 @classdesc A Shell Organization component
 @htmltag coral-shell-organization
 @extends {ListItem}
 */
class ShellOrganization extends List.Item {
  /** @ignore */
  constructor() {
    super();
 
    // Events
    this._delegateEvents({
      'click': '_onClick',
      'key:enter': '_onClick',
      'key:space': '_onClick',
      // Private
      'coral-shell-suborganization:_selectedchanged': '_onItemSelectedChanged'
    });
 
    const template = {};
    organization.call(template, {Icon});
    commons.extend(this._elements, template);
 
    // Used for eventing
    this._oldSelection = null;
 
    // Item handling
    this.items._startHandlingItems(true);
  }
 
  /**
   The item collection.
 
   @type {SelectableCollection}
   @readonly
   */
  get items() {
    // Construct the collection on first request:
    if (!this._items) {
      this._items = new SelectableCollection({
        host: this,
        itemTagName: 'coral-shell-suborganization',
        container: this._elements.items,
        onItemAdded: this._onItemAdded,
        onItemRemoved: this._onItemRemoved
      });
    }
 
    return this._items;
  }
 
  /**
   Returns the selected workspace.
 
   @type {HTMLElement}
   @readonly
   */
  get selectedItem() {
    return this.items._getLastSelected();
  }
 
  /**
   Whether this organization is selected.
 
   @type {Boolean}
   @default false
   @htmlattribute selected
   @htmlattributereflected
   */
  get selected() {
    return this._selected || false;
  }
 
  set selected(value) {
    this._selected = transform.booleanAttr(value);
    this._reflectAttribute('selected', this._selected);
 
    this.setAttribute('aria-selected', this._selected);
    this.classList.toggle('is-selected', this._selected);
    this._elements.checkmark.hidden = !this._selected;

    if (this.items) {
      const selectedItem = this.selectedItem;
 
      this.classList.toggle('is-child-selected', selectedItem);
 
      if (!this._selected && selectedItem) {
        // Always de-select children when de-selected
        selectedItem.removeAttribute('selected');
      }
    }
 
    this.trigger(`${this.tagName.toLowerCase()}:_selectedchanged`);
  }
 
  /**
   The name of this organization.
 
   @type {String}
   @default ""
   @htmlattribute name
   @htmlattributereflected
   */
  get name() {
    return this._name || '';
  }
 
  set name(value) {
    this._name = transform.string(value);
    this._reflectAttribute('name', this._name);
  }
 
  /** @private */
  _onItemSelectedChanged(event) {
    // Validate the selection only if we're an organization
    if (this.tagName === 'CORAL-SHELL-ORGANIZATION') {
      // Don't stop propagation here as the orgSwitcher listens to the event too
      const item = event.target;
      this._validateSelection(item);
    }
  }
 
  /** @private */
  _onItemAdded(item) {
    // Move all items into the right place
    this._moveItems();
 
    this._validateSelection(item);
  }
 
  /** @private */
  _onItemRemoved(item) {
    this._setParent();
 
    this._validateSelection(item);
  }
 
  /** @private */
  _validateSelection(item) {
    // gets the current selection
    const selection = this.items._getAllSelected();
    const selectionCount = selection.length;
 
    if (selectionCount > 1) {
      for (let i = 0 ; i < selectionCount ; i++) {
        if (selection[i] !== item) {
          // Don't trigger change events
          this._preventTriggeringEvents = true;
          selection[i].removeAttribute('selected');
        }
      }
 
      // We can trigger change events again
      this._preventTriggeringEvents = false;
    }
 
    this._triggerChangeEvent();
  }

  /** @private */
  _triggerChangeEvent() {
    const selectedItem = this.selectedItem;
    const oldSelection = this._oldSelection;
 
    if (!this._preventTriggeringEvents && selectedItem !== oldSelection) {
      this.trigger(`${this.tagName.toLowerCase()}:change`, {
        oldSelection: oldSelection,
        selection: selectedItem
      });
 
      this._oldSelection = selectedItem;
    }
  }
 
  /** @private */
  _moveItems() {
    Array.prototype.forEach.call(this.querySelectorAll('coral-shell-suborganization'), (item) => {
      if (!this._elements.items.contains(item)) {
        this._elements.items.appendChild(item);
      }
    });
  }
 
  /** @private */
  _setParent() {
    const hasChildren = this.items.length !== 0;
 
    if (hasChildren) {
      this.removeAttribute('role');
      this.removeAttribute('tabindex');
    } else {
      // Be accessible
      this.setAttribute('role', 'button');
      this.setAttribute('tabindex', 0);
    }
 
    this.classList.toggle('is-parent', hasChildren);
  }
 
  /** @private */
  _onClick() {
    if (this.items.length !== 0) {
      // You can't be selected if you have sub-organizations
      return;
    }
 
    this.selected = true;
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat(['name', 'selected']);
  }
 
  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    // Move items into the right place
    this._moveItems();
 
    this.setAttribute('id', this.id || commons.getUID());
 
    // Support cloneNode
    const items = this.querySelector(`#${this.id} > ._coral-Shell-orgSwitcher-subitems`);
    if (items) {
      items.remove();
    }
    const checkmark = this.querySelector(`#${this.id} > ._coral-Shell-orgSwitcher-checkmark`);
    if (checkmark) {
      checkmark.remove();
    }
 
    // Render template
    const frag = document.createDocumentFragment();
 
    frag.appendChild(this._elements.checkmark);
    frag.appendChild(this._elements.items);
    this.appendChild(frag);
 
    // Don't trigger events once connected
    this._preventTriggeringEvents = true;
    this._validateSelection();
    this._preventTriggeringEvents = false;
 
    this._oldSelection = this.selectedItem;
 
    this._setParent();
  }

  /**
   Triggered when a {@link ShellOrganization} selection changed.
 
   @typedef {CustomEvent} coral-shell-organization:_selectedchanged
 
   @private
   */
}
 
export default ShellOrganization;