static/graph.js
author Scott Ehlert <ds@alliedmods.net>
Sat Oct 17 21:03:13 2009 -0500 (2009-10-17)
changeset 1 feedc61d5d39
parent 0 08bd5d57b87b
permissions -rw-r--r--
Created gitweb_am as copy of gitweb.
ds@0
     1
// branch_renderer.js - Rendering of branch DAGs on the client side
ds@0
     2
//
ds@0
     3
// Copyright 2008 Dirkjan Ochtman <dirkjan AT ochtman DOT nl>
ds@0
     4
// Copyright 2006 Alexander Schremmer <alex AT alexanderweb DOT de>
ds@0
     5
//
ds@0
     6
// derived from code written by Scott James Remnant <[email protected]>
ds@0
     7
// Copyright 2005 Canonical Ltd.
ds@0
     8
//
ds@0
     9
// This software may be used and distributed according to the terms
ds@0
    10
// of the GNU General Public License, incorporated herein by reference.
ds@0
    11
ds@0
    12
var colors = [
ds@0
    13
	[ 1.0, 0.0, 0.0 ],
ds@0
    14
	[ 1.0, 1.0, 0.0 ],
ds@0
    15
	[ 0.0, 1.0, 0.0 ],
ds@0
    16
	[ 0.0, 1.0, 1.0 ],
ds@0
    17
	[ 0.0, 0.0, 1.0 ],
ds@0
    18
	[ 1.0, 0.0, 1.0 ]
ds@0
    19
];
ds@0
    20
ds@0
    21
function Graph() {
ds@0
    22
	
ds@0
    23
	this.canvas = document.getElementById('graph');
ds@0
    24
	if (navigator.userAgent.indexOf('MSIE') >= 0) this.canvas = window.G_vmlCanvasManager.initElement(this.canvas);
ds@0
    25
	this.ctx = this.canvas.getContext('2d');
ds@0
    26
	this.ctx.strokeStyle = 'rgb(0, 0, 0)';
ds@0
    27
	this.ctx.fillStyle = 'rgb(0, 0, 0)';
ds@0
    28
	this.cur = [0, 0];
ds@0
    29
	this.line_width = 3;
ds@0
    30
	this.bg = [0, 4];
ds@0
    31
	this.cell = [2, 0];
ds@0
    32
	this.columns = 0;
ds@0
    33
	this.revlink = '';
ds@0
    34
	
ds@0
    35
	this.scale = function(height) {
ds@0
    36
		this.bg_height = height;
ds@0
    37
		this.box_size = Math.floor(this.bg_height / 1.2);
ds@0
    38
		this.cell_height = this.box_size;
ds@0
    39
	}
ds@0
    40
	
ds@0
    41
	function colorPart(num) {
ds@0
    42
		num *= 255
ds@0
    43
		num = num < 0 ? 0 : num;
ds@0
    44
		num = num > 255 ? 255 : num;
ds@0
    45
		var digits = Math.round(num).toString(16);
ds@0
    46
		if (num < 16) {
ds@0
    47
			return '0' + digits;
ds@0
    48
		} else {
ds@0
    49
			return digits;
ds@0
    50
		}
ds@0
    51
	}
ds@0
    52
ds@0
    53
	this.setColor = function(color, bg, fg) {
ds@0
    54
		
ds@0
    55
		// Set the colour.
ds@0
    56
		//
ds@0
    57
		// Picks a distinct colour based on an internal wheel; the bg
ds@0
    58
		// parameter provides the value that should be assigned to the 'zero'
ds@0
    59
		// colours and the fg parameter provides the multiplier that should be
ds@0
    60
		// applied to the foreground colours.
ds@0
    61
		
ds@0
    62
		color %= colors.length;
ds@0
    63
		var red = (colors[color][0] * fg) || bg;
ds@0
    64
		var green = (colors[color][1] * fg) || bg;
ds@0
    65
		var blue = (colors[color][2] * fg) || bg;
ds@0
    66
		red = Math.round(red * 255);
ds@0
    67
		green = Math.round(green * 255);
ds@0
    68
		blue = Math.round(blue * 255);
ds@0
    69
		var s = 'rgb(' + red + ', ' + green + ', ' + blue + ')';
ds@0
    70
		this.ctx.strokeStyle = s;
ds@0
    71
		this.ctx.fillStyle = s;
ds@0
    72
		return s;
ds@0
    73
		
ds@0
    74
	}
ds@0
    75
ds@0
    76
	this.render = function(data) {
ds@0
    77
		
ds@0
    78
		var backgrounds = '';
ds@0
    79
		var nodedata = '';
ds@0
    80
		
ds@0
    81
		for (var i in data) {
ds@0
    82
			
ds@0
    83
			var parity = i % 2;
ds@0
    84
			this.cell[1] += this.bg_height;
ds@0
    85
			this.bg[1] += this.bg_height;
ds@0
    86
			
ds@0
    87
			var cur = data[i];
ds@0
    88
			var node = cur[1];
ds@0
    89
			var edges = cur[2];
ds@0
    90
			var fold = false;
ds@0
    91
			
ds@0
    92
			for (var j in edges) {
ds@0
    93
				
ds@0
    94
				line = edges[j];
ds@0
    95
				start = line[0];
ds@0
    96
				end = line[1];
ds@0
    97
				color = line[2];
ds@0
    98
ds@0
    99
				if (end > this.columns || start > this.columns) {
ds@0
   100
					this.columns += 1;
ds@0
   101
				}
ds@0
   102
				
ds@0
   103
				if (start == this.columns && start > end) {
ds@0
   104
					var fold = true;
ds@0
   105
				}
ds@0
   106
				
ds@0
   107
				x0 = this.cell[0] + this.box_size * start + this.box_size / 2;
ds@0
   108
				y0 = this.bg[1] - this.bg_height / 2;
ds@0
   109
				x1 = this.cell[0] + this.box_size * end + this.box_size / 2;
ds@0
   110
				y1 = this.bg[1] + this.bg_height / 2;
ds@0
   111
				
ds@0
   112
				this.edge(x0, y0, x1, y1, color);
ds@0
   113
				
ds@0
   114
			}
ds@0
   115
			
ds@0
   116
			// Draw the revision node in the right column
ds@0
   117
			
ds@0
   118
			column = node[0]
ds@0
   119
			color = node[1]
ds@0
   120
			
ds@0
   121
			radius = this.box_size / 8;
ds@0
   122
			x = this.cell[0] + this.box_size * column + this.box_size / 2;
ds@0
   123
			y = this.bg[1] - this.bg_height / 2;
ds@0
   124
			var add = this.vertex(x, y, color, parity, cur);
ds@0
   125
			backgrounds += add[0];
ds@0
   126
			nodedata += add[1];
ds@0
   127
			
ds@0
   128
			if (fold) this.columns -= 1;
ds@0
   129
			
ds@0
   130
		}
ds@0
   131
		
ds@0
   132
		document.getElementById('nodebgs').innerHTML += backgrounds;
ds@0
   133
		document.getElementById('graphnodes').innerHTML += nodedata;
ds@0
   134
		
ds@0
   135
	}
ds@0
   136
ds@0
   137
}