annotate templates/static/mercurial.js @ 0:dfc36e7ed22c

init
author Vadim Filimonov <fffilimonov@yandex.ru>
date Thu, 12 May 2022 13:51:59 +0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
1 // mercurial.js - JavaScript utility functions
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
2 //
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
3 // Rendering of branch DAGs on the client side
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
4 // Display of elapsed time
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
5 // Show or hide diffstat
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
6 //
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
7 // Copyright 2008 Dirkjan Ochtman <dirkjan AT ochtman DOT nl>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
8 // Copyright 2006 Alexander Schremmer <alex AT alexanderweb DOT de>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
9 //
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
10 // derived from code written by Scott James Remnant <scott@ubuntu.com>
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
11 // Copyright 2005 Canonical Ltd.
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
12 //
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
13 // This software may be used and distributed according to the terms
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
14 // of the GNU General Public License, incorporated herein by reference.
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
15
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
16 var colors = [
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
17 [ 1.0, 0.0, 0.0 ],
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
18 [ 1.0, 1.0, 0.0 ],
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
19 [ 0.0, 1.0, 0.0 ],
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
20 [ 0.0, 1.0, 1.0 ],
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
21 [ 0.0, 0.0, 1.0 ],
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
22 [ 1.0, 0.0, 1.0 ]
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
23 ];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
24
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
25 function Graph() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
26
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
27 this.canvas = document.getElementById('graph');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
28 this.ctx = this.canvas.getContext('2d');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
29 this.ctx.strokeStyle = 'rgb(0, 0, 0)';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
30 this.ctx.fillStyle = 'rgb(0, 0, 0)';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
31 this.bg = [0, 4];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
32 this.cell = [2, 0];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
33 this.columns = 0;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
34
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
35 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
36
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
37 Graph.prototype = {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
38 reset: function() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
39 this.bg = [0, 4];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
40 this.cell = [2, 0];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
41 this.columns = 0;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
42 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
43
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
44 scale: function(height) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
45 this.bg_height = height;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
46 this.box_size = Math.floor(this.bg_height / 1.2);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
47 this.cell_height = this.box_size;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
48 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
49
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
50 setColor: function(color, bg, fg) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
51
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
52 // Set the colour.
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
53 //
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
54 // If color is a string, expect an hexadecimal RGB
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
55 // value and apply it unchanged. If color is a number,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
56 // pick a distinct colour based on an internal wheel;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
57 // the bg parameter provides the value that should be
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
58 // assigned to the 'zero' colours and the fg parameter
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
59 // provides the multiplier that should be applied to
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
60 // the foreground colours.
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
61 var s;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
62 if(typeof color === "string") {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
63 s = "#" + color;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
64 } else { //typeof color === "number"
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
65 color %= colors.length;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
66 var red = (colors[color][0] * fg) || bg;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
67 var green = (colors[color][1] * fg) || bg;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
68 var blue = (colors[color][2] * fg) || bg;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
69 red = Math.round(red * 255);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
70 green = Math.round(green * 255);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
71 blue = Math.round(blue * 255);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
72 s = 'rgb(' + red + ', ' + green + ', ' + blue + ')';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
73 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
74 this.ctx.strokeStyle = s;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
75 this.ctx.fillStyle = s;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
76 return s;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
77
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
78 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
79
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
80 edge: function(x0, y0, x1, y1, color, width) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
81
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
82 this.setColor(color, 0.0, 0.65);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
83 if(width >= 0)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
84 this.ctx.lineWidth = width;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
85 this.ctx.beginPath();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
86 this.ctx.moveTo(x0, y0);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
87 this.ctx.lineTo(x1, y1);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
88 this.ctx.stroke();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
89
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
90 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
91
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
92 graphNodeCurrent: function(x, y, radius) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
93 this.ctx.lineWidth = 2;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
94 this.ctx.beginPath();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
95 this.ctx.arc(x, y, radius * 1.75, 0, Math.PI * 2, true);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
96 this.ctx.stroke();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
97 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
98
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
99 graphNodeClosing: function(x, y, radius) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
100 this.ctx.fillRect(x - radius, y - 1.5, radius * 2, 3);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
101 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
102
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
103 graphNodeUnstable: function(x, y, radius) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
104 var x30 = radius * Math.cos(Math.PI / 6);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
105 var y30 = radius * Math.sin(Math.PI / 6);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
106 this.ctx.lineWidth = 2;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
107 this.ctx.beginPath();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
108 this.ctx.moveTo(x, y - radius);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
109 this.ctx.lineTo(x, y + radius);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
110 this.ctx.moveTo(x - x30, y - y30);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
111 this.ctx.lineTo(x + x30, y + y30);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
112 this.ctx.moveTo(x - x30, y + y30);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
113 this.ctx.lineTo(x + x30, y - y30);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
114 this.ctx.stroke();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
115 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
116
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
117 graphNodeObsolete: function(x, y, radius) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
118 var p45 = radius * Math.cos(Math.PI / 4);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
119 this.ctx.lineWidth = 3;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
120 this.ctx.beginPath();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
121 this.ctx.moveTo(x - p45, y - p45);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
122 this.ctx.lineTo(x + p45, y + p45);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
123 this.ctx.moveTo(x - p45, y + p45);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
124 this.ctx.lineTo(x + p45, y - p45);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
125 this.ctx.stroke();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
126 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
127
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
128 graphNodeNormal: function(x, y, radius) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
129 this.ctx.beginPath();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
130 this.ctx.arc(x, y, radius, 0, Math.PI * 2, true);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
131 this.ctx.fill();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
132 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
133
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
134 vertex: function(x, y, radius, color, parity, cur) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
135 this.ctx.save();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
136 this.setColor(color, 0.25, 0.75);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
137 if (cur.graphnode[0] === '@') {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
138 this.graphNodeCurrent(x, y, radius);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
139 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
140 switch (cur.graphnode.substr(-1)) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
141 case '_':
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
142 this.graphNodeClosing(x, y, radius);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
143 break;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
144 case '*':
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
145 this.graphNodeUnstable(x, y, radius);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
146 break;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
147 case 'x':
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
148 this.graphNodeObsolete(x, y, radius);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
149 break;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
150 default:
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
151 this.graphNodeNormal(x, y, radius);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
152 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
153 this.ctx.restore();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
154
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
155 var left = (this.bg_height - this.box_size) + (this.columns + 1) * this.box_size;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
156 var item = document.querySelector('[data-node="' + cur.node + '"]');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
157 if (item) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
158 item.style.paddingLeft = left + 'px';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
159 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
160 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
161
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
162 render: function(data) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
163
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
164 var i, j, cur, line, start, end, color, x, y, x0, y0, x1, y1, column, radius;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
165
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
166 var cols = 0;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
167 for (i = 0; i < data.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
168 cur = data[i];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
169 for (j = 0; j < cur.edges.length; j++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
170 line = cur.edges[j];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
171 cols = Math.max(cols, line[0], line[1]);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
172 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
173 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
174 this.canvas.width = (cols + 1) * this.bg_height;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
175 this.canvas.height = (data.length + 1) * this.bg_height - 27;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
176
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
177 for (i = 0; i < data.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
178
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
179 var parity = i % 2;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
180 this.cell[1] += this.bg_height;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
181 this.bg[1] += this.bg_height;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
182
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
183 cur = data[i];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
184 var fold = false;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
185
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
186 var prevWidth = this.ctx.lineWidth;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
187 for (j = 0; j < cur.edges.length; j++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
188
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
189 line = cur.edges[j];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
190 start = line[0];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
191 end = line[1];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
192 color = line[2];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
193 var width = line[3];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
194 if(width < 0)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
195 width = prevWidth;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
196 var branchcolor = line[4];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
197 if(branchcolor)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
198 color = branchcolor;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
199
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
200 if (end > this.columns || start > this.columns) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
201 this.columns += 1;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
202 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
203
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
204 if (start === this.columns && start > end) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
205 fold = true;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
206 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
207
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
208 x0 = this.cell[0] + this.box_size * start + this.box_size / 2;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
209 y0 = this.bg[1] - this.bg_height / 2;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
210 x1 = this.cell[0] + this.box_size * end + this.box_size / 2;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
211 y1 = this.bg[1] + this.bg_height / 2;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
212
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
213 this.edge(x0, y0, x1, y1, color, width);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
214
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
215 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
216 this.ctx.lineWidth = prevWidth;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
217
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
218 // Draw the revision node in the right column
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
219
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
220 column = cur.vertex[0];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
221 color = cur.vertex[1];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
222
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
223 radius = this.box_size / 8;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
224 x = this.cell[0] + this.box_size * column + this.box_size / 2;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
225 y = this.bg[1] - this.bg_height / 2;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
226 this.vertex(x, y, radius, color, parity, cur);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
227
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
228 if (fold) this.columns -= 1;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
229
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
230 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
231
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
232 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
233
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
234 };
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
235
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
236
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
237 function process_dates(parentSelector){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
238
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
239 // derived from code from mercurial/templatefilter.py
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
240
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
241 var scales = {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
242 'year': 365 * 24 * 60 * 60,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
243 'month': 30 * 24 * 60 * 60,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
244 'week': 7 * 24 * 60 * 60,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
245 'day': 24 * 60 * 60,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
246 'hour': 60 * 60,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
247 'minute': 60,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
248 'second': 1
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
249 };
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
250
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
251 function format(count, string){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
252 var ret = count + ' ' + string;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
253 if (count > 1){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
254 ret = ret + 's';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
255 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
256 return ret;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
257 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
258
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
259 function shortdate(date){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
260 var ret = date.getFullYear() + '-';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
261 // getMonth() gives a 0-11 result
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
262 var month = date.getMonth() + 1;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
263 if (month <= 9){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
264 ret += '0' + month;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
265 } else {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
266 ret += month;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
267 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
268 ret += '-';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
269 var day = date.getDate();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
270 if (day <= 9){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
271 ret += '0' + day;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
272 } else {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
273 ret += day;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
274 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
275 return ret;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
276 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
277
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
278 function age(datestr){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
279 var now = new Date();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
280 var once = new Date(datestr);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
281 if (isNaN(once.getTime())){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
282 // parsing error
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
283 return datestr;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
284 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
285
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
286 var delta = Math.floor((now.getTime() - once.getTime()) / 1000);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
287
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
288 var future = false;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
289 if (delta < 0){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
290 future = true;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
291 delta = -delta;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
292 if (delta > (30 * scales.year)){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
293 return "in the distant future";
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
294 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
295 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
296
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
297 if (delta > (2 * scales.year)){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
298 return shortdate(once);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
299 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
300
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
301 for (var unit in scales){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
302 if (!scales.hasOwnProperty(unit)) { continue; }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
303 var s = scales[unit];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
304 var n = Math.floor(delta / s);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
305 if ((n >= 2) || (s === 1)){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
306 if (future){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
307 return format(n, unit) + ' from now';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
308 } else {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
309 return format(n, unit) + ' ago';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
310 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
311 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
312 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
313 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
314
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
315 var nodes = document.querySelectorAll((parentSelector || '') + ' .age');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
316 var dateclass = new RegExp('\\bdate\\b');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
317 for (var i=0; i<nodes.length; ++i){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
318 var node = nodes[i];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
319 var classes = node.className;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
320 var agevalue = age(node.textContent);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
321 if (dateclass.test(classes)){
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
322 // We want both: date + (age)
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
323 node.textContent += ' ('+agevalue+')';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
324 } else {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
325 node.title = node.textContent;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
326 node.textContent = agevalue;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
327 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
328 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
329 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
330
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
331 function toggleDiffstat(event) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
332 var curdetails = document.getElementById('diffstatdetails').style.display;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
333 var curexpand = curdetails === 'none' ? 'inline' : 'none';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
334 document.getElementById('diffstatdetails').style.display = curexpand;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
335 document.getElementById('diffstatexpand').style.display = curdetails;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
336 event.preventDefault();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
337 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
338
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
339 function toggleLinewrap(event) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
340 function getLinewrap() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
341 var nodes = document.getElementsByClassName('sourcelines');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
342 // if there are no such nodes, error is thrown here
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
343 return nodes[0].classList.contains('wrap');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
344 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
345
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
346 function setLinewrap(enable) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
347 var nodes = document.getElementsByClassName('sourcelines');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
348 var i;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
349 for (i = 0; i < nodes.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
350 if (enable) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
351 nodes[i].classList.add('wrap');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
352 } else {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
353 nodes[i].classList.remove('wrap');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
354 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
355 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
356
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
357 var links = document.getElementsByClassName('linewraplink');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
358 for (i = 0; i < links.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
359 links[i].innerHTML = enable ? 'on' : 'off';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
360 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
361 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
362
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
363 setLinewrap(!getLinewrap());
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
364 event.preventDefault();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
365 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
366
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
367 function format(str, replacements) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
368 return str.replace(/%(\w+)%/g, function(match, p1) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
369 return String(replacements[p1]);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
370 });
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
371 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
372
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
373 function makeRequest(url, method, onstart, onsuccess, onerror, oncomplete) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
374 var xhr = new XMLHttpRequest();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
375 xhr.onreadystatechange = function() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
376 if (xhr.readyState === 4) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
377 try {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
378 if (xhr.status === 200) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
379 onsuccess(xhr.responseText);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
380 } else {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
381 throw 'server error';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
382 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
383 } catch (e) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
384 onerror(e);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
385 } finally {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
386 oncomplete();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
387 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
388 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
389 };
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
390
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
391 xhr.open(method, url);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
392 xhr.overrideMimeType("text/xhtml; charset=" + document.characterSet.toLowerCase());
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
393 xhr.send();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
394 onstart();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
395 return xhr;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
396 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
397
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
398 function removeByClassName(className) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
399 var nodes = document.getElementsByClassName(className);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
400 while (nodes.length) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
401 nodes[0].parentNode.removeChild(nodes[0]);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
402 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
403 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
404
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
405 function docFromHTML(html) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
406 var doc = document.implementation.createHTMLDocument('');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
407 doc.documentElement.innerHTML = html;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
408 return doc;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
409 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
410
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
411 function appendFormatHTML(element, formatStr, replacements) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
412 element.insertAdjacentHTML('beforeend', format(formatStr, replacements));
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
413 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
414
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
415 function adoptChildren(from, to) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
416 var nodes = from.children;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
417 var curClass = 'c' + Date.now();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
418 while (nodes.length) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
419 var node = nodes[0];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
420 node = document.adoptNode(node);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
421 node.classList.add(curClass);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
422 to.appendChild(node);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
423 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
424 process_dates('.' + curClass);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
425 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
426
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
427 function ajaxScrollInit(urlFormat,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
428 nextPageVar,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
429 nextPageVarGet,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
430 containerSelector,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
431 messageFormat,
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
432 mode) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
433 var updateInitiated = false;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
434 var container = document.querySelector(containerSelector);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
435
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
436 function scrollHandler() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
437 if (updateInitiated) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
438 return;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
439 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
440
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
441 var scrollHeight = document.documentElement.scrollHeight;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
442 var clientHeight = document.documentElement.clientHeight;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
443 var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
444
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
445 if (scrollHeight - (scrollTop + clientHeight) < 50) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
446 updateInitiated = true;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
447 removeByClassName('scroll-loading-error');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
448 container.lastElementChild.classList.add('scroll-separator');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
449
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
450 if (!nextPageVar) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
451 var message = {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
452 'class': 'scroll-loading-info',
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
453 text: 'No more entries'
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
454 };
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
455 appendFormatHTML(container, messageFormat, message);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
456 return;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
457 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
458
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
459 makeRequest(
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
460 format(urlFormat, {next: nextPageVar}),
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
461 'GET',
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
462 function onstart() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
463 var message = {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
464 'class': 'scroll-loading',
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
465 text: 'Loading...'
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
466 };
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
467 appendFormatHTML(container, messageFormat, message);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
468 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
469 function onsuccess(htmlText) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
470 var doc = docFromHTML(htmlText);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
471
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
472 if (mode === 'graph') {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
473 var graph = window.graph;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
474 var dataStr = htmlText.match(/^\s*var data = (.*);$/m)[1];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
475 var data = JSON.parse(dataStr);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
476 graph.reset();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
477 adoptChildren(doc.querySelector('#graphnodes'), container.querySelector('#graphnodes'));
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
478 graph.render(data);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
479 } else {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
480 adoptChildren(doc.querySelector(containerSelector), container);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
481 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
482
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
483 nextPageVar = nextPageVarGet(htmlText);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
484 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
485 function onerror(errorText) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
486 var message = {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
487 'class': 'scroll-loading-error',
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
488 text: 'Error: ' + errorText
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
489 };
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
490 appendFormatHTML(container, messageFormat, message);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
491 },
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
492 function oncomplete() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
493 removeByClassName('scroll-loading');
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
494 updateInitiated = false;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
495 scrollHandler();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
496 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
497 );
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
498 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
499 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
500
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
501 window.addEventListener('scroll', scrollHandler);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
502 window.addEventListener('resize', scrollHandler);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
503 scrollHandler();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
504 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
505
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
506 function renderDiffOptsForm() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
507 // We use URLSearchParams for query string manipulation. Old browsers don't
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
508 // support this API.
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
509 if (!("URLSearchParams" in window)) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
510 return;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
511 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
512
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
513 var form = document.getElementById("diffopts-form");
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
514
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
515 var KEYS = [
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
516 "ignorews",
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
517 "ignorewsamount",
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
518 "ignorewseol",
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
519 "ignoreblanklines",
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
520 ];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
521
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
522 var urlParams = new window.URLSearchParams(window.location.search);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
523
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
524 function updateAndRefresh(e) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
525 var checkbox = e.target;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
526 var name = checkbox.id.substr(0, checkbox.id.indexOf("-"));
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
527 urlParams.set(name, checkbox.checked ? "1" : "0");
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
528 window.location.search = urlParams.toString();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
529 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
530
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
531 var allChecked = form.getAttribute("data-ignorews") === "1";
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
532
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
533 for (var i = 0; i < KEYS.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
534 var key = KEYS[i];
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
535
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
536 var checkbox = document.getElementById(key + "-checkbox");
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
537 if (!checkbox) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
538 continue;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
539 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
540
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
541 var currentValue = form.getAttribute("data-" + key);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
542 checkbox.checked = currentValue !== "0";
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
543
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
544 // ignorews implies ignorewsamount and ignorewseol.
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
545 if (allChecked && (key === "ignorewsamount" || key === "ignorewseol")) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
546 checkbox.checked = true;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
547 checkbox.disabled = true;
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
548 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
549
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
550 checkbox.addEventListener("change", updateAndRefresh, false);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
551 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
552
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
553 form.style.display = 'block';
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
554 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
555
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
556 function addDiffStatToggle() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
557 var els = document.getElementsByClassName("diffstattoggle");
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
558
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
559 for (var i = 0; i < els.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
560 els[i].addEventListener("click", toggleDiffstat, false);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
561 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
562 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
563
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
564 function addLineWrapToggle() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
565 var els = document.getElementsByClassName("linewraptoggle");
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
566
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
567 for (var i = 0; i < els.length; i++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
568 var nodes = els[i].getElementsByClassName("linewraplink");
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
569
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
570 for (var j = 0; j < nodes.length; j++) {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
571 nodes[j].addEventListener("click", toggleLinewrap, false);
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
572 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
573 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
574 }
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
575
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
576 document.addEventListener('DOMContentLoaded', function() {
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
577 process_dates();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
578 addDiffStatToggle();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
579 addLineWrapToggle();
Vadim Filimonov <fffilimonov@yandex.ru>
parents:
diff changeset
580 }, false);