All files / coral-component-wizardview/src/scripts WizardView.js

92.73% Statements 102/110
82.98% Branches 39/47
100% Functions 23/23
92.73% Lines 102/110

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                                                  2x                   2x 2x   2x           98x   98x   98x               98x   98x     98x 89x   3145x 2634x   2634x 92x           98x         98x                 2x               92x         2x                     10x 10x 10x   10x   10x                     6x 6x 6x   6x   6x                     146x   146x   146x   146x         146x             300x   300x 44x     256x   256x 256x                         256x                           822x   822x   822x   748x   74x     74x   74x     822x   816x 226x           6x   2x 4x   4x     6x 2x                 176x     176x 236x     176x 290x                     96x     96x   96x 22x 22x             96x 112x                     96x     96x   96x 42x 42x             96x 92x                     22x   22x 2x       20x   20x                   12x   12x 2x       10x   10x             96x   96x   96x   96x     96x 96x   96x 284x                               444x 98x                  
/**
 * 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 {Collection} from '../../../coral-collection';
import '../../../coral-component-steplist';
import '../../../coral-component-panelstack';
import {commons} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
 
const CLASSNAME = '_coral-WizardView';
 
/**
 @class Coral.WizardView
 @classdesc A WizardView component is the wrapping container used to create the typical Wizard pattern. This is intended
 to be used with a {@link StepList} and a {@link PanelStack}.
 @htmltag coral-wizardview
 @extends {HTMLElement}
 @extends {BaseComponent}
 */
