Mercurial > hg
comparison mercurial/templates/static/mercurial.js @ 35160:69a865dc2ada
hgweb: define locally used variables as actually local in mercurial.js
Variables that are used or assigned without any declaration using var (or let,
or const) are considered global. In many cases this is inadvertent and actually
causes a variable leaking to a broader scope, such as a temporary variable used
inside a loop suddenly being accessible in global scope. (This corresponds to
"undef" option of jshint).
So this patch limits the scope of variables that don't need to be global. There
are a lot of helper variables in Graph.render() used in a loop, I've declared
them all on one line to reduce patch size. "radius" is special because it
wasn't passed to graph.vertex, but was used there (it worked because this
variable leaked to global scope). "window.graph" is created by an inline script
in graph.tmpl so that it can be used in ajaxScrollInit() function, this patch
makes this fact explicit by assigning window.graph to a local variable.
author | Anton Shestakov <av6@dwimlabs.net> |
---|---|
date | Wed, 22 Nov 2017 21:49:36 +0800 |
parents | 018aac6d7cb0 |
children | 1207a50a6dc3 |
comparison
equal
deleted
inserted
replaced
35159:018aac6d7cb0 | 35160:69a865dc2ada |
---|---|
92 | 92 |
93 this.render = function(data) { | 93 this.render = function(data) { |
94 | 94 |
95 var backgrounds = ''; | 95 var backgrounds = ''; |
96 var nodedata = ''; | 96 var nodedata = ''; |
97 var line, start, end, color, x, y, x0, y0, x1, y1, column, radius; | |
97 | 98 |
98 for (var i = 0; i < data.length; i++) { | 99 for (var i = 0; i < data.length; i++) { |
99 | 100 |
100 var parity = i % 2; | 101 var parity = i % 2; |
101 this.cell[1] += this.bg_height; | 102 this.cell[1] += this.bg_height; |
144 color = node[1]; | 145 color = node[1]; |
145 | 146 |
146 radius = this.box_size / 8; | 147 radius = this.box_size / 8; |
147 x = this.cell[0] + this.box_size * column + this.box_size / 2; | 148 x = this.cell[0] + this.box_size * column + this.box_size / 2; |
148 y = this.bg[1] - this.bg_height / 2; | 149 y = this.bg[1] - this.bg_height / 2; |
149 var add = this.vertex(x, y, color, parity, cur); | 150 var add = this.vertex(x, y, radius, color, parity, cur); |
150 backgrounds += add[0]; | 151 backgrounds += add[0]; |
151 nodedata += add[1]; | 152 nodedata += add[1]; |
152 | 153 |
153 if (fold) this.columns -= 1; | 154 if (fold) this.columns -= 1; |
154 | 155 |
295 return String(replacements[p1]); | 296 return String(replacements[p1]); |
296 }); | 297 }); |
297 } | 298 } |
298 | 299 |
299 function makeRequest(url, method, onstart, onsuccess, onerror, oncomplete) { | 300 function makeRequest(url, method, onstart, onsuccess, onerror, oncomplete) { |
300 xhr = new XMLHttpRequest(); | 301 var xhr = new XMLHttpRequest(); |
301 xhr.onreadystatechange = function() { | 302 xhr.onreadystatechange = function() { |
302 if (xhr.readyState === 4) { | 303 if (xhr.readyState === 4) { |
303 try { | 304 try { |
304 if (xhr.status === 200) { | 305 if (xhr.status === 200) { |
305 onsuccess(xhr.responseText); | 306 onsuccess(xhr.responseText); |
342 nextPageVar, | 343 nextPageVar, |
343 nextPageVarGet, | 344 nextPageVarGet, |
344 containerSelector, | 345 containerSelector, |
345 messageFormat, | 346 messageFormat, |
346 mode) { | 347 mode) { |
347 updateInitiated = false; | 348 var updateInitiated = false; |
348 container = document.querySelector(containerSelector); | 349 var container = document.querySelector(containerSelector); |
349 | 350 |
350 function scrollHandler() { | 351 function scrollHandler() { |
351 if (updateInitiated) { | 352 if (updateInitiated) { |
352 return; | 353 return; |
353 } | 354 } |
380 }; | 381 }; |
381 appendFormatHTML(container, messageFormat, message); | 382 appendFormatHTML(container, messageFormat, message); |
382 }, | 383 }, |
383 function onsuccess(htmlText) { | 384 function onsuccess(htmlText) { |
384 if (mode === 'graph') { | 385 if (mode === 'graph') { |
386 var graph = window.graph; | |
385 var sizes = htmlText.match(/^\s*<canvas id="graph" width="(\d+)" height="(\d+)"><\/canvas>$/m); | 387 var sizes = htmlText.match(/^\s*<canvas id="graph" width="(\d+)" height="(\d+)"><\/canvas>$/m); |
386 var addWidth = sizes[1]; | 388 var addWidth = sizes[1]; |
387 var addHeight = sizes[2]; | 389 var addHeight = sizes[2]; |
388 addWidth = parseInt(addWidth); | 390 addWidth = parseInt(addWidth); |
389 addHeight = parseInt(addHeight); | 391 addHeight = parseInt(addHeight); |
466 var checkbox = document.getElementById(key + "-checkbox"); | 468 var checkbox = document.getElementById(key + "-checkbox"); |
467 if (!checkbox) { | 469 if (!checkbox) { |
468 continue; | 470 continue; |
469 } | 471 } |
470 | 472 |
471 currentValue = form.getAttribute("data-" + key); | 473 var currentValue = form.getAttribute("data-" + key); |
472 checkbox.checked = currentValue !== "0"; | 474 checkbox.checked = currentValue !== "0"; |
473 | 475 |
474 // ignorews implies ignorewsamount and ignorewseol. | 476 // ignorews implies ignorewsamount and ignorewseol. |
475 if (allChecked && (key === "ignorewsamount" || key === "ignorewseol")) { | 477 if (allChecked && (key === "ignorewsamount" || key === "ignorewseol")) { |
476 checkbox.checked = true; | 478 checkbox.checked = true; |