[PATCH 2 of 3] hgweb: refactor graph customization javascript

Patrick Mezard patrick at mezard.eu
Sat Feb 18 05:35:07 CST 2012


# HG changeset patch
# User Patrick Mezard <patrick at mezard.eu>
# Date 1327235726 -25200
# Node ID 920b6ea5b9193e786cad23435cf01a77951b6864
# Parent  750f231ae63677c429af3f51170df10e7df324ff
hgweb: refactor graph customization javascript

- Avoid flipping lineWidth state around the edge() call, pass it to the
  function instead.
- Pass the line width and color appended to the other parameters instead of in
  a dictionary. The javascript code is simpler, no need to check for all
  containers existence, and the JSON output is smaller.
- Reindent setColor() comments and fix code spacing.

diff --git a/mercurial/graphmod.py b/mercurial/graphmod.py
--- a/mercurial/graphmod.py
+++ b/mercurial/graphmod.py
@@ -91,14 +91,15 @@
             branch, setting = key.rsplit('.', 1)
             # Validation
             if setting == "width" and val.isdigit():
-                config.setdefault(branch, {})[setting] = val
+                config.setdefault(branch, {})[setting] = int(val)
             elif setting == "color" and val.isalnum():
                 config.setdefault(branch, {})[setting] = val
 
     if config:
-        getconf = util.lrucachefunc(lambda rev: config.get(repo[rev].branch()))
+        getconf = util.lrucachefunc(
+            lambda rev: config.get(repo[rev].branch(), {}))
     else:
-        getconf = lambda rev: None
+        getconf = lambda rev: {}
 
     for (cur, type, data, parents) in dag:
 
@@ -128,14 +129,18 @@
         edges = []
         for ecol, eid in enumerate(seen):
             if eid in next:
+                bconf = getconf(eid)
                 edges.append((
                     ecol, next.index(eid), colors[eid],
-                    getconf(eid)))
+                    bconf.get('width', -1),
+                    bconf.get('color', '')))
             elif eid == cur:
                 for p in parents:
+                    bconf = getconf(p)
                     edges.append((
                         ecol, next.index(p), color,
-                        getconf(p)))
+                        bconf.get('width', -1),
+                        bconf.get('color', '')))
 
         # Yield and move on
         yield (cur, type, data, (col, color), edges)
diff --git a/mercurial/templates/static/mercurial.js b/mercurial/templates/static/mercurial.js
--- a/mercurial/templates/static/mercurial.js
+++ b/mercurial/templates/static/mercurial.js
@@ -58,16 +58,17 @@
 		
 		// Set the colour.
 		//
-		// If color is hex string "FFFFFF" then append sharp and apply as is 
-		// If color is number
-		// picks a distinct colour based on an internal wheel; the bg
-		// parameter provides the value that should be assigned to the 'zero'
-		// colours and the fg parameter provides the multiplier that should be
-		// applied to the foreground colours.
+		// If color is a string, expect an hexadecimal RGB
+		// value and apply it unchanged. If color is a number,
+		// pick a distinct colour based on an internal wheel;
+		// the bg parameter provides the value that should be
+		// assigned to the 'zero' colours and the fg parameter
+		// provides the multiplier that should be applied to
+		// the foreground colours.
 		var s;
