# HG changeset patch
# User Toshi MARUYAMA
# Parent 70dd23e0d4ec8fa61beb9914e8b362dc1aaf8475
qnew

diff --git a/app/views/gantts/show.html.erb b/app/views/gantts/show.html.erb
--- a/app/views/gantts/show.html.erb
+++ b/app/views/gantts/show.html.erb
@@ -1,3 +1,11 @@
+<% content_for :header_tags do %>
+  <%= javascript_include_tag 'raphael' %>
+  <%= javascript_include_tag 'gantt_progress_line' %>
+<% end %>
+<%= javascript_tag do %>
+  $(document).ready(drawGanttLinesHandler);
+  $(window).resize(drawGanttLinesHandler);
+<% end %>
 <% @gantt.view = self %>
 <h2><%= @query.new_record? ? l(:label_gantt) : h(@query.name) %></h2>
 
@@ -228,9 +236,17 @@
     style += "width:10px;"
     style += "border-left: 1px dashed red;"
   %>
-  <%= content_tag(:div, '&nbsp;'.html_safe, :style => style) %>
+  <%= content_tag(:div, '&nbsp;'.html_safe, :style => style, :id => 'today_red_line') %>
 <% end %>
-
+<%
+  style  = ""
+  style += "position: absolute;"
+  style += "height: #{g_height}px;"
+  style += "top: #{headers_height + 1}px;"
+  style += "left: 0px;"
+  style += "width: #{g_width - 1}px;"
+%>
+<%= content_tag(:div, '', :style => style, :id => "gantt_lines") %>
 </div>
 </td>
 </tr>
diff --git a/lib/redmine/helpers/gantt.rb b/lib/redmine/helpers/gantt.rb
--- a/lib/redmine/helpers/gantt.rb
+++ b/lib/redmine/helpers/gantt.rb
@@ -288,10 +288,18 @@ module Redmine
           html_class << 'icon icon-package '
           html_class << (version.behind_schedule? ? 'version-behind-schedule' : '') << " "
           html_class << (version.overdue? ? 'version-overdue' : '')
+          html_class << ' version-closed' unless version.open?
+          if version.start_date && version.due_date && version.completed_pourcent
+            progress_date = calc_progress_date(version.start_date,
+                                               version.due_date, version.completed_pourcent)
+            html_class << ' behind-start-date' if progress_date < self.date_from
+            html_class << ' over-end-date' if progress_date > self.date_to
+          end
           s = view.link_to_version(version).html_safe
           subject = view.content_tag(:span, s,
                                      :class => html_class).html_safe
-          html_subject(options, subject, :css => "version-name")
+          html_subject(options, subject, :css => "version-name",
+                       :id => "version-#{version.id}")
         when :image
           image_subject(options, version.to_s_with_project)
         when :pdf
@@ -312,7 +320,8 @@ module Redmine
           label = h("#{version.project} -") + label unless @project && @project == version.project
           case options[:format]
           when :html
-            html_task(options, coords, :css => "version task", :label => label, :markers => true)
+            html_task(options, coords, :css => "version task",
+                      :label => label, :markers => true, :version => version)
           when :image
             image_task(options, coords, :label => label, :markers => true, :height => 3)
           when :pdf
@@ -335,6 +344,13 @@ module Redmine
           css_classes << ' issue-overdue' if issue.overdue?
           css_classes << ' issue-behind-schedule' if issue.behind_schedule?
           css_classes << ' icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to
+          css_classes << ' issue-closed' if issue.closed?
+          if issue.start_date && issue.due_before && issue.done_ratio
+            progress_date = calc_progress_date(issue.start_date,
+                                               issue.due_before, issue.done_ratio)
+            css_classes << ' behind-start-date' if progress_date < self.date_from
+            css_classes << ' over-end-date' if progress_date > self.date_to
+          end
           s = "".html_safe
           if issue.assigned_to.present?
             assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
@@ -346,7 +362,7 @@ module Redmine
           s << view.link_to_issue(issue).html_safe
           subject = view.content_tag(:span, s, :class => css_classes).html_safe
           html_subject(options, subject, :css => "issue-subject",
-                       :title => issue.subject) + "\n"
+                       :title => issue.subject, :id => "issue-#{issue.id}") + "\n"
         when :image
           image_subject(options, issue.subject)
         when :pdf
@@ -609,7 +625,7 @@ module Redmine
             coords[:bar_end] = self.date_to - self.date_from + 1
           end
           if progress
-            progress_date = start_date + (end_date - start_date + 1) * (progress / 100.0)
+            progress_date = calc_progress_date(start_date, end_date, progress)
             if progress_date > self.date_from && progress_date > start_date
               if progress_date < self.date_to
                 coords[:bar_progress_end] = progress_date - self.date_from
@@ -636,6 +652,10 @@ module Redmine
         coords
       end
 
+      def calc_progress_date(start_date, end_date, progress)
+        start_date + (end_date - start_date + 1) * (progress / 100.0)
+      end
+
       # Sorts a collection of issues by start_date, due_date, id for gantt rendering
       def sort_issues!(issues)
         issues.sort! { |a, b| gantt_issue_compare(a, b, issues) }
