forked from clu3/bootstro.js
/
bootstro.js
395 lines (349 loc) · 15.4 KB
/
bootstro.js
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
390
391
392
393
394
395
/**
* Bootstro.js Simple way to show your user around, especially first time users
* Http://github.com/clu3/bootstro.js
*
* Credit thanks to
* Revealing Module Pattern from
* http://enterprisejquery.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-1/
*
* Bootstrap popover variable width
* http://stackoverflow.com/questions/10028218/twitter-bootstrap-popovers-multiple-widths-and-other-css-properties
*
*/
$(document).ready(function(){
//Self-Executing Anonymous Func: Part 2 (Public & Private)
(function( bootstro, $, undefined ) {
var $elements; //jquery elements to be highlighted
var count;
var popovers = []; //contains array of the popovers data
var activeIndex = null; //index of active item
var defaults = {
nextButtonText : 'Next »', //will be wrapped with button as below
//nextButton : '<button class="btn btn-primary btn-mini bootstro-next-btn">Next »</button>',
prevButtonText : '« Prev',
//prevButton : '<button class="btn btn-primary btn-mini bootstro-prev-btn">« Prev</button>',
finishButtonText : '<i class="icon-ok"></i> Ok I got it, get back to the site',
//finishButton : '<button class="btn btn-mini btn-success bootstro-finish-btn"><i class="icon-ok"></i> Ok I got it, get back to the site</button>',
stopOnBackdropClick : true,
stopOnEsc : true,
//onComplete : function(params){} //params = {idx : activeIndex}
//onExit : function(params){} //params = {idx : activeIndex}
//onStep : function(params){} //params = {idx : activeIndex, direction : [next|prev]}
//url : String // ajaxed url to get show data from
margin : 100, //if the currently shown element's margin is less than this value
// the element should be scrolled so that i can be viewed properly. This is useful
// for sites which have fixed top/bottom nav bar
};
var settings;
//===================PRIVATE METHODS======================
//http://stackoverflow.com/questions/487073/check-if-element-is-visible-after-scrolling
function is_entirely_visible($elem)
{
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $elem.offset().top;
var elemBottom = elemTop + $elem.height();
return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
&& (elemBottom <= docViewBottom) && (elemTop >= docViewTop) );
}
//add the nav buttons to the popover content;
function add_nav_btn(content, i)
{
var $el = get_element(i);
var nextButton, prevButton, finishButton;
content = content + "<div class='bootstro-nav-wrapper'>";
if ($el.attr('data-bootstro-nextButton'))
{
nextButton = $el.attr('data-bootstro-nextButton');
}
else if ( $el.attr('data-bootstro-nextButtonText') )
{
nextButton = '<button class="btn btn-primary btn-mini bootstro-next-btn">' + $el.attr('data-bootstro-nextButtonText') + '</button>';
}
else
{
if (typeof settings.nextButton != 'undefined' /*&& settings.nextButton != ''*/)
nextButton = settings.nextButton;
else
nextButton = '<button class="btn btn-primary btn-mini bootstro-next-btn">' + settings.nextButtonText + '</button>';
}
if ($el.attr('data-bootstro-prevButton'))
{
prevButton = $el.attr('data-bootstro-prevButton');
}
else if ( $el.attr('data-bootstro-prevButtonText') )
{
prevButton = '<button class="btn btn-primary btn-mini bootstro-prev-btn">' + $el.attr('data-bootstro-prevButtonText') + '</button>';
}
else
{
if (typeof settings.prevButton != 'undefined' /*&& settings.prevButton != ''*/)
prevButton = settings.prevButton;
else
prevButton = '<button class="btn btn-primary btn-mini bootstro-prev-btn">' + settings.prevButtonText + '</button>';
}
if ($el.attr('data-bootstro-finishButton'))
{
finishButton = $el.attr('data-bootstro-finishButton');
}
else if ( $el.attr('data-bootstro-finishButtonText') )
{
finishButton = '<button class="btn btn-primary btn-mini bootstro-finish-btn">' + $el.attr('data-bootstro-finishButtonText') + '</button>';
}
else
{
if (typeof settings.finishButton != 'undefined' /*&& settings.finishButton != ''*/)
finishButton = settings.finishButton;
else
finishButton = '<button class="btn btn-primary btn-mini bootstro-finish-btn">' + settings.finishButtonText + '</button>';
}
if (count != 1)
{
if (i == 0)
content = content + nextButton;
else if (i == count -1 )
content = content + prevButton;
else
content = content + nextButton + prevButton
}
content = content + '</div>';
content = content +'<div class="bootstro-finish-btn-wrapper">' + finishButton + '</div>';
return content;
}
//get the element to intro at stack i
get_element = function(i)
{
//get the element with data-bootstro-step=i
//or otherwise the the natural order of the set
if ($elements.filter("[data-bootstro-step=" + i +"]").size() > 0)
return $elements.filter("[data-bootstro-step=" + i +"]");
else
{
return $elements.eq(i);
/*
nrOfElementsWithStep = 0;
$elements.filter("[data-bootstro-step!='']").each(function(j,e){
nrOfElementsWithStep ++;
if (j > i)
return $elements.filter(":not([data-bootstro-step])").eq(i - nrOfElementsWithStep);
})
*/
}
}
get_popup = function(i)
{
var p = {};
var $el = get_element(i);
//p.selector = selector;
var t = '';
if (count > 1)
{
t = "<span class='label label-success'>" + (i +1) + "/" + count + "</span>";
}
p.title = $el.attr('data-bootstro-title') || '';
if (p.title != '' && t != '')
p.title = t + ' - ' + p.title;
else if (p.title == '')
p.title = t;
p.content = $el.attr('data-bootstro-content') || '';
p.content = add_nav_btn(p.content, i);
p.placement = $el.attr('data-bootstro-placement') || 'top';
var style = '';
if ($el.attr('data-bootstro-width'))
{
p.width = $el.attr('data-bootstro-width');
style = style + 'width:' + $el.attr('data-bootstro-width') + ';'
}
if ($el.attr('data-bootstro-height'))
{
p.height = $el.attr('data-bootstro-height');
style = style + 'height:' + $el.attr('data-bootstro-height') + ';'
}
p.trigger = 'manual'; //always set to manual.
p.html = $el.attr('data-bootstro-html') || 'top';
//resize popover if it's explicitly specified
//note: this is ugly. Could have been best if popover supports width & height
p.template = '<div class="popover" style="' + style + '"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div>' +
'</div>';
return p;
}
//===================PUBLIC METHODS======================
//destroy popover at stack index i
bootstro.destroy_popover = function(i)
{
var i = i || 0;
if (i != 'all')
{
var $el = get_element(i);//$elements.eq(i);
$el.popover('destroy').removeClass('bootstro-highlight');
}
/*
else //destroy all
{
$elements.each(function(e){
$(e).popover('destroy').removeClass('bootstro-highlight');
});
}
*/
};
//destroy active popover and remove backdrop
bootstro.stop = function()
{
bootstro.destroy_popover(activeIndex);
bootstro.unbind();
$("div.bootstro-backdrop").remove();
if (typeof settings.onExit == 'function')
settings.onExit.call(this,{idx : activeIndex});
};
//go to the popover number idx starting from 0
bootstro.go_to = function(idx)
{
//destroy current popover if any
bootstro.destroy_popover(activeIndex);
if (count != 0)
{
var p = get_popup(idx);
var $el = get_element(idx);
$el.popover(p).popover('show');
//scroll if neccessary
var docviewTop = $(window).scrollTop();
var top = Math.min($(".popover.in").offset().top, $el.offset().top);
//distance between docviewTop & min.
var topDistance = top - docviewTop;
if (topDistance < settings.margin) //the element too up above
$('html,body').animate({
scrollTop: top - settings.margin},
'slow');
else if(!is_entirely_visible($(".popover.in")) || !is_entirely_visible($el))
//the element is too down below
$('html,body').animate({
scrollTop: top - settings.margin},
'slow');
// html
$el.addClass('bootstro-highlight');
activeIndex = idx;
}
};
bootstro.next = function()
{
if (activeIndex + 1 == count)
{
if (typeof settings.onComplete == 'function')
settings.onComplete.call(this, {idx : activeIndex});//
}
else
{
bootstro.go_to(activeIndex + 1);
if (typeof settings.onStep == 'function')
settings.onStep.call(this, {idx : activeIndex, direction : 'next'});//
}
};
bootstro.prev = function()
{
if (activeIndex == 0)
{
/*
if (typeof settings.onRewind == 'function')
settings.onRewind.call(this, {idx : activeIndex, direction : 'prev'});//
*/
}
else
{
bootstro.go_to(activeIndex -1);
if (typeof settings.onStep == 'function')
settings.onStep.call(this, {idx : activeIndex, direction : 'prev'});//
}
};
bootstro._start = function(selector)
{
selector = selector || '.bootstro';
$elements = $(selector);
count = $elements.size();
if (count > 0 && $('div.bootstro-backdrop').length === 0)
{
// Prevents multiple copies
$('<div class="bootstro-backdrop"></div>').appendTo('body');
bootstro.bind();
bootstro.go_to(0);
}
};
bootstro.start = function(selector, options)
{
settings = $.extend(true, {}, defaults); //deep copy
$.extend(settings, options || {});
//if options specifies a URL, get the intro configuration from URL via ajax
if (typeof settings.url != 'undefined')
{
//get config from ajax
$.ajax({
url : settings.url,
success : function(data){
if (data.success)
{
//result is an array of {selector:'','title':'','width', ...}
var popover = data.result;
//console.log(popover);
var selectorArr = [];
$.each(popover, function(t,e){
//only deal with the visible element
//build the selector
$.each(e, function(j, attr){
$(e.selector).attr('data-bootstro-' + j, attr);
});
if ($(e.selector).is(":visible"))
selectorArr.push(e.selector);
});
selector = selectorArr.join(",");
bootstro._start(selector);
}
}
});
}
else
{
bootstro._start(selector);
}
};
//bind the nav buttons click event
bootstro.bind = function()
{
bootstro.unbind();
$("html").on('click.bootstro', ".bootstro-next-btn", function(e){
bootstro.next();
e.preventDefault();
return false;
});
$("html").on('click.bootstro', ".bootstro-prev-btn", function(e){
bootstro.prev();
e.preventDefault();
return false;
});
//end of show
$("html").on('click.bootstro', ".bootstro-finish-btn", function(e){
bootstro.stop();
});
if (settings.stopOnBackdropClick)
{
$("html").on('click.bootstro', 'div.bootstro-backdrop', function(e){
if ($(e.target).hasClass('bootstro-backdrop'))
bootstro.stop();
});
}
//bind the key event
$(document).on('keydown.bootstro', function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 39 || code == 40)
bootstro.next();
else if (code == 37 || code == 38)
bootstro.prev();
else if(code == 27 && settings.stopOnEsc)
bootstro.stop();
})
};
bootstro.unbind = function()
{
$("html").unbind('click.bootstro');
$(document).unbind('keydown.bootstro');
}
}( window.bootstro = window.bootstro || {}, jQuery ));
});