source: extensions/takeatour/js/bootstrap-tour.js @ 27898

Last change on this file since 27898 was 27898, checked in by flop25, 10 years ago

first upload
working WIP
one tour available

File size: 23.5 KB
Line 
1/* ===========================================================
2# bootstrap-tour - v0.8.1
3# http://bootstraptour.com
4# ==============================================================
5# Copyright 2012-2013 Ulrich Sossou
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11#     http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18*/
19(function($, window) {
20  var Tour, document;
21  document = window.document;
22  Tour = (function() {
23    function Tour(options) {
24      this._options = $.extend({
25        name: "tour",
26        container: "body",
27        keyboard: true,
28        storage: window.localStorage,
29        debug: false,
30        backdrop: false,
31        redirect: true,
32        orphan: false,
33        duration: false,
34        basePath: "",
35        template: "<div class='popover'>          <div class='arrow'></div>          <h3 class='popover-title'></h3>          <div class='popover-content'></div>          <div class='popover-navigation'>            <div class='btn-group'>              <button class='btn btn-sm btn-default' data-role='prev'>&laquo; Prev</button>              <button class='btn btn-sm btn-default' data-role='next'>Next &raquo;</button>              <button class='btn btn-sm btn-default' data-role='pause-resume'                data-pause-text='Pause'                data-resume-text='Resume'              >Pause</button>            </div>            <button class='btn btn-sm btn-default' data-role='end'>End tour</button>          </div>        </div>",
36        afterSetState: function(key, value) {},
37        afterGetState: function(key, value) {},
38        afterRemoveState: function(key) {},
39        onStart: function(tour) {},
40        onEnd: function(tour) {},
41        onShow: function(tour) {},
42        onShown: function(tour) {},
43        onHide: function(tour) {},
44        onHidden: function(tour) {},
45        onNext: function(tour) {},
46        onPrev: function(tour) {},
47        onPause: function(tour, duration) {},
48        onResume: function(tour, duration) {}
49      }, options);
50      this._force = false;
51      this._inited = false;
52      this._steps = [];
53      this.backdrop = {
54        overlay: null,
55        $element: null,
56        $background: null,
57        backgroundShown: false,
58        overlayElementShown: false
59      };
60    }
61
62    Tour.prototype.setState = function(key, value) {
63      var e, keyName;
64      if (this._options.storage) {
65        keyName = "" + this._options.name + "_" + key;
66        try {
67          this._options.storage.setItem(keyName, value);
68        } catch (_error) {
69          e = _error;
70          if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
71            this.debug("LocalStorage quota exceeded. setState failed.");
72          }
73        }
74        return this._options.afterSetState(keyName, value);
75      } else {
76        if (this._state == null) {
77          this._state = {};
78        }
79        return this._state[key] = value;
80      }
81    };
82
83    Tour.prototype.removeState = function(key) {
84      var keyName;
85      if (this._options.storage) {
86        keyName = "" + this._options.name + "_" + key;
87        this._options.storage.removeItem(keyName);
88        return this._options.afterRemoveState(keyName);
89      } else {
90        if (this._state != null) {
91          return delete this._state[key];
92        }
93      }
94    };
95
96    Tour.prototype.getState = function(key) {
97      var keyName, value;
98      if (this._options.storage) {
99        keyName = "" + this._options.name + "_" + key;
100        value = this._options.storage.getItem(keyName);
101      } else {
102        if (this._state != null) {
103          value = this._state[key];
104        }
105      }
106      if (value === void 0 || value === "null") {
107        value = null;
108      }
109      this._options.afterGetState(key, value);
110      return value;
111    };
112
113    Tour.prototype.addSteps = function(steps) {
114      var step, _i, _len, _results;
115      _results = [];
116      for (_i = 0, _len = steps.length; _i < _len; _i++) {
117        step = steps[_i];
118        _results.push(this.addStep(step));
119      }
120      return _results;
121    };
122
123    Tour.prototype.addStep = function(step) {
124      return this._steps.push(step);
125    };
126
127    Tour.prototype.getStep = function(i) {
128      if (this._steps[i] != null) {
129        return $.extend({
130          id: "step-" + i,
131          path: "",
132          placement: "right",
133          title: "",
134          content: "<p></p>",
135          next: i === this._steps.length - 1 ? -1 : i + 1,
136          prev: i - 1,
137          animation: true,
138          container: this._options.container,
139          backdrop: this._options.backdrop,
140          redirect: this._options.redirect,
141          orphan: this._options.orphan,
142          duration: this._options.duration,
143          template: this._options.template,
144          onShow: this._options.onShow,
145          onShown: this._options.onShown,
146          onHide: this._options.onHide,
147          onHidden: this._options.onHidden,
148          onNext: this._options.onNext,
149          onPrev: this._options.onPrev,
150          onPause: this._options.onPause,
151          onResume: this._options.onResume
152        }, this._steps[i]);
153      }
154    };
155
156    Tour.prototype.init = function(force) {
157      var _this = this;
158      this._force = force;
159      if (this.ended()) {
160        return this._debug("Tour ended, init prevented.");
161      }
162      this.setCurrentStep();
163      this._setupMouseNavigation();
164      this._setupKeyboardNavigation();
165      this._onResize(function() {
166        return _this.showStep(_this._current);
167      });
168      if (this._current !== null) {
169        this.showStep(this._current);
170      }
171      this._inited = true;
172      return this;
173    };
174
175    Tour.prototype.start = function(force) {
176      var promise;
177      if (force == null) {
178        force = false;
179      }
180      if (!this._inited) {
181        this.init(force);
182      }
183      if (this._current === null) {
184        promise = this._makePromise(this._options.onStart != null ? this._options.onStart(this) : void 0);
185        return this._callOnPromiseDone(promise, this.showStep, 0);
186      }
187    };
188
189    Tour.prototype.next = function() {
190      var promise;
191      if (this.ended()) {
192        return this._debug("Tour ended, next prevented.");
193      }
194      promise = this.hideStep(this._current);
195      return this._callOnPromiseDone(promise, this._showNextStep);
196    };
197
198    Tour.prototype.prev = function() {
199      var promise;
200      if (this.ended()) {
201        return this._debug("Tour ended, prev prevented.");
202      }
203      promise = this.hideStep(this._current);
204      return this._callOnPromiseDone(promise, this._showPrevStep);
205    };
206
207    Tour.prototype.goTo = function(i) {
208      var promise;
209      if (this.ended()) {
210        return this._debug("Tour ended, goTo prevented.");
211      }
212      promise = this.hideStep(this._current);
213      return this._callOnPromiseDone(promise, this.showStep, i);
214    };
215
216    Tour.prototype.end = function() {
217      var endHelper, promise,
218        _this = this;
219      endHelper = function(e) {
220        $(document).off("click.tour-" + _this._options.name);
221        $(document).off("keyup.tour-" + _this._options.name);
222        $(window).off("resize.tour-" + _this._options.name);
223        _this.setState("end", "yes");
224        _this._inited = false;
225        _this._force = false;
226        _this._clearTimer();
227        if (_this._options.onEnd != null) {
228          return _this._options.onEnd(_this);
229        }
230      };
231      promise = this.hideStep(this._current);
232      return this._callOnPromiseDone(promise, endHelper);
233    };
234
235    Tour.prototype.ended = function() {
236      return !this._force && !!this.getState("end");
237    };
238
239    Tour.prototype.restart = function() {
240      this.removeState("current_step");
241      this.removeState("end");
242      this.setCurrentStep(0);
243      return this.start();
244    };
245
246    Tour.prototype.pause = function() {
247      var step;
248      step = this.getStep(this._current);
249      if (!(step && step.duration)) {
250        return;
251      }
252      this._paused = true;
253      this._duration -= new Date().getTime() - this._start;
254      window.clearTimeout(this._timer);
255      this._debug("Paused/Stopped step " + (this._current + 1) + " timer (" + this._duration + " remaining).");
256      if (step.onPause != null) {
257        return step.onPause(this, this._duration);
258      }
259    };
260
261    Tour.prototype.resume = function() {
262      var step,
263        _this = this;
264      step = this.getStep(this._current);
265      if (!(step && step.duration)) {
266        return;
267      }
268      this._paused = false;
269      this._start = new Date().getTime();
270      this._duration = this._duration || step.duration;
271      this._timer = window.setTimeout(function() {
272        if (_this._isLast()) {
273          return _this.next();
274        } else {
275          return _this.end();
276        }
277      }, this._duration);
278      this._debug("Started step " + (this._current + 1) + " timer with duration " + this._duration);
279      if ((step.onResume != null) && this._duration !== step.duration) {
280        return step.onResume(this, this._duration);
281      }
282    };
283
284    Tour.prototype.hideStep = function(i) {
285      var hideStepHelper, promise, step,
286        _this = this;
287      step = this.getStep(i);
288      if (!step) {
289        return;
290      }
291      this._clearTimer();
292      promise = this._makePromise(step.onHide != null ? step.onHide(this, i) : void 0);
293      hideStepHelper = function(e) {
294        var $element;
295        $element = $(step.element);
296        if (!($element.data("bs.popover") || $element.data("popover"))) {
297          $element = $("body");
298        }
299        $element.popover("destroy");
300        if (step.reflex) {
301          $element.css("cursor", "").off("click.tour-" + _this._options.name);
302        }
303        if (step.backdrop) {
304          _this._hideBackdrop();
305        }
306        if (step.onHidden != null) {
307          return step.onHidden(_this);
308        }
309      };
310      this._callOnPromiseDone(promise, hideStepHelper);
311      return promise;
312    };
313
314    Tour.prototype.showStep = function(i) {
315      var promise, showStepHelper, skipToPrevious, step,
316        _this = this;
317      step = this.getStep(i);
318      if (!step) {
319        return;
320      }
321      skipToPrevious = i < this._current;
322      promise = this._makePromise(step.onShow != null ? step.onShow(this, i) : void 0);
323      showStepHelper = function(e) {
324        var current_path, path;
325        _this.setCurrentStep(i);
326        path = $.isFunction(step.path) ? step.path.call() : _this._options.basePath + step.path;
327        current_path = [document.location.pathname, document.location.hash].join("");
328        if (_this._isRedirect(path, current_path)) {
329          _this._redirect(step, path);
330          return;
331        }
332        if (_this._isOrphan(step)) {
333          if (!step.orphan) {
334            _this._debug("Skip the orphan step " + (_this._current + 1) + ". Orphan option is false and the element doesn't exist or is hidden.");
335            if (skipToPrevious) {
336              _this._showPrevStep();
337            } else {
338              _this._showNextStep();
339            }
340            return;
341          }
342          _this._debug("Show the orphan step " + (_this._current + 1) + ". Orphans option is true.");
343        }
344        if (step.backdrop) {
345          _this._showBackdrop(!_this._isOrphan(step) ? step.element : void 0);
346        }
347        _this._scrollIntoView(step.element, function() {
348          if ((step.element != null) && step.backdrop) {
349            _this._showOverlayElement(step.element);
350          }
351          _this._showPopover(step, i);
352          if (step.onShown != null) {
353            step.onShown(_this);
354          }
355          return _this._debug("Step " + (_this._current + 1) + " of " + _this._steps.length);
356        });
357        if (step.duration) {
358          return _this.resume();
359        }
360      };
361      this._callOnPromiseDone(promise, showStepHelper);
362      return promise;
363    };
364
365    Tour.prototype.getCurrentStep = function() {
366      return this._current;
367    };
368
369    Tour.prototype.setCurrentStep = function(value) {
370      if (value != null) {
371        this._current = value;
372        this.setState("current_step", value);
373      } else {
374        this._current = this.getState("current_step");
375        this._current = this._current === null ? null : parseInt(this._current, 10);
376      }
377      return this;
378    };
379
380    Tour.prototype._showNextStep = function() {
381      var promise, showNextStepHelper, step,
382        _this = this;
383      step = this.getStep(this._current);
384      showNextStepHelper = function(e) {
385        return _this.showStep(step.next);
386      };
387      promise = this._makePromise((step.onNext != null ? step.onNext(this) : void 0));
388      return this._callOnPromiseDone(promise, showNextStepHelper);
389    };
390
391    Tour.prototype._showPrevStep = function() {
392      var promise, showPrevStepHelper, step,
393        _this = this;
394      step = this.getStep(this._current);
395      showPrevStepHelper = function(e) {
396        return _this.showStep(step.prev);
397      };
398      promise = this._makePromise((step.onPrev != null ? step.onPrev(this) : void 0));
399      return this._callOnPromiseDone(promise, showPrevStepHelper);
400    };
401
402    Tour.prototype._debug = function(text) {
403      if (this._options.debug) {
404        return window.console.log("Bootstrap Tour '" + this._options.name + "' | " + text);
405      }
406    };
407
408    Tour.prototype._isRedirect = function(path, currentPath) {
409      return (path != null) && path !== "" && path.replace(/\?.*$/, "").replace(/\/?$/, "") !== currentPath.replace(/\/?$/, "");
410    };
411
412    Tour.prototype._redirect = function(step, path) {
413      if ($.isFunction(step.redirect)) {
414        return step.redirect.call(this, path);
415      } else if (step.redirect === true) {
416        this._debug("Redirect to " + path);
417        return document.location.href = path;
418      }
419    };
420
421    Tour.prototype._isOrphan = function(step) {
422      return (step.element == null) || !$(step.element).length || $(step.element).is(":hidden") && ($(step.element)[0].namespaceURI !== "http://www.w3.org/2000/svg");
423    };
424
425    Tour.prototype._isLast = function() {
426      return this._current < this._steps.length - 1;
427    };
428
429    Tour.prototype._showPopover = function(step, i) {
430      var $element, $navigation, $template, $tip, isOrphan, options,
431        _this = this;
432      options = $.extend({}, this._options);
433      $template = $.isFunction(step.template) ? $(step.template(i, step)) : $(step.template);
434      $navigation = $template.find(".popover-navigation");
435      isOrphan = this._isOrphan(step);
436      if (isOrphan) {
437        step.element = "body";
438        step.placement = "top";
439        $template = $template.addClass("orphan");
440      }
441      $element = $(step.element);
442      $template.addClass("tour-" + this._options.name);
443      if (step.options) {
444        $.extend(options, step.options);
445      }
446      if (step.reflex) {
447        $element.css("cursor", "pointer").on("click.tour-" + this._options.name, function() {
448          if (_this._isLast()) {
449            return _this.next();
450          } else {
451            return _this.end();
452          }
453        });
454      }
455      if (step.prev < 0) {
456        $navigation.find("*[data-role=prev]").addClass("disabled");
457      }
458      if (step.next < 0) {
459        $navigation.find("*[data-role=next]").addClass("disabled");
460      }
461      if (!step.duration) {
462        $navigation.find("*[data-role='pause-resume']").remove();
463      }
464      step.template = $template.clone().wrap("<div>").parent().html();
465      $element.popover({
466        placement: step.placement,
467        trigger: "manual",
468        title: step.title,
469        content: step.content,
470        html: true,
471        animation: step.animation,
472        container: step.container,
473        template: step.template,
474        selector: step.element
475      }).popover("show");
476      $tip = $element.data("bs.popover") ? $element.data("bs.popover").tip() : $element.data("popover").tip();
477      $tip.attr("id", step.id);
478      this._reposition($tip, step);
479      if (isOrphan) {
480        return this._center($tip);
481      }
482    };
483
484    Tour.prototype._reposition = function($tip, step) {
485      var offsetBottom, offsetHeight, offsetRight, offsetWidth, originalLeft, originalTop, tipOffset;
486      offsetWidth = $tip[0].offsetWidth;
487      offsetHeight = $tip[0].offsetHeight;
488      tipOffset = $tip.offset();
489      originalLeft = tipOffset.left;
490      originalTop = tipOffset.top;
491      offsetBottom = $(document).outerHeight() - tipOffset.top - $tip.outerHeight();
492      if (offsetBottom < 0) {
493        tipOffset.top = tipOffset.top + offsetBottom;
494      }
495      offsetRight = $("html").outerWidth() - tipOffset.left - $tip.outerWidth();
496      if (offsetRight < 0) {
497        tipOffset.left = tipOffset.left + offsetRight;
498      }
499      if (tipOffset.top < 0) {
500        tipOffset.top = 0;
501      }
502      if (tipOffset.left < 0) {
503        tipOffset.left = 0;
504      }
505      $tip.offset(tipOffset);
506      if (step.placement === "bottom" || step.placement === "top") {
507        if (originalLeft !== tipOffset.left) {
508          return this._replaceArrow($tip, (tipOffset.left - originalLeft) * 2, offsetWidth, "left");
509        }
510      } else {
511        if (originalTop !== tipOffset.top) {
512          return this._replaceArrow($tip, (tipOffset.top - originalTop) * 2, offsetHeight, "top");
513        }
514      }
515    };
516
517    Tour.prototype._center = function($tip) {
518      return $tip.css("top", $(window).outerHeight() / 2 - $tip.outerHeight() / 2);
519    };
520
521    Tour.prototype._replaceArrow = function($tip, delta, dimension, position) {
522      return $tip.find(".arrow").css(position, delta ? 50 * (1 - delta / dimension) + "%" : "");
523    };
524
525    Tour.prototype._scrollIntoView = function(element, callback) {
526      var $element, $window, counter, offsetTop, scrollTop, windowHeight,
527        _this = this;
528      $element = $(element);
529      if (!$element.length) {
530        return callback();
531      }
532      $window = $(window);
533      offsetTop = $element.offset().top;
534      windowHeight = $window.height();
535      scrollTop = Math.max(0, offsetTop - (windowHeight / 2));
536      this._debug("Scroll into view. ScrollTop: " + scrollTop + ". Element offset: " + offsetTop + ". Window height: " + windowHeight + ".");
537      counter = 0;
538      return $("body,html").stop(true, true).animate({
539        scrollTop: Math.ceil(scrollTop)
540      }, function() {
541        if (++counter === 2) {
542          callback();
543          return _this._debug("Scroll into view. Animation end element offset: " + ($element.offset().top) + ". Window height: " + ($window.height()) + ".");
544        }
545      });
546    };
547
548    Tour.prototype._onResize = function(callback, timeout) {
549      return $(window).on("resize.tour-" + this._options.name, function() {
550        clearTimeout(timeout);
551        return timeout = setTimeout(callback, 100);
552      });
553    };
554
555    Tour.prototype._setupMouseNavigation = function() {
556      var _this = this;
557      _this = this;
558      $(document).off("click.tour-" + this._options.name, ".popover.tour-" + this._options.name + " *[data-role=next]:not(.disabled)").on("click.tour-" + this._options.name, ".popover.tour-" + this._options.name + " *[data-role=next]:not(.disabled)", function(e) {
559        e.preventDefault();
560        return _this.next();
561      });
562      $(document).off("click.tour-" + this._options.name, ".popover.tour-" + this._options.name + " *[data-role=prev]:not(.disabled)").on("click.tour-" + this._options.name, ".popover.tour-" + this._options.name + " *[data-role=prev]:not(.disabled)", function(e) {
563        e.preventDefault();
564        return _this.prev();
565      });
566      $(document).off("click.tour-" + this._options.name, ".popover.tour-" + this._options.name + " *[data-role=end]").on("click.tour-" + this._options.name, ".popover.tour-" + this._options.name + " *[data-role=end]", function(e) {
567        e.preventDefault();
568        return _this.end();
569      });
570      return $(document).off("click.tour-" + this._options.name, ".popover.tour-" + this._options.name + " *[data-role=pause-resume]").on("click.tour-" + this._options.name, ".popover.tour-" + this._options.name + " *[data-role=pause-resume]", function(e) {
571        var $this;
572        e.preventDefault();
573        $this = $(this);
574        $this.text(_this._paused ? $this.data("pause-text") : $this.data("resume-text"));
575        if (_this._paused) {
576          return _this.resume();
577        } else {
578          return _this.pause();
579        }
580      });
581    };
582
583    Tour.prototype._setupKeyboardNavigation = function() {
584      var _this = this;
585      if (!this._options.keyboard) {
586        return;
587      }
588      return $(document).on("keyup.tour-" + this._options.name, function(e) {
589        if (!e.which) {
590          return;
591        }
592        switch (e.which) {
593          case 39:
594            e.preventDefault();
595            if (_this._isLast()) {
596              return _this.next();
597            } else {
598              return _this.end();
599            }
600            break;
601          case 37:
602            e.preventDefault();
603            if (_this._current > 0) {
604              return _this.prev();
605            }
606            break;
607          case 27:
608            e.preventDefault();
609            return _this.end();
610        }
611      });
612    };
613
614    Tour.prototype._makePromise = function(result) {
615      if (result && $.isFunction(result.then)) {
616        return result;
617      } else {
618        return null;
619      }
620    };
621
622    Tour.prototype._callOnPromiseDone = function(promise, cb, arg) {
623      var _this = this;
624      if (promise) {
625        return promise.then(function(e) {
626          return cb.call(_this, arg);
627        });
628      } else {
629        return cb.call(this, arg);
630      }
631    };
632
633    Tour.prototype._showBackdrop = function(element) {
634      if (this.backdrop.backgroundShown) {
635        return;
636      }
637      this.backdrop = $("<div/>", {
638        "class": "tour-backdrop"
639      });
640      this.backdrop.backgroundShown = true;
641      return $("body").append(this.backdrop);
642    };
643
644    Tour.prototype._hideBackdrop = function() {
645      this._hideOverlayElement();
646      return this._hideBackground();
647    };
648
649    Tour.prototype._hideBackground = function() {
650      this.backdrop.remove();
651      this.backdrop.overlay = null;
652      return this.backdrop.backgroundShown = false;
653    };
654
655    Tour.prototype._showOverlayElement = function(element) {
656      var $background, $element, offset;
657      if (this.backdrop.overlayElementShown) {
658        return;
659      }
660      this.backdrop.overlayElementShown = true;
661      $element = $(element);
662      $background = $("<div/>");
663      offset = $element.offset();
664      offset.top = offset.top;
665      offset.left = offset.left;
666      $background.width($element.innerWidth()).height($element.innerHeight()).addClass("tour-step-background").offset(offset);
667      $element.addClass("tour-step-backdrop");
668      $("body").append($background);
669      this.backdrop.$element = $element;
670      return this.backdrop.$background = $background;
671    };
672
673    Tour.prototype._hideOverlayElement = function() {
674      if (!this.backdrop.overlayElementShown) {
675        return;
676      }
677      this.backdrop.$element.removeClass("tour-step-backdrop");
678      this.backdrop.$background.remove();
679      this.backdrop.$element = null;
680      this.backdrop.$background = null;
681      return this.backdrop.overlayElementShown = false;
682    };
683
684    Tour.prototype._clearTimer = function() {
685      window.clearTimeout(this._timer);
686      this._timer = null;
687      return this._duration = null;
688    };
689
690    return Tour;
691
692  })();
693  return window.Tour = Tour;
694})(jQuery, window);
Note: See TracBrowser for help on using the repository browser.