@@ -676,9 +696,10 @@ module Redmine
       def html_subject(params, subject, options={})
         style = "position: absolute;top:#{params[:top]}px;left:#{params[:indent]}px;"
         style << "width:#{params[:subject_width] - params[:indent]}px;" if params[:subject_width]
-        output = view.content_tag('div', subject,
+        output = view.content_tag(:div, subject,
                                   :class => options[:css], :style => style,
-                                  :title => options[:title])
+                                  :title => options[:title],
+                                  :id => options[:id])
         @subjects << output
         output
       end
@@ -712,9 +733,12 @@ module Redmine
           style << "top:#{params[:top]}px;"
           style << "left:#{coords[:bar_start]}px;"
           style << "width:#{width}px;"
+          html_id = "task-todo-issue-#{options[:issue].id}" if options[:issue]
+          html_id = "task-todo-version-#{options[:version].id}" if options[:version]
           output << view.content_tag(:div, '&nbsp;'.html_safe,
                                      :style => style,
-                                     :class => "#{options[:css]} task_todo")
+                                     :class => "#{options[:css]} task_todo",
+                                     :id => html_id)
           if coords[:bar_late_end]
             width = coords[:bar_late_end] - coords[:bar_start] - 2
             style = ""
@@ -731,9 +755,12 @@ module Redmine
             style << "top:#{params[:top]}px;"
             style << "left:#{coords[:bar_start]}px;"
             style << "width:#{width}px;"
+            html_id = "task-done-issue-#{options[:issue].id}" if options[:issue]
+            html_id = "task-done-version-#{options[:version].id}" if options[:version]
             output << view.content_tag(:div, '&nbsp;'.html_safe,
                                        :style => style,
-                                       :class => "#{options[:css]} task_done")
+                                       :class => "#{options[:css]} task_done",
+                                       :id => html_id)
           end
         end
         # Renders the markers
diff --git a/public/javascripts/gantt_progress_line.js b/public/javascripts/gantt_progress_line.js
new file mode 100644
--- /dev/null
+++ b/public/javascripts/gantt_progress_line.js
@@ -0,0 +1,61 @@
+var draw_gantt_line = null;
+
+function drawGanttLines(holder) {
+  if(draw_gantt_line != null)
+    draw_gantt_line.clear();
+  else
+    draw_gantt_line = Raphael(holder);
+  var arr = new Array();
+  var today_left = $('#today_red_line').position().left;
+  arr.push({left: today_left, top: 0});
+  var line_top   = $("#gantt_lines").position().top;
+  var line_right = $("#gantt_lines").width();
+  $.each($('div.issue-subject, div.version-name'), function(index, element) {
+    var t0 = $(element).position().top - line_top ;
+    var h = ($(element).height() / 9);
+    var t1 = t0 - h;
+    var t2 = t0 + (h * 8);
+    var t  = t0 + (h * 3);
+    var ci = $(element).children('span').hasClass('issue-closed');
+    var cv = $(element).children('span').hasClass('version-closed');
+    if (ci || cv) {
+      arr.push({left: today_left, top: t});
+    } else {
+      var issue_done = $("#task-done-" + $(element).attr("id"));
+      var is_behind_start = $(element).children('span').hasClass('behind-start-date');
+      var is_over_end     = $(element).children('span').hasClass('over-end-date');
+      if (is_over_end) {
+        arr.push({left: line_right, top: t1, is_right_edge: true});
+        arr.push({left: line_right, top: t2, is_right_edge: true, none_stroke: true});
+      } else if (issue_done.size() > 0) {
+        var l = issue_done.first().position().left +
+                    issue_done.first().width();
+        arr.push({left: l, top: t});
+      } else if (is_behind_start) {
+        arr.push({left: 0 , top: t1, is_left_edge: true});
+        arr.push({left: 0 , top: t2, is_left_edge: true, none_stroke: true});
+      } else {
+        var l1 = today_left;
+        var issue_todo = $("#task-todo-" + $(element).attr("id"));
+        if (issue_todo.size() > 0){
+          l1 = issue_todo.first().position().left;
+        }
+        arr.push({left: Math.min(today_left, l1), top: t});
+      }
+    }
+  });
+  var i;
+  for(i = 1 ; i < arr.length ; i++) {
+    if (!("none_stroke" in arr[i]) &&
+        (!("is_right_edge" in arr[i - 1] && "is_right_edge" in arr[i]) &&
+         !("is_left_edge" in arr[i - 1] && "is_left_edge" in arr[i]))
+        ) {
+      draw_gantt_line.path(["M",arr[i - 1].left, arr[i - 1].top, "L", arr[i].left, arr[i].top])
+            .attr({stroke: "#ff0000", "stroke-width": 2});
+    }
+  }
+}
+
+function drawGanttLinesHandler() {
+  drawGanttLines(document.getElementById('gantt_lines'));
+}
