Commit 40576b87092598b28a11ba796bab6857d009702b

Authored by Koen Punt
1 parent 12b4bb59

Fixed #1509 by converting the entities in js

Converted BranchGraph to some sort of Class
app/views/projects/graph.html.haml
... ... @@ -2,13 +2,14 @@
2 2 %br
3 3 .graph_holder
4 4 %h4
5   - %small You can move around the graph by using arrow keys.
  5 + %small You can move around the graph by using the arrow keys.
6 6 #holder.graph
  7 +
7 8 :javascript
8   - var chunk1={commits:#{@commits_json}};
9   - var days=#{@days_json};
10   - initGraph();
  9 + var commits = #{@commits_json}
  10 + , days = #{@days_json};
  11 + var branch_graph = new BranchGraph(days, commits);
11 12 $(function(){
12   - branchGraph($("#holder")[0]);
  13 + branch_graph.buildGraph($("#holder")[0]);
13 14 GraphNav.init();
14 15 });
... ...
vendor/assets/javascripts/branch-graph.js
1   -var commits = {},
2   - comms = {},
3   - pixelsX = [],
4   - pixelsY = [],
5   - mmax = Math.max,
6   - mtime = 0,
7   - mspace = 0,
8   - parents = {},
9   - ii = 0,
10   - colors = ["#000"];
  1 +!function(){
11 2  
12   -function initGraph(){
13   - commits = chunk1.commits;
14   - ii = commits.length;
15   - for (var i = 0; i < ii; i++) {
16   - for (var j = 0, jj = commits[i].parents.length; j < jj; j++) {
17   - parents[commits[i].parents[j][0]] = true;
  3 + var BranchGraph = function(days, commits){
  4 +
  5 + this.days = days || {};
  6 + this.commits = commits || {};
  7 + this.comms = {};
  8 + this.pixelsX = [];
  9 + this.pixelsY = [];
  10 + this.mtime = 0;
  11 + this.mspace = 0;
  12 + this.parents = {};
  13 + this.ii = 0;
  14 + this.colors = ["#000"];
  15 +
  16 + this.prepareData();
  17 + };
  18 +
  19 + BranchGraph.prototype.prepareData = function(){
  20 + ii = this.commits.length;
  21 + for (var i = 0; i < ii; i++) {
  22 + for (var j = 0, jj = this.commits[i].parents.length; j < jj; j++) {
  23 + this.parents[this.commits[i].parents[j][0]] = true;
18 24 }
19   - mtime = Math.max(mtime, commits[i].time);
20   - mspace = Math.max(mspace, commits[i].space);
21   - }
22   - mtime = mtime + 4;
23   - mspace = mspace + 10;
24   - for (i = 0; i < ii; i++) {
25   - if (commits[i].id in parents) {
26   - commits[i].isParent = true;
  25 + this.mtime = Math.max(this.mtime, this.commits[i].time);
  26 + this.mspace = Math.max(this.mspace, this.commits[i].space);
  27 + }
  28 + this.mtime = this.mtime + 4;
  29 + this.mspace = this.mspace + 10;
  30 + for (i = 0; i < ii; i++) {
  31 + if (this.commits[i].id in this.parents) {
  32 + this.commits[i].isParent = true;
27 33 }
28   - comms[commits[i].id] = commits[i];
29   - }
30   - for (var k = 0; k < mspace; k++) {
31   - colors.push(Raphael.getColor());
32   - }
33   -}
34   -
35   -function branchGraph(holder) {
36   - var ch = mspace * 20 + 20, cw = mtime * 20 + 20,
37   - r = Raphael("holder", cw, ch),
38   - top = r.set();
39   - var cuday = 0, cumonth = "";
40   - r.rect(0, 0, days.length * 20 + 80, 30).attr({fill: "#222"});
41   - r.rect(0, 30, days.length * 20 + 80, 20).attr({fill: "#444"});
42   -
43   - for (mm = 0; mm < days.length; mm++) {
44   - if(days[mm] != null){
45   - if(cuday != days[mm][0]){
46   - r.text(10 + mm * 20, 40, days[mm][0]).attr({font: "14px Fontin-Sans, Arial", fill: "#DDD"});
47   - cuday = days[mm][0]
48   - }
49   - if(cumonth != days[mm][1]){
50   - r.text(10 + mm * 20, 15, days[mm][1]).attr({font: "14px Fontin-Sans, Arial", fill: "#EEE"});
51   - cumonth = days[mm][1]
52   - }
  34 + this.comms[this.commits[i].id] = this.commits[i];
  35 + }
  36 + for (var k = 0; k < this.mspace; k++) {
  37 + this.colors.push(Raphael.getColor());
  38 + }
  39 + };
53 40  
  41 + BranchGraph.prototype.buildGraph = function(holder){
  42 + var ch = this.mspace * 20 + 20
  43 + , cw = this.mtime * 20 + 20
  44 + , r = Raphael("holder", cw, ch)
  45 + , top = r.set()
  46 + , cuday = 0
  47 + , cumonth = "";
  48 +
  49 + r.rect(0, 0, this.days.length * 20 + 80, 30).attr({fill: "#222"});
  50 + r.rect(0, 30, this.days.length * 20 + 80, 20).attr({fill: "#444"});
  51 +
  52 + for (mm = 0; mm < this.days.length; mm++) {
  53 + if(this.days[mm] != null){
  54 + if(cuday != this.days[mm][0]){
  55 + r.text(10 + mm * 20, 40, this.days[mm][0]).attr({
  56 + font: "14px Fontin-Sans, Arial",
  57 + fill: "#DDD"
  58 + });
  59 + cuday = this.days[mm][0];
  60 + }
  61 + if(cumonth != this.days[mm][1]){
  62 + r.text(10 + mm * 20, 15, this.days[mm][1]).attr({
  63 + font: "14px Fontin-Sans, Arial",
  64 + fill: "#EEE"
  65 + });
  66 + cumonth = this.days[mm][1];
54 67 }
  68 + }
55 69 }
  70 +
56 71 for (i = 0; i < ii; i++) {
57   - var x = 10 + 20 * commits[i].time,
58   - y = 70 + 20 * commits[i].space;
59   - r.circle(x, y, 3).attr({fill: colors[commits[i].space], stroke: "none"});
60   - if (commits[i].refs != null && commits[i].refs != "") {
61   - var longrefs = commits[i].refs
62   - var shortrefs = commits[i].refs;
63   - if (shortrefs.length > 15){
64   - shortrefs = shortrefs.substr(0,13) + "...";
65   - }
66   - var t = r.text(x+5, y+5, shortrefs).attr({font: "12px Fontin-Sans, Arial", fill: "#666",
67   - title: longrefs, cursor: "pointer", rotation: "90"});
68   -
69   - var textbox = t.getBBox();
70   - t.translate(textbox.height/-4,textbox.width/2);
  72 + var x = 10 + 20 * this.commits[i].time
  73 + , y = 70 + 20 * this.commits[i].space;
  74 + r.circle(x, y, 3).attr({
  75 + fill: this.colors[this.commits[i].space],
  76 + stroke: "none"
  77 + });
  78 + if (this.commits[i].refs != null && this.commits[i].refs != "") {
  79 + var longrefs = this.commits[i].refs
  80 + , shortrefs = this.commits[i].refs;
  81 + if (shortrefs.length > 15){
  82 + shortrefs = shortrefs.substr(0,13) + "...";
71 83 }
72   - for (var j = 0, jj = commits[i].parents.length; j < jj; j++) {
73   - var c = comms[commits[i].parents[j][0]];
74   - if (c) {
75   - var cx = 10 + 20 * c.time,
76   - cy = 70 + 20 * c.space;
77   - if (c.space == commits[i].space) {
78   - r.path("M" + (x - 5) + "," + (y + .0001) + "L" + (15 + 20 * c.time) + "," + (y + .0001))
79   - .attr({stroke: colors[c.space], "stroke-width": 2});
  84 + var t = r.text(x+5, y+5, shortrefs).attr({
  85 + font: "12px Fontin-Sans, Arial", fill: "#666",
  86 + title: longrefs, cursor: "pointer", rotation: "90"
  87 + });
80 88  
81   - } else if (c.space < commits[i].space) {
82   - r.path(["M", x - 5, y + .0001, "l-5-2,0,4,5,-2C", x - 5, y, x - 17, y + 2, x - 20, y - 5, "L", cx, y - 5, cx, cy])
83   - .attr({stroke: colors[commits[i].space], "stroke-width": 2});
84   - } else {
85   - r.path(["M", x - 3, y + 6, "l-4,3,4,2,0,-5L", x - 10, y + 20, "L", x - 10, cy, cx, cy])
86   - .attr({stroke: colors[c.space], "stroke-width": 2});
87   - }
88   - }
  89 + var textbox = t.getBBox();
  90 + t.translate(textbox.height/-4, textbox.width/2);
  91 + }
  92 + for (var j = 0, jj = this.commits[i].parents.length; j < jj; j++) {
  93 + var c = this.comms[this.commits[i].parents[j][0]];
  94 + if (c) {
  95 + var cx = 10 + 20 * c.time
  96 + , cy = 70 + 20 * c.space;
  97 + if (c.space == this.commits[i].space) {
  98 + r.path("M" + (x - 5) + "," + (y + .0001) + "L" + (15 + 20 * c.time) + "," + (y + .0001))
  99 + .attr({
  100 + stroke: this.colors[c.space],
  101 + "stroke-width": 2
  102 + });
  103 +
  104 + } else if (c.space < this.commits[i].space) {
  105 + r.path(["M", x - 5, y + .0001, "l-5-2,0,4,5,-2C", x - 5, y, x - 17, y + 2, x - 20, y - 5, "L", cx, y - 5, cx, cy])
  106 + .attr({
  107 + stroke: this.colors[this.commits[i].space],
  108 + "stroke-width": 2
  109 + });
  110 + } else {
  111 + r.path(["M", x - 3, y + 6, "l-4,3,4,2,0,-5L", x - 10, y + 20, "L", x - 10, cy, cx, cy])
  112 + .attr({
  113 + stroke: this.colors[c.space],
  114 + "stroke-width": 2
  115 + });
  116 + }
89 117 }
90   - (function (c, x, y) {
91   - top.push(r.circle(x, y, 10).attr({fill: "#000", opacity: 0, cursor: "pointer"})
92   - .click(function(){
93   - location.href = location.href.replace("graph", "commits/" + c.id);
94   - })
95   - .hover(function () {
96   - var s = r.text(100, 100,c.author + "\n \n" +c.id + "\n \n" + c.message).attr({fill: "#fff"});
97   - this.popup = r.popupit(x, y + 5, s, 0);
98   - top.push(this.popup.insertBefore(this));
99   - }, function () {
100   - this.popup && this.popup.remove() && delete this.popup;
101   - }));
102   - }(commits[i], x, y));
103   - }
104   - top.toFront();
105   - var hw = holder.offsetWidth,
106   - hh = holder.offsetHeight,
107   - v = r.rect(hw - 8, 0, 4, Math.pow(hh, 2) / ch, 2).attr({fill: "#000", opacity: 0}),
108   - h = r.rect(0, hh - 8, Math.pow(hw, 2) / cw, 4, 2).attr({fill: "#000", opacity: 0}),
109   - bars = r.set(v, h),
110   - drag,
111   - dragger = function (e) {
  118 + }
  119 + (function (c, x, y) {
  120 + top.push(r.circle(x, y, 10).attr({
  121 + fill: "#000",
  122 + opacity: 0,
  123 + cursor: "pointer"
  124 + })
  125 + .click(function(){
  126 + location.href = location.href.replace("graph", "commits/" + c.id);
  127 + })
  128 + .hover(function () {
  129 + // Create empty node to convert entities to character
  130 + var m = $('<div />').html(c.message).text()
  131 + , s = r.text(100, 100, c.author + "\n \n" + c.id + "\n \n" + m).attr({
  132 + fill: "#fff"
  133 + });
  134 + this.popup = r.popupit(x, y + 5, s, 0);
  135 + top.push(this.popup.insertBefore(this));
  136 + }, function () {
  137 + this.popup && this.popup.remove() && delete this.popup;
  138 + }));
  139 + }(this.commits[i], x, y));
  140 +
  141 + top.toFront();
  142 + var hw = holder.offsetWidth
  143 + , hh = holder.offsetHeight
  144 + , v = r.rect(hw - 8, 0, 4, Math.pow(hh, 2) / ch, 2).attr({
  145 + fill: "#000",
  146 + opacity: 0
  147 + })
  148 + , h = r.rect(0, hh - 8, Math.pow(hw, 2) / cw, 4, 2).attr({
  149 + fill: "#000",
  150 + opacity: 0
  151 + })
  152 + , bars = r.set(v, h)
  153 + , drag
  154 + , dragger = function (event) {
112 155 if (drag) {
113   - e = e || window.event;
114   - holder.scrollLeft = drag.sl - (e.clientX - drag.x);
115   - holder.scrollTop = drag.st - (e.clientY - drag.y);
  156 + event = event || window.event;
  157 + holder.scrollLeft = drag.sl - (event.clientX - drag.x);
  158 + holder.scrollTop = drag.st - (event.clientY - drag.y);
116 159 }
117 160 };
118   - holder.onmousedown = function (e) {
119   - e = e || window.event;
120   - drag = {x: e.clientX, y: e.clientY, st: holder.scrollTop, sl: holder.scrollLeft};
  161 + holder.onmousedown = function (event) {
  162 + event = event || window.event;
  163 + drag = {
  164 + x: event.clientX,
  165 + y: event.clientY,
  166 + st: holder.scrollTop,
  167 + sl: holder.scrollLeft
  168 + };
121 169 document.onmousemove = dragger;
122 170 bars.animate({opacity: .5}, 300);
123   - };
124   - document.onmouseup = function () {
  171 + };
  172 + document.onmouseup = function () {
125 173 drag = false;
126 174 document.onmousemove = null;
127 175 bars.animate({opacity: 0}, 300);
128   - };
129   - holder.scrollLeft = cw;
130   -};
  176 + };
  177 + holder.scrollLeft = cw;
  178 + }
  179 + };
  180 +
  181 + this.BranchGraph = BranchGraph;
  182 +
  183 +}(this);
131 184 Raphael.fn.popupit = function (x, y, set, dir, size) {
132 185 dir = dir == null ? 2 : dir;
133 186 size = size || 5;
134 187 x = Math.round(x);
135 188 y = Math.round(y);
136   - var bb = set.getBBox(),
  189 + var mmax = Math.max,
  190 + bb = set.getBBox(),
137 191 w = Math.round(bb.width / 2),
138 192 h = Math.round(bb.height / 2),
139 193 dx = [0, w + size * 2, 0, -w - size * 2],
... ...