##// END OF EJS Templates
Use JSON so we don't have to parse data-rels manually (#3436)....
Jean-Philippe Lang -
r10889:59ddbf8c09a7
parent child
Show More
@@ -1,922 +1,918
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module Helpers
20 20 # Simple class to handle gantt chart data
21 21 class Gantt
22 22 include ERB::Util
23 23 include Redmine::I18n
24 24 include Redmine::Utils::DateCalculation
25 25
26 26 # Relation types that are rendered
27 27 DRAW_TYPES = {
28 28 IssueRelation::TYPE_BLOCKS => { :landscape_margin => 16, :color => '#F34F4F' },
29 29 IssueRelation::TYPE_PRECEDES => { :landscape_margin => 20, :color => '#628FEA' }
30 30 }.freeze
31 31
32 32 # :nodoc:
33 33 # Some utility methods for the PDF export
34 34 class PDF
35 35 MaxCharactorsForSubject = 45
36 36 TotalWidth = 280
37 37 LeftPaneWidth = 100
38 38
39 39 def self.right_pane_width
40 40 TotalWidth - LeftPaneWidth
41 41 end
42 42 end
43 43
44 44 attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months, :truncated, :max_rows
45 45 attr_accessor :query
46 46 attr_accessor :project
47 47 attr_accessor :view
48 48
49 49 def initialize(options={})
50 50 options = options.dup
51 51 if options[:year] && options[:year].to_i >0
52 52 @year_from = options[:year].to_i
53 53 if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12
54 54 @month_from = options[:month].to_i
55 55 else
56 56 @month_from = 1
57 57 end
58 58 else
59 59 @month_from ||= Date.today.month
60 60 @year_from ||= Date.today.year
61 61 end
62 62 zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i
63 63 @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
64 64 months = (options[:months] || User.current.pref[:gantt_months]).to_i
65 65 @months = (months > 0 && months < 25) ? months : 6
66 66 # Save gantt parameters as user preference (zoom and months count)
67 67 if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] ||
68 68 @months != User.current.pref[:gantt_months]))
69 69 User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
70 70 User.current.preference.save
71 71 end
72 72 @date_from = Date.civil(@year_from, @month_from, 1)
73 73 @date_to = (@date_from >> @months) - 1
74 74 @subjects = ''
75 75 @lines = ''
76 76 @number_of_rows = nil
77 77 @issue_ancestors = []
78 78 @truncated = false
79 79 if options.has_key?(:max_rows)
80 80 @max_rows = options[:max_rows]
81 81 else
82 82 @max_rows = Setting.gantt_items_limit.blank? ? nil : Setting.gantt_items_limit.to_i
83 83 end
84 84 end
85 85
86 86 def common_params
87 87 { :controller => 'gantts', :action => 'show', :project_id => @project }
88 88 end
89 89
90 90 def params
91 91 common_params.merge({:zoom => zoom, :year => year_from,
92 92 :month => month_from, :months => months})
93 93 end
94 94
95 95 def params_previous
96 96 common_params.merge({:year => (date_from << months).year,
97 97 :month => (date_from << months).month,
98 98 :zoom => zoom, :months => months})
99 99 end
100 100
101 101 def params_next
102 102 common_params.merge({:year => (date_from >> months).year,
103 103 :month => (date_from >> months).month,
104 104 :zoom => zoom, :months => months})
105 105 end
106 106
107 107 # Returns the number of rows that will be rendered on the Gantt chart
108 108 def number_of_rows
109 109 return @number_of_rows if @number_of_rows
110 110 rows = projects.inject(0) {|total, p| total += number_of_rows_on_project(p)}
111 111 rows > @max_rows ? @max_rows : rows
112 112 end
113 113
114 114 # Returns the number of rows that will be used to list a project on
115 115 # the Gantt chart. This will recurse for each subproject.
116 116 def number_of_rows_on_project(project)
117 117 return 0 unless projects.include?(project)
118 118 count = 1
119 119 count += project_issues(project).size
120 120 count += project_versions(project).size
121 121 count
122 122 end
123 123
124 124 # Renders the subjects of the Gantt chart, the left side.
125 125 def subjects(options={})
126 126 render(options.merge(:only => :subjects)) unless @subjects_rendered
127 127 @subjects
128 128 end
129 129
130 130 # Renders the lines of the Gantt chart, the right side
131 131 def lines(options={})
132 132 render(options.merge(:only => :lines)) unless @lines_rendered
133 133 @lines
134 134 end
135 135
136 136 # Returns issues that will be rendered
137 137 def issues
138 138 @issues ||= @query.issues(
139 139 :include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
140 140 :order => "#{Project.table_name}.lft ASC, #{Issue.table_name}.id ASC",
141 141 :limit => @max_rows
142 142 )
143 143 end
144 144
145 145 # Returns a hash of the relations between the issues that are present on the gantt
146 146 # and that should be displayed, grouped by issue ids.
147 147 def relations
148 148 return @relations if @relations
149 149 if issues.any?
150 150 issue_ids = issues.map(&:id)
151 151 @relations = IssueRelation.
152 152 where(:issue_from_id => issue_ids, :issue_to_id => issue_ids, :relation_type => DRAW_TYPES.keys).
153 153 group_by(&:issue_from_id)
154 154 else
155 155 @relations = {}
156 156 end
157 157 end
158 158
159 159 # Return all the project nodes that will be displayed
160 160 def projects
161 161 return @projects if @projects
162 162 ids = issues.collect(&:project).uniq.collect(&:id)
163 163 if ids.any?
164 164 # All issues projects and their visible ancestors
165 165 @projects = Project.visible.all(
166 166 :joins => "LEFT JOIN #{Project.table_name} child ON #{Project.table_name}.lft <= child.lft AND #{Project.table_name}.rgt >= child.rgt",
167 167 :conditions => ["child.id IN (?)", ids],
168 168 :order => "#{Project.table_name}.lft ASC"
169 169 ).uniq
170 170 else
171 171 @projects = []
172 172 end
173 173 end
174 174
175 175 # Returns the issues that belong to +project+
176 176 def project_issues(project)
177 177 @issues_by_project ||= issues.group_by(&:project)
178 178 @issues_by_project[project] || []
179 179 end
180 180
181 181 # Returns the distinct versions of the issues that belong to +project+
182 182 def project_versions(project)
183 183 project_issues(project).collect(&:fixed_version).compact.uniq
184 184 end
185 185
186 186 # Returns the issues that belong to +project+ and are assigned to +version+
187 187 def version_issues(project, version)
188 188 project_issues(project).select {|issue| issue.fixed_version == version}
189 189 end
190 190
191 191 def render(options={})
192 192 options = {:top => 0, :top_increment => 20,
193 193 :indent_increment => 20, :render => :subject,
194 194 :format => :html}.merge(options)
195 195 indent = options[:indent] || 4
196 196 @subjects = '' unless options[:only] == :lines
197 197 @lines = '' unless options[:only] == :subjects
198 198 @number_of_rows = 0
199 199 Project.project_tree(projects) do |project, level|
200 200 options[:indent] = indent + level * options[:indent_increment]
201 201 render_project(project, options)
202 202 break if abort?
203 203 end
204 204 @subjects_rendered = true unless options[:only] == :lines
205 205 @lines_rendered = true unless options[:only] == :subjects
206 206 render_end(options)
207 207 end
208 208
209 209 def render_project(project, options={})
210 210 subject_for_project(project, options) unless options[:only] == :lines
211 211 line_for_project(project, options) unless options[:only] == :subjects
212 212 options[:top] += options[:top_increment]
213 213 options[:indent] += options[:indent_increment]
214 214 @number_of_rows += 1
215 215 return if abort?
216 216 issues = project_issues(project).select {|i| i.fixed_version.nil?}
217 217 sort_issues!(issues)
218 218 if issues
219 219 render_issues(issues, options)
220 220 return if abort?
221 221 end
222 222 versions = project_versions(project)
223 223 versions.each do |version|
224 224 render_version(project, version, options)
225 225 end
226 226 # Remove indent to hit the next sibling
227 227 options[:indent] -= options[:indent_increment]
228 228 end
229 229
230 230 def render_issues(issues, options={})
231 231 @issue_ancestors = []
232 232 issues.each do |i|
233 233 subject_for_issue(i, options) unless options[:only] == :lines
234 234 line_for_issue(i, options) unless options[:only] == :subjects
235 235 options[:top] += options[:top_increment]
236 236 @number_of_rows += 1
237 237 break if abort?
238 238 end
239 239 options[:indent] -= (options[:indent_increment] * @issue_ancestors.size)
240 240 end
241 241
242 242 def render_version(project, version, options={})
243 243 # Version header
244 244 subject_for_version(version, options) unless options[:only] == :lines
245 245 line_for_version(version, options) unless options[:only] == :subjects
246 246 options[:top] += options[:top_increment]
247 247 @number_of_rows += 1
248 248 return if abort?
249 249 issues = version_issues(project, version)
250 250 if issues
251 251 sort_issues!(issues)
252 252 # Indent issues
253 253 options[:indent] += options[:indent_increment]
254 254 render_issues(issues, options)
255 255 options[:indent] -= options[:indent_increment]
256 256 end
257 257 end
258 258
259 259 def render_end(options={})
260 260 case options[:format]
261 261 when :pdf
262 262 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
263 263 end
264 264 end
265 265
266 266 def subject_for_project(project, options)
267 267 case options[:format]
268 268 when :html
269 269 html_class = ""
270 270 html_class << 'icon icon-projects '
271 271 html_class << (project.overdue? ? 'project-overdue' : '')
272 272 s = view.link_to_project(project).html_safe
273 273 subject = view.content_tag(:span, s,
274 274 :class => html_class).html_safe
275 275 html_subject(options, subject, :css => "project-name")
276 276 when :image
277 277 image_subject(options, project.name)
278 278 when :pdf
279 279 pdf_new_page?(options)
280 280 pdf_subject(options, project.name)
281 281 end
282 282 end
283 283
284 284 def line_for_project(project, options)
285 285 # Skip versions that don't have a start_date or due date
286 286 if project.is_a?(Project) && project.start_date && project.due_date
287 287 options[:zoom] ||= 1
288 288 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
289 289 coords = coordinates(project.start_date, project.due_date, nil, options[:zoom])
290 290 label = h(project)
291 291 case options[:format]
292 292 when :html
293 293 html_task(options, coords, :css => "project task", :label => label, :markers => true)
294 294 when :image
295 295 image_task(options, coords, :label => label, :markers => true, :height => 3)
296 296 when :pdf
297 297 pdf_task(options, coords, :label => label, :markers => true, :height => 0.8)
298 298 end
299 299 else
300 300 ActiveRecord::Base.logger.debug "Gantt#line_for_project was not given a project with a start_date"
301 301 ''
302 302 end
303 303 end
304 304
305 305 def subject_for_version(version, options)
306 306 case options[:format]
307 307 when :html
308 308 html_class = ""
309 309 html_class << 'icon icon-package '
310 310 html_class << (version.behind_schedule? ? 'version-behind-schedule' : '') << " "
311 311 html_class << (version.overdue? ? 'version-overdue' : '')
312 312 s = view.link_to_version(version).html_safe
313 313 subject = view.content_tag(:span, s,
314 314 :class => html_class).html_safe
315 315 html_subject(options, subject, :css => "version-name")
316 316 when :image
317 317 image_subject(options, version.to_s_with_project)
318 318 when :pdf
319 319 pdf_new_page?(options)
320 320 pdf_subject(options, version.to_s_with_project)
321 321 end
322 322 end
323 323
324 324 def line_for_version(version, options)
325 325 # Skip versions that don't have a start_date
326 326 if version.is_a?(Version) && version.start_date && version.due_date
327 327 options[:zoom] ||= 1
328 328 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
329 329 coords = coordinates(version.start_date,
330 330 version.due_date, version.completed_percent,
331 331 options[:zoom])
332 332 label = "#{h version} #{h version.completed_percent.to_i.to_s}%"
333 333 label = h("#{version.project} -") + label unless @project && @project == version.project
334 334 case options[:format]
335 335 when :html
336 336 html_task(options, coords, :css => "version task", :label => label, :markers => true)
337 337 when :image
338 338 image_task(options, coords, :label => label, :markers => true, :height => 3)
339 339 when :pdf
340 340 pdf_task(options, coords, :label => label, :markers => true, :height => 0.8)
341 341 end
342 342 else
343 343 ActiveRecord::Base.logger.debug "Gantt#line_for_version was not given a version with a start_date"
344 344 ''
345 345 end
346 346 end
347 347
348 348 def subject_for_issue(issue, options)
349 349 while @issue_ancestors.any? && !issue.is_descendant_of?(@issue_ancestors.last)
350 350 @issue_ancestors.pop
351 351 options[:indent] -= options[:indent_increment]
352 352 end
353 353 output = case options[:format]
354 354 when :html
355 355 css_classes = ''
356 356 css_classes << ' issue-overdue' if issue.overdue?
357 357 css_classes << ' issue-behind-schedule' if issue.behind_schedule?
358 358 css_classes << ' icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to
359 359 s = "".html_safe
360 360 if issue.assigned_to.present?
361 361 assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
362 362 s << view.avatar(issue.assigned_to,
363 363 :class => 'gravatar icon-gravatar',
364 364 :size => 10,
365 365 :title => assigned_string).to_s.html_safe
366 366 end
367 367 s << view.link_to_issue(issue).html_safe
368 368 subject = view.content_tag(:span, s, :class => css_classes).html_safe
369 369 html_subject(options, subject, :css => "issue-subject",
370 370 :title => issue.subject) + "\n"
371 371 when :image
372 372 image_subject(options, issue.subject)
373 373 when :pdf
374 374 pdf_new_page?(options)
375 375 pdf_subject(options, issue.subject)
376 376 end
377 377 unless issue.leaf?
378 378 @issue_ancestors << issue
379 379 options[:indent] += options[:indent_increment]
380 380 end
381 381 output
382 382 end
383 383
384 384 def line_for_issue(issue, options)
385 385 # Skip issues that don't have a due_before (due_date or version's due_date)
386 386 if issue.is_a?(Issue) && issue.due_before
387 387 coords = coordinates(issue.start_date, issue.due_before, issue.done_ratio, options[:zoom])
388 388 label = "#{issue.status.name} #{issue.done_ratio}%"
389 389 case options[:format]
390 390 when :html
391 391 html_task(options, coords,
392 392 :css => "task " + (issue.leaf? ? 'leaf' : 'parent'),
393 393 :label => label, :issue => issue,
394 394 :markers => !issue.leaf?)
395 395 when :image
396 396 image_task(options, coords, :label => label)
397 397 when :pdf
398 398 pdf_task(options, coords, :label => label)
399 399 end
400 400 else
401 401 ActiveRecord::Base.logger.debug "GanttHelper#line_for_issue was not given an issue with a due_before"
402 402 ''
403 403 end
404 404 end
405 405
406 406 # Generates a gantt image
407 407 # Only defined if RMagick is avalaible
408 408 def to_image(format='PNG')
409 409 date_to = (@date_from >> @months) - 1
410 410 show_weeks = @zoom > 1
411 411 show_days = @zoom > 2
412 412 subject_width = 400
413 413 header_height = 18
414 414 # width of one day in pixels
415 415 zoom = @zoom * 2
416 416 g_width = (@date_to - @date_from + 1) * zoom
417 417 g_height = 20 * number_of_rows + 30
418 418 headers_height = (show_weeks ? 2 * header_height : header_height)
419 419 height = g_height + headers_height
420 420 imgl = Magick::ImageList.new
421 421 imgl.new_image(subject_width + g_width + 1, height)
422 422 gc = Magick::Draw.new
423 423 gc.font = Redmine::Configuration['rmagick_font_path'] || ""
424 424 # Subjects
425 425 gc.stroke('transparent')
426 426 subjects(:image => gc, :top => (headers_height + 20), :indent => 4, :format => :image)
427 427 # Months headers
428 428 month_f = @date_from
429 429 left = subject_width
430 430 @months.times do
431 431 width = ((month_f >> 1) - month_f) * zoom
432 432 gc.fill('white')
433 433 gc.stroke('grey')
434 434 gc.stroke_width(1)
435 435 gc.rectangle(left, 0, left + width, height)
436 436 gc.fill('black')
437 437 gc.stroke('transparent')
438 438 gc.stroke_width(1)
439 439 gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}")
440 440 left = left + width
441 441 month_f = month_f >> 1
442 442 end
443 443 # Weeks headers
444 444 if show_weeks
445 445 left = subject_width
446 446 height = header_height
447 447 if @date_from.cwday == 1
448 448 # date_from is monday
449 449 week_f = date_from
450 450 else
451 451 # find next monday after date_from
452 452 week_f = @date_from + (7 - @date_from.cwday + 1)
453 453 width = (7 - @date_from.cwday + 1) * zoom
454 454 gc.fill('white')
455 455 gc.stroke('grey')
456 456 gc.stroke_width(1)
457 457 gc.rectangle(left, header_height, left + width, 2 * header_height + g_height - 1)
458 458 left = left + width
459 459 end
460 460 while week_f <= date_to
461 461 width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom
462 462 gc.fill('white')
463 463 gc.stroke('grey')
464 464 gc.stroke_width(1)
465 465 gc.rectangle(left.round, header_height, left.round + width, 2 * header_height + g_height - 1)
466 466 gc.fill('black')
467 467 gc.stroke('transparent')
468 468 gc.stroke_width(1)
469 469 gc.text(left.round + 2, header_height + 14, week_f.cweek.to_s)
470 470 left = left + width
471 471 week_f = week_f + 7
472 472 end
473 473 end
474 474 # Days details (week-end in grey)
475 475 if show_days
476 476 left = subject_width
477 477 height = g_height + header_height - 1
478 478 wday = @date_from.cwday
479 479 (date_to - @date_from + 1).to_i.times do
480 480 width = zoom
481 481 gc.fill(non_working_week_days.include?(wday) ? '#eee' : 'white')
482 482 gc.stroke('#ddd')
483 483 gc.stroke_width(1)
484 484 gc.rectangle(left, 2 * header_height, left + width, 2 * header_height + g_height - 1)
485 485 left = left + width
486 486 wday = wday + 1
487 487 wday = 1 if wday > 7
488 488 end
489 489 end
490 490 # border
491 491 gc.fill('transparent')
492 492 gc.stroke('grey')
493 493 gc.stroke_width(1)
494 494 gc.rectangle(0, 0, subject_width + g_width, headers_height)
495 495 gc.stroke('black')
496 496 gc.rectangle(0, 0, subject_width + g_width, g_height + headers_height - 1)
497 497 # content
498 498 top = headers_height + 20
499 499 gc.stroke('transparent')
500 500 lines(:image => gc, :top => top, :zoom => zoom,
501 501 :subject_width => subject_width, :format => :image)
502 502 # today red line
503 503 if Date.today >= @date_from and Date.today <= date_to
504 504 gc.stroke('red')
505 505 x = (Date.today - @date_from + 1) * zoom + subject_width
506 506 gc.line(x, headers_height, x, headers_height + g_height - 1)
507 507 end
508 508 gc.draw(imgl)
509 509 imgl.format = format
510 510 imgl.to_blob
511 511 end if Object.const_defined?(:Magick)
512 512
513 513 def to_pdf
514 514 pdf = ::Redmine::Export::PDF::ITCPDF.new(current_language)
515 515 pdf.SetTitle("#{l(:label_gantt)} #{project}")
516 516 pdf.alias_nb_pages
517 517 pdf.footer_date = format_date(Date.today)
518 518 pdf.AddPage("L")
519 519 pdf.SetFontStyle('B', 12)
520 520 pdf.SetX(15)
521 521 pdf.RDMCell(PDF::LeftPaneWidth, 20, project.to_s)
522 522 pdf.Ln
523 523 pdf.SetFontStyle('B', 9)
524 524 subject_width = PDF::LeftPaneWidth
525 525 header_height = 5
526 526 headers_height = header_height
527 527 show_weeks = false
528 528 show_days = false
529 529 if self.months < 7
530 530 show_weeks = true
531 531 headers_height = 2 * header_height
532 532 if self.months < 3
533 533 show_days = true
534 534 headers_height = 3 * header_height
535 535 end
536 536 end
537 537 g_width = PDF.right_pane_width
538 538 zoom = (g_width) / (self.date_to - self.date_from + 1)
539 539 g_height = 120
540 540 t_height = g_height + headers_height
541 541 y_start = pdf.GetY
542 542 # Months headers
543 543 month_f = self.date_from
544 544 left = subject_width
545 545 height = header_height
546 546 self.months.times do
547 547 width = ((month_f >> 1) - month_f) * zoom
548 548 pdf.SetY(y_start)
549 549 pdf.SetX(left)
550 550 pdf.RDMCell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C")
551 551 left = left + width
552 552 month_f = month_f >> 1
553 553 end
554 554 # Weeks headers
555 555 if show_weeks
556 556 left = subject_width
557 557 height = header_height
558 558 if self.date_from.cwday == 1
559 559 # self.date_from is monday
560 560 week_f = self.date_from
561 561 else
562 562 # find next monday after self.date_from
563 563 week_f = self.date_from + (7 - self.date_from.cwday + 1)
564 564 width = (7 - self.date_from.cwday + 1) * zoom-1
565 565 pdf.SetY(y_start + header_height)
566 566 pdf.SetX(left)
567 567 pdf.RDMCell(width + 1, height, "", "LTR")
568 568 left = left + width + 1
569 569 end
570 570 while week_f <= self.date_to
571 571 width = (week_f + 6 <= self.date_to) ? 7 * zoom : (self.date_to - week_f + 1) * zoom
572 572 pdf.SetY(y_start + header_height)
573 573 pdf.SetX(left)
574 574 pdf.RDMCell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C")
575 575 left = left + width
576 576 week_f = week_f + 7
577 577 end
578 578 end
579 579 # Days headers
580 580 if show_days
581 581 left = subject_width
582 582 height = header_height
583 583 wday = self.date_from.cwday
584 584 pdf.SetFontStyle('B', 7)
585 585 (self.date_to - self.date_from + 1).to_i.times do
586 586 width = zoom
587 587 pdf.SetY(y_start + 2 * header_height)
588 588 pdf.SetX(left)
589 589 pdf.RDMCell(width, height, day_name(wday).first, "LTR", 0, "C")
590 590 left = left + width
591 591 wday = wday + 1
592 592 wday = 1 if wday > 7
593 593 end
594 594 end
595 595 pdf.SetY(y_start)
596 596 pdf.SetX(15)
597 597 pdf.RDMCell(subject_width + g_width - 15, headers_height, "", 1)
598 598 # Tasks
599 599 top = headers_height + y_start
600 600 options = {
601 601 :top => top,
602 602 :zoom => zoom,
603 603 :subject_width => subject_width,
604 604 :g_width => g_width,
605 605 :indent => 0,
606 606 :indent_increment => 5,
607 607 :top_increment => 5,
608 608 :format => :pdf,
609 609 :pdf => pdf
610 610 }
611 611 render(options)
612 612 pdf.Output
613 613 end
614 614
615 615 private
616 616
617 617 def coordinates(start_date, end_date, progress, zoom=nil)
618 618 zoom ||= @zoom
619 619 coords = {}
620 620 if start_date && end_date && start_date < self.date_to && end_date > self.date_from
621 621 if start_date > self.date_from
622 622 coords[:start] = start_date - self.date_from
623 623 coords[:bar_start] = start_date - self.date_from
624 624 else
625 625 coords[:bar_start] = 0
626 626 end
627 627 if end_date < self.date_to
628 628 coords[:end] = end_date - self.date_from
629 629 coords[:bar_end] = end_date - self.date_from + 1
630 630 else
631 631 coords[:bar_end] = self.date_to - self.date_from + 1
632 632 end
633 633 if progress
634 634 progress_date = start_date + (end_date - start_date + 1) * (progress / 100.0)
635 635 if progress_date > self.date_from && progress_date > start_date
636 636 if progress_date < self.date_to
637 637 coords[:bar_progress_end] = progress_date - self.date_from
638 638 else
639 639 coords[:bar_progress_end] = self.date_to - self.date_from + 1
640 640 end
641 641 end
642 642 if progress_date < Date.today
643 643 late_date = [Date.today, end_date].min
644 644 if late_date > self.date_from && late_date > start_date
645 645 if late_date < self.date_to
646 646 coords[:bar_late_end] = late_date - self.date_from + 1
647 647 else
648 648 coords[:bar_late_end] = self.date_to - self.date_from + 1
649 649 end
650 650 end
651 651 end
652 652 end
653 653 end
654 654 # Transforms dates into pixels witdh
655 655 coords.keys.each do |key|
656 656 coords[key] = (coords[key] * zoom).floor
657 657 end
658 658 coords
659 659 end
660 660
661 661 # Sorts a collection of issues by start_date, due_date, id for gantt rendering
662 662 def sort_issues!(issues)
663 663 issues.sort! { |a, b| gantt_issue_compare(a, b) }
664 664 end
665 665
666 666 # TODO: top level issues should be sorted by start date
667 667 def gantt_issue_compare(x, y)
668 668 if x.root_id == y.root_id
669 669 x.lft <=> y.lft
670 670 else
671 671 x.root_id <=> y.root_id
672 672 end
673 673 end
674 674
675 675 def current_limit
676 676 if @max_rows
677 677 @max_rows - @number_of_rows
678 678 else
679 679 nil
680 680 end
681 681 end
682 682
683 683 def abort?
684 684 if @max_rows && @number_of_rows >= @max_rows
685 685 @truncated = true
686 686 end
687 687 end
688 688
689 689 def pdf_new_page?(options)
690 690 if options[:top] > 180
691 691 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
692 692 options[:pdf].AddPage("L")
693 693 options[:top] = 15
694 694 options[:pdf].Line(15, options[:top] - 0.1, PDF::TotalWidth, options[:top] - 0.1)
695 695 end
696 696 end
697 697
698 698 def html_subject(params, subject, options={})
699 699 style = "position: absolute;top:#{params[:top]}px;left:#{params[:indent]}px;"
700 700 style << "width:#{params[:subject_width] - params[:indent]}px;" if params[:subject_width]
701 701 output = view.content_tag('div', subject,
702 702 :class => options[:css], :style => style,
703 703 :title => options[:title])
704 704 @subjects << output
705 705 output
706 706 end
707 707
708 708 def pdf_subject(params, subject, options={})
709 709 params[:pdf].SetY(params[:top])
710 710 params[:pdf].SetX(15)
711 711 char_limit = PDF::MaxCharactorsForSubject - params[:indent]
712 712 params[:pdf].RDMCell(params[:subject_width] - 15, 5,
713 713 (" " * params[:indent]) +
714 714 subject.to_s.sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'),
715 715 "LR")
716 716 params[:pdf].SetY(params[:top])
717 717 params[:pdf].SetX(params[:subject_width])
718 718 params[:pdf].RDMCell(params[:g_width], 5, "", "LR")
719 719 end
720 720
721 721 def image_subject(params, subject, options={})
722 722 params[:image].fill('black')
723 723 params[:image].stroke('transparent')
724 724 params[:image].stroke_width(1)
725 725 params[:image].text(params[:indent], params[:top] + 2, subject)
726 726 end
727 727
728 728 def issue_relations(issue)
729 729 rels = {}
730 730 if relations[issue.id]
731 731 relations[issue.id].each do |relation|
732 732 (rels[relation.relation_type] ||= []) << relation.issue_to_id
733 733 end
734 734 end
735 735 rels
736 736 end
737 737
738 738 def html_task(params, coords, options={})
739 739 output = ''
740 740 # Renders the task bar, with progress and late
741 741 if coords[:bar_start] && coords[:bar_end]
742 742 width = coords[:bar_end] - coords[:bar_start] - 2
743 743 style = ""
744 744 style << "top:#{params[:top]}px;"
745 745 style << "left:#{coords[:bar_start]}px;"
746 746 style << "width:#{width}px;"
747 747 html_id = "task-todo-issue-#{options[:issue].id}" if options[:issue]
748 748 content_opt = {:style => style,
749 749 :class => "#{options[:css]} task_todo",
750 750 :id => html_id}
751 751 if options[:issue]
752 rels_hash = {}
753 issue_relations(options[:issue]).each do |k, v|
754 rels_hash[k] = v.join(',')
755 end
756 content_opt[:data] = {"rels" => rels_hash}
752 content_opt[:data] = {"rels" => issue_relations(options[:issue]).to_json}
757 753 end
758 754 output << view.content_tag(:div, '&nbsp;'.html_safe, content_opt)
759 755 if coords[:bar_late_end]
760 756 width = coords[:bar_late_end] - coords[:bar_start] - 2
761 757 style = ""
762 758 style << "top:#{params[:top]}px;"
763 759 style << "left:#{coords[:bar_start]}px;"
764 760 style << "width:#{width}px;"
765 761 output << view.content_tag(:div, '&nbsp;'.html_safe,
766 762 :style => style,
767 763 :class => "#{options[:css]} task_late")
768 764 end
769 765 if coords[:bar_progress_end]
770 766 width = coords[:bar_progress_end] - coords[:bar_start] - 2
771 767 style = ""
772 768 style << "top:#{params[:top]}px;"
773 769 style << "left:#{coords[:bar_start]}px;"
774 770 style << "width:#{width}px;"
775 771 output << view.content_tag(:div, '&nbsp;'.html_safe,
776 772 :style => style,
777 773 :class => "#{options[:css]} task_done")
778 774 end
779 775 end
780 776 # Renders the markers
781 777 if options[:markers]
782 778 if coords[:start]
783 779 style = ""
784 780 style << "top:#{params[:top]}px;"
785 781 style << "left:#{coords[:start]}px;"
786 782 style << "width:15px;"
787 783 output << view.content_tag(:div, '&nbsp;'.html_safe,
788 784 :style => style,
789 785 :class => "#{options[:css]} marker starting")
790 786 end
791 787 if coords[:end]
792 788 style = ""
793 789 style << "top:#{params[:top]}px;"
794 790 style << "left:#{coords[:end] + params[:zoom]}px;"
795 791 style << "width:15px;"
796 792 output << view.content_tag(:div, '&nbsp;'.html_safe,
797 793 :style => style,
798 794 :class => "#{options[:css]} marker ending")
799 795 end
800 796 end
801 797 # Renders the label on the right
802 798 if options[:label]
803 799 style = ""
804 800 style << "top:#{params[:top]}px;"
805 801 style << "left:#{(coords[:bar_end] || 0) + 8}px;"
806 802 style << "width:15px;"
807 803 output << view.content_tag(:div, options[:label],
808 804 :style => style,
809 805 :class => "#{options[:css]} label")
810 806 end
811 807 # Renders the tooltip
812 808 if options[:issue] && coords[:bar_start] && coords[:bar_end]
813 809 s = view.content_tag(:span,
814 810 view.render_issue_tooltip(options[:issue]).html_safe,
815 811 :class => "tip")
816 812 style = ""
817 813 style << "position: absolute;"
818 814 style << "top:#{params[:top]}px;"
819 815 style << "left:#{coords[:bar_start]}px;"
820 816 style << "width:#{coords[:bar_end] - coords[:bar_start]}px;"
821 817 style << "height:12px;"
822 818 output << view.content_tag(:div, s.html_safe,
823 819 :style => style,
824 820 :class => "tooltip")
825 821 end
826 822 @lines << output
827 823 output
828 824 end
829 825
830 826 def pdf_task(params, coords, options={})
831 827 height = options[:height] || 2
832 828 # Renders the task bar, with progress and late
833 829 if coords[:bar_start] && coords[:bar_end]
834 830 params[:pdf].SetY(params[:top] + 1.5)
835 831 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
836 832 params[:pdf].SetFillColor(200, 200, 200)
837 833 params[:pdf].RDMCell(coords[:bar_end] - coords[:bar_start], height, "", 0, 0, "", 1)
838 834 if coords[:bar_late_end]
839 835 params[:pdf].SetY(params[:top] + 1.5)
840 836 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
841 837 params[:pdf].SetFillColor(255, 100, 100)
842 838 params[:pdf].RDMCell(coords[:bar_late_end] - coords[:bar_start], height, "", 0, 0, "", 1)
843 839 end
844 840 if coords[:bar_progress_end]
845 841 params[:pdf].SetY(params[:top] + 1.5)
846 842 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
847 843 params[:pdf].SetFillColor(90, 200, 90)
848 844 params[:pdf].RDMCell(coords[:bar_progress_end] - coords[:bar_start], height, "", 0, 0, "", 1)
849 845 end
850 846 end
851 847 # Renders the markers
852 848 if options[:markers]
853 849 if coords[:start]
854 850 params[:pdf].SetY(params[:top] + 1)
855 851 params[:pdf].SetX(params[:subject_width] + coords[:start] - 1)
856 852 params[:pdf].SetFillColor(50, 50, 200)
857 853 params[:pdf].RDMCell(2, 2, "", 0, 0, "", 1)
858 854 end
859 855 if coords[:end]
860 856 params[:pdf].SetY(params[:top] + 1)
861 857 params[:pdf].SetX(params[:subject_width] + coords[:end] - 1)
862 858 params[:pdf].SetFillColor(50, 50, 200)
863 859 params[:pdf].RDMCell(2, 2, "", 0, 0, "", 1)
864 860 end
865 861 end
866 862 # Renders the label on the right
867 863 if options[:label]
868 864 params[:pdf].SetX(params[:subject_width] + (coords[:bar_end] || 0) + 5)
869 865 params[:pdf].RDMCell(30, 2, options[:label])
870 866 end
871 867 end
872 868
873 869 def image_task(params, coords, options={})
874 870 height = options[:height] || 6
875 871 # Renders the task bar, with progress and late
876 872 if coords[:bar_start] && coords[:bar_end]
877 873 params[:image].fill('#aaa')
878 874 params[:image].rectangle(params[:subject_width] + coords[:bar_start],
879 875 params[:top],
880 876 params[:subject_width] + coords[:bar_end],
881 877 params[:top] - height)
882 878 if coords[:bar_late_end]
883 879 params[:image].fill('#f66')
884 880 params[:image].rectangle(params[:subject_width] + coords[:bar_start],
885 881 params[:top],
886 882 params[:subject_width] + coords[:bar_late_end],
887 883 params[:top] - height)
888 884 end
889 885 if coords[:bar_progress_end]
890 886 params[:image].fill('#00c600')
891 887 params[:image].rectangle(params[:subject_width] + coords[:bar_start],
892 888 params[:top],
893 889 params[:subject_width] + coords[:bar_progress_end],
894 890 params[:top] - height)
895 891 end
896 892 end
897 893 # Renders the markers
898 894 if options[:markers]
899 895 if coords[:start]
900 896 x = params[:subject_width] + coords[:start]
901 897 y = params[:top] - height / 2
902 898 params[:image].fill('blue')
903 899 params[:image].polygon(x - 4, y, x, y - 4, x + 4, y, x, y + 4)
904 900 end
905 901 if coords[:end]
906 902 x = params[:subject_width] + coords[:end] + params[:zoom]
907 903 y = params[:top] - height / 2
908 904 params[:image].fill('blue')
909 905 params[:image].polygon(x - 4, y, x, y - 4, x + 4, y, x, y + 4)
910 906 end
911 907 end
912 908 # Renders the label on the right
913 909 if options[:label]
914 910 params[:image].fill('black')
915 911 params[:image].text(params[:subject_width] + (coords[:bar_end] || 0) + 5,
916 912 params[:top] + 1,
917 913 options[:label])
918 914 end
919 915 end
920 916 end
921 917 end
922 918 end
@@ -1,114 +1,111
1 1 var draw_gantt = null;
2 2 var draw_top;
3 3 var draw_right;
4 4 var draw_left;
5 5
6 6 var rels_stroke_width = 2;
7 7
8 8 function setDrawArea() {
9 9 draw_top = $("#gantt_draw_area").position().top;
10 10 draw_right = $("#gantt_draw_area").width();
11 11 draw_left = $("#gantt_area").scrollLeft();
12 12 }
13 13
14 14 function getRelationsArray() {
15 15 var arr = new Array();
16 16 $.each($('div.task_todo'), function(index_div, element) {
17 17 var element_id = $(element).attr("id");
18 18 if (element_id != null) {
19 19 var issue_id = element_id.replace("task-todo-issue-", "");
20 20 var data_rels = $(element).data("rels");
21 21 if (data_rels != null) {
22 for (rel_type_key in issue_relation_type) {
23 if (rel_type_key in data_rels) {
24 var issue_arr = data_rels[rel_type_key].toString().split(",");
25 $.each(issue_arr, function(index_issue, element_issue) {
26 arr.push({issue_from: issue_id, issue_to: element_issue,
27 rel_type: rel_type_key});
28 });
29 }
22 for (rel_type_key in data_rels) {
23 $.each(data_rels[rel_type_key], function(index_issue, element_issue) {
24 arr.push({issue_from: issue_id, issue_to: element_issue,
25 rel_type: rel_type_key});
26 });
30 27 }
31 28 }
32 29 }
33 30 });
34 31 return arr;
35 32 }
36 33
37 34 function drawRelations() {
38 35 var arr = getRelationsArray();
39 36 $.each(arr, function(index_issue, element_issue) {
40 37 var issue_from = $("#task-todo-issue-" + element_issue["issue_from"]);
41 38 var issue_to = $("#task-todo-issue-" + element_issue["issue_to"]);
42 39 if (issue_from.size() == 0 || issue_to.size() == 0) {
43 40 return;
44 41 }
45 42 var issue_height = issue_from.height();
46 43 var issue_from_top = issue_from.position().top + (issue_height / 2) - draw_top;
47 44 var issue_from_right = issue_from.position().left + issue_from.width();
48 45 var issue_to_top = issue_to.position().top + (issue_height / 2) - draw_top;
49 46 var issue_to_left = issue_to.position().left;
50 47 var color = issue_relation_type[element_issue["rel_type"]]["color"];
51 48 var landscape_margin = issue_relation_type[element_issue["rel_type"]]["landscape_margin"];
52 49 var issue_from_right_rel = issue_from_right + landscape_margin;
53 50 var issue_to_left_rel = issue_to_left - landscape_margin;
54 51 draw_gantt.path(["M", issue_from_right + draw_left, issue_from_top,
55 52 "L", issue_from_right_rel + draw_left, issue_from_top])
56 53 .attr({stroke: color,
57 54 "stroke-width": rels_stroke_width
58 55 });
59 56 if (issue_from_right_rel < issue_to_left_rel) {
60 57 draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_from_top,
61 58 "L", issue_from_right_rel + draw_left, issue_to_top])
62 59 .attr({stroke: color,
63 60 "stroke-width": rels_stroke_width
64 61 });
65 62 draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_to_top,
66 63 "L", issue_to_left + draw_left, issue_to_top])
67 64 .attr({stroke: color,
68 65 "stroke-width": rels_stroke_width
69 66 });
70 67 } else {
71 68 var issue_middle_top = issue_to_top +
72 69 (issue_height *
73 70 ((issue_from_top > issue_to_top) ? 1 : -1));
74 71 draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_from_top,
75 72 "L", issue_from_right_rel + draw_left, issue_middle_top])
76 73 .attr({stroke: color,
77 74 "stroke-width": rels_stroke_width
78 75 });
79 76 draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_middle_top,
80 77 "L", issue_to_left_rel + draw_left, issue_middle_top])
81 78 .attr({stroke: color,
82 79 "stroke-width": rels_stroke_width
83 80 });
84 81 draw_gantt.path(["M", issue_to_left_rel + draw_left, issue_middle_top,
85 82 "L", issue_to_left_rel + draw_left, issue_to_top])
86 83 .attr({stroke: color,
87 84 "stroke-width": rels_stroke_width
88 85 });
89 86 draw_gantt.path(["M", issue_to_left_rel + draw_left, issue_to_top,
90 87 "L", issue_to_left + draw_left, issue_to_top])
91 88 .attr({stroke: color,
92 89 "stroke-width": rels_stroke_width
93 90 });
94 91 }
95 92 draw_gantt.path(["M", issue_to_left + draw_left, issue_to_top,
96 93 "l", -4 * rels_stroke_width, -2 * rels_stroke_width,
97 94 "l", 0, 4 * rels_stroke_width, "z"])
98 95 .attr({stroke: "none",
99 96 fill: color,
100 97 "stroke-linecap": "butt",
101 98 "stroke-linejoin": "miter",
102 99 });
103 100 });
104 101 }
105 102
106 103 function drawGanttHandler() {
107 104 var folder = document.getElementById('gantt_draw_area');
108 105 if(draw_gantt != null)
109 106 draw_gantt.clear();
110 107 else
111 108 draw_gantt = Raphael(folder);
112 109 setDrawArea();
113 110 drawRelations();
114 111 }
General Comments 0
You need to be logged in to leave comments. Login now