-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline.js
213 lines (174 loc) · 7.73 KB
/
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
if (!jQuery) { throw("Timeline requires jQuery"); }
(function($){
var Timeline = function(args) {
this.init(args);
return this;
};
Timeline.prototype = {
/*
* data: an array of two-element arrays
* the first ones are Date objects, the second ones are strings of html content
*
* eg: [ [t1, html1], [t2, html2] ]
*
* If absent, it is assumed that the required html structure is already in the document.
*/
init: function(args) {
if ( !args.el ) throw("Cannot initialize a Timeline without an element (the 'el' argument)");
this.el = args.el;
if (args.scrollers) {
if (args.scrollers[0] && args.scrollers[1]) {
if ( $(args.scrollers[0]).size() != 0 && $(args.scrollers[1]).size() ) {
this.scrollers = args.scrollers;
}
else {
throw("One (or both) of the scroller elements is missing.");
}
}
else {
throw("Scrollers should be given as an array of two elements, they can be either css selector strings, or the element objects.");
}
}
if ( args.data ) {
this.data = args.data;
this.build();
}
this.setup_style();
this.bind_mouse_events();
},
// Construct any necessary html elements for this timeline
build: function() {
var self = this;
this.el.innerHTML =
'<div class="timeline-wrapper clearfix"><div class="timeline-band"></div></div>' +
'<div class="timeline-year-index-wrapper clearfix"><div class="timeline-year-index clearfix"></div></div>' +
'<div class="timeline-scrollbar clearfix"></div>';
var years = [];
$.each(this.data, function() {
var y = this[0].getFullYear();
years.push(y);
$(".timeline-band", self.el).append('<div class="timeline-content">' + this[1] + '</div>');
$(".timeline-year-index", self.el).append('<a class="timeline-year" href="javascript:void(0);">' + y + '</a>');
});
},
setup_style: function() {
var self = this;
var band_width = 0;
$(".timeline-band .timeline-content", self.el).each(function() {
band_width += $(this).outerWidth(true);
});
$(".timeline-band", self.el).width( band_width );
var year_index_width = 0;
$(".timeline-year-index .timeline-year", self.el).each(function() {
year_index_width += $(this).outerWidth(true);
});
$(".timeline-year-index", self.el).width( year_index_width );
},
bind_mouse_events: function() {
this.bind_mouse_events_of_scrollers()
this.bind_mouse_events_of_index();
},
bind_mouse_events_of_scrollers: function() {
if (!this.scrollers) return;
var self = this;
var $bands = $(".timeline-band, .timeline-year-index", self.el);
var timeline_wrapper_width = $(".timeline-wrapper", self.el).width();
var step = timeline_wrapper_width / 3 * 2;
var scrolling;
$(this.scrollers[0]).bind("click",
function() {
$bands.each(function() {
var l = parseFloat( $(this).css("margin-left") );
l += step;
if (l > 0) l = 0;
$(this).animate({"marginLeft": l});
});
return false;
}
);
$(this.scrollers[1]).bind("click",
function() {
$bands.each(function() {
var min = -1 * ($(this).outerWidth() - timeline_wrapper_width);
var l = parseFloat( $(this).css("margin-left") );
l -= step;
if (l < min) l = min;
$(this).animate({"marginLeft": l});
});
return false;
}
);
},
bind_mouse_events_of_index: function() {
var self = this;
var year_width = $(".timeline-band .timeline-content", self.el).outerWidth();
var year_divs = $(".timeline-year-index .timeline-year", self.el).size();
var year_divs_in_viewport = $(self.el).outerWidth()/year_width;
var middle_ordinal = parseInt( year_divs_in_viewport/2 ) + 1;
$(".timeline-year-index .timeline-year", this.el).bind(
"click",
function() {
var i = $(".timeline-year-index .timeline-year", self.el).index(this);
var l;
if (i < middle_ordinal) {
l = 0;
}
else if (i > year_divs - parseInt(year_divs_in_viewport)) {
l = -1 * year_width * (year_divs - year_divs_in_viewport);
}
else {
l = -1 * year_width * (i - middle_ordinal + 1);
}
$(".timeline-band", self.el).animate({"marginLeft": l});
return false;
}
);
var $scrollbar = $(".timeline-scrollbar", self.el);
var scrollbar_width = $scrollbar.outerWidth();
var scrollbar_offset = $scrollbar.offset();
var timeline_band_width = $(".timeline-band", self.el).outerWidth();
var year_index_width = $(".timeline-year-index", self.el).outerWidth();
var timeline_wrapper_width = $(".timeline-wrapper", self.el).width();
var threshold_left = 100;
var threshold_right = 100;
var timeline_wrapper_scrollbar_width_diff = scrollbar_width - timeline_wrapper_width;
var timeline_year_index_wrapper_scrollbar_width_diff = scrollbar_width - $(".timeline-year-index-wrapper", self.el).width();
scrollbar_width -= (threshold_left + threshold_right);
var marginLeft = function(x, w, el) {
if (x < threshold_left) {
return 0;
}
x-= threshold_left;
if (x > scrollbar_width) {
x = scrollbar_width;
}
var x_percentage = x / scrollbar_width;
var l = -1 * x_percentage * (w - scrollbar_width - threshold_left - threshold_right);
if (el == "band") {
l -= timeline_wrapper_scrollbar_width_diff;
}
else if (el == "year-index") {
l -= timeline_year_index_wrapper_scrollbar_width_diff;
}
return l;
};
$(".timeline-year-index", self.el).bind("mousemove", function(e) {
var x = e.pageX - scrollbar_offset.left;
$(".timeline-band", self.el).css({
"marginLeft": marginLeft(x, timeline_band_width, "band")
});
$(".timeline-year-index", self.el).css({
"marginLeft": marginLeft(x, year_index_width, "year-index")
});
});
}
};
$.fn.timeline = function (args) {
this.each(function() {
var args_ext = $.extend({}, args);
args_ext.el = this;
var tl = new Timeline(args_ext);
$(this).data("Timeline", tl);
});
};
})(jQuery);