Commit 916c61fc64292bf4ae2fd84cbbbc86a5afa943bf

Authored by Sato Hiroyuki
1 parent 1f6b6b6c

Refactor: clean up code.

Showing 1 changed file with 21 additions and 20 deletions   Show diff stats
app/assets/javascripts/branch-graph.js.coffee
@@ -5,6 +5,10 @@ class BranchGraph @@ -5,6 +5,10 @@ class BranchGraph
5 @mspace = 0 5 @mspace = 0
6 @parents = {} 6 @parents = {}
7 @colors = ["#000"] 7 @colors = ["#000"]
  8 + @offsetX = 120
  9 + @offsetY = 20
  10 + @unitTime = 20
  11 + @unitSpace = 10
8 @load() 12 @load()
9 13
10 load: -> 14 load: ->
@@ -47,24 +51,21 @@ class BranchGraph @@ -47,24 +51,21 @@ class BranchGraph
47 buildGraph: -> 51 buildGraph: ->
48 graphHeight = $(@element).height() 52 graphHeight = $(@element).height()
49 graphWidth = $(@element).width() 53 graphWidth = $(@element).width()
50 - ch = Math.max(graphHeight, @mtime * 20 + 100)  
51 - cw = Math.max(graphWidth, @mspace * 10 + 260)  
52 - r = Raphael(@element.get(0), cw, ch) 54 + ch = Math.max(graphHeight, @unitTime * @mtime + 100)
  55 + cw = Math.max(graphWidth, @unitSpace * @mspace + 260)
  56 + @r = r = Raphael(@element.get(0), cw, ch)
53 top = r.set() 57 top = r.set()
54 cuday = 0 58 cuday = 0
55 cumonth = "" 59 cumonth = ""
56 - @offsetX = 120  
57 - @offsetY = 20  
58 - barHeight = Math.max(graphHeight, @days.length * 20 + 320)  
59 - @scrollLeft = cw  
60 - @raphael = r 60 + barHeight = Math.max(graphHeight, @unitTime * @days.length + 320)
  61 +
61 r.rect(0, 0, 20, barHeight).attr fill: "#222" 62 r.rect(0, 0, 20, barHeight).attr fill: "#222"
62 r.rect(20, 0, 20, barHeight).attr fill: "#444" 63 r.rect(20, 0, 20, barHeight).attr fill: "#444"
63 64
64 for day, mm in @days 65 for day, mm in @days
65 if cuday isnt day[0] 66 if cuday isnt day[0]
66 # Dates 67 # Dates
67 - r.text(30, @offsetY + mm * 20, day[0]) 68 + r.text(30, @offsetY + @unitTime * mm, day[0])
68 .attr( 69 .attr(
69 font: "12px Monaco, monospace" 70 font: "12px Monaco, monospace"
70 fill: "#DDD" 71 fill: "#DDD"
@@ -73,7 +74,7 @@ class BranchGraph @@ -73,7 +74,7 @@ class BranchGraph
73 74
74 if cumonth isnt day[1] 75 if cumonth isnt day[1]
75 # Months 76 # Months
76 - r.text(10, @offsetY + mm * 20, day[1]) 77 + r.text(10, @offsetY + @unitTime * mm, day[1])
77 .attr( 78 .attr(
78 font: "12px Monaco, monospace" 79 font: "12px Monaco, monospace"
79 fill: "#EEE" 80 fill: "#EEE"
@@ -81,8 +82,8 @@ class BranchGraph @@ -81,8 +82,8 @@ class BranchGraph
81 cumonth = day[1] 82 cumonth = day[1]
82 83
83 for commit in @commits 84 for commit in @commits
84 - x = @offsetX + 10 * (@mspace - commit.space)  
85 - y = @offsetY + 20 * commit.time 85 + x = @offsetX + @unitSpace * (@mspace - commit.space)
  86 + y = @offsetY + @unitTime * commit.time
86 87
87 @drawDot(x, y, commit) 88 @drawDot(x, y, commit)
88 89
@@ -126,7 +127,7 @@ class BranchGraph @@ -126,7 +127,7 @@ class BranchGraph
126 element.scrollTop element.scrollTop() + 50 if event.keyCode is 40 127 element.scrollTop element.scrollTop() + 50 if event.keyCode is 40
127 128
128 appendLabel: (x, y, refs) -> 129 appendLabel: (x, y, refs) ->
129 - r = @raphael 130 + r = @r
130 shortrefs = refs 131 shortrefs = refs
131 # Truncate if longer than 15 chars 132 # Truncate if longer than 15 chars
132 shortrefs = shortrefs.substr(0, 15) + "…" if shortrefs.length > 17 133 shortrefs = shortrefs.substr(0, 15) + "…" if shortrefs.length > 17
@@ -156,7 +157,7 @@ class BranchGraph @@ -156,7 +157,7 @@ class BranchGraph
156 text.toFront() 157 text.toFront()
157 158
158 appendAnchor: (top, commit, x, y) -> 159 appendAnchor: (top, commit, x, y) ->
159 - r = @raphael 160 + r = @r
160 options = @options 161 options = @options
161 anchor = r.circle(x, y, 10).attr( 162 anchor = r.circle(x, y, 10).attr(
162 fill: "#000" 163 fill: "#000"
@@ -173,19 +174,19 @@ class BranchGraph @@ -173,19 +174,19 @@ class BranchGraph
173 top.push anchor 174 top.push anchor
174 175
175 drawDot: (x, y, commit) -> 176 drawDot: (x, y, commit) ->
176 - r = @raphael 177 + r = @r
177 r.circle(x, y, 3).attr( 178 r.circle(x, y, 3).attr(
178 fill: @colors[commit.space] 179 fill: @colors[commit.space]
179 stroke: "none" 180 stroke: "none"
180 ) 181 )
181 182
182 drawLines: (x, y, commit) -> 183 drawLines: (x, y, commit) ->
183 - r = @raphael 184 + r = @r
184 for parent in commit.parents 185 for parent in commit.parents
185 parentCommit = @preparedCommits[parent[0]] 186 parentCommit = @preparedCommits[parent[0]]
186 - parentY = @offsetY + 20 * parentCommit.time  
187 - parentX1 = @offsetX + 10 * (@mspace - parentCommit.space)  
188 - parentX2 = @offsetX + 10 * (@mspace - parent[1]) 187 + parentY = @offsetY + @unitTime * parentCommit.time
  188 + parentX1 = @offsetX + @unitSpace * (@mspace - parentCommit.space)
  189 + parentX2 = @offsetX + @unitSpace * (@mspace - parent[1])
189 190
190 if parentCommit.space is commit.space and parentCommit.space is parent[1] 191 if parentCommit.space is commit.space and parentCommit.space is parent[1]
191 r.path(["M", x, y, "L", parentX1, parentY]).attr( 192 r.path(["M", x, y, "L", parentX1, parentY]).attr(
@@ -235,7 +236,7 @@ class BranchGraph @@ -235,7 +236,7 @@ class BranchGraph
235 236
236 markCommit: (x, y, commit, graphHeight) -> 237 markCommit: (x, y, commit, graphHeight) ->
237 if commit.id is @options.commit_id 238 if commit.id is @options.commit_id
238 - r = @raphael 239 + r = @r
239 r.path(["M", x + 5, y, "L", x + 15, y + 4, "L", x + 15, y - 4, "Z"]).attr( 240 r.path(["M", x + 5, y, "L", x + 15, y + 4, "L", x + 15, y - 4, "Z"]).attr(
240 fill: "#000" 241 fill: "#000"
241 "fill-opacity": .5 242 "fill-opacity": .5