1 // Generated by CoffeeScript 1.3.3
3 /* ============================================================
4 # bootstrap-tour.js v0.1
5 # http://pushly.github.com/bootstrap-tour/
6 # ==============================================================
7 # Copyright 2012 Push.ly
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
25 (function($, window) {
27 document = window.document;
30 function Tour(options) {
32 this._options = $.extend({
33 afterSetState: function(key, value) {},
34 afterGetState: function(key, value) {}
37 this.setCurrentStep();
38 $(document).on("click", ".popover .next", function(e) {
42 $(document).on("click", ".popover .end", function(e) {
48 Tour.prototype.setState = function(key, value) {
49 $.cookie("tour_" + key, value, {
53 return this._options.afterSetState(key, value);
56 Tour.prototype.getState = function(key) {
58 value = $.cookie("tour_" + key);
59 this._options.afterGetState(key, value);
63 Tour.prototype.addStep = function(step) {
64 return this._steps.push(step);
67 Tour.prototype.getStep = function(i) {
74 end: i === this._steps.length - 1,
79 Tour.prototype.start = function(force) {
83 if (force || !this.ended()) {
84 return this.showStep(this._current);
88 Tour.prototype.next = function() {
89 this.hideStep(this._current);
90 return this.showNextStep();
93 Tour.prototype.end = function() {
94 this.hideStep(this._current);
95 return this.setState("end", "yes");
98 Tour.prototype.ended = function() {
99 return !!this.getState("end");
102 Tour.prototype.restart = function() {
103 this.setState("current_step", null);
104 this.setState("end", null);
105 this.setCurrentStep(0);
109 Tour.prototype.hideStep = function(i) {
111 step = this.getStep(i);
112 if (step.onHide != null) {
115 return $(step.element).popover("hide");
118 Tour.prototype.showStep = function(i) {
119 var endOnClick, step,
121 step = this.getStep(i);
122 if (step.element == null) {
126 this.setCurrentStep(i);
127 if (step.path !== "" && document.location.pathname !== step.path && document.location.pathname.replace(/^.*[\\\/]/, '') !== step.path) {
128 document.location.href = step.path;
131 if ($(step.element).is(":hidden")) {
135 endOnClick = step.endOnClick || step.element;
136 $(endOnClick).one("click", function() {
137 return _this.endCurrentStep();
139 if (step.onShow != null) {
142 return this._showPopover(step, i);
145 Tour.prototype.setCurrentStep = function(value) {
147 this._current = value;
148 return this.setState("current_step", value);
150 this._current = this.getState("current_step");
151 if (this._current === null || this._current === "null") {
152 return this._current = 0;
154 return this._current = parseInt(this._current);
159 Tour.prototype.endCurrentStep = function() {
161 this.hideStep(this._current);
162 step = this.getStep(this._current);
163 return this.setCurrentStep(step.next);
166 Tour.prototype.showNextStep = function() {
168 step = this.getStep(this._current);
169 return this.showStep(step.next);
172 Tour.prototype._showPopover = function(step, i) {
174 content = "" + step.content + "<br /><p>";
176 content += "<a href='#' class='end'>End</a>";
178 content += "<a href='#" + step.next + "' class='next'>Next »</a> <a href='#' class='pull-right end'>End tour</a></p>";
180 $(step.element).popover({
181 placement: step.placement,
185 animation: step.animation
187 tip = $(step.element).data("popover").tip();
188 this._reposition(tip);
189 return this._scrollIntoView(tip);
192 Tour.prototype._reposition = function(tip) {
193 var offsetBottom, offsetRight, tipOffset;
194 tipOffset = tip.offset();
195 offsetBottom = $(document).outerHeight() - tipOffset.top - $(tip).outerHeight();
196 if (offsetBottom < 0) {
197 tipOffset.top = tipOffset.top + offsetBottom;
199 offsetRight = $(document).outerWidth() - tipOffset.left - $(tip).outerWidth();
200 if (offsetRight < 0) {
201 tipOffset.left = tipOffset.left + offsetRight;
203 if (tipOffset.top < 0) {
206 if (tipOffset.left < 0) {
209 return tip.offset(tipOffset);
212 Tour.prototype._scrollIntoView = function(tip) {
214 tipRect = tip.get(0).getBoundingClientRect();
215 if (!(tipRect.top > 0 && tipRect.bottom < $(window).height() && tipRect.left > 0 && tipRect.right < $(window).width())) {
216 return tip.get(0).scrollIntoView(true);
223 return window.Tour = Tour;