shepherd.js
746 lines
| 1 | /*! tether-shepherd 1.8.1 */ |
| 2 | |
| 3 | (function(root, factory) { |
| 4 | if (typeof define === 'function' && define.amd) { |
| 5 | define(["tether"], factory); |
| 6 | } else if (typeof exports === 'object') { |
| 7 | module.exports = factory(require('tether')); |
| 8 | } else { |
| 9 | root.Shepherd = factory(root.Tether); |
| 10 | } |
| 11 | }(this, function(Tether) { |
| 12 | |
| 13 | /* global Tether */ |
| 14 | |
| 15 | 'use strict'; |
| 16 | |
| 17 | var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); |
| 18 | |
| 19 | var _get = function get(_x5, _x6, _x7) { var _again = true; _function: while (_again) { var object = _x5, property = _x6, receiver = _x7; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x5 = parent; _x6 = property; _x7 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; |
| 20 | |
| 21 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } |
| 22 | |
| 23 | function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } |
| 24 | |
| 25 | var _Tether$Utils = Tether.Utils; |
| 26 | var Evented = _Tether$Utils.Evented; |
| 27 | var addClass = _Tether$Utils.addClass; |
| 28 | var extend = _Tether$Utils.extend; |
| 29 | var hasClass = _Tether$Utils.hasClass; |
| 30 | var removeClass = _Tether$Utils.removeClass; |
| 31 | var uniqueId = _Tether$Utils.uniqueId; |
| 32 | |
| 33 | var Shepherd = new Evented(); |
| 34 | |
| 35 | function isUndefined(obj) { |
| 36 | return typeof obj === 'undefined'; |
| 37 | }; |
| 38 | |
| 39 | function isArray(obj) { |
| 40 | return obj && obj.constructor === Array; |
| 41 | }; |
| 42 | |
| 43 | function isObject(obj) { |
| 44 | return obj && obj.constructor === Object; |
| 45 | }; |
| 46 | |
| 47 | function isObjectLoose(obj) { |
| 48 | return typeof obj === 'object'; |
| 49 | }; |
| 50 | |
| 51 | var ATTACHMENT = { |
| 52 | 'top right': 'bottom left', |
| 53 | 'top left': 'bottom right', |
| 54 | 'top center': 'bottom center', |
| 55 | 'middle right': 'middle left', |
| 56 | 'middle left': 'middle right', |
| 57 | 'middle center': 'middle center', |
| 58 | 'bottom left': 'top right', |
| 59 | 'bottom right': 'top left', |
| 60 | 'bottom center': 'top center', |
| 61 | 'top': 'bottom center', |
| 62 | 'left': 'middle right', |
| 63 | 'right': 'middle left', |
| 64 | 'bottom': 'top center', |
| 65 | 'center': 'middle center', |
| 66 | 'middle': 'middle center' |
| 67 | }; |
| 68 | |
| 69 | function createFromHTML(html) { |
| 70 | var el = document.createElement('div'); |
| 71 | el.innerHTML = html; |
| 72 | return el.children[0]; |
| 73 | } |
| 74 | |
| 75 | function matchesSelector(el, sel) { |
| 76 | var matches = undefined; |
| 77 | if (!isUndefined(el.matches)) { |
| 78 | matches = el.matches; |
| 79 | } else if (!isUndefined(el.matchesSelector)) { |
| 80 | matches = el.matchesSelector; |
| 81 | } else if (!isUndefined(el.msMatchesSelector)) { |
| 82 | matches = el.msMatchesSelector; |
| 83 | } else if (!isUndefined(el.webkitMatchesSelector)) { |
| 84 | matches = el.webkitMatchesSelector; |
| 85 | } else if (!isUndefined(el.mozMatchesSelector)) { |
| 86 | matches = el.mozMatchesSelector; |
| 87 | } else if (!isUndefined(el.oMatchesSelector)) { |
| 88 | matches = el.oMatchesSelector; |
| 89 | } |
| 90 | return matches.call(el, sel); |
| 91 | } |
| 92 | |
| 93 | var positionRe = /^(.+) (top|left|right|bottom|center|\[[a-z ]+\])$/; |
| 94 | |
| 95 | function parsePosition(str) { |
| 96 | if (isObjectLoose(str)) { |
| 97 | if (str.hasOwnProperty("element") && str.hasOwnProperty("on")) { |
| 98 | return str; |
| 99 | } |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | var matches = positionRe.exec(str); |
| 104 | if (!matches) { |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | var on = matches[2]; |
| 109 | if (on[0] === '[') { |
| 110 | on = on.substring(1, on.length - 1); |
| 111 | } |
| 112 | |
| 113 | return { |
| 114 | 'element': matches[1], |
| 115 | 'on': on |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | function parseShorthand(obj, props) { |
| 120 | if (obj === null || isUndefined(obj)) { |
| 121 | return obj; |
| 122 | } else if (isObjectLoose(obj)) { |
| 123 | return obj; |
| 124 | } |
| 125 | |
| 126 | var vals = obj.split(' '); |
| 127 | var out = {}; |
| 128 | var j = props.length - 1; |
| 129 | for (var i = vals.length - 1; i >= 0; i--) { |
| 130 | if (j === 0) { |
| 131 | out[props[j]] = vals.slice(0, i + 1).join(' '); |
| 132 | break; |
| 133 | } else { |
| 134 | out[props[j]] = vals[i]; |
| 135 | } |
| 136 | |
| 137 | j--; |
| 138 | } |
| 139 | |
| 140 | return out; |
| 141 | } |
| 142 | |
| 143 | var Step = (function (_Evented) { |
| 144 | _inherits(Step, _Evented); |
| 145 | |
| 146 | function Step(tour, options) { |
| 147 | _classCallCheck(this, Step); |
| 148 | |
| 149 | _get(Object.getPrototypeOf(Step.prototype), 'constructor', this).call(this, tour, options); |
| 150 | this.tour = tour; |
| 151 | this.bindMethods(); |
| 152 | this.setOptions(options); |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | _createClass(Step, [{ |
| 157 | key: 'bindMethods', |
| 158 | value: function bindMethods() { |
| 159 | var _this = this; |
| 160 | |
| 161 | var methods = ['_show', 'show', 'hide', 'isOpen', 'cancel', 'complete', 'scrollTo', 'destroy', 'render']; |
| 162 | methods.map(function (method) { |
| 163 | _this[method] = _this[method].bind(_this); |
| 164 | }); |
| 165 | } |
| 166 | }, { |
| 167 | key: 'setOptions', |
| 168 | value: function setOptions() { |
| 169 | var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; |
| 170 | |
| 171 | this.options = options; |
| 172 | this.destroy(); |
| 173 | |
| 174 | this.id = this.options.id || this.id || 'step-' + uniqueId(); |
| 175 | |
| 176 | var when = this.options.when; |
| 177 | if (when) { |
| 178 | for (var _event in when) { |
| 179 | if (({}).hasOwnProperty.call(when, _event)) { |
| 180 | var handler = when[_event]; |
| 181 | this.on(_event, handler, this); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Button configuration |
| 187 | |
| 188 | var buttonsJson = JSON.stringify(this.options.buttons); |
| 189 | var buttonsAreDefault = isUndefined(buttonsJson) || buttonsJson === "true"; |
| 190 | |
| 191 | var buttonsAreEmpty = buttonsJson === "{}" || buttonsJson === "[]" || buttonsJson === "null" || buttonsJson === "false"; |
| 192 | |
| 193 | var buttonsAreArray = !buttonsAreDefault && isArray(this.options.buttons); |
| 194 | |
| 195 | var buttonsAreObject = !buttonsAreDefault && isObject(this.options.buttons); |
| 196 | |
| 197 | // Show default button if undefined or 'true' |
| 198 | if (buttonsAreDefault) { |
| 199 | this.options.buttons = [{ |
| 200 | text: 'Next', |
| 201 | action: this.tour.next, |
| 202 | classes: 'btn' |
| 203 | }]; |
| 204 | |
| 205 | // Can pass in an object which will assume asingle button |
| 206 | } else if (!buttonsAreEmpty && buttonsAreObject) { |
| 207 | this.options.buttons = [this.options.buttons]; |
| 208 | |
| 209 | // Falsey/empty values or non-object values prevent buttons from rendering |
| 210 | } else if (buttonsAreEmpty || !buttonsAreArray) { |
| 211 | this.options.buttons = false; |
| 212 | } |
| 213 | } |
| 214 | }, { |
| 215 | key: 'getTour', |
| 216 | value: function getTour() { |
| 217 | return this.tour; |
| 218 | } |
| 219 | }, { |
| 220 | key: 'bindAdvance', |
| 221 | value: function bindAdvance() { |
| 222 | var _this2 = this; |
| 223 | |
| 224 | // An empty selector matches the step element |
| 225 | |
| 226 | var _parseShorthand = parseShorthand(this.options.advanceOn, ['selector', 'event']); |
| 227 | |
| 228 | var event = _parseShorthand.event; |
| 229 | var selector = _parseShorthand.selector; |
| 230 | |
| 231 | var handler = function handler(e) { |
| 232 | if (!_this2.isOpen()) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | if (!isUndefined(selector)) { |
| 237 | if (matchesSelector(e.target, selector)) { |
| 238 | _this2.tour.next(); |
| 239 | } |
| 240 | } else { |
| 241 | if (_this2.el && e.target === _this2.el) { |
| 242 | _this2.tour.next(); |
| 243 | } |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | // TODO: this should also bind/unbind on show/hide |
| 248 | document.body.addEventListener(event, handler); |
| 249 | this.on('destroy', function () { |
| 250 | return document.body.removeEventListener(event, handler); |
| 251 | }); |
| 252 | } |
| 253 | }, { |
| 254 | key: 'getAttachTo', |
| 255 | value: function getAttachTo() { |
| 256 | var opts = parsePosition(this.options.attachTo) || {}; |
| 257 | var returnOpts = extend({}, opts); |
| 258 | |
| 259 | if (typeof opts.element === 'string') { |
| 260 | // Can't override the element in user opts reference because we can't |
| 261 | // guarantee that the element will exist in the future. |
| 262 | returnOpts.element = document.querySelector(opts.element); |
| 263 | if (!returnOpts.element) { |
| 264 | console.error('The element for this Shepherd step was not found ' + opts.element); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return returnOpts; |
| 269 | } |
| 270 | }, { |
| 271 | key: 'setupTether', |
| 272 | value: function setupTether() { |
| 273 | if (isUndefined(Tether)) { |
| 274 | throw new Error("Using the attachment feature of Shepherd requires the Tether library"); |
| 275 | } |
| 276 | |
| 277 | var opts = this.getAttachTo(); |
| 278 | var attachment = ATTACHMENT[opts.on] || ATTACHMENT.right; |
| 279 | if (isUndefined(opts.element)) { |
| 280 | opts.element = 'viewport'; |
| 281 | attachment = 'middle center'; |
| 282 | } |
| 283 | |
| 284 | var tetherOpts = { |
| 285 | classPrefix: 'shepherd', |
| 286 | element: this.el, |
| 287 | constraints: [{ |
| 288 | to: 'window', |
| 289 | pin: true, |
| 290 | attachment: 'together' |
| 291 | }], |
| 292 | target: opts.element, |
| 293 | offset: opts.offset || '0 0', |
| 294 | attachment: attachment |
| 295 | }; |
| 296 | |
| 297 | if (this.tether) { |
| 298 | this.tether.destroy(); |
| 299 | } |
| 300 | |
| 301 | this.tether = new Tether(extend(tetherOpts, this.options.tetherOptions)); |
| 302 | } |
| 303 | }, { |
| 304 | key: 'show', |
| 305 | value: function show() { |
| 306 | var _this3 = this; |
| 307 | |
| 308 | if (!isUndefined(this.options.beforeShowPromise)) { |
| 309 | var beforeShowPromise = this.options.beforeShowPromise(); |
| 310 | if (!isUndefined(beforeShowPromise)) { |
| 311 | return beforeShowPromise.then(function () { |
| 312 | return _this3._show(); |
| 313 | }); |
| 314 | } |
| 315 | } |
| 316 | this._show(); |
| 317 | } |
| 318 | }, { |
| 319 | key: '_show', |
| 320 | value: function _show() { |
| 321 | var _this4 = this; |
| 322 | |
| 323 | this.trigger('before-show'); |
| 324 | |
| 325 | if (!this.el) { |
| 326 | this.render(); |
| 327 | } |
| 328 | |
| 329 | addClass(this.el, 'shepherd-open'); |
| 330 | |
| 331 | document.body.setAttribute('data-shepherd-step', this.id); |
| 332 | |
| 333 | this.setupTether(); |
| 334 | |
| 335 | if (this.options.scrollTo) { |
| 336 | setTimeout(function () { |
| 337 | _this4.scrollTo(); |
| 338 | }); |
| 339 | } |
| 340 | |
| 341 | this.trigger('show'); |
| 342 | } |
| 343 | }, { |
| 344 | key: 'hide', |
| 345 | value: function hide() { |
| 346 | this.trigger('before-hide'); |
| 347 | |
| 348 | removeClass(this.el, 'shepherd-open'); |
| 349 | |
| 350 | document.body.removeAttribute('data-shepherd-step'); |
| 351 | |
| 352 | if (this.tether) { |
| 353 | this.tether.destroy(); |
| 354 | } |
| 355 | this.tether = null; |
| 356 | |
| 357 | this.trigger('hide'); |
| 358 | } |
| 359 | }, { |
| 360 | key: 'isOpen', |
| 361 | value: function isOpen() { |
| 362 | return this.el && hasClass(this.el, 'shepherd-open'); |
| 363 | } |
| 364 | }, { |
| 365 | key: 'cancel', |
| 366 | value: function cancel() { |
| 367 | this.tour.cancel(); |
| 368 | this.trigger('cancel'); |
| 369 | } |
| 370 | }, { |
| 371 | key: 'complete', |
| 372 | value: function complete() { |
| 373 | this.tour.complete(); |
| 374 | this.trigger('complete'); |
| 375 | } |
| 376 | }, { |
| 377 | key: 'scrollTo', |
| 378 | value: function scrollTo() { |
| 379 | var _getAttachTo = this.getAttachTo(); |
| 380 | |
| 381 | var element = _getAttachTo.element; |
| 382 | |
| 383 | if (!isUndefined(this.options.scrollToHandler)) { |
| 384 | this.options.scrollToHandler(element); |
| 385 | } else if (!isUndefined(element)) { |
| 386 | element.scrollIntoView(); |
| 387 | } |
| 388 | } |
| 389 | }, { |
| 390 | key: 'destroy', |
| 391 | value: function destroy() { |
| 392 | if (!isUndefined(this.el) && this.el.parentNode) { |
| 393 | this.el.parentNode.removeChild(this.el); |
| 394 | delete this.el; |
| 395 | } |
| 396 | |
| 397 | if (this.tether) { |
| 398 | this.tether.destroy(); |
| 399 | } |
| 400 | this.tether = null; |
| 401 | |
| 402 | this.trigger('destroy'); |
| 403 | } |
| 404 | }, { |
| 405 | key: 'render', |
| 406 | value: function render() { |
| 407 | var _this5 = this; |
| 408 | |
| 409 | if (!isUndefined(this.el)) { |
| 410 | this.destroy(); |
| 411 | } |
| 412 | |
| 413 | this.el = createFromHTML('<div class=\'shepherd-step ' + (this.options.classes || '') + '\' data-id=\'' + this.id + '\' ' + (this.options.idAttribute ? 'id="' + this.options.idAttribute + '"' : '') + '></div>'); |
| 414 | |
| 415 | var content = document.createElement('div'); |
| 416 | content.className = 'shepherd-content'; |
| 417 | this.el.appendChild(content); |
| 418 | |
| 419 | var header = document.createElement('header'); |
| 420 | content.appendChild(header); |
| 421 | |
| 422 | if (this.options.title) { |
| 423 | header.innerHTML += '<h3 class=\'shepherd-title\'>' + this.options.title + '</h3>'; |
| 424 | this.el.className += ' shepherd-has-title'; |
| 425 | } |
| 426 | |
| 427 | if (this.options.showCancelLink) { |
| 428 | var link = createFromHTML("<a href class='shepherd-cancel-link'>✕</a>"); |
| 429 | header.appendChild(link); |
| 430 | |
| 431 | this.el.className += ' shepherd-has-cancel-link'; |
| 432 | |
| 433 | this.bindCancelLink(link); |
| 434 | } |
| 435 | |
| 436 | if (!isUndefined(this.options.text)) { |
| 437 | (function () { |
| 438 | var text = createFromHTML("<div class='shepherd-text'></div>"); |
| 439 | var paragraphs = _this5.options.text; |
| 440 | |
| 441 | if (typeof paragraphs === 'function') { |
| 442 | paragraphs = paragraphs.call(_this5, text); |
| 443 | } |
| 444 | |
| 445 | if (paragraphs instanceof HTMLElement) { |
| 446 | text.appendChild(paragraphs); |
| 447 | } else { |
| 448 | if (typeof paragraphs === 'string') { |
| 449 | paragraphs = [paragraphs]; |
| 450 | } |
| 451 | |
| 452 | paragraphs.map(function (paragraph) { |
| 453 | text.innerHTML += '<p>' + paragraph + '</p>'; |
| 454 | }); |
| 455 | } |
| 456 | |
| 457 | content.appendChild(text); |
| 458 | })(); |
| 459 | } |
| 460 | |
| 461 | if (this.options.buttons) { |
| 462 | (function () { |
| 463 | var footer = document.createElement('footer'); |
| 464 | var buttons = createFromHTML("<ul class='shepherd-buttons'></ul>"); |
| 465 | |
| 466 | _this5.options.buttons.map(function (cfg) { |
| 467 | var button = createFromHTML('<li><a class=\'shepherd-button ' + (cfg.classes || '') + '\'>' + cfg.text + '</a>'); |
| 468 | buttons.appendChild(button); |
| 469 | _this5.bindButtonEvents(cfg, button.querySelector('a')); |
| 470 | }); |
| 471 | |
| 472 | footer.appendChild(buttons); |
| 473 | content.appendChild(footer); |
| 474 | })(); |
| 475 | } |
| 476 | |
| 477 | document.body.appendChild(this.el); |
| 478 | |
| 479 | this.setupTether(); |
| 480 | |
| 481 | if (this.options.advanceOn) { |
| 482 | this.bindAdvance(); |
| 483 | } |
| 484 | } |
| 485 | }, { |
| 486 | key: 'bindCancelLink', |
| 487 | value: function bindCancelLink(link) { |
| 488 | var _this6 = this; |
| 489 | |
| 490 | link.addEventListener('click', function (e) { |
| 491 | e.preventDefault(); |
| 492 | _this6.cancel(); |
| 493 | }); |
| 494 | } |
| 495 | }, { |
| 496 | key: 'bindButtonEvents', |
| 497 | value: function bindButtonEvents(cfg, el) { |
| 498 | var _this7 = this; |
| 499 | |
| 500 | cfg.events = cfg.events || {}; |
| 501 | if (!isUndefined(cfg.action)) { |
| 502 | // Including both a click event and an action is not supported |
| 503 | cfg.events.click = cfg.action; |
| 504 | } |
| 505 | |
| 506 | for (var _event2 in cfg.events) { |
| 507 | if (({}).hasOwnProperty.call(cfg.events, _event2)) { |
| 508 | var handler = cfg.events[_event2]; |
| 509 | if (typeof handler === 'string') { |
| 510 | (function () { |
| 511 | var page = handler; |
| 512 | handler = function () { |
| 513 | return _this7.tour.show(page); |
| 514 | }; |
| 515 | })(); |
| 516 | } |
| 517 | el.addEventListener(_event2, handler); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | this.on('destroy', function () { |
| 522 | for (var _event3 in cfg.events) { |
| 523 | if (({}).hasOwnProperty.call(cfg.events, _event3)) { |
| 524 | var handler = cfg.events[_event3]; |
| 525 | el.removeEventListener(_event3, handler); |
| 526 | } |
| 527 | } |
| 528 | }); |
| 529 | } |
| 530 | }]); |
| 531 | |
| 532 | return Step; |
| 533 | })(Evented); |
| 534 | |
| 535 | var Tour = (function (_Evented2) { |
| 536 | _inherits(Tour, _Evented2); |
| 537 | |
| 538 | function Tour() { |
| 539 | var _this8 = this; |
| 540 | |
| 541 | var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; |
| 542 | |
| 543 | _classCallCheck(this, Tour); |
| 544 | |
| 545 | _get(Object.getPrototypeOf(Tour.prototype), 'constructor', this).call(this, options); |
| 546 | this.bindMethods(); |
| 547 | this.options = options; |
| 548 | this.steps = this.options.steps || []; |
| 549 | |
| 550 | // Pass these events onto the global Shepherd object |
| 551 | var events = ['complete', 'cancel', 'hide', 'start', 'show', 'active', 'inactive']; |
| 552 | events.map(function (event) { |
| 553 | (function (e) { |
| 554 | _this8.on(e, function (opts) { |
| 555 | opts = opts || {}; |
| 556 | opts.tour = _this8; |
| 557 | Shepherd.trigger(e, opts); |
| 558 | }); |
| 559 | })(event); |
| 560 | }); |
| 561 | |
| 562 | return this; |
| 563 | } |
| 564 | |
| 565 | _createClass(Tour, [{ |
| 566 | key: 'bindMethods', |
| 567 | value: function bindMethods() { |
| 568 | var _this9 = this; |
| 569 | |
| 570 | var methods = ['next', 'back', 'cancel', 'complete', 'hide']; |
| 571 | methods.map(function (method) { |
| 572 | _this9[method] = _this9[method].bind(_this9); |
| 573 | }); |
| 574 | } |
| 575 | }, { |
| 576 | key: 'addStep', |
| 577 | value: function addStep(name, step) { |
| 578 | if (isUndefined(step)) { |
| 579 | step = name; |
| 580 | } |
| 581 | |
| 582 | if (!(step instanceof Step)) { |
| 583 | if (typeof name === 'string' || typeof name === 'number') { |
| 584 | step.id = name.toString(); |
| 585 | } |
| 586 | step = extend({}, this.options.defaults, step); |
| 587 | step = new Step(this, step); |
| 588 | } else { |
| 589 | step.tour = this; |
| 590 | } |
| 591 | |
| 592 | this.steps.push(step); |
| 593 | return this; |
| 594 | } |
| 595 | }, { |
| 596 | key: 'removeStep', |
| 597 | value: function removeStep(name) { |
| 598 | var current = this.getCurrentStep(); |
| 599 | |
| 600 | for (var i = 0; i < this.steps.length; ++i) { |
| 601 | var step = this.steps[i]; |
| 602 | if (step.id === name) { |
| 603 | if (step.isOpen()) { |
| 604 | step.hide(); |
| 605 | } |
| 606 | step.destroy(); |
| 607 | this.steps.splice(i, 1); |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (current && current.id === name) { |
| 613 | this.currentStep = undefined; |
| 614 | |
| 615 | if (this.steps.length) this.show(0);else this.hide(); |
| 616 | } |
| 617 | } |
| 618 | }, { |
| 619 | key: 'getById', |
| 620 | value: function getById(id) { |
| 621 | for (var i = 0; i < this.steps.length; ++i) { |
| 622 | var step = this.steps[i]; |
| 623 | if (step.id === id) { |
| 624 | return step; |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | }, { |
| 629 | key: 'getCurrentStep', |
| 630 | value: function getCurrentStep() { |
| 631 | return this.currentStep; |
| 632 | } |
| 633 | }, { |
| 634 | key: 'next', |
| 635 | value: function next() { |
| 636 | var index = this.steps.indexOf(this.currentStep); |
| 637 | |
| 638 | if (index === this.steps.length - 1) { |
| 639 | this.hide(index); |
| 640 | this.trigger('complete'); |
| 641 | this.done(); |
| 642 | } else { |
| 643 | this.show(index + 1, true); |
| 644 | } |
| 645 | } |
| 646 | }, { |
| 647 | key: 'back', |
| 648 | value: function back() { |
| 649 | var index = this.steps.indexOf(this.currentStep); |
| 650 | this.show(index - 1, false); |
| 651 | } |
| 652 | }, { |
| 653 | key: 'cancel', |
| 654 | value: function cancel() { |
| 655 | if (this.currentStep) { |
| 656 | this.currentStep.hide(); |
| 657 | } |
| 658 | this.trigger('cancel'); |
| 659 | this.done(); |
| 660 | } |
| 661 | }, { |
| 662 | key: 'complete', |
| 663 | value: function complete() { |
| 664 | if (this.currentStep) { |
| 665 | this.currentStep.hide(); |
| 666 | } |
| 667 | this.trigger('complete'); |
| 668 | this.done(); |
| 669 | } |
| 670 | }, { |
| 671 | key: 'hide', |
| 672 | value: function hide() { |
| 673 | if (this.currentStep) { |
| 674 | this.currentStep.hide(); |
| 675 | } |
| 676 | this.trigger('hide'); |
| 677 | this.done(); |
| 678 | } |
| 679 | }, { |
| 680 | key: 'done', |
| 681 | value: function done() { |
| 682 | Shepherd.activeTour = null; |
| 683 | removeClass(document.body, 'shepherd-active'); |
| 684 | this.trigger('inactive', { tour: this }); |
| 685 | } |
| 686 | }, { |
| 687 | key: 'show', |
| 688 | value: function show() { |
| 689 | var key = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0]; |
| 690 | var forward = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; |
| 691 | |
| 692 | if (this.currentStep) { |
| 693 | this.currentStep.hide(); |
| 694 | } else { |
| 695 | addClass(document.body, 'shepherd-active'); |
| 696 | this.trigger('active', { tour: this }); |
| 697 | } |
| 698 | |
| 699 | Shepherd.activeTour = this; |
| 700 | |
| 701 | var next = undefined; |
| 702 | |
| 703 | if (typeof key === 'string') { |
| 704 | next = this.getById(key); |
| 705 | } else { |
| 706 | next = this.steps[key]; |
| 707 | } |
| 708 | |
| 709 | if (next) { |
| 710 | if (!isUndefined(next.options.showOn) && !next.options.showOn()) { |
| 711 | var index = this.steps.indexOf(next); |
| 712 | var nextIndex = forward ? index + 1 : index - 1; |
| 713 | this.show(nextIndex, forward); |
| 714 | } else { |
| 715 | this.trigger('show', { |
| 716 | step: next, |
| 717 | previous: this.currentStep |
| 718 | }); |
| 719 | |
| 720 | if (this.currentStep) { |
| 721 | this.currentStep.hide(); |
| 722 | } |
| 723 | |
| 724 | this.currentStep = next; |
| 725 | next.show(); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | }, { |
| 730 | key: 'start', |
| 731 | value: function start() { |
| 732 | this.trigger('start'); |
| 733 | |
| 734 | this.currentStep = null; |
| 735 | this.next(); |
| 736 | } |
| 737 | }]); |
| 738 | |
| 739 | return Tour; |
| 740 | })(Evented); |
| 741 | |
| 742 | extend(Shepherd, { Tour: Tour, Step: Step, Evented: Evented }); |
| 743 | return Shepherd; |
| 744 | |
| 745 | })); |
| 746 |