Commit 85c468ec480a1541de36da07fc5fb4ca73c9ad5e

Authored by Sato Hiroyuki
1 parent 63b58b94

Render graph partially.

Showing 1 changed file with 55 additions and 27 deletions   Show diff stats
app/assets/javascripts/branch-graph.js.coffee
@@ -9,6 +9,7 @@ class BranchGraph @@ -9,6 +9,7 @@ class BranchGraph
9 @offsetY = 20 9 @offsetY = 20
10 @unitTime = 30 10 @unitTime = 30
11 @unitSpace = 10 11 @unitSpace = 10
  12 + @prev_start = -1
12 @load() 13 @load()
13 14
14 load: -> 15 load: ->
@@ -24,10 +25,18 @@ class BranchGraph @@ -24,10 +25,18 @@ class BranchGraph
24 25
25 prepareData: (@days, @commits) -> 26 prepareData: (@days, @commits) ->
26 @collectParents() 27 @collectParents()
  28 + @graphHeight = $(@element).height()
  29 + @graphWidth = $(@element).width()
  30 + ch = Math.max(@graphHeight, @offsetY + @unitTime * @mtime + 150)
  31 + cw = Math.max(@graphWidth, @offsetX + @unitSpace * @mspace + 300)
  32 + @r = Raphael(@element.get(0), cw, ch)
  33 + @top = @r.set()
  34 + @barHeight = Math.max(@graphHeight, @unitTime * @days.length + 320)
27 35
28 for c in @commits 36 for c in @commits
29 c.isParent = true if c.id of @parents 37 c.isParent = true if c.id of @parents
30 @preparedCommits[c.id] = c 38 @preparedCommits[c.id] = c
  39 + @markCommit(c)
31 40
32 @collectColors() 41 @collectColors()
33 42
@@ -49,18 +58,12 @@ class BranchGraph @@ -49,18 +58,12 @@ class BranchGraph
49 k++ 58 k++
50 59
51 buildGraph: -> 60 buildGraph: ->
52 - graphHeight = $(@element).height()  
53 - graphWidth = $(@element).width()  
54 - ch = Math.max(graphHeight, @offsetY + @unitTime * @mtime + 150)  
55 - cw = Math.max(graphWidth, @offsetX + @unitSpace * @mspace + 300)  
56 - @r = r = Raphael(@element.get(0), cw, ch)  
57 - top = r.set() 61 + r = @r
58 cuday = 0 62 cuday = 0
59 cumonth = "" 63 cumonth = ""
60 - barHeight = Math.max(graphHeight, @unitTime * @days.length + 320)  
61 64
62 - r.rect(0, 0, 26, barHeight).attr fill: "#222"  
63 - r.rect(26, 0, 20, barHeight).attr fill: "#444" 65 + r.rect(0, 0, 26, @barHeight).attr fill: "#222"
  66 + r.rect(26, 0, 20, @barHeight).attr fill: "#444"
64 67
65 for day, mm in @days 68 for day, mm in @days
66 if cuday isnt day[0] 69 if cuday isnt day[0]
@@ -81,22 +84,40 @@ class BranchGraph @@ -81,22 +84,40 @@ class BranchGraph
81 ) 84 )
82 cumonth = day[1] 85 cumonth = day[1]
83 86
84 - for commit in @commits  
85 - x = @offsetX + @unitSpace * (@mspace - commit.space)  
86 - y = @offsetY + @unitTime * commit.time 87 + @renderPartialGraph()
87 88
88 - @drawDot(x, y, commit) 89 + @bindEvents()
89 90
90 - @drawLines(x, y, commit) 91 + renderPartialGraph: ->
  92 + start = Math.floor((@element.scrollTop() - @offsetY) / @unitTime) - 10
  93 + start = 0 if start < 0
  94 + end = start + 40
  95 + end = @commits.length if @commits.length < end
91 96
92 - @appendLabel(x, y, commit.refs) if commit.refs 97 + if @prev_start == -1 or Math.abs(@prev_start - start) > 10
  98 + i = start
93 99
94 - @appendAnchor(top, commit, x, y) 100 + @prev_start = start
95 101
96 - @markCommit(x, y, commit, graphHeight) 102 + while i < end
  103 + commit = @commits[i]
  104 + i += 1
97 105
98 - top.toFront()  
99 - @bindEvents() 106 + if commit.hasDrawn isnt true
  107 + x = @offsetX + @unitSpace * (@mspace - commit.space)
  108 + y = @offsetY + @unitTime * commit.time
  109 +
  110 + @drawDot(x, y, commit)
  111 +
  112 + @drawLines(x, y, commit)
  113 +
  114 + @appendLabel(x, y, commit)
  115 +
  116 + @appendAnchor(x, y, commit)
  117 +
  118 + commit.hasDrawn = true
  119 +
  120 + @top.toFront()