const WizardView = Decorator(class extends BaseComponent(HTMLElement) {
  /** @ignore */
  constructor() {
    super();
 
    this._delegateEvents({
      'capture:click coral-steplist[coral-wizardview-steplist] > coral-step': '_onStepClick',
      'coral-steplist:change coral-steplist[coral-wizardview-steplist]': '_onStepListChange',
      'click [coral-wizardview-previous]': '_onPreviousClick',
      'click [coral-wizardview-next]': '_onNextClick'
    });
 
    // Init the collection mutation observer
    this.stepLists._startHandlingItems(true);
    this.panelStacks._startHandlingItems(true);
 
    // Disable tracking for specific elements that are attached to the component.
    this._observer = new MutationObserver((mutations) => {
      mutations.forEach((mutation) => {
        // Sync added nodes
        for (let i = 0 ; i < mutation.addedNodes.length ; i++) {
          const addedNode = mutation.addedNodes[i];
 
          if (addedNode.setAttribute &&
            (
              addedNode.hasAttribute('coral-wizardview-next') ||
              addedNode.hasAttribute('coral-wizardview-previous') ||
              addedNode.hasAttribute('coral-wizardview-steplist') ||
              addedNode.hasAttribute('coral-wizardview-panelstack')
            )) {
            addedNode.setAttribute('tracking', 'off');
          }
        }
      });
    });
 
    this._observer.observe(this, {
      childList: true,
      subtree: true
    });
  }
 
  /**
   The set of controlled PanelStacks. Each PanelStack must have the <code>coral-wizardview-panelstack</code> attribute.
 
   @type {Collection}
   @readonly
   */
  get panelStacks() {
    // Construct the collection on first request:
    if (!this._panelStacks) {
      this._panelStacks = new Collection({
        host: this,
        itemTagName: 'coral-panelstack',
        // allows panelstack to be nested
        itemSelector: ':scope > coral-panelstack[coral-wizardview-panelstack]',
        onlyHandleChildren: true,
        onItemAdded: this._onItemAdded
      });
    }
 
    return this._panelStacks;
  }
 
  /**
   The set of controlling StepLists. Each StepList must have the <code>coral-wizardview-steplist</code> attribute.
 
   @type {Collection}
   @readonly
   */
  get stepLists() {
    // Construct the collection on first request:
    if (!this._stepLists) {
      this._stepLists = new Collection({
        host: this,
        itemTagName: 'coral-steplist',
        // allows steplist to be nested
        itemSelector: ':scope > coral-steplist[coral-wizardview-steplist]',
        onlyHandleChildren: true,
        onItemAdded: this._onItemAdded
      });
    }
 
    return this._stepLists;
  }
 
  /**
   Called by the Collection when an item is added
 
   @private
   */
  _onItemAdded(item) {
    this._selectItemByIndex(item, this._getSelectedIndex());
  }
 
  _onStepClick(event) {
    this._trackEvent('click', 'coral-wizardview-steplist-step', event, event.matchedTarget);
  }
 
  /**
   Handles the next button click.
 
   @private
   */
  _onNextClick(event) {
    // we stop propagation in case the wizard views are nested
    event.stopPropagation();
 
    this.next();
 
    const stepList = this.stepLists.first();
    const step = stepList.items.getAll()[this._getSelectedIndex()];
    this._trackEvent('click', 'coral-wizardview-next', event, step);
  }
 
  /**
   Handles the previous button click.
 
   @private
   */
  _onPreviousClick(event) {
    // we stop propagation in case the wizard views are nested
    event.stopPropagation();
 
    this.previous();
 
    const stepList = this.stepLists.first();
    const step = stepList.items.getAll()[this._getSelectedIndex()];
    this._trackEvent('click', 'coral-wizardview-previous', event, step);
  }
 
  /**
   Detects a change in the StepList and triggers an event.
 
   @private
   */
  _onStepListChange(event) {
    // Stop propagation of the events to support nested panels
    event.stopPropagation();
 
    // Get the step number
    const index = event.target.items.getAll().indexOf(event.detail.selection);
 
    //E Sync the other StepLists
    this._selectStep(index);
 
    this.trigger('coral-wizardview:change', {
      selection: event.detail.selection,
      oldSelection: event.detail.oldSelection
    });

    this._trackEvent('change', 'coral-wizardview', event);
  }
 
  /** @private */
  _getSelectedIndex() {
    const stepList = this.stepLists.first();
    if (!stepList) {
      return -1;
    }
 
    let stepIndex = -1;
    if (stepList.items) {
      stepIndex = stepList.items.getAll().indexOf(stepList.selectedItem);
    } else {
      // Manually get the selected step
      const steps = stepList.querySelectorAll('coral-step');
 
      // Find the last selected step
      for (let i = steps.length - 1 ; i >= 0 ; i--) {
        if (steps[i].hasAttribute('selected')) {
          stepIndex = i;
          break;
        }
      }
    }
 
    return stepIndex;
  }
I
  /**
   Select the step according to the provided index.
E
   @param {*} component
   The StepList or PanelStack to select the step on.
   @param {Number} index
   The index of the step that should be selected.
 
   @private
   */
  _selectItemByIndex(component, index) {
    let item = null;
 
    // we need to set an id to be able to find direct children
    component.id = component.id || commons.getUID();
 
    // if collection api is available we use it to find the correct item
    if (component.items) {
      // Get the corresponding item
      item = compEonent.items.getAll()[index];
    }
    // Resort to querying manually on immediately children
    else if (component.tagName === 'CORAL-STEPLIST') {
      // @polyfill IE - we use id since :scope is not supported
      item = component.querySelectorAll(`#${component.id} > coral-step`)[index];
    } else if (component.tagName === 'CORAL-PANELSTACK') {
      // @polyfill IE - we use id since :scope is not supported
      item = component.querySelectorAll(`#${component.id} > coral-panel`)[index];
    }
 
    if (item) {
      // we only select if not select to avoid mutations
      if (!item.hasAttribute('selected')) {
        item.setAttribute('selected', '');
      }
    }
      // if we did not find an item to select, it means that the "index" is not available in the component, therefore we
    // need to deselect all items
    else {
      // we use the component id to be able to find direct children
      if (component.tagName === 'CORAL-STEPLIST') {
        // @polyfill IE - we use id since :scope is not supported
        item = component.querySelector(`#${component.id} > coral-step[selected]`);
      } else if (component.tagName === 'CORAL-PANELSTACK') {
        // @polyfill IE - we use id since :scope is not supported
        item = component.querySelector(`#${component.id} > coral-panel[selected]`);
      }
 
      if (item) {
        item.removeAttribute('selected');
      }
    }
  }
 
  /** @private */
  _selectStep(index) {
    // we apply the selection to all available steplists
    this.stepLists.getAll().forEach((stepList) => {
      this._selectItemByIndex(stepList, index);
    });E
 
    // we apply the selection to all available panelstacks
    this.panelStacks.getAll().forEach((panelStack) => {
      this._selectItemByIndex(panelStack, index);
    });
  }
 
  /**
   Sets the correct selected item in every PanelStack.
 
   @private
   */
  _syncPanelStackSelection(defaultIndex) {
    // Find out which step we're on by checking the first StepList
    let index = this._getSelectedIndex();
 
    if (index === -1) {
      if (typeof defaultIndex !== 'undefined') {
        index = defaultIndex;
      } else {
        // No panel selected
        return;
      }
    }
 
    thisE.panelStacks.getAll().forEach((panelStack) => {
      this._selectItemByIndex(panelStack, index);
    });
  }

  /**
   Selects the correct step in every StepList.
 
   @private
   */
  _syncStepListSelection(defaultIndex) {
    // Find out which step we're on by checking the first StepList
    let index = this._getSelectedIndex();
 
    if (index === -1) {
      if (typeof defaultIndex !== 'undefined') {
        index = defaultIndex;
      } else {
        // No step selected
        return;
      }
    }
 
    this.stepLists.getAll().forEach((stepList) => {
      this._selectItemByIndex(stepList, index);
    });
  }
 
  /**
   Shows the next step. If the WizardView is already in the last step nothing will happen.
 
   @emits {coral-wizardview:change}
   */
  next() {
    const stepList = this.stepLists.first();
    if (!stepList) {
      return;
    }
 
    // Change to the next step
    stepList.next();
 
    // Select the step everywhere
    this._selectStep(stepList.items.getAll().indexOf(stepList.selectedItem));
  }
 
  /**
   Shows the previous step. If the WizardView is already in the first step nothing will happen.
 
   @emits {coral-wizardview:change}
   */
  previous() {
    const stepList = this.stepLists.first();
    if (!stepList) {
      return;
    }
 
    // Change to the previous step
    stepList.previous();
 
    // Select the step everywhere
    this._selectStep(stepList.items.getAll().indexOf(stepList.selectedItem));
  }
 
  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    this._syncStepListSelection(0);
    this._syncPanelStackSelection(0);
 
    // Disable tracking for specific elements that are attached to the component.
    const selector = '[coral-wizardview-next],[coral-wizardview-previous],[coral-wizardview-steplist],[coral-wizardview-panelstack]';
    const items = this.querySelectorAll(selector);
    for (let i = 0 ; i < items.length ; i++) {
      items[i].setAttribute('tracking', 'off');
    }
  }
 
  /**
   Triggered when the {@link WizardView} selected step list item has changed.
 
   @typedef {CustomEvent} coral-wizardview:change
 
   @property {Step} event.detail.selection
   The new selected step list item.
   @property {Step} event.detail.oldSelection
   The prior selected step list item.
   */
});
 
export default WizardView;