/
timeline.js
167 lines (138 loc) · 6.27 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
if ("undefined" == typeof console) var console = { log : function(what) {}, info : function(what) {}, error : function(what) {} }
function l() { console.log(arguments) }
var iframe_loaded = function() {
$('#dialog #loading').fadeOut();
$('#dialog iframe').show();
};
function WikipediaHistoryTimeline(data) {
var $this = this;
var history_page = 'http://en.wikipedia.org/w/index.php?title=Metal_umlaut&diff=prev&oldid=';
initialize(data);
function initialize(data) {
$this.data = data;
$('#dialog').dialog({ autoOpen: false,
title: 'Wikipedia',
width: $(document).width()*0.8,
height: $(document).height()*0.8,
close: function(event, ui) { $('#dialog iframe').hide() } })
draw();
return this;
};
// Adapted from http://raphaeljs.com/analytics.html
function draw() {
// Setup drawing
var marker_width = 15,
marker_height = 3,
marker_stroke = 1;
var width = $this.data.length*(marker_width+marker_stroke)+50,
height = Math.max.apply( Math, $.map($this.data, function(n,i) {return n.revisions.length*6})),
leftgutter = 30,
bottomgutter = 100,
topgutter = 20,
r = Raphael("timeline", width, height),
txt_white = {font: '11px Arial', fill: "#ccc"},
txt_normal = {font: '11px Arial', fill: "#000"},
txt_small = {font: '9px Arial', fill: "#666"};
var frame = r.rect(10, 10, 120, 50, 5).attr({fill: "#000", stroke: "none"}).hide(),
leave_timer,
label = [],
is_label_visible = false,
blanket = r.set();
var total_revision_count = 0;
label[0] = r.text(10, 12, "--").attr(txt_white).hide();
label[1] = r.text(10, 22, "--").attr(txt_small).attr({fill:"#999"}).hide();
label[2] = r.text(10, 32, "--").attr(txt_small).hide();
// Draw year and month marks
var draw_year = function(year, x) {
var y = height - bottomgutter + 50;
var text = r.text(x+40, y, year).attr({"fill":"#999", "font-size":32}).toBack();
r.path(["M", x, y+10, "L", x, y-text.getBBox().height-10]).attr({"stroke":"#999"});
};
var draw_month = function(month_index, x) {
var month_names = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
var y = height - bottomgutter + 10;
var content = month_names[month_index];
r.text(x-leftgutter+8, y+4, content).attr({"fill":"#999", "font-size":8}).rotate(90).toBack();
};
// Draw line at graph base
r.path(["M", leftgutter, height - bottomgutter, "L", height - bottomgutter + width, height - bottomgutter]).attr({stroke: '#ccc'});
// Draw first year
var current_year = new Date($this.data[0].date).getFullYear();
draw_year(current_year, leftgutter);
// Iterate over months
for (var i = 0, ii = $this.data.length; i < ii; i++) {
var x = leftgutter + (marker_width * i);
// Draw year mark
var year = new Date($this.data[i].date).getFullYear();
if (year > current_year) {
draw_year(year, x);
current_year = year;
}
// Draw month mark
var month_index = new Date($this.data[i].date).getMonth();
draw_month(month_index, x+(2*marker_width));
var ratio = 0;
var move_by = 0;
// Iterate over revisions in month
$.each( data[i].revisions, function(rev_index, rev) {
total_revision_count += 1;
var y = Math.round(height - bottomgutter - ((marker_height)*rev_index) - move_by);
// Colorize dot according to revision impact
if (rev.impact > 1) { var marker_color = '#29cc62'; } // Green : more lines added
else if (rev.impact < -1) { var marker_color = '#cc144f'; } // Red : more lines deleted
else { var marker_color = '#0091e5'; } // Blue : neutral impact
// Colorize bots as grey
if (rev.bot) var marker_color = '#686f74';
// Draw revision marker
var marker = r.rect(x, y, marker_width, marker_height).attr({fill: marker_color, stroke: "#fff", "stroke-width": marker_stroke});
// Scale revision marker according to it's impact
if ( rev.impact && (rev.impact < 0 || rev.impact > 0) ) {
var impact = Math.abs(rev.impact);
if (impact > 2*marker_height) impact = 2*marker_height; // Top limit for scaling the marker
marker.attr({height: Math.round(marker.getBBox().height + impact)});
marker.translate( 0, -impact );
move_by += impact;
};
// Add marker handlers
$(marker.node)
.hover(
function () {
var newcoord = { x: marker.getBBox().x, y: marker.getBBox().y+(marker.getBBox().height) };
frame.attr({x: newcoord.x, y: newcoord.y}).show();
var xpos_adj = frame.getBBox().width/2;
label[0].attr({text: rev.author, x: +newcoord.x+xpos_adj, y: +newcoord.y+12}).show();
label[1].attr({text: rev.subject.substr(0, 20), x: +newcoord.x+xpos_adj, y: +newcoord.y+24}).show();
label[2].attr({text: rev.date, x: +newcoord.x+xpos_adj, y: +newcoord.y+36}).show();
marker.attr({fill: '#000'});
is_label_visible = true;
},
function () {
marker.attr({fill: marker_color});
frame.hide();
label[0].hide();
label[1].hide();
label[2].hide();
is_label_visible = false;
}
)
.click(
function() {
$("#dialog #loading").show();
$("#dialog iframe").attr("src", "");
$("#dialog iframe").attr("src", history_page + rev.tag);
$("#dialog").dialog('open', { title: "Wikipedia, revision number " + rev.tag });
}
)
}); // end revision
frame.toFront();
label[0].toFront();
label[1].toFront();
label[2].toFront();
blanket.toFront();
}; // end month
// Display total number of displayed revisions
r.text(leftgutter+55, height-bottomgutter+90, 'Displaying ' + total_revision_count + ' revisions').attr({fill: "#b2b2b2"}).toBack();
};
};
// OnLoad callback
jQuery( function() { window.app = new WikipediaHistoryTimeline(data) } );