Commit 351c952192da7cdd088fe693289e96d945dcafe3

Authored by Sato Hiroyuki
1 parent 96a58421

Refactor: extract method.

Showing 1 changed file with 63 additions and 53 deletions   Show diff stats
app/assets/javascripts/branch-graph.js.coffee
@@ -53,10 +53,10 @@ class BranchGraph @@ -53,10 +53,10 @@ class BranchGraph
53 top = r.set() 53 top = r.set()
54 cuday = 0 54 cuday = 0
55 cumonth = "" 55 cumonth = ""
56 - offsetX = 20  
57 - offsetY = 60 56 + @offsetX = 20
  57 + @offsetY = 60
58 barWidth = Math.max(graphWidth, @days.length * 20 + 320) 58 barWidth = Math.max(graphWidth, @days.length * 20 + 320)
59 - scrollLeft = cw 59 + @scrollLeft = cw
60 @raphael = r 60 @raphael = r
61 r.rect(0, 0, barWidth, 20).attr fill: "#222" 61 r.rect(0, 0, barWidth, 20).attr fill: "#222"
62 r.rect(0, 20, barWidth, 20).attr fill: "#444" 62 r.rect(0, 20, barWidth, 20).attr fill: "#444"
@@ -64,7 +64,7 @@ class BranchGraph @@ -64,7 +64,7 @@ class BranchGraph
64 for day, mm in @days 64 for day, mm in @days
65 if cuday isnt day[0] 65 if cuday isnt day[0]
66 # Dates 66 # Dates
67 - r.text(offsetX + mm * 20, 31, day[0]) 67 + r.text(@offsetX + mm * 20, 31, day[0])
68 .attr( 68 .attr(
69 font: "12px Monaco, monospace" 69 font: "12px Monaco, monospace"
70 fill: "#DDD" 70 fill: "#DDD"
@@ -73,7 +73,7 @@ class BranchGraph @@ -73,7 +73,7 @@ class BranchGraph
73 73
74 if cumonth isnt day[1] 74 if cumonth isnt day[1]
75 # Months 75 # Months
76 - r.text(offsetX + mm * 20, 11, day[1]) 76 + r.text(@offsetX + mm * 20, 11, day[1])
77 .attr( 77 .attr(
78 font: "12px Monaco, monospace" 78 font: "12px Monaco, monospace"
79 fill: "#EEE" 79 fill: "#EEE"
@@ -81,61 +81,21 @@ class BranchGraph @@ -81,61 +81,21 @@ class BranchGraph
81 cumonth = day[1] 81 cumonth = day[1]
82 82
83 for commit in @commits 83 for commit in @commits
84 - x = offsetX + 20 * commit.time  
85 - y = offsetY + 10 * commit.space  
86 - # Draw dot  
87 - r.circle(x, y, 3).attr(  
88 - fill: @colors[commit.space]  
89 - stroke: "none"  
90 - ) 84 + x = @offsetX + 20 * commit.time
  85 + y = @offsetY + 10 * commit.space
91 86
92 - # Draw lines  
93 - for parent in commit.parents  
94 - parentCommit = @preparedCommits[parent[0]]  
95 - parentX = offsetX + 20 * parentCommit.time  
96 - parentY1 = offsetY + 10 * parentCommit.space  
97 - parentY2 = offsetY + 10 * parent[1]  
98 - if parentCommit.space is commit.space and parentCommit.space is parent[1]  
99 - r.path(["M", x, y, "L", parentX, parentY1]).attr(  
100 - stroke: @colors[parentCommit.space]  
101 - "stroke-width": 2  
102 - ) 87 + @drawDot(x, y, commit)
103 88
104 - else if parentCommit.space < commit.space  
105 - if y is parentY2  
106 - r.path(["M", x - 5, y, "l-5,-2,0,4,5,-2", "L", x - 10, y, "L", x - 15, parentY2, "L", parentX + 5, parentY2, "L", parentX, parentY1]).attr(  
107 - stroke: @colors[commit.space]  
108 - "stroke-width": 2  
109 - ) 89 + @drawLines(x, y, commit)
110 90
111 - else  
112 - r.path(["M", x - 3, y - 6, "l-4,-3,4,-2,0,5", "L", x - 5, y - 10, "L", x - 10, parentY2, "L", parentX + 5, parentY2, "L", parentX, parentY1]).attr(  
113 - stroke: @colors[commit.space]  
114 - "stroke-width": 2  
115 - ) 91 + @appendLabel(x, y, commit.refs) if commit.refs
116 92
117 - else  
118 - r.path(["M", x - 3, y + 6, "l-4,3,4,2,0,-5", "L", x - 5, y + 10, "L", x - 10, parentY2, "L", parentX + 5, parentY2, "L", parentX, parentY1]).attr(  
119 - stroke: @colors[parentCommit.space]  
120 - "stroke-width": 2  
121 - ) 93 + @appendAnchor(top, commit, x, y)
122 94
123 - @appendLabel x, y, commit.refs if commit.refs  
124 -  
125 - # Mark commit and displayed in the center  
126 - if commit.id is @options.commit_id  
127 - r.path(["M", x, y - 5, "L", x + 4, y - 15, "L", x - 4, y - 15, "Z"]).attr(  
128 - fill: "#000"  
129 - "fill-opacity": .7  
130 - stroke: "none"  
131 - )  
132 -  
133 - scrollLeft = x - graphWidth / 2  
134 -  
135 - @appendAnchor top, commit, x, y 95 + @markCommit(x, y, commit, graphWidth)
136 96
137 top.toFront() 97 top.toFront()
138 - @element.scrollLeft scrollLeft 98 + @element.scrollLeft @scrollLeft
139 @bindEvents() 99 @bindEvents()
140 100
141 bindEvents: -> 101 bindEvents: ->
@@ -211,6 +171,56 @@ class BranchGraph @@ -211,6 +171,56 @@ class BranchGraph
211 ) 171 )
212 top.push anchor 172 top.push anchor
213 173
  174 + drawDot: (x, y, commit) ->
  175 + r = @raphael
  176 + r.circle(x, y, 3).attr(
  177 + fill: @colors[commit.space]
  178 + stroke: "none"
  179 + )
  180 +
  181 + drawLines: (x, y, commit) ->
  182 + r = @raphael
  183 + for parent in commit.parents
  184 + parentCommit = @preparedCommits[parent[0]]
  185 + parentX = @offsetX + 20 * parentCommit.time
  186 + parentY1 = @offsetY + 10 * parentCommit.space
  187 + parentY2 = @offsetY + 10 * parent[1]
  188 + if parentCommit.space is commit.space and parentCommit.space is parent[1]
  189 + r.path(["M", x, y, "L", parentX, parentY1]).attr(
  190 + stroke: @colors[parentCommit.space]
  191 + "stroke-width": 2
  192 + )
  193 +
  194 + else if parentCommit.space < commit.space
  195 + if y is parentY2
  196 + r.path(["M", x - 5, y, "l-5,-2,0,4,5,-2", "L", x - 10, y, "L", x - 15, parentY2, "L", parentX + 5, parentY2, "L", parentX, parentY1]).attr(
  197 + stroke: @colors[commit.space]
  198 + "stroke-width": 2
  199 + )
  200 +
  201 + else
  202 + r.path(["M", x - 3, y - 6, "l-4,-3,4,-2,0,5", "L", x - 5, y - 10, "L", x - 10, parentY2, "L", parentX + 5, parentY2, "L", parentX, parentY1]).attr(
  203 + stroke: @colors[commit.space]
  204 + "stroke-width": 2
  205 + )
  206 +
  207 + else
  208 + r.path(["M", x - 3, y + 6, "l-4,3,4,2,0,-5", "L", x - 5, y + 10, "L", x - 10, parentY2, "L", parentX + 5, parentY2, "L", parentX, parentY1]).attr(
  209 + stroke: @colors[parentCommit.space]
  210 + "stroke-width": 2
  211 + )
  212 +
  213 + markCommit: (x, y, commit, graphWidth) ->
  214 + if commit.id is @options.commit_id
  215 + r = @raphael
  216 + r.path(["M", x, y - 5, "L", x + 4, y - 15, "L", x - 4, y - 15, "Z"]).attr(
  217 + fill: "#000"
  218 + "fill-opacity": .7
  219 + stroke: "none"
  220 + )
  221 + # Displayed in the center
  222 + @scrollLeft = x - graphWidth / 2
  223 +
214 Raphael::commitTooltip = (x, y, commit) -> 224 Raphael::commitTooltip = (x, y, commit) ->
215 boxWidth = 300 225 boxWidth = 300
216 boxHeight = 200 226 boxHeight = 200