Commit 4b2ecbc420ca10ad78e0e6f921d6740c84f2574a

Authored by Koen Punt
1 parent e1282d50

Updated branch-graph, abstracted some code in seperate functions

Removed unused Raphael.fn.popup
app/controllers/projects_controller.rb
@@ -54,12 +54,12 @@ class ProjectsController < ProjectResourceController @@ -54,12 +54,12 @@ class ProjectsController < ProjectResourceController
54 54
55 respond_to do |format| 55 respond_to do |format|
56 format.html do 56 format.html do
57 - unless @project.empty_repo?  
58 - @last_push = current_user.recent_push(@project.id)  
59 - render :show  
60 - else  
61 - render "projects/empty"  
62 - end 57 + unless @project.empty_repo?
  58 + @last_push = current_user.recent_push(@project.id)
  59 + render :show
  60 + else
  61 + render "projects/empty"
  62 + end
63 end 63 end
64 format.js 64 format.js
65 end 65 end
@@ -83,19 +83,13 @@ class ProjectsController < ProjectResourceController @@ -83,19 +83,13 @@ class ProjectsController < ProjectResourceController
83 end 83 end
84 84
85 def graph 85 def graph
86 -  
87 respond_to do |format| 86 respond_to do |format|
88 format.html 87 format.html
89 format.json do 88 format.json do
90 graph = Gitlab::Graph::JsonBuilder.new(project) 89 graph = Gitlab::Graph::JsonBuilder.new(project)
91 - #@days_json, @commits_json = graph.days_json, graph.commits_json  
92 - render :text => graph.to_json 90 + render :json => graph.to_json
93 end 91 end
94 end 92 end
95 -  
96 -  
97 -  
98 -  
99 end 93 end
100 94
101 def destroy 95 def destroy
app/views/projects/graph.html.haml
@@ -9,5 +9,8 @@ @@ -9,5 +9,8 @@
9 :javascript 9 :javascript
10 var branch_graph; 10 var branch_graph;
11 $(function(){ 11 $(function(){
12 - branch_graph = new BranchGraph($("#holder"), '#{url_for :controller => 'projects', :action => 'graph'}'); 12 + branch_graph = new BranchGraph($("#holder"), {
  13 + url: '#{url_for controller: 'projects', action: 'graph', format: :json}',
  14 + commit_url: '#{url_for controller: 'projects', action: 'show'}/commits/%s'
  15 + });
13 }); 16 });
lib/gitlab/graph/json_builder.rb
@@ -18,11 +18,11 @@ module Gitlab @@ -18,11 +18,11 @@ module Gitlab
18 @days = index_commits 18 @days = index_commits
19 end 19 end
20 20
21 - def to_json 21 + def to_json(*args)
22 { 22 {
23 days: @days.compact.map { |d| [d.day, d.strftime("%b")] }, 23 days: @days.compact.map { |d| [d.day, d.strftime("%b")] },
24 commits: @commits.map(&:to_graph_hash) 24 commits: @commits.map(&:to_graph_hash)
25 - }.to_json 25 + }.to_json(*args)
26 end 26 end
27 27
28 protected 28 protected
vendor/assets/javascripts/branch-graph.js
1 !function(){ 1 !function(){
2 2
3 - var BranchGraph = function(element, url){ 3 + var BranchGraph = function(element, options){
4 this.element = element; 4 this.element = element;
5 - this.url = url; 5 + this.options = options;
6 6
7 - this.comms = {}; 7 + this.preparedCommits = {};
8 this.mtime = 0; 8 this.mtime = 0;
9 this.mspace = 0; 9 this.mspace = 0;
10 this.parents = {}; 10 this.parents = {};
@@ -15,67 +15,83 @@ @@ -15,67 +15,83 @@
15 15
16 BranchGraph.prototype.load = function(){ 16 BranchGraph.prototype.load = function(){
17 $.ajax({ 17 $.ajax({
18 - url: this.url, 18 + url: this.options.url,
19 method: 'get', 19 method: 'get',
20 dataType: 'json', 20 dataType: 'json',
21 success: $.proxy(function(data){ 21 success: $.proxy(function(data){
22 $('.loading', this.element).hide(); 22 $('.loading', this.element).hide();
23 this.prepareData(data.days, data.commits); 23 this.prepareData(data.days, data.commits);
24 - this.buildGraph(this.element.get(0)); 24 + this.buildGraph();
25 }, this) 25 }, this)
26 }); 26 });
27 - }, 27 + };
28 28
29 BranchGraph.prototype.prepareData = function(days, commits){ 29 BranchGraph.prototype.prepareData = function(days, commits){
30 this.days = days; 30 this.days = days;
  31 + this.dayCount = days.length;
31 this.commits = commits; 32 this.commits = commits;
32 - ii = this.commits.length;  
33 - for (var i = 0; i < ii; i++) {  
34 - for (var j = 0, jj = this.commits[i].parents.length; j < jj; j++) {  
35 - this.parents[this.commits[i].parents[j][0]] = true;  
36 - }  
37 - this.mtime = Math.max(this.mtime, this.commits[i].time);  
38 - this.mspace = Math.max(this.mspace, this.commits[i].space);  
39 - } 33 + this.commitCount = commits.length;
  34 +
  35 + this.collectParents();
  36 +
40 this.mtime += 4; 37 this.mtime += 4;
41 this.mspace += 10; 38 this.mspace += 10;
42 - for (i = 0; i < ii; i++) { 39 + for (var i = 0; i < this.commitCount; i++) {
43 if (this.commits[i].id in this.parents) { 40 if (this.commits[i].id in this.parents) {
44 this.commits[i].isParent = true; 41 this.commits[i].isParent = true;
45 } 42 }
46 - this.comms[this.commits[i].id] = this.commits[i]; 43 + this.preparedCommits[this.commits[i].id] = this.commits[i];
47 } 44 }
  45 + this.collectColors();
  46 + };
  47 +
  48 + BranchGraph.prototype.collectParents = function(){
  49 + for (var i = 0; i < this.commitCount; i++) {
  50 + for (var j = 0, jj = this.commits[i].parents.length; j < jj; j++) {
  51 + this.parents[this.commits[i].parents[j][0]] = true;
  52 + }
  53 + this.mtime = Math.max(this.mtime, this.commits[i].time);
  54 + this.mspace = Math.max(this.mspace, this.commits[i].space);
  55 + }
  56 + };
  57 +
  58 + BranchGraph.prototype.collectColors = function(){
48 for (var k = 0; k < this.mspace; k++) { 59 for (var k = 0; k < this.mspace; k++) {
49 this.colors.push(Raphael.getColor()); 60 this.colors.push(Raphael.getColor());
50 } 61 }
51 }; 62 };
52 63
53 - BranchGraph.prototype.buildGraph = function(holder){  
54 - var ch = this.mspace * 20 + 20  
55 - , cw = this.mtime * 20 + 20  
56 - , r = Raphael(holder, cw, ch) 64 + BranchGraph.prototype.buildGraph = function(){
  65 + var graphWidth = $(this.element).width()
  66 + , ch = this.mspace * 20 + 20
  67 + , cw = Math.max(graphWidth, this.mtime * 20 + 20)
  68 + , r = Raphael(this.element.get(0), cw, ch)
57 , top = r.set() 69 , top = r.set()
58 , cuday = 0 70 , cuday = 0
59 , cumonth = "" 71 , cumonth = ""
60 - , r; 72 + , offsetX = 20
  73 + , offsetY = 60
  74 + , barWidth = Math.max(graphWidth, this.dayCount * 20 + 80);
61 75
62 this.raphael = r; 76 this.raphael = r;
63 77
64 - r.rect(0, 0, this.days.length * 20 + 80, 30).attr({fill: "#222"});  
65 - r.rect(0, 30, this.days.length * 20 + 80, 20).attr({fill: "#444"}); 78 + r.rect(0, 0, barWidth, 20).attr({fill: "#222"});
  79 + r.rect(0, 20, barWidth, 20).attr({fill: "#444"});
66 80
67 - for (mm = 0; mm < this.days.length; mm++) { 81 + for (mm = 0; mm < this.dayCount; mm++) {
68 if(this.days[mm] != null){ 82 if(this.days[mm] != null){
69 if(cuday != this.days[mm][0]){ 83 if(cuday != this.days[mm][0]){
70 - r.text(10 + mm * 20, 40, this.days[mm][0]).attr({  
71 - font: "14px Fontin-Sans, Arial", 84 + // Dates
  85 + r.text(offsetX + mm * 20, 31, this.days[mm][0]).attr({
  86 + font: "12px Monaco, Arial",
72 fill: "#DDD" 87 fill: "#DDD"
73 }); 88 });
74 cuday = this.days[mm][0]; 89 cuday = this.days[mm][0];
75 } 90 }
76 if(cumonth != this.days[mm][1]){ 91 if(cumonth != this.days[mm][1]){
77 - r.text(10 + mm * 20, 15, this.days[mm][1]).attr({  
78 - font: "14px Fontin-Sans, Arial", 92 + // Months
  93 + r.text(offsetX + mm * 20, 11, this.days[mm][1]).attr({
  94 + font: "12px Monaco, Arial",
79 fill: "#EEE" 95 fill: "#EEE"
80 }); 96 });
81 cumonth = this.days[mm][1]; 97 cumonth = this.days[mm][1];
@@ -83,9 +99,9 @@ @@ -83,9 +99,9 @@
83 } 99 }
84 } 100 }
85 101
86 - for (i = 0; i < ii; i++) {  
87 - var x = 10 + 20 * this.commits[i].time  
88 - , y = 70 + 20 * this.commits[i].space; 102 + for (i = 0; i < this.commitCount; i++) {
  103 + var x = offsetX + 20 * this.commits[i].time
  104 + , y = offsetY + 20 * this.commits[i].space;
89 r.circle(x, y, 3).attr({ 105 r.circle(x, y, 3).attr({
90 fill: this.colors[this.commits[i].space], 106 fill: this.colors[this.commits[i].space],
91 stroke: "none" 107 stroke: "none"
@@ -96,22 +112,28 @@ @@ -96,22 +112,28 @@
96 if (shortrefs.length > 15){ 112 if (shortrefs.length > 15){
97 shortrefs = shortrefs.substr(0,13) + "..."; 113 shortrefs = shortrefs.substr(0,13) + "...";
98 } 114 }
99 - var t = r.text(x+5, y+5, shortrefs).attr({  
100 - font: "12px Fontin-Sans, Arial", fill: "#666",  
101 - title: longrefs, cursor: "pointer", rotation: "90" 115 + var t = r.text(x+5, y+8, shortrefs).attr({
  116 + font: "12px Monaco, Arial",
  117 + fill: "#666",
  118 + title: longrefs,
  119 + cursor: "pointer",
  120 + rotation: "90"
102 }); 121 });
103 122
104 var textbox = t.getBBox(); 123 var textbox = t.getBBox();
105 t.translate(textbox.height/-4, textbox.width/2); 124 t.translate(textbox.height/-4, textbox.width/2);
106 } 125 }
  126 + var c;
107 for (var j = 0, jj = this.commits[i].parents.length; j < jj; j++) { 127 for (var j = 0, jj = this.commits[i].parents.length; j < jj; j++) {
108 - var c = this.comms[this.commits[i].parents[j][0]]; 128 + c = this.preparedCommits[this.commits[i].parents[j][0]];
109 if (c) { 129 if (c) {
110 - var cx = 10 + 20 * c.time  
111 - , cy = 70 + 20 * c.space; 130 + var cx = offsetX + 20 * c.time
  131 + , cy = offsetY + 20 * c.space;
112 if (c.space == this.commits[i].space) { 132 if (c.space == this.commits[i].space) {
113 - r.path("M" + (x - 5) + "," + (y + .0001) + "L" + (15 + 20 * c.time) + "," + (y + .0001))  
114 - .attr({ 133 + r.path([
  134 + "M", x, y,
  135 + "L", x - 20 * (c.time + 1), y
  136 + ]).attr({
115 stroke: this.colors[c.space], 137 stroke: this.colors[c.space],
116 "stroke-width": 2 138 "stroke-width": 2
117 }); 139 });
@@ -134,130 +156,100 @@ @@ -134,130 +156,100 @@
134 this.appendAnchor(top, this.commits[i], x, y); 156 this.appendAnchor(top, this.commits[i], x, y);
135 } 157 }
136 top.toFront(); 158 top.toFront();
137 - var hw = holder.offsetWidth  
138 - , hh = holder.offsetHeight  
139 - , v = r.rect(hw - 8, 0, 4, Math.pow(hh, 2) / ch, 2).attr({  
140 - fill: "#000",  
141 - opacity: 0  
142 - })  
143 - , h = r.rect(0, hh - 8, Math.pow(hw, 2) / cw, 4, 2).attr({  
144 - fill: "#000",  
145 - opacity: 0  
146 - })  
147 - , bars = r.set(v, h)  
148 - , drag  
149 - , dragger = function (event) {  
150 - if (drag) {  
151 - event = event || window.event;  
152 - holder.scrollLeft = drag.sl - (event.clientX - drag.x);  
153 - holder.scrollTop = drag.st - (event.clientY - drag.y);  
154 - }  
155 - };  
156 - holder.onmousedown = function (event) {  
157 - event = event || window.event;  
158 - drag = {  
159 - x: event.clientX,  
160 - y: event.clientY,  
161 - st: holder.scrollTop,  
162 - sl: holder.scrollLeft  
163 - };  
164 - document.onmousemove = dragger;  
165 - bars.animate({opacity: .5}, 300);  
166 - };  
167 - document.onmouseup = function () {  
168 - drag = false;  
169 - document.onmousemove = null;  
170 - bars.animate({opacity: 0}, 300);  
171 - }; 159 + this.element.scrollLeft(cw);
  160 + this.bindEvents();
  161 + };
  162 +
  163 + BranchGraph.prototype.bindEvents = function(){
  164 + var drag = {}
  165 + , element = this.element;
172 166
173 - $(window).on('keydown', function(event){  
174 - if(event.keyCode == 37){  
175 - holder.scrollLeft -= 50; 167 + var dragger = function(event){
  168 + element.scrollLeft(drag.sl - (event.clientX - drag.x));
  169 + element.scrollTop(drag.st - (event.clientY - drag.y));
  170 + };
  171 +
  172 + element.on({
  173 + mousedown: function (event) {
  174 + drag = {
  175 + x: event.clientX,
  176 + y: event.clientY,
  177 + st: element.scrollTop(),
  178 + sl: element.scrollLeft()
  179 + };
  180 + $(window).on('mousemove', dragger);
176 } 181 }
177 - if(event.keyCode == 39){  
178 - // right  
179 - holder.scrollLeft += 50; 182 + });
  183 + $(window).on({
  184 + mouseup: function(){
  185 + //bars.animate({opacity: 0}, 300);
  186 + $(window).off('mousemove', dragger);
  187 + },
  188 + keydown: function(event){
  189 + if(event.keyCode == 37){
  190 + // left
  191 + element.scrollLeft( element.scrollLeft() - 50);
  192 + }
  193 + if(event.keyCode == 38){
  194 + // top
  195 + element.scrollTop( element.scrollTop() - 50);
  196 + }
  197 + if(event.keyCode == 39){
  198 + // right
  199 + element.scrollLeft( element.scrollLeft() + 50);
  200 + }
  201 + if(event.keyCode == 40){
  202 + // bottom
  203 + element.scrollTop( element.scrollTop() + 50);
  204 + }
180 } 205 }
181 }); 206 });
182 -  
183 -  
184 - holder.scrollLeft = cw;  
185 }; 207 };
186 208
187 BranchGraph.prototype.appendAnchor = function(top, c, x, y) { 209 BranchGraph.prototype.appendAnchor = function(top, c, x, y) {
188 - var r = this.raphael;  
189 - top.push(r.circle(x, y, 10).attr({ 210 + var r = this.raphael
  211 + , options = this.options
  212 + , anchor;
  213 + anchor = r.circle(x, y, 10).attr({
190 fill: "#000", 214 fill: "#000",
191 opacity: 0, 215 opacity: 0,
192 cursor: "pointer" 216 cursor: "pointer"
193 }) 217 })
194 .click(function(){ 218 .click(function(){
195 - location.href = location.href.replace("graph", "commits/" + c.id); 219 + window.location = options.commit_url.replace('%s', c.id);
196 }) 220 })
197 - .hover(function () {  
198 - // Create empty node to convert entities to character  
199 - var s = r.text(100, 100, c.author + "\n \n" + c.id + "\n \n" + c.message).attr({ 221 + .hover(function(){
  222 + var text = r.text(100, 100, c.author + "\n \n" + c.id + "\n \n" + c.message).attr({
200 fill: "#fff" 223 fill: "#fff"
201 }); 224 });
202 - this.popup = r.popupit(x, y + 5, s, 0); 225 + this.popup = r.tooltip(x, y + 5, text, 0);
203 top.push(this.popup.insertBefore(this)); 226 top.push(this.popup.insertBefore(this));
204 - }, function () { 227 + }, function(){
205 this.popup && this.popup.remove() && delete this.popup; 228 this.popup && this.popup.remove() && delete this.popup;
206 - })); 229 + });
  230 + top.push(anchor);
207 }; 231 };
208 232
209 this.BranchGraph = BranchGraph; 233 this.BranchGraph = BranchGraph;
210 234
211 }(this); 235 }(this);
212 -Raphael.fn.popupit = function (x, y, set, dir, size) {  
213 - dir = dir == null ? 2 : dir;  
214 - size = size || 5;  
215 - x = Math.round(x);  
216 - y = Math.round(y);  
217 - var mmax = Math.max,  
218 - bb = set.getBBox(),  
219 - w = Math.round(bb.width / 2),  
220 - h = Math.round(bb.height / 2),  
221 - dx = [0, w + size * 2, 0, -w - size * 2],  
222 - dy = [-h * 2 - size * 3, -h - size, 0, -h - size],  
223 - p = ["M", x - dx[dir], y - dy[dir], "l", -size, (dir == 2) * -size, -mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size,  
224 - "l", 0, -mmax(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -mmax(h - size, 0), "a", size, size, 0, 0, 1, size, -size,  
225 - "l", mmax(w - size, 0), 0, size, !dir * -size, size, !dir * size, mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size,  
226 - "l", 0, mmax(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, mmax(h - size, 0), "a", size, size, 0, 0, 1, -size, size,  
227 - "l", -mmax(w - size, 0), 0, "z"].join(","),  
228 - xy = [{x: x, y: y + size * 2 + h}, {x: x - size * 2 - w, y: y}, {x: x, y: y - size * 2 - h}, {x: x + size * 2 + w, y: y}][dir];  
229 - set.translate(xy.x - w - bb.x, xy.y - h - bb.y);  
230 - return this.set(this.path(p).attr({fill: "#234", stroke: "none"}).insertBefore(set.node ? set : set[0]), set);  
231 -};  
232 -Raphael.fn.popup = function (x, y, text, dir, size) {  
233 - dir = dir == null ? 2 : dir > 3 ? 3 : dir;  
234 - size = size || 5;  
235 - text = text || "$9.99";  
236 - var res = this.set(),  
237 - d = 3;  
238 - res.push(this.path().attr({fill: "#000", stroke: "#000"}));  
239 - res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff", "font-family": "Helvetica, Arial"}));  
240 - res.update = function (X, Y, withAnimation) {  
241 - X = X || x;  
242 - Y = Y || y;  
243 - var bb = this[1].getBBox(),  
244 - w = bb.width / 2,  
245 - h = bb.height / 2,  
246 - dx = [0, w + size * 2, 0, -w - size * 2],  
247 - dy = [-h * 2 - size * 3, -h - size, 0, -h - size],  
248 - p = ["M", X - dx[dir], Y - dy[dir], "l", -size, (dir == 2) * -size, -mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size,  
249 - "l", 0, -mmax(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -mmax(h - size, 0), "a", size, size, 0, 0, 1, size, -size,  
250 - "l", mmax(w - size, 0), 0, size, !dir * -size, size, !dir * size, mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size,  
251 - "l", 0, mmax(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, mmax(h - size, 0), "a", size, size, 0, 0, 1, -size, size,  
252 - "l", -mmax(w - size, 0), 0, "z"].join(","),  
253 - xy = [{x: X, y: Y + size * 2 + h}, {x: X - size * 2 - w, y: Y}, {x: X, y: Y - size * 2 - h}, {x: X + size * 2 + w, y: Y}][dir];  
254 - xy.path = p;  
255 - if (withAnimation) {  
256 - this.animate(xy, 500, ">");  
257 - } else {  
258 - this.attr(xy);  
259 - }  
260 - return this;  
261 - };  
262 - return res.update(x, y);  
263 -}; 236 +Raphael.fn.tooltip = function (x, y, set, dir, size) {
  237 + dir = dir == null ? 2 : dir;
  238 + size = size || 5;
  239 + x = Math.round(x);
  240 + y = Math.round(y);
  241 + var mmax = Math.max
  242 + , bb = set.getBBox()
  243 + , w = Math.round(bb.width / 2)
  244 + , h = Math.round(bb.height / 2)
  245 + , dx = [0, w + size * 2, 0, -w - size * 2]
  246 + , dy = [-h * 2 - size * 3, -h - size, 0, -h - size]
  247 + , p = ["M", x - dx[dir], y - dy[dir], "l", -size, (dir == 2) * -size, -mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size,
  248 + "l", 0, -mmax(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -mmax(h - size, 0), "a", size, size, 0, 0, 1, size, -size,
  249 + "l", mmax(w - size, 0), 0, size, !dir * -size, size, !dir * size, mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size,
  250 + "l", 0, mmax(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, mmax(h - size, 0), "a", size, size, 0, 0, 1, -size, size,
  251 + "l", -mmax(w - size, 0), 0, "z"].join(",")
  252 + , xy = [{x: x, y: y + size * 2 + h}, {x: x - size * 2 - w, y: y}, {x: x, y: y - size * 2 - h}, {x: x + size * 2 + w, y: y}][dir];
  253 + set.translate(xy.x - w - bb.x, xy.y - h - bb.y);
  254 + return this.set(this.path(p).attr({fill: "#234", stroke: "none"}).insertBefore(set.node ? set : set[0]), set);
  255 +};
264 \ No newline at end of file 256 \ No newline at end of file