-		if(typeof color == "string"){
-			s = "#"+color;
-		}else{ //typeof color == "number"
+		if(typeof color == "string") {
+			s = "#" + color;
+		} else { //typeof color == "number"
 			color %= colors.length;
 			var red = (colors[color][0] * fg) || bg;
 			var green = (colors[color][1] * fg) || bg;
@@ -83,9 +84,11 @@
 		
 	}
 
-	this.edge = function(x0, y0, x1, y1, color) {
+	this.edge = function(x0, y0, x1, y1, color, width) {
 		
 		this.setColor(color, 0.0, 0.65);
+		if(width >= 0)
+			 this.ctx.lineWidth = width;
 		this.ctx.beginPath();
 		this.ctx.moveTo(x0, y0);
 		this.ctx.lineTo(x1, y1);
@@ -109,13 +112,20 @@
 			var edges = cur[2];
 			var fold = false;
 			
+			var prevWidth = this.ctx.lineWidth;
 			for (var j in edges) {
 				
 				line = edges[j];
 				start = line[0];
 				end = line[1];
 				color = line[2];
-
+				var width = line[3];
+				if(width < 0)
+					 width = prevWidth;
+				var branchcolor = line[4];
+				if(branchcolor)
+					color = branchcolor;
+				
 				if (end > this.columns || start > this.columns) {
 					this.columns += 1;
 				}
@@ -128,17 +138,11 @@
 				y0 = this.bg[1] - this.bg_height / 2;
 				x1 = this.cell[0] + this.box_size * end + this.box_size / 2;
 				y1 = this.bg[1] + this.bg_height / 2;
-				// Width if exists
-				if(line[3] && line[3]['width']){
-					this.ctx.lineWidth = line[3]['width'];
-				}
-				// Color if exists
-				if(line[3] && line[3]['color']){
-					color = line[3]['color'];
-				}
-				this.edge(x0, y0, x1, y1, color);
-				this.ctx.lineWidth = 1;
+				
+				this.edge(x0, y0, x1, y1, color, width);
+				
 			}
+			this.ctx.lineWidth = prevWidth;
 			
 			// Draw the revision node in the right column
 			
diff --git a/tests/test-hgweb-commands.t b/tests/test-hgweb-commands.t
--- a/tests/test-hgweb-commands.t
+++ b/tests/test-hgweb-commands.t
@@ -957,7 +957,7 @@
   <script>
   <!-- hide script content
   
-  var data = [["ba87b23d29ca", [0, 1], [[0, 0, 1, {"color": "FF0000", "width": "3"}]], "branch", "test", "1970-01-01", ["unstable", true], ["tip"], ["something"]], ["1d22e65f027e", [0, 1], [[0, 0, 1, {"width": "3"}]], "branch", "test", "1970-01-01", ["stable", true], [], []], ["a4f92ed23982", [0, 1], [[0, 0, 1, {"width": "3"}]], "Added tag 1.0 for changeset 2ef0ac749a14", "test", "1970-01-01", ["default", true], [], []], ["2ef0ac749a14", [0, 1], [], "base", "test", "1970-01-01", ["default", false], ["1.0"], ["anotherthing"]]];
+  var data = [["ba87b23d29ca", [0, 1], [[0, 0, 1, 3, "FF0000"]], "branch", "test", "1970-01-01", ["unstable", true], ["tip"], ["something"]], ["1d22e65f027e", [0, 1], [[0, 0, 1, 3, ""]], "branch", "test", "1970-01-01", ["stable", true], [], []], ["a4f92ed23982", [0, 1], [[0, 0, 1, 3, ""]], "Added tag 1.0 for changeset 2ef0ac749a14", "test", "1970-01-01", ["default", true], [], []], ["2ef0ac749a14", [0, 1], [], "base", "test", "1970-01-01", ["default", false], ["1.0"], ["anotherthing"]]];
   var graph = new Graph();
   graph.scale(39);
   
@@ -1214,7 +1214,7 @@
 
   $ "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT '/graph/' \
   >     | grep '^var data ='
-  var data = [["548001d11f45", [0, 1], [[0, 0, 1, null]], "\u80fd", "test", "1970-01-01", ["unstable", true], ["tip"], ["something"]], ["ba87b23d29ca", [0, 1], [[0, 0, 1, {"color": "FF0000", "width": "3"}]], "branch", "test", "1970-01-01", ["unstable", false], [], []], ["1d22e65f027e", [0, 1], [[0, 0, 1, {"width": "3"}]], "branch", "test", "1970-01-01", ["stable", true], [], []], ["a4f92ed23982", [0, 1], [[0, 0, 1, {"width": "3"}]], "Added tag 1.0 for changeset 2ef0ac749a14", "test", "1970-01-01", ["default", true], [], []], ["2ef0ac749a14", [0, 1], [], "base", "test", "1970-01-01", ["default", false], ["1.0"], ["anotherthing"]]];
+  var data = [["548001d11f45", [0, 1], [[0, 0, 1, -1, ""]], "\u80fd", "test", "1970-01-01", ["unstable", true], ["tip"], ["something"]], ["ba87b23d29ca", [0, 1], [[0, 0, 1, 3, "FF0000"]], "branch", "test", "1970-01-01", ["unstable", false], [], []], ["1d22e65f027e", [0, 1], [[0, 0, 1, 3, ""]], "branch", "test", "1970-01-01", ["stable", true], [], []], ["a4f92ed23982", [0, 1], [[0, 0, 1, 3, ""]], "Added tag 1.0 for changeset 2ef0ac749a14", "test", "1970-01-01", ["default", true], [], []], ["2ef0ac749a14", [0, 1], [], "base", "test", "1970-01-01", ["default", false], ["1.0"], ["anotherthing"]]];
 
 ERRORS ENCOUNTERED
 


More information about the Mercurial-devel mailing list