-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.timeline.js
380 lines (340 loc) · 11 KB
/
jquery.timeline.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
/*
* Jquery timeline : jQuery plugin allowing you to transform a list of elements into a two columns timeline
* Copyright 2013 Loïc B. Florin (@bf_loic)
* Licensed under the MIT license.
* Version 1.0
*/
(function($) {
/*
* throttledresize: special jQuery event that happens at a reduced rate compared to "resize"
*
* latest version and complete README available on Github:
* https://github.com/louisremi/jquery-smartresize
*
* Copyright 2012 @louis_remi
* Licensed under the MIT license.
*/
var $event = $.event,
$special,
dummy = {_:0},
frame = 0,
wasResized, animRunning;
$special = $event.special.throttledresize = {
setup: function() {
$( this ).on( "resize", $special.handler );
},
teardown: function() {
$( this ).off( "resize", $special.handler );
},
handler: function( event, execAsap ) {
// Save the context
var context = this,
args = arguments;
wasResized = true;
if ( !animRunning ) {
setInterval(function(){
frame++;
if ( frame > $special.threshold && wasResized || execAsap ) {
// set correct event type
event.type = "throttledresize";
$event.dispatch.apply( context, args );
wasResized = false;
frame = 0;
}
if ( frame > 9 ) {
$(dummy).stop();
animRunning = false;
frame = 0;
}
}, 30);
animRunning = true;
}
},
threshold: 0
};
/*
* Now, jQuery.timeline
*/
$.timeline = function(element, options) {
var defaults = {
'item': '.block',
'corner': false,
'wide': false,
'line': false,
'arrows': false,
'deletebtn': false,
'column': 2,
'first': 'right',
'animation': false,
'duration': 2000,
'onComplete': false,
'onRemoveItem': false
}
var plugin = this;
plugin.settings = {}
var $element = $(element),
element = element;
/**
* Initialize
* First initalization with callback
*/
plugin.initialize = function() {
plugin.settings = $.extend({}, defaults, options);
if(typeof(plugin.settings.onComplete) == 'function') plugin.settings.onComplete($element);
if(plugin.settings.animation) {
if($.inArray(plugin.settings.animation, ['fade', 'slide']) == -1) {
plugin.settings.animation = 'show';
plugin.settings.duration = 1;
} else plugin.settings.animation+='Toggle';
} else {
plugin.settings.animation = 'show';
plugin.settings.duration = 1;
}
plugin.init();
}
/**
* Init
* Initialize plugin and apply styling
*/
plugin.init = function() {
// Listener on window resize
$(window).on('throttledresize', function() {
plugin.position($(this));
});
$element.addClass('timeline-container');
$element.find(plugin.settings.item).each(function() {
$(this).wrap('<div class="timeline-item" />');
});
if(plugin.settings.line != false) {
$element.prepend('<div class="timeline-central-line" />');
}
if(plugin.settings.wide != false) {
$element.find(plugin.settings.wide).parent().addClass('timeline-wide-item');
}
if(plugin.settings.corner != false) {
$corner = $element.find(plugin.settings.corner);
$element.find(plugin.settings.item).each(function() {
var index = $element.find(plugin.settings.item).index(this);
$(this).attr('data-timeline-initial-position', index);
});
$element.find(plugin.settings.corner).unwrap().detach();
if(plugin.settings.first == 'left') {
$($element.children('.timeline-item').get(0)).after($corner);
}
else $element.prepend($corner);
$corner.wrapAll('<div class="timeline-item timeline-corner-item" />');
}
if(plugin.settings.arrows) {
$element.find('.timeline-item:not(.timeline-wide-item)').find(plugin.settings.item).each(function() {
$(this).prepend('<span class="timeline-item-arrow" />');
});
}
if(plugin.settings.deletebtn) {
$element.find('.timeline-item').find(plugin.settings.item).each(function() {
$(this).prepend('<span class="timeline-item-close '+plugin.settings.deletebtn+'" />');
});
$('.timeline-item-close').on('click', function() {
if(typeof(plugin.settings.onRemoveItem) === 'function')
plugin.removeItem({element:$(this), callback:plugin.settings.onRemoveItem});
else plugin.removeItem($(this));
});
}
plugin.position();
}
/**
* Reload
* Reapply styling for the current set of elements
*/
plugin.reload = function() {
plugin.remove();
plugin.init();
}
/**
* Position
* Place elements in the adequat column
*/
plugin.position = function() {
var left = 0,
right = 0;
if(typeof(plugin.settings.column) == 'function') {
column = plugin.settings.column(element);
} else column = plugin.settings.column;
if(column == 1) $element.addClass('timeline-wide-format');
else $element.removeClass('timeline-wide-format');
$element.find('.timeline-item').each(function() {
if($(this).hasClass('timeline-wide-item') || (column == 1)) {
$(this).removeClass('timeline-position-left timeline-position-right');
left += $(this).height();
right = left;
}
else {
if(left > right || $(this).hasClass('timeline-corner-item')) {
$(this).removeClass('timeline-position-left timeline-position-right').addClass('timeline-position-right');
right += $(this).height();
}
else {
$(this).removeClass('timeline-position-left timeline-position-right').addClass('timeline-position-left');
left += $(this).height();
}
}
});
}
/**
* Prepend
* Prepend the given new element and throw a callback
*/
plugin.prepend = function(args) {
if(typeof(args) == 'undefined') {
window.console.error('cannot prepend undefined element');
return 0;
}
// ! Must allow anonymous arguments
if(typeof(args.element) != 'undefined') {
var callback = args.callback;
args = args.element;
}
plugin.remove();
var newitem = $(args).prependTo($element).hide()[plugin.settings.animation](plugin.settings.duration, function() {
if(plugin.settings.duration) plugin.reload();
if(callback) callback(newitem);
});
plugin.init();
}
/**
* Append
* Append the given new element and throw a callback
*/
plugin.append = function(args) {
if(typeof(args) == 'undefined') {
window.console.error('cannot append undefined element');
return 0;
}
// ! Must allows anonymous arguments
if(typeof(args.element) != 'undefined') {
var callback = args.callback;
args = args.element;
}
plugin.remove();
var newitem = $(args).appendTo($element).hide()[plugin.settings.animation](plugin.settings.duration, function() {
if(plugin.settings.duration) plugin.reload();
if(callback) callback(newitem);
});
plugin.init();
}
/**
* RemoveItem
* Remove the given new element and throw a callback
*/
plugin.removeItem = function(args) {
if(typeof(args) == 'undefined') {
window.console.error('cannot remove undefined element');
return 0;
}
// ! Must allow anonymous arguments
if(typeof(args.element) != 'undefined') {
var callback = args.callback;
var elm = args.element;
} else var elm = args;
var item = $(elm).is('.timeline-item')?$(elm):$(elm).closest('.timeline-item');
if(item.is('.timeline-corner-item')) elm = $(elm).closest(plugin.settings.item);
else elm = item;
// Copy of element for callback return
var clbElm = elm.is(plugin.settings.item)?elm:elm.find(plugin.settings.item);
var removed = $element.find(elm).index();
$element.find(plugin.settings.corner).each(function() {
var index = $(this).attr('data-timeline-initial-position');
if(index > removed) $(this).attr('data-timeline-initial-position', parseInt(index)-1);
});
elm[plugin.settings.animation](plugin.settings.duration, function() {
elm.remove();
if(plugin.settings.duration) plugin.reload();
if(callback) callback(clbElm);
});
}
/**
* Destroy
* Remove all listeners and styles applied then destroy the instance of timeline
*/
plugin.destroy = function(args) {
plugin.remove();
if(typeof(args) == 'function') args($element);
$element.removeData('timeline');
}
/**
* Remove
* Remove all listeners and styles applied but keep the options set
*/
plugin.remove = function() {
// Reposition corner items to their origin
if(plugin.settings.corner != false) {
var $corners = $element.find(plugin.settings.corner);
$element.find('.timeline-corner-item '+plugin.settings.corner).detach();
$element.find('.timeline-corner-item').remove();
$element.find('.timeline-item '+plugin.settings.item).unwrap();
var last = $element.find(plugin.settings.item+':last').index();
$corners.each(function() {
var index = $(this).attr('data-timeline-initial-position');
if(index == 0) $(this).insertBefore($element.find(plugin.settings.item).get(parseInt(index))).removeAttr('data-timeline-initial-position');
else $(this).insertAfter($element.find(plugin.settings.item).get(parseInt(index)-1)).removeAttr('data-timeline-initial-position');
});
} else $element.find('.timeline-item '+plugin.settings.item).unwrap();
// Remove extra options
if(plugin.settings.line != false) {
$element.find('.timeline-central-line').remove();
}
if(plugin.settings.arrows) {
$element.find(plugin.settings.item).each(function() {
$element.find(plugin.settings.item+' .timeline-item-arrow').remove();
});
}
if(plugin.settings.deletebtn) {
$element.find(plugin.settings.item).each(function() {
$element.find(plugin.settings.item+' .timeline-item-close').remove();
});
}
$element.removeClass('timeline-container');
// Disable listener on resize
$(window).off('throttledresize');
}
jQuery.expr[':'].parents = function(a,i,m){
return jQuery(a).parents(m[3]).length < 1;
};
plugin.initialize();
}
$.fn.timeline = function(options) {
if ( typeof options === 'string' ) {
// call method
var args = Array.prototype.slice.call( arguments, 1 );
this.each(function() {
var instance = $.data( this, 'timeline' );
if ( !instance ) {
window.console.error( "cannot call methods on timeline prior to initialization; " +
"attempted to call method '" + options + "'" );
return;
}
if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
window.console.error( "no such method '" + options + "' for timeline instance" );
return;
}
// apply method
instance[ options ].apply( instance, args );
});
}
else {
return this.each(function() {
// New instance
if (undefined == $(this).data('timeline')) {
var plugin = new $.timeline(this, options);
$(this).data('timeline', plugin);
}
// Replace instance
else {
$(this).data('timeline').destroy();
var plugin = new $.timeline(this, options);
$(this).data('timeline', plugin);
//return;
}
});
}
}
})(jQuery);