100 121
101 bindEvents: -> 122 bindEvents: ->
102 drag = {} 123 drag = {}
@@ -114,9 +135,10 @@ class BranchGraph @@ -114,9 +135,10 @@ class BranchGraph
114 $(window).on "mousemove", dragger 135 $(window).on "mousemove", dragger
115 136
116 $(window).on 137 $(window).on
117 - mouseup: -> 138 + mouseup: =>
118 $(window).off "mousemove", dragger 139 $(window).off "mousemove", dragger
119 - keydown: (event) -> 140 + @renderPartialGraph()
  141 + keydown: (event) =>
120 # left 142 # left
121 element.scrollLeft element.scrollLeft() - 50 if event.keyCode is 37 143 element.scrollLeft element.scrollLeft() - 50 if event.keyCode is 37
122 # top 144 # top
@@ -125,17 +147,20 @@ class BranchGraph @@ -125,17 +147,20 @@ class BranchGraph
125 element.scrollLeft element.scrollLeft() + 50 if event.keyCode is 39 147 element.scrollLeft element.scrollLeft() + 50 if event.keyCode is 39
126 # bottom 148 # bottom
127 element.scrollTop element.scrollTop() + 50 if event.keyCode is 40 149 element.scrollTop element.scrollTop() + 50 if event.keyCode is 40
  150 + @renderPartialGraph()
  151 +
  152 + appendLabel: (x, y, commit) ->
  153 + return unless commit.refs
128 154
129 - appendLabel: (x, y, refs) ->  
130 r = @r 155 r = @r
131 - shortrefs = refs 156 + shortrefs = commit.refs
132 # Truncate if longer than 15 chars 157 # Truncate if longer than 15 chars
133 shortrefs = shortrefs.substr(0, 15) + "…" if shortrefs.length > 17 158 shortrefs = shortrefs.substr(0, 15) + "…" if shortrefs.length > 17
134 text = r.text(x + 4, y, shortrefs).attr( 159 text = r.text(x + 4, y, shortrefs).attr(
135 "text-anchor": "start" 160 "text-anchor": "start"
136 font: "10px Monaco, monospace" 161 font: "10px Monaco, monospace"
137 fill: "#FFF" 162 fill: "#FFF"
138 - title: refs 163 + title: commit.refs
139 ) 164 )
140 textbox = text.getBBox() 165 textbox = text.getBBox()
141 # Create rectangle based on the size of the textbox 166 # Create rectangle based on the size of the textbox
@@ -156,8 +181,9 @@ class BranchGraph @@ -156,8 +181,9 @@ class BranchGraph
156 # Set text to front 181 # Set text to front
157 text.toFront() 182 text.toFront()
158 183
159 - appendAnchor: (top, commit, x, y) -> 184 + appendAnchor: (x, y, commit) ->
160 r = @r 185 r = @r
  186 + top = @top
161 options = @options 187 options = @options
162 anchor = r.circle(x, y, 10).attr( 188 anchor = r.circle(x, y, 10).attr(
163 fill: "#000" 189 fill: "#000"
@@ -240,16 +266,18 @@ class BranchGraph @@ -240,16 +266,18 @@ class BranchGraph
240 stroke: color 266 stroke: color
241 "stroke-width": 2) 267 "stroke-width": 2)
242 268
243 - markCommit: (x, y, commit, graphHeight) -> 269 + markCommit: (commit) ->
244 if commit.id is @options.commit_id 270 if commit.id is @options.commit_id
245 r = @r 271 r = @r
  272 + x = @offsetX + @unitSpace * (@mspace - commit.space)
  273 + y = @offsetY + @unitTime * commit.time
246 r.path(["M", x + 5, y, "L", x + 15, y + 4, "L", x + 15, y - 4, "Z"]).attr( 274 r.path(["M", x + 5, y, "L", x + 15, y + 4, "L", x + 15, y - 4, "Z"]).attr(
247 fill: "#000" 275 fill: "#000"
248 "fill-opacity": .5 276 "fill-opacity": .5
249 stroke: "none" 277 stroke: "none"
250 ) 278 )
251 # Displayed in the center 279 # Displayed in the center
252 - @element.scrollTop(y - graphHeight / 2) 280 + @element.scrollTop(y - @graphHeight / 2)
253 281
254 Raphael::commitTooltip = (x, y, commit) -> 282 Raphael::commitTooltip = (x, y, commit) ->
255 boxWidth = 300 283 boxWidth = 300