-
Notifications
You must be signed in to change notification settings - Fork 10
/
Gitgraph.js
132 lines (115 loc) · 4.16 KB
/
Gitgraph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var Gitgraph = function(args){
if(!args || !args.user || !args.repo){
throw new Error('Gitgraph: missing user and/or repo arg ');
}else{
this.go = function(){
this.total = this.data.all;
this.own = this.data.owner;
//Populate canvas
this.graphContainer.innerHTML = '';
this.canvas = dojo.create('canvas',{width:this.width,height:this.height,style:'position:relative;margin-top:11px;'},this.graphContainer);
var img = dojo.create('img',{
src:'https://a248.e.akamai.net/assets.github.com/images/modules/dashboard/dossier/participation_legend.png?1315937721',
style:'position:relative;top:-4px'
},this.graphContainer);
var context = this.canvas.getContext("2d"),
width = this.width / this.total.length,
height = this.height,
max = Math.max.apply(Math, this.total),
scale = max >= height ? parseFloat(height - 1) / max : 1;
function render(value, index){
value *= scale;
context.fillRect(index * width, height - value, width - 1, value);
}
context.fillStyle = 'rgb(202, 202, 202)';
dojo.forEach(this.total, render);
context.fillStyle = 'rgb(51, 102, 153)';
dojo.forEach(this.own, render);
};
this.loadScript = function(sScriptSrc,callbackfunction) {
//gets document head element
var oHead = document.getElementsByTagName('head')[0];
if(oHead){
//creates a new script tag
var oScript = document.createElement('script');
//adds src and type attribute to script tag
oScript.setAttribute('src',sScriptSrc);
oScript.setAttribute('type','text/javascript');
//calling a function after the js is loaded (IE)
var loadFunction = function()
{
if (this.readyState == 'complete' || this.readyState == 'loaded')
{
callbackfunction();
}
};
oScript.onreadystatechange = loadFunction;
//calling a function after the js is loaded (Firefox)
oScript.onload = callbackfunction;
//append the script tag to document head element
oHead.appendChild(oScript);
}
};
this.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP
? this
: oThis || window,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
this.kickStart = function(){
dojo.ready(this, function(){
//Get particiption data
dojo.xhrGet({
url: 'http://logicalcognition.com/files/gitgraph.php?user='+args.user+'&repo='+args.repo,
handleAs: 'json',
preventCache: true,
load: dojo.hitch(this,function(data){
this.data = data;
this.go();
})
});
});
};
this.width = 416;
this.height = 20;
this.node = args.domNode ? args.domNode : document.body;
if(!window.dojo)
this.loadScript('http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js',this.kickStart.bind(this));
else
this.kickStart.bind(this)();
}
this.graphContainer = document.createElement('div');
this.graphContainer.innerHTML = '<img src="http://biganimals.com/wp-content/themes/biganimals/images/loading_transparent_4.gif"/>';
this.graphContainer.style.cssText = 'border-radius:3px;border:1px solid #E5E5E5;'
+'background:white;height:55px;width:430px;text-align:center;';
this.node.appendChild(this.graphContainer);
return this.graphContainer;
};
//Make Jquery folks happy
if (window.jQuery) {
jQuery.fn.gitgraph = function (args) {
if(!args || !args.user || !args.repo){
throw new Error('Gitgraph: missing user and/or repo arg ');
}else{
this.each(function () {
var view = new Gitgraph({
user : args.user,
domNode : $(this)[0],
repo : args.repo
});
});
}
};
}