Show More
@@ -1,443 +1,443 b'' | |||||
1 | // mercurial.js - JavaScript utility functions |
|
1 | // mercurial.js - JavaScript utility functions | |
2 | // |
|
2 | // | |
3 | // Rendering of branch DAGs on the client side |
|
3 | // Rendering of branch DAGs on the client side | |
4 | // Display of elapsed time |
|
4 | // Display of elapsed time | |
5 | // Show or hide diffstat |
|
5 | // Show or hide diffstat | |
6 | // |
|
6 | // | |
7 | // Copyright 2008 Dirkjan Ochtman <dirkjan AT ochtman DOT nl> |
|
7 | // Copyright 2008 Dirkjan Ochtman <dirkjan AT ochtman DOT nl> | |
8 | // Copyright 2006 Alexander Schremmer <alex AT alexanderweb DOT de> |
|
8 | // Copyright 2006 Alexander Schremmer <alex AT alexanderweb DOT de> | |
9 | // |
|
9 | // | |
10 | // derived from code written by Scott James Remnant <scott@ubuntu.com> |
|
10 | // derived from code written by Scott James Remnant <scott@ubuntu.com> | |
11 | // Copyright 2005 Canonical Ltd. |
|
11 | // Copyright 2005 Canonical Ltd. | |
12 | // |
|
12 | // | |
13 | // This software may be used and distributed according to the terms |
|
13 | // This software may be used and distributed according to the terms | |
14 | // of the GNU General Public License, incorporated herein by reference. |
|
14 | // of the GNU General Public License, incorporated herein by reference. | |
15 |
|
15 | |||
16 | var colors = [ |
|
16 | var colors = [ | |
17 | [ 1.0, 0.0, 0.0 ], |
|
17 | [ 1.0, 0.0, 0.0 ], | |
18 | [ 1.0, 1.0, 0.0 ], |
|
18 | [ 1.0, 1.0, 0.0 ], | |
19 | [ 0.0, 1.0, 0.0 ], |
|
19 | [ 0.0, 1.0, 0.0 ], | |
20 | [ 0.0, 1.0, 1.0 ], |
|
20 | [ 0.0, 1.0, 1.0 ], | |
21 | [ 0.0, 0.0, 1.0 ], |
|
21 | [ 0.0, 0.0, 1.0 ], | |
22 | [ 1.0, 0.0, 1.0 ] |
|
22 | [ 1.0, 0.0, 1.0 ] | |
23 | ]; |
|
23 | ]; | |
24 |
|
24 | |||
25 | function Graph() { |
|
25 | function Graph() { | |
26 |
|
26 | |||
27 | this.canvas = document.getElementById('graph'); |
|
27 | this.canvas = document.getElementById('graph'); | |
28 | if (window.G_vmlCanvasManager) this.canvas = window.G_vmlCanvasManager.initElement(this.canvas); |
|
28 | if (window.G_vmlCanvasManager) this.canvas = window.G_vmlCanvasManager.initElement(this.canvas); | |
29 | this.ctx = this.canvas.getContext('2d'); |
|
29 | this.ctx = this.canvas.getContext('2d'); | |
30 | this.ctx.strokeStyle = 'rgb(0, 0, 0)'; |
|
30 | this.ctx.strokeStyle = 'rgb(0, 0, 0)'; | |
31 | this.ctx.fillStyle = 'rgb(0, 0, 0)'; |
|
31 | this.ctx.fillStyle = 'rgb(0, 0, 0)'; | |
32 | this.cur = [0, 0]; |
|
32 | this.cur = [0, 0]; | |
33 | this.line_width = 3; |
|
33 | this.line_width = 3; | |
34 | this.bg = [0, 4]; |
|
34 | this.bg = [0, 4]; | |
35 | this.cell = [2, 0]; |
|
35 | this.cell = [2, 0]; | |
36 | this.columns = 0; |
|
36 | this.columns = 0; | |
37 | this.revlink = ''; |
|
37 | this.revlink = ''; | |
38 |
|
38 | |||
39 | this.reset = function() { |
|
39 | this.reset = function() { | |
40 | this.bg = [0, 4]; |
|
40 | this.bg = [0, 4]; | |
41 | this.cell = [2, 0]; |
|
41 | this.cell = [2, 0]; | |
42 | this.columns = 0; |
|
42 | this.columns = 0; | |
43 | document.getElementById('nodebgs').innerHTML = ''; |
|
43 | document.getElementById('nodebgs').innerHTML = ''; | |
44 | document.getElementById('graphnodes').innerHTML = ''; |
|
44 | document.getElementById('graphnodes').innerHTML = ''; | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | this.scale = function(height) { |
|
47 | this.scale = function(height) { | |
48 | this.bg_height = height; |
|
48 | this.bg_height = height; | |
49 | this.box_size = Math.floor(this.bg_height / 1.2); |
|
49 | this.box_size = Math.floor(this.bg_height / 1.2); | |
50 | this.cell_height = this.box_size; |
|
50 | this.cell_height = this.box_size; | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | function colorPart(num) { |
|
53 | function colorPart(num) { | |
54 | num *= 255 |
|
54 | num *= 255 | |
55 | num = num < 0 ? 0 : num; |
|
55 | num = num < 0 ? 0 : num; | |
56 | num = num > 255 ? 255 : num; |
|
56 | num = num > 255 ? 255 : num; | |
57 | var digits = Math.round(num).toString(16); |
|
57 | var digits = Math.round(num).toString(16); | |
58 | if (num < 16) { |
|
58 | if (num < 16) { | |
59 | return '0' + digits; |
|
59 | return '0' + digits; | |
60 | } else { |
|
60 | } else { | |
61 | return digits; |
|
61 | return digits; | |
62 | } |
|
62 | } | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | this.setColor = function(color, bg, fg) { |
|
65 | this.setColor = function(color, bg, fg) { | |
66 |
|
66 | |||
67 | // Set the colour. |
|
67 | // Set the colour. | |
68 | // |
|
68 | // | |
69 | // If color is a string, expect an hexadecimal RGB |
|
69 | // If color is a string, expect an hexadecimal RGB | |
70 | // value and apply it unchanged. If color is a number, |
|
70 | // value and apply it unchanged. If color is a number, | |
71 | // pick a distinct colour based on an internal wheel; |
|
71 | // pick a distinct colour based on an internal wheel; | |
72 | // the bg parameter provides the value that should be |
|
72 | // the bg parameter provides the value that should be | |
73 | // assigned to the 'zero' colours and the fg parameter |
|
73 | // assigned to the 'zero' colours and the fg parameter | |
74 | // provides the multiplier that should be applied to |
|
74 | // provides the multiplier that should be applied to | |
75 | // the foreground colours. |
|
75 | // the foreground colours. | |
76 | var s; |
|
76 | var s; | |
77 | if(typeof color == "string") { |
|
77 | if(typeof color == "string") { | |
78 | s = "#" + color; |
|
78 | s = "#" + color; | |
79 | } else { //typeof color == "number" |
|
79 | } else { //typeof color == "number" | |
80 | color %= colors.length; |
|
80 | color %= colors.length; | |
81 | var red = (colors[color][0] * fg) || bg; |
|
81 | var red = (colors[color][0] * fg) || bg; | |
82 | var green = (colors[color][1] * fg) || bg; |
|
82 | var green = (colors[color][1] * fg) || bg; | |
83 | var blue = (colors[color][2] * fg) || bg; |
|
83 | var blue = (colors[color][2] * fg) || bg; | |
84 | red = Math.round(red * 255); |
|
84 | red = Math.round(red * 255); | |
85 | green = Math.round(green * 255); |
|
85 | green = Math.round(green * 255); | |
86 | blue = Math.round(blue * 255); |
|
86 | blue = Math.round(blue * 255); | |
87 | s = 'rgb(' + red + ', ' + green + ', ' + blue + ')'; |
|
87 | s = 'rgb(' + red + ', ' + green + ', ' + blue + ')'; | |
88 | } |
|
88 | } | |
89 | this.ctx.strokeStyle = s; |
|
89 | this.ctx.strokeStyle = s; | |
90 | this.ctx.fillStyle = s; |
|
90 | this.ctx.fillStyle = s; | |
91 | return s; |
|
91 | return s; | |
92 |
|
92 | |||
93 | } |
|
93 | } | |
94 |
|
94 | |||
95 | this.edge = function(x0, y0, x1, y1, color, width) { |
|
95 | this.edge = function(x0, y0, x1, y1, color, width) { | |
96 |
|
96 | |||
97 | this.setColor(color, 0.0, 0.65); |
|
97 | this.setColor(color, 0.0, 0.65); | |
98 | if(width >= 0) |
|
98 | if(width >= 0) | |
99 | this.ctx.lineWidth = width; |
|
99 | this.ctx.lineWidth = width; | |
100 | this.ctx.beginPath(); |
|
100 | this.ctx.beginPath(); | |
101 | this.ctx.moveTo(x0, y0); |
|
101 | this.ctx.moveTo(x0, y0); | |
102 | this.ctx.lineTo(x1, y1); |
|
102 | this.ctx.lineTo(x1, y1); | |
103 | this.ctx.stroke(); |
|
103 | this.ctx.stroke(); | |
104 |
|
104 | |||
105 | } |
|
105 | } | |
106 |
|
106 | |||
107 | this.render = function(data) { |
|
107 | this.render = function(data) { | |
108 |
|
108 | |||
109 | var backgrounds = ''; |
|
109 | var backgrounds = ''; | |
110 | var nodedata = ''; |
|
110 | var nodedata = ''; | |
111 |
|
111 | |||
112 | for (var i in data) { |
|
112 | for (var i in data) { | |
113 |
|
113 | |||
114 | var parity = i % 2; |
|
114 | var parity = i % 2; | |
115 | this.cell[1] += this.bg_height; |
|
115 | this.cell[1] += this.bg_height; | |
116 | this.bg[1] += this.bg_height; |
|
116 | this.bg[1] += this.bg_height; | |
117 |
|
117 | |||
118 | var cur = data[i]; |
|
118 | var cur = data[i]; | |
119 | var node = cur[1]; |
|
119 | var node = cur[1]; | |
120 | var edges = cur[2]; |
|
120 | var edges = cur[2]; | |
121 | var fold = false; |
|
121 | var fold = false; | |
122 |
|
122 | |||
123 | var prevWidth = this.ctx.lineWidth; |
|
123 | var prevWidth = this.ctx.lineWidth; | |
124 | for (var j in edges) { |
|
124 | for (var j in edges) { | |
125 |
|
125 | |||
126 | line = edges[j]; |
|
126 | line = edges[j]; | |
127 | start = line[0]; |
|
127 | start = line[0]; | |
128 | end = line[1]; |
|
128 | end = line[1]; | |
129 | color = line[2]; |
|
129 | color = line[2]; | |
130 | var width = line[3]; |
|
130 | var width = line[3]; | |
131 | if(width < 0) |
|
131 | if(width < 0) | |
132 | width = prevWidth; |
|
132 | width = prevWidth; | |
133 | var branchcolor = line[4]; |
|
133 | var branchcolor = line[4]; | |
134 | if(branchcolor) |
|
134 | if(branchcolor) | |
135 | color = branchcolor; |
|
135 | color = branchcolor; | |
136 |
|
136 | |||
137 | if (end > this.columns || start > this.columns) { |
|
137 | if (end > this.columns || start > this.columns) { | |
138 | this.columns += 1; |
|
138 | this.columns += 1; | |
139 | } |
|
139 | } | |
140 |
|
140 | |||
141 | if (start == this.columns && start > end) { |
|
141 | if (start == this.columns && start > end) { | |
142 | var fold = true; |
|
142 | var fold = true; | |
143 | } |
|
143 | } | |
144 |
|
144 | |||
145 | x0 = this.cell[0] + this.box_size * start + this.box_size / 2; |
|
145 | x0 = this.cell[0] + this.box_size * start + this.box_size / 2; | |
146 | y0 = this.bg[1] - this.bg_height / 2; |
|
146 | y0 = this.bg[1] - this.bg_height / 2; | |
147 | x1 = this.cell[0] + this.box_size * end + this.box_size / 2; |
|
147 | x1 = this.cell[0] + this.box_size * end + this.box_size / 2; | |
148 | y1 = this.bg[1] + this.bg_height / 2; |
|
148 | y1 = this.bg[1] + this.bg_height / 2; | |
149 |
|
149 | |||
150 | this.edge(x0, y0, x1, y1, color, width); |
|
150 | this.edge(x0, y0, x1, y1, color, width); | |
151 |
|
151 | |||
152 | } |
|
152 | } | |
153 | this.ctx.lineWidth = prevWidth; |
|
153 | this.ctx.lineWidth = prevWidth; | |
154 |
|
154 | |||
155 | // Draw the revision node in the right column |
|
155 | // Draw the revision node in the right column | |
156 |
|
156 | |||
157 | column = node[0] |
|
157 | column = node[0] | |
158 | color = node[1] |
|
158 | color = node[1] | |
159 |
|
159 | |||
160 | radius = this.box_size / 8; |
|
160 | radius = this.box_size / 8; | |
161 | x = this.cell[0] + this.box_size * column + this.box_size / 2; |
|
161 | x = this.cell[0] + this.box_size * column + this.box_size / 2; | |
162 | y = this.bg[1] - this.bg_height / 2; |
|
162 | y = this.bg[1] - this.bg_height / 2; | |
163 | var add = this.vertex(x, y, color, parity, cur); |
|
163 | var add = this.vertex(x, y, color, parity, cur); | |
164 | backgrounds += add[0]; |
|
164 | backgrounds += add[0]; | |
165 | nodedata += add[1]; |
|
165 | nodedata += add[1]; | |
166 |
|
166 | |||
167 | if (fold) this.columns -= 1; |
|
167 | if (fold) this.columns -= 1; | |
168 |
|
168 | |||
169 | } |
|
169 | } | |
170 |
|
170 | |||
171 | document.getElementById('nodebgs').innerHTML += backgrounds; |
|
171 | document.getElementById('nodebgs').innerHTML += backgrounds; | |
172 | document.getElementById('graphnodes').innerHTML += nodedata; |
|
172 | document.getElementById('graphnodes').innerHTML += nodedata; | |
173 |
|
173 | |||
174 | } |
|
174 | } | |
175 |
|
175 | |||
176 | } |
|
176 | } | |
177 |
|
177 | |||
178 |
|
178 | |||
179 | function process_dates(parentSelector){ |
|
179 | function process_dates(parentSelector){ | |
180 |
|
180 | |||
181 | // derived from code from mercurial/templatefilter.py |
|
181 | // derived from code from mercurial/templatefilter.py | |
182 |
|
182 | |||
183 | var scales = { |
|
183 | var scales = { | |
184 | 'year': 365 * 24 * 60 * 60, |
|
184 | 'year': 365 * 24 * 60 * 60, | |
185 | 'month': 30 * 24 * 60 * 60, |
|
185 | 'month': 30 * 24 * 60 * 60, | |
186 | 'week': 7 * 24 * 60 * 60, |
|
186 | 'week': 7 * 24 * 60 * 60, | |
187 | 'day': 24 * 60 * 60, |
|
187 | 'day': 24 * 60 * 60, | |
188 | 'hour': 60 * 60, |
|
188 | 'hour': 60 * 60, | |
189 | 'minute': 60, |
|
189 | 'minute': 60, | |
190 | 'second': 1 |
|
190 | 'second': 1 | |
191 | }; |
|
191 | }; | |
192 |
|
192 | |||
193 | function format(count, string){ |
|
193 | function format(count, string){ | |
194 | var ret = count + ' ' + string; |
|
194 | var ret = count + ' ' + string; | |
195 | if (count > 1){ |
|
195 | if (count > 1){ | |
196 | ret = ret + 's'; |
|
196 | ret = ret + 's'; | |
197 | } |
|
197 | } | |
198 | return ret; |
|
198 | return ret; | |
199 | } |
|
199 | } | |
200 |
|
200 | |||
201 | function shortdate(date){ |
|
201 | function shortdate(date){ | |
202 | var ret = date.getFullYear() + '-'; |
|
202 | var ret = date.getFullYear() + '-'; | |
203 | // getMonth() gives a 0-11 result |
|
203 | // getMonth() gives a 0-11 result | |
204 | var month = date.getMonth() + 1; |
|
204 | var month = date.getMonth() + 1; | |
205 | if (month <= 9){ |
|
205 | if (month <= 9){ | |
206 | ret += '0' + month; |
|
206 | ret += '0' + month; | |
207 | } else { |
|
207 | } else { | |
208 | ret += month; |
|
208 | ret += month; | |
209 | } |
|
209 | } | |
210 | ret += '-'; |
|
210 | ret += '-'; | |
211 | var day = date.getDate(); |
|
211 | var day = date.getDate(); | |
212 | if (day <= 9){ |
|
212 | if (day <= 9){ | |
213 | ret += '0' + day; |
|
213 | ret += '0' + day; | |
214 | } else { |
|
214 | } else { | |
215 | ret += day; |
|
215 | ret += day; | |
216 | } |
|
216 | } | |
217 | return ret; |
|
217 | return ret; | |
218 | } |
|
218 | } | |
219 |
|
219 | |||
220 | function age(datestr){ |
|
220 | function age(datestr){ | |
221 | var now = new Date(); |
|
221 | var now = new Date(); | |
222 | var once = new Date(datestr); |
|
222 | var once = new Date(datestr); | |
223 | if (isNaN(once.getTime())){ |
|
223 | if (isNaN(once.getTime())){ | |
224 | // parsing error |
|
224 | // parsing error | |
225 | return datestr; |
|
225 | return datestr; | |
226 | } |
|
226 | } | |
227 |
|
227 | |||
228 | var delta = Math.floor((now.getTime() - once.getTime()) / 1000); |
|
228 | var delta = Math.floor((now.getTime() - once.getTime()) / 1000); | |
229 |
|
229 | |||
230 | var future = false; |
|
230 | var future = false; | |
231 | if (delta < 0){ |
|
231 | if (delta < 0){ | |
232 | future = true; |
|
232 | future = true; | |
233 | delta = -delta; |
|
233 | delta = -delta; | |
234 | if (delta > (30 * scales.year)){ |
|
234 | if (delta > (30 * scales.year)){ | |
235 | return "in the distant future"; |
|
235 | return "in the distant future"; | |
236 | } |
|
236 | } | |
237 | } |
|
237 | } | |
238 |
|
238 | |||
239 | if (delta > (2 * scales.year)){ |
|
239 | if (delta > (2 * scales.year)){ | |
240 | return shortdate(once); |
|
240 | return shortdate(once); | |
241 | } |
|
241 | } | |
242 |
|
242 | |||
243 | for (unit in scales){ |
|
243 | for (unit in scales){ | |
244 | var s = scales[unit]; |
|
244 | var s = scales[unit]; | |
245 | var n = Math.floor(delta / s); |
|
245 | var n = Math.floor(delta / s); | |
246 | if ((n >= 2) || (s == 1)){ |
|
246 | if ((n >= 2) || (s == 1)){ | |
247 | if (future){ |
|
247 | if (future){ | |
248 | return format(n, unit) + ' from now'; |
|
248 | return format(n, unit) + ' from now'; | |
249 | } else { |
|
249 | } else { | |
250 | return format(n, unit) + ' ago'; |
|
250 | return format(n, unit) + ' ago'; | |
251 | } |
|
251 | } | |
252 | } |
|
252 | } | |
253 | } |
|
253 | } | |
254 | } |
|
254 | } | |
255 |
|
255 | |||
256 | var nodes = document.querySelectorAll((parentSelector || '') + ' .age'); |
|
256 | var nodes = document.querySelectorAll((parentSelector || '') + ' .age'); | |
257 | var dateclass = new RegExp('\\bdate\\b'); |
|
257 | var dateclass = new RegExp('\\bdate\\b'); | |
258 | for (var i=0; i<nodes.length; ++i){ |
|
258 | for (var i=0; i<nodes.length; ++i){ | |
259 | var node = nodes[i]; |
|
259 | var node = nodes[i]; | |
260 | var classes = node.className; |
|
260 | var classes = node.className; | |
261 | var agevalue = age(node.textContent); |
|
261 | var agevalue = age(node.textContent); | |
262 | if (dateclass.test(classes)){ |
|
262 | if (dateclass.test(classes)){ | |
263 | // We want both: date + (age) |
|
263 | // We want both: date + (age) | |
264 | node.textContent += ' ('+agevalue+')'; |
|
264 | node.textContent += ' ('+agevalue+')'; | |
265 | } else { |
|
265 | } else { | |
266 | node.title = node.textContent; |
|
266 | node.title = node.textContent; | |
267 | node.textContent = agevalue; |
|
267 | node.textContent = agevalue; | |
268 | } |
|
268 | } | |
269 | } |
|
269 | } | |
270 | } |
|
270 | } | |
271 |
|
271 | |||
272 | function toggleDiffstat() { |
|
272 | function toggleDiffstat() { | |
273 | var curdetails = document.getElementById('diffstatdetails').style.display; |
|
273 | var curdetails = document.getElementById('diffstatdetails').style.display; | |
274 | var curexpand = curdetails == 'none' ? 'inline' : 'none'; |
|
274 | var curexpand = curdetails == 'none' ? 'inline' : 'none'; | |
275 | document.getElementById('diffstatdetails').style.display = curexpand; |
|
275 | document.getElementById('diffstatdetails').style.display = curexpand; | |
276 | document.getElementById('diffstatexpand').style.display = curdetails; |
|
276 | document.getElementById('diffstatexpand').style.display = curdetails; | |
277 | } |
|
277 | } | |
278 |
|
278 | |||
279 | function toggleLinewrap() { |
|
279 | function toggleLinewrap() { | |
280 | function getLinewrap() { |
|
280 | function getLinewrap() { | |
281 | var nodes = document.getElementsByClassName('sourcelines'); |
|
281 | var nodes = document.getElementsByClassName('sourcelines'); | |
282 | // if there are no such nodes, error is thrown here |
|
282 | // if there are no such nodes, error is thrown here | |
283 | return nodes[0].classList.contains('wrap'); |
|
283 | return nodes[0].classList.contains('wrap'); | |
284 | } |
|
284 | } | |
285 |
|
285 | |||
286 | function setLinewrap(enable) { |
|
286 | function setLinewrap(enable) { | |
287 | var nodes = document.getElementsByClassName('sourcelines'); |
|
287 | var nodes = document.getElementsByClassName('sourcelines'); | |
288 | for (var i = 0; i < nodes.length; i++) { |
|
288 | for (var i = 0; i < nodes.length; i++) { | |
289 | if (enable) { |
|
289 | if (enable) { | |
290 | nodes[i].classList.add('wrap'); |
|
290 | nodes[i].classList.add('wrap'); | |
291 | } else { |
|
291 | } else { | |
292 | nodes[i].classList.remove('wrap'); |
|
292 | nodes[i].classList.remove('wrap'); | |
293 | } |
|
293 | } | |
294 | } |
|
294 | } | |
295 |
|
295 | |||
296 | var links = document.getElementsByClassName('linewraplink'); |
|
296 | var links = document.getElementsByClassName('linewraplink'); | |
297 | for (var i = 0; i < links.length; i++) { |
|
297 | for (var i = 0; i < links.length; i++) { | |
298 | links[i].innerHTML = enable ? 'on' : 'off'; |
|
298 | links[i].innerHTML = enable ? 'on' : 'off'; | |
299 | } |
|
299 | } | |
300 | } |
|
300 | } | |
301 |
|
301 | |||
302 | setLinewrap(!getLinewrap()); |
|
302 | setLinewrap(!getLinewrap()); | |
303 | } |
|
303 | } | |
304 |
|
304 | |||
305 | function format(str, replacements) { |
|
305 | function format(str, replacements) { | |
306 | return str.replace(/%(\w+)%/g, function(match, p1) { |
|
306 | return str.replace(/%(\w+)%/g, function(match, p1) { | |
307 | return String(replacements[p1]); |
|
307 | return String(replacements[p1]); | |
308 | }); |
|
308 | }); | |
309 | } |
|
309 | } | |
310 |
|
310 | |||
311 | function makeRequest(url, method, onstart, onsuccess, onerror, oncomplete) { |
|
311 | function makeRequest(url, method, onstart, onsuccess, onerror, oncomplete) { | |
312 | xfr = new XMLHttpRequest(); |
|
312 | xfr = new XMLHttpRequest(); | |
313 | xfr.onreadystatechange = function() { |
|
313 | xfr.onreadystatechange = function() { | |
314 | if (xfr.readyState === 4) { |
|
314 | if (xfr.readyState === 4) { | |
315 | try { |
|
315 | try { | |
316 | if (xfr.status === 200) { |
|
316 | if (xfr.status === 200) { | |
317 | onsuccess(xfr.responseText); |
|
317 | onsuccess(xfr.responseText); | |
318 | } else { |
|
318 | } else { | |
319 | throw 'server error'; |
|
319 | throw 'server error'; | |
320 | } |
|
320 | } | |
321 | } catch (e) { |
|
321 | } catch (e) { | |
322 | onerror(e); |
|
322 | onerror(e); | |
323 | } finally { |
|
323 | } finally { | |
324 | oncomplete(); |
|
324 | oncomplete(); | |
325 | } |
|
325 | } | |
326 | } |
|
326 | } | |
327 | }; |
|
327 | }; | |
328 |
|
328 | |||
329 | xfr.open(method, url); |
|
329 | xfr.open(method, url); | |
330 | xfr.overrideMimeType("text/xhtml; charset=" + document.characterSet.toLowerCase()); |
|
330 | xfr.overrideMimeType("text/xhtml; charset=" + document.characterSet.toLowerCase()); | |
331 | xfr.send(); |
|
331 | xfr.send(); | |
332 | onstart(); |
|
332 | onstart(); | |
333 | return xfr; |
|
333 | return xfr; | |
334 | } |
|
334 | } | |
335 |
|
335 | |||
336 | function removeByClassName(className) { |
|
336 | function removeByClassName(className) { | |
337 | var nodes = document.getElementsByClassName(className); |
|
337 | var nodes = document.getElementsByClassName(className); | |
338 | while (nodes.length) { |
|
338 | while (nodes.length) { | |
339 | nodes[0].parentNode.removeChild(nodes[0]); |
|
339 | nodes[0].parentNode.removeChild(nodes[0]); | |
340 | } |
|
340 | } | |
341 | } |
|
341 | } | |
342 |
|
342 | |||
343 | function docFromHTML(html) { |
|
343 | function docFromHTML(html) { | |
344 | var doc = document.implementation.createHTMLDocument(''); |
|
344 | var doc = document.implementation.createHTMLDocument(''); | |
345 | doc.documentElement.innerHTML = html; |
|
345 | doc.documentElement.innerHTML = html; | |
346 | return doc; |
|
346 | return doc; | |
347 | } |
|
347 | } | |
348 |
|
348 | |||
349 | function appendFormatHTML(element, formatStr, replacements) { |
|
349 | function appendFormatHTML(element, formatStr, replacements) { | |
350 | element.insertAdjacentHTML('beforeend', format(formatStr, replacements)); |
|
350 | element.insertAdjacentHTML('beforeend', format(formatStr, replacements)); | |
351 | } |
|
351 | } | |
352 |
|
352 | |||
353 | function ajaxScrollInit(urlFormat, |
|
353 | function ajaxScrollInit(urlFormat, | |
354 | nextPageVar, |
|
354 | nextPageVar, | |
355 | nextPageVarGet, |
|
355 | nextPageVarGet, | |
356 | containerSelector, |
|
356 | containerSelector, | |
357 | messageFormat, |
|
357 | messageFormat, | |
358 | mode) { |
|
358 | mode) { | |
359 | updateInitiated = false; |
|
359 | updateInitiated = false; | |
360 | container = document.querySelector(containerSelector); |
|
360 | container = document.querySelector(containerSelector); | |
361 |
|
361 | |||
362 | function scrollHandler() { |
|
362 | function scrollHandler() { | |
363 | if (updateInitiated) { |
|
363 | if (updateInitiated) { | |
364 | return; |
|
364 | return; | |
365 | } |
|
365 | } | |
366 |
|
366 | |||
367 | var scrollHeight = document.documentElement.scrollHeight; |
|
367 | var scrollHeight = document.documentElement.scrollHeight; | |
368 | var clientHeight = document.documentElement.clientHeight; |
|
368 | var clientHeight = document.documentElement.clientHeight; | |
369 | var scrollTop = document.body.scrollTop |
|
369 | var scrollTop = document.body.scrollTop | |
370 | || document.documentElement.scrollTop; |
|
370 | || document.documentElement.scrollTop; | |
371 |
|
371 | |||
372 | if (scrollHeight - (scrollTop + clientHeight) < 50) { |
|
372 | if (scrollHeight - (scrollTop + clientHeight) < 50) { | |
373 | updateInitiated = true; |
|
373 | updateInitiated = true; | |
374 | removeByClassName('scroll-loading-error'); |
|
374 | removeByClassName('scroll-loading-error'); | |
375 | container.lastElementChild.classList.add('scroll-separator'); |
|
375 | container.lastElementChild.classList.add('scroll-separator'); | |
376 |
|
376 | |||
377 | if (!nextPageVar) { |
|
377 | if (!nextPageVar) { | |
378 | var message = { |
|
378 | var message = { | |
379 | class: 'scroll-loading-info', |
|
379 | 'class': 'scroll-loading-info', | |
380 | text: 'No more entries' |
|
380 | text: 'No more entries' | |
381 | }; |
|
381 | }; | |
382 | appendFormatHTML(container, messageFormat, message); |
|
382 | appendFormatHTML(container, messageFormat, message); | |
383 | return; |
|
383 | return; | |
384 | } |
|
384 | } | |
385 |
|
385 | |||
386 | makeRequest( |
|
386 | makeRequest( | |
387 | format(urlFormat, {next: nextPageVar}), |
|
387 | format(urlFormat, {next: nextPageVar}), | |
388 | 'GET', |
|
388 | 'GET', | |
389 | function onstart() { |
|
389 | function onstart() { | |
390 | var message = { |
|
390 | var message = { | |
391 | class: 'scroll-loading', |
|
391 | 'class': 'scroll-loading', | |
392 | text: 'Loading...' |
|
392 | text: 'Loading...' | |
393 | }; |
|
393 | }; | |
394 | appendFormatHTML(container, messageFormat, message); |
|
394 | appendFormatHTML(container, messageFormat, message); | |
395 | }, |
|
395 | }, | |
396 | function onsuccess(htmlText) { |
|
396 | function onsuccess(htmlText) { | |
397 | if (mode == 'graph') { |
|
397 | if (mode == 'graph') { | |
398 | var addHeight = htmlText.match(/^\s*<canvas id="graph".*height="(\d+)"><\/canvas>$/m)[1]; |
|
398 | var addHeight = htmlText.match(/^\s*<canvas id="graph".*height="(\d+)"><\/canvas>$/m)[1]; | |
399 | addHeight = parseInt(addHeight); |
|
399 | addHeight = parseInt(addHeight); | |
400 | graph.canvas.height = addHeight; |
|
400 | graph.canvas.height = addHeight; | |
401 |
|
401 | |||
402 | var dataStr = htmlText.match(/^\s*var data = (.*);$/m)[1]; |
|
402 | var dataStr = htmlText.match(/^\s*var data = (.*);$/m)[1]; | |
403 | var data = JSON.parse(dataStr); |
|
403 | var data = JSON.parse(dataStr); | |
404 | if (data.length < nextPageVar) { |
|
404 | if (data.length < nextPageVar) { | |
405 | nextPageVar = undefined; |
|
405 | nextPageVar = undefined; | |
406 | } |
|
406 | } | |
407 | graph.reset(); |
|
407 | graph.reset(); | |
408 | graph.render(data); |
|
408 | graph.render(data); | |
409 | } else { |
|
409 | } else { | |
410 | var doc = docFromHTML(htmlText); |
|
410 | var doc = docFromHTML(htmlText); | |
411 | var nodes = doc.querySelector(containerSelector).children; |
|
411 | var nodes = doc.querySelector(containerSelector).children; | |
412 | var curClass = 'c' + Date.now(); |
|
412 | var curClass = 'c' + Date.now(); | |
413 | while (nodes.length) { |
|
413 | while (nodes.length) { | |
414 | var node = nodes[0]; |
|
414 | var node = nodes[0]; | |
415 | node = document.adoptNode(node); |
|
415 | node = document.adoptNode(node); | |
416 | node.classList.add(curClass); |
|
416 | node.classList.add(curClass); | |
417 | container.appendChild(node); |
|
417 | container.appendChild(node); | |
418 | } |
|
418 | } | |
419 | process_dates('.' + curClass); |
|
419 | process_dates('.' + curClass); | |
420 | } |
|
420 | } | |
421 |
|
421 | |||
422 | nextPageVar = nextPageVarGet(htmlText, nextPageVar); |
|
422 | nextPageVar = nextPageVarGet(htmlText, nextPageVar); | |
423 | }, |
|
423 | }, | |
424 | function onerror(errorText) { |
|
424 | function onerror(errorText) { | |
425 | var message = { |
|
425 | var message = { | |
426 | class: 'scroll-loading-error', |
|
426 | 'class': 'scroll-loading-error', | |
427 | text: 'Error: ' + errorText |
|
427 | text: 'Error: ' + errorText | |
428 | }; |
|
428 | }; | |
429 | appendFormatHTML(container, messageFormat, message); |
|
429 | appendFormatHTML(container, messageFormat, message); | |
430 | }, |
|
430 | }, | |
431 | function oncomplete() { |
|
431 | function oncomplete() { | |
432 | removeByClassName('scroll-loading'); |
|
432 | removeByClassName('scroll-loading'); | |
433 | updateInitiated = false; |
|
433 | updateInitiated = false; | |
434 | scrollHandler(); |
|
434 | scrollHandler(); | |
435 | } |
|
435 | } | |
436 | ); |
|
436 | ); | |
437 | } |
|
437 | } | |
438 | } |
|
438 | } | |
439 |
|
439 | |||
440 | window.addEventListener('scroll', scrollHandler); |
|
440 | window.addEventListener('scroll', scrollHandler); | |
441 | window.addEventListener('resize', scrollHandler); |
|
441 | window.addEventListener('resize', scrollHandler); | |
442 | scrollHandler(); |
|
442 | scrollHandler(); | |
443 | } |
|
443 | } |
General Comments 0
You need to be logged in to leave comments.
Login now