jquery.crumble.js
5.53 KB
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
/*
* jQuery.crumble - A tour extension for grumble
* Tom Moor, http://tommoor.com
* Copyright (c) 2012 Tom Moor
* MIT Licensed
* @version 0.3
*/
(function($){
var Crumble = function(){
// locals
var position = 0;
var setup = [];
var $setup;
var defaults = {
grumble: {
text: '',
angle: 85,
distance: 30,
showAfter: 0,
hasHideButton: false
},
scrollSpeed: 'fast',
onStep: function(){},
onStart: function(){},
onFinish: function(){}
};
var scrollToGrumble = function($grumble) {
var top = $grumble.position().top;
var bottom = $grumble.height() + top;
var windowHeight = $(window).innerHeight();
var windowTop = $(window).scrollTop();
var windowBottom = windowHeight + windowTop;
var pad = 100;
if (top < windowTop) {
$('html, body').animate({scrollTop: top - pad}, defaults.scrollSpeed);
} else if (bottom > windowBottom) {
$('html, body').animate({scrollTop: bottom - windowHeight + pad}, defaults.scrollSpeed);
}
};
var getCurrentGrumble = function() {
return $('.grumble').last().next();
};
var calculateAngle = function($element) {
var windowTop = $(window).scrollTop();
var centerX = window.innerWidth/2;
var centerY = window.innerHeight/2;
var elementX = $element.position().left;
var elementY = $element.position().top+windowTop;
var o = elementY-centerY;
var a = elementX-centerX;
var angle = Math.atan2(o, a) * (180/Math.PI);
if (angle < 0) return angle-90;
return angle+90;
};
var bindKeys = function() {
// Pressing the escape key will stop the tour
$(document).bind('keyup.crumble',function(ev){
if(ev.keyCode === 27){
ev.stopImmediatePropagation();
methods.clear();
$(document).unbind('keyup.crumble');
}
});
};
var methods = {
init: function(o){
// allow user to pass in an object and overwrite default args
defaults = $.extend(defaults, o);
// parse setup
$('li', this).each(function(index, stop){
var $stop = $(stop);
var opt = $stop.data('options');
var options = {};
// contents of node becomes the grumble text
options.text = $stop.html();
// optionally angle is in data-angle
options.angle = $stop.data('angle');
// other options contained in data-options
if (opt) {
var j;
var data = opt.split(';');
for (var i in data) {
j = data[i].split(':');
if (j.length === 2) {
options[j[0]] = j[1];
} else {
// invalid format
continue;
}
}
}
// push this step into the current setup
setup.push({
target: $stop.data('target'),
options: options
});
});
methods.clear();
methods.step();
bindKeys();
return this;
},
go: function(pos) {
// bound the position by the current setup
position = Math.max(0, Math.min(pos, setup.length-1));
// show this step
this.step();
},
forward: function() {
// move forward through the tour
position++;
// show the next step
methods.step();
// trigger any bound events
defaults.onStep(position, setup[position]);
},
step: function() {
// if we're at the start, trigger an event
if (position === 0) {
defaults.onStart();
}
// if we've reached the end, trigger an event
// and reset back to the beginning
if (position >= setup.length) {
methods.clear();
defaults.onFinish();
return;
}
var current = setup[position];
var $current = $(current.target).first();
// check we have a target for this step
if (!current.target) {
$.error('Crumble step does not include data-target property');
return;
}
// check if target exists, if not jump forward
if (!$current.length) {
methods.forward();
return;
}
// everything looks good, continue through the tour
var options = $.extend({}, defaults.grumble, current.options, {
angle: current.options.angle || calculateAngle($current),
onHide: function(){
methods.forward();
},
onShow: function() {
var $grumble = getCurrentGrumble();
scrollToGrumble($grumble);
$grumble.data('clicked', false);
$grumble.click(function(ev){
ev.stopImmediatePropagation();
if (!$grumble.data('clicked')) {
$grumble.data('clicked', true);
$current.trigger('hide.bubble');
}
});
$grumble.hover(function(){
$grumble.prev().addClass('hover');
}, function(){
$grumble.prev().removeClass('hover');
});
}
});
// ensure distance is a number, otherwise this breaks
options.distance = parseInt(options.distance, 10);
$current.grumble(options);
},
clear: function() {
// reset counter back to the beginning
position = 0;
// clear up all created DOM elements
$('.grumble, .grumble-text, .grumble-button').remove();
}
};
return methods;
};
$.fn.crumble = function(method) {
// so that arguments are accessible within each closure
var args = arguments;
return this.each(function(){
var state = $(this).data('Crumble');
// Method calling logic
if (state && state[method] ) {
state[ method ].apply( this, Array.prototype.slice.call( args, 1 ));
} else if ( typeof method === 'object' || ! method ) {
// create new tinyrange
var tr = (new Crumble(this));
tr.init.apply( this, args );
// save state in jquery data
$(this).data('Crumble', tr);
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.crumble' );
}
});
};
})(jQuery);