static/graph.js
changeset 0 08bd5d57b87b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/static/graph.js	Fri Oct 16 23:25:00 2009 -0500
     1.3 @@ -0,0 +1,137 @@
     1.4 +// branch_renderer.js - Rendering of branch DAGs on the client side
     1.5 +//
     1.6 +// Copyright 2008 Dirkjan Ochtman <dirkjan AT ochtman DOT nl>
     1.7 +// Copyright 2006 Alexander Schremmer <alex AT alexanderweb DOT de>
     1.8 +//
     1.9 +// derived from code written by Scott James Remnant <[email protected]>
    1.10 +// Copyright 2005 Canonical Ltd.
    1.11 +//
    1.12 +// This software may be used and distributed according to the terms
    1.13 +// of the GNU General Public License, incorporated herein by reference.
    1.14 +
    1.15 +var colors = [
    1.16 +	[ 1.0, 0.0, 0.0 ],
    1.17 +	[ 1.0, 1.0, 0.0 ],
    1.18 +	[ 0.0, 1.0, 0.0 ],
    1.19 +	[ 0.0, 1.0, 1.0 ],
    1.20 +	[ 0.0, 0.0, 1.0 ],
    1.21 +	[ 1.0, 0.0, 1.0 ]
    1.22 +];
    1.23 +
    1.24 +function Graph() {
    1.25 +	
    1.26 +	this.canvas = document.getElementById('graph');
    1.27 +	if (navigator.userAgent.indexOf('MSIE') >= 0) this.canvas = window.G_vmlCanvasManager.initElement(this.canvas);
    1.28 +	this.ctx = this.canvas.getContext('2d');
    1.29 +	this.ctx.strokeStyle = 'rgb(0, 0, 0)';
    1.30 +	this.ctx.fillStyle = 'rgb(0, 0, 0)';
    1.31 +	this.cur = [0, 0];
    1.32 +	this.line_width = 3;
    1.33 +	this.bg = [0, 4];
    1.34 +	this.cell = [2, 0];
    1.35 +	this.columns = 0;
    1.36 +	this.revlink = '';
    1.37 +	
    1.38 +	this.scale = function(height) {
    1.39 +		this.bg_height = height;
    1.40 +		this.box_size = Math.floor(this.bg_height / 1.2);
    1.41 +		this.cell_height = this.box_size;
    1.42 +	}
    1.43 +	
    1.44 +	function colorPart(num) {
    1.45 +		num *= 255
    1.46 +		num = num < 0 ? 0 : num;
    1.47 +		num = num > 255 ? 255 : num;
    1.48 +		var digits = Math.round(num).toString(16);
    1.49 +		if (num < 16) {
    1.50 +			return '0' + digits;
    1.51 +		} else {
    1.52 +			return digits;
    1.53 +		}
    1.54 +	}
    1.55 +
    1.56 +	this.setColor = function(color, bg, fg) {
    1.57 +		
    1.58 +		// Set the colour.
    1.59 +		//
    1.60 +		// Picks a distinct colour based on an internal wheel; the bg
    1.61 +		// parameter provides the value that should be assigned to the 'zero'
    1.62 +		// colours and the fg parameter provides the multiplier that should be
    1.63 +		// applied to the foreground colours.
    1.64 +		
    1.65 +		color %= colors.length;
    1.66 +		var red = (colors[color][0] * fg) || bg;
    1.67 +		var green = (colors[color][1] * fg) || bg;
    1.68 +		var blue = (colors[color][2] * fg) || bg;
    1.69 +		red = Math.round(red * 255);
    1.70 +		green = Math.round(green * 255);
    1.71 +		blue = Math.round(blue * 255);
    1.72 +		var s = 'rgb(' + red + ', ' + green + ', ' + blue + ')';
    1.73 +		this.ctx.strokeStyle = s;
    1.74 +		this.ctx.fillStyle = s;
    1.75 +		return s;
    1.76 +		
    1.77 +	}
    1.78 +
    1.79 +	this.render = function(data) {
    1.80 +		
    1.81 +		var backgrounds = '';
    1.82 +		var nodedata = '';
    1.83 +		
    1.84 +		for (var i in data) {
    1.85 +			
    1.86 +			var parity = i % 2;
    1.87 +			this.cell[1] += this.bg_height;
    1.88 +			this.bg[1] += this.bg_height;
    1.89 +			
    1.90 +			var cur = data[i];
    1.91 +			var node = cur[1];
    1.92 +			var edges = cur[2];
    1.93 +			var fold = false;
    1.94 +			
    1.95 +			for (var j in edges) {
    1.96 +				
    1.97 +				line = edges[j];
    1.98 +				start = line[0];
    1.99 +				end = line[1];
   1.100 +				color = line[2];
   1.101 +
   1.102 +				if (end > this.columns || start > this.columns) {
   1.103 +					this.columns += 1;
   1.104 +				}
   1.105 +				
   1.106 +				if (start == this.columns && start > end) {
   1.107 +					var fold = true;
   1.108 +				}
   1.109 +				
   1.110 +				x0 = this.cell[0] + this.box_size * start + this.box_size / 2;
   1.111 +				y0 = this.bg[1] - this.bg_height / 2;
   1.112 +				x1 = this.cell[0] + this.box_size * end + this.box_size / 2;
   1.113 +				y1 = this.bg[1] + this.bg_height / 2;
   1.114 +				
   1.115 +				this.edge(x0, y0, x1, y1, color);
   1.116 +				
   1.117 +			}
   1.118 +			
   1.119 +			// Draw the revision node in the right column
   1.120 +			
   1.121 +			column = node[0]
   1.122 +			color = node[1]
   1.123 +			
   1.124 +			radius = this.box_size / 8;
   1.125 +			x = this.cell[0] + this.box_size * column + this.box_size / 2;
   1.126 +			y = this.bg[1] - this.bg_height / 2;
   1.127 +			var add = this.vertex(x, y, color, parity, cur);
   1.128 +			backgrounds += add[0];
   1.129 +			nodedata += add[1];
   1.130 +			
   1.131 +			if (fold) this.columns -= 1;
   1.132 +			
   1.133 +		}
   1.134 +		
   1.135 +		document.getElementById('nodebgs').innerHTML += backgrounds;
   1.136 +		document.getElementById('graphnodes').innerHTML += nodedata;
   1.137 +		
   1.138 +	}
   1.139 +
   1.140 +}