all files / addon/components/ intro-js.js

97.87% Statements 46/47
90% Branches 9/10
100% Functions 15/15
97.87% Lines 46/47
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                                                              14×                                                                                                                 12×   12× 216× 216× 216×   216×         12×   12×         12× 12× 10×     12×       14× 13×   14× 14×   14× 11× 11× 11×   11×           11×   11× 11× 11× 11× 11× 11×       16×       15× 15× 15× 15×   15×       15×       15×       27×                   26×       30×       71×        
import { A } from '@ember/array';
import { camelize, underscore } from '@ember/string';
import { scheduleOnce, bind } from '@ember/runloop';
import { observer, computed } from '@ember/object';
import { on } from '@ember/object/evented';
import Component from '@ember/component';
import introJS from 'intro-js'
 
let INTRO_JS_OPTIONS = [
  'next-label',
  'prev-label',
  'skip-label',
  'done-label',
  'tooltip-position',
  'tooltip-class',
  'highlightClass',
  'exit-on-esc',
  'exit-on-overlay-click',
  'show-step-numbers',
  'show-step-numbers',
  'keyboard-navigation',
  'show-buttons',
  'show-bullets',
  'show-progress',
  'scroll-to-element',
  'overlay-opacity',
  'disable-interaction'
];
 
export default Component.extend({
 
  setupIntroJS: on('didInsertElement', observer('start-if', function() {
    scheduleOnce('afterRender', this, this.startIntroJS);
  })),
 
  /**
   * Options passed to IntroJS. You can specify the options when using the
   * Handlebars helper:
   *
   * ```handlebars
   * {{intro-js steps=steps show-bullets=true}}
   * ```
   *
   * Or you could extend your own base class to override defaults
   * instead of specifying them every time in the Handlebars helper:
   *
   * ```javascript
   * myapp/app/components/my-intro-js.js
   *
   * import IntroJSComponent from 'ember-introjs/components/intro-js';
   *
   * export default IntroJSComponent.extend({
   *   'exit-on-esc': true
   * });
   * ```
   *
   * You can also reopen the class:
   *
   * ```javascript
   * import IntroJSComponent from 'ember-introjs/components/intro-js';
   *
   * IntroJSComponent.reopen({
   *   'exit-on-esc': true
   * });
   * ```
   *
   * @property
  */
  introJSOptions: computed(
    'next-label',
    'prev-label',
    'skip-label',
    'done-label',
    'tooltip-position',
    'tooltip-class',
    'highlightClass',
    'exit-on-esc',
    'exit-on-overlay-click',
    'show-step-numbers',
    'keyboard-navigation',
    'show-buttons',
    'show-bullets',
    'show-progress',
    'scroll-to-element',
    'overlay-opacity',
    'disable-interaction',
    'steps',
 
    function(){
      let option, normalizedName, value, options = {};
 
      for(let i = 0; i < INTRO_JS_OPTIONS.length; i++){
        option = INTRO_JS_OPTIONS[i];
        normalizedName = camelize(underscore(option));
        value = this.get(option);
 
        Iif (value !== null && value !== undefined) {
          options[normalizedName] = value;
        }
      }
 
      options.steps = this.get('steps');
 
      return options;
    }
  ),
 
  willDestroyElement() {
    let intro = this.get('introJS');
    if (intro) {
      intro.exit();
    }
 
    this._super(...arguments);
  },
 
  startIntroJS(){
    if (!this.get('introJS')) {
      this._setIntroJS(introJS());
    }
    let intro = this.get('introJS');
    let options = this.get('introJSOptions');
 
    if (this.get('start-if')){
      intro.setOptions(options);
      this.registerCallbacksWithIntroJS();
      this._setCurrentStep(0);
 
      intro.start();
    } else {
      intro.exit();
      this._setIntroJS(null);
    }
  },
 
  registerCallbacksWithIntroJS(){
    let intro = this.get('introJS');
 
    intro.onbeforechange(bind(this, this._onBeforeChange));
    intro.onchange(bind(this, this._onChange));
    intro.onafterchange(bind(this, this._onAfterChange));
    intro.oncomplete(bind(this, this._onComplete));
    intro.onexit(bind(this, this._onExit));
    intro.onskip(bind(this, this._onSkip));
  },
 
  _setIntroJS(introJS){
    this.set('introJS', introJS);
  },
 
  _onBeforeChange(elementOfNewStep) {
    let prevStep = this.get('currentStep');
    let currentStepIndex = this.get('introJS._currentStep');
    this._setCurrentStep(currentStepIndex);
    let nextStep = this._getStep(++currentStepIndex);
 
    this.sendAction('on-before-change', prevStep, nextStep, this, elementOfNewStep);
  },
 
  _onChange(targetElement) {
    this.sendAction('on-change', this._getNextStep(), this, targetElement);
  },
 
  _onAfterChange(targetElement){
    this.sendAction('on-after-change', this._getNextStep(), this, targetElement);
  },
 
  _onExit(){
    this.sendAction('on-exit', this.get('currentStep'), this);
  },
 
  _onSkip(){
    this.sendAction('on-skip', this.get('currentStep'), this);
  },
 
  _onComplete() {
    this.sendAction('on-complete', this.get('currentStep'));
  },
 
  _setCurrentStep(step){
    this.set('currentStep', this._getStep(step));
  },
 
  _getNextStep() {
    return this._getStep(this.get('introJS._currentStep') + 1);
  },
 
  _getStep(step) {
    return A(this.get('steps')).objectAt(step);
  }
});