Project

General

Profile

Feature #2024 » gantt_edit_r3764.patch

Hiroyuki Yoshioka, 2010-05-29 17:57

View differences:

app/controllers/gantts_controller.rb (working copy)
class GanttsController < ApplicationController
before_filter :find_optional_project
before_filter :find_issue, :only => [:edit_gantt]
rescue_from Query::StatementInvalid, :with => :query_statement_invalid
......
helper :sort
include SortHelper
include Redmine::Export::PDF
include IssuesHelper
def show
@gantt = Redmine::Helpers::Gantt.new(params)
......
end
end
def find_optional_project
@project = Project.find(params[:project_id]) unless params[:project_id].blank?
if params[:action] == "edit_gantt"
allowed = User.current.allowed_to?({:controller => 'issues', :action => 'edit'}, @project, :global => true)
else
allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
end
allowed ? true : deny_access
rescue ActiveRecord::RecordNotFound
render_404
end
def edit_gantt
if !@issue.start_date || !@issue.due_before
render :text=>l(:notice_locking_conflict), :status=>400
return
end
@issue.init_journal(User.current)
date_from = Date.parse(params[:date_from])
old_start_date = @issue.start_date
o = get_position(@issue, date_from, Date.parse(params[:date_to]), params[:zoom])
text_for_revert = "#{@issue.id}=#{format_date(@issue.start_date)},#{@issue.start_date},#{format_date(@issue.due_before)},#{@issue.due_before},#{o[0]},#{o[1]},#{o[2]},#{o[3]}"
if params[:day]
#bar moved
day = params[:day].to_i
duration = @issue.due_before - @issue.start_date
@issue.start_date = date_from + day
@issue.due_date = @issue.start_date + duration.to_i if @issue.due_date
elsif params[:start_date]
#start date changed
start_date = Date.parse(params[:start_date])
if @issue.start_date == start_date
render :text=>""
return #nothing has changed
end
@issue.start_date = start_date
@issue.due_date = start_date if @issue.due_date && start_date > @issue.due_date
elsif params[:due_date]
#due date changed
due_date = Date.parse(params[:due_date])
if @issue.due_date == due_date
render :text=>""
return #nothing has changed
end
@issue.due_date = due_date
@issue.start_date = due_date if due_date < @issue.start_date
end
fv = @issue.fixed_version
if fv && fv.effective_date && !@issue.due_date && fv.effective_date < @issue.start_date
@issue.start_date = old_start_date
end
begin
@issue.save!
o = get_position(@issue, date_from, Date.parse(params[:date_to]), params[:zoom])
text = "#{@issue.id}=#{format_date(@issue.start_date)},#{@issue.start_date},#{format_date(@issue.due_before)},#{@issue.due_before},#{o[0]},#{o[1]},#{o[2]},#{o[3]}"
#check dependencies
issues = @issue.all_precedes_issues
issues.each do |i|
o = get_position(i, date_from, Date.parse(params[:date_to]), params[:zoom])
text += "|#{i.id}=#{format_date(i.start_date)},#{i.start_date},#{format_date(i.due_before)},#{i.due_before},#{o[0]},#{o[1]},#{o[2]},#{o[3]}"
end
render :text=>text
rescue
render :text=>@issue.errors.full_messages.join("\n") + "|" + text_for_revert , :status=>400
end
end
private
def find_issue
@issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
@project = @issue.project
rescue ActiveRecord::RecordNotFound
render_404
end
end
app/helpers/issues_helper.rb (working copy)
@cached_label_priority ||= l(:field_priority)
link_to_issue(issue) + "<br /><br />" +
"<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />" +
"<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />" +
"<strong>#{@cached_label_start_date}</strong>: <span id='tooltip_start_date_#{issue.id}'>#{format_date(issue.start_date)}</span><br />" +
"<strong>#{@cached_label_due_date}</strong>: <span id='tooltip_due_date_#{issue.id}'>#{format_date(issue.due_date)}</span><br />" +
"<strong>#{@cached_label_assigned_to}</strong>: #{issue.assigned_to}<br />" +
"<strong>#{@cached_label_priority}</strong>: #{issue.priority.name}"
end
......
end
end
end
def get_position(event, date_from, date_to, zoom_str)
zoom = zoom_str.to_i
i_start_date = (event.start_date >= date_from ? event.start_date : date_from )
i_end_date = (event.due_before <= date_to ? event.due_before : date_to )
i_done_date = event.start_date + ((event.due_before - event.start_date+1)*event.done_ratio/100).floor
i_done_date = (i_done_date <= date_from ? date_from : i_done_date )
i_done_date = (i_done_date >= date_to ? date_to : i_done_date )
i_late_date = [i_end_date, Date.today].min if i_start_date <= Date.today
i_left = ((i_start_date - date_from)*zoom).floor
i_left = 0 if i_left < 0
i_width = ((i_end_date - i_start_date + 1)*zoom).floor - 2 # total width of the issue (- 2 for left and right borders)
i_width = 0 if i_width < 0
d_width = ((i_done_date - i_start_date)*zoom).floor - 2 # done width
d_width = 0 if d_width < 0
l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor - 2 : 0 # delay width
l_width = 0 if l_width < 0
return i_left, i_width, l_width, d_width
end
end
app/models/issue.rb (working copy)
end
dependencies
end
def all_precedes_issues
dependencies = []
relations_from.each do |relation|
next unless relation.relation_type == IssueRelation::TYPE_PRECEDES
dependencies << relation.issue_to
dependencies += relation.issue_to.all_dependent_issues
end
dependencies
end
# Returns an array of issues that duplicate this one
def duplicates
app/views/gantts/show.html.erb (working copy)
<% include_calendar_headers_tags %>
<h2><%= l(:label_gantt) %></h2>
<% form_tag(params.merge(:month => nil, :year => nil, :months => nil), :id => 'query_form') do %>
......
<% zoom = 1
@gantt.zoom.times { zoom = zoom * 2 }
subject_width = 330
subject_width = 280
header_heigth = 18
headers_height = header_heigth
......
g_height = [(20 * @gantt.events.length + 6)+150, 206].max
t_height = g_height + headers_height
%>
<input type="hidden" name="_date_from" id="i_date_from" value="<%=h(@gantt.date_from)%>" />
<input type="hidden" name="_date_to" id="i_date_to" value="<%=h(@gantt.date_to)%>" />
<input type="hidden" name="zm" id="i_zm" value="<%=h(zoom)%>" />
<script type='text/javascript'>
function issue_moved(elem) {
var id_str = elem.id.substring(3, elem.id.length);
var v_date_from = document.getElementById('i_date_from').getAttribute("value");
var v_date_to = document.getElementById('i_date_to').getAttribute("value");
var v_zm = document.getElementById('i_zm').getAttribute("value");
var url_str = '/issues/edit_gantt/' + id_str;
var day = parseInt(elem.style.left)/parseInt(v_zm);
new Ajax.Request(url_str, {asynchronous:true, evalScripts:true,
parameters: 'day='+day+'&date_from='+v_date_from+'&date_to='+v_date_to+'&zoom='+v_zm,
onSuccess:function(obj) {
change_dates(obj.responseText);
},
onFailure:function(obj) {
handle_failure(obj.responseText);
}
});
}
function handle_failure(res_text) {
var text = res_text.split('|');
alert(text[0]);
if (text.length == 1) {
return;
}
change_dates(text[1]);//revert
}
function change_dates(issue_infos) {
if (!issue_infos) {
return;
}
var issue_list = issue_infos.split("|");
for (i = 0; i < issue_list.length; i++) {
change_date(issue_list[i]);
}
}
function change_date(text) {
if (!text) {
return;
}
var issue_info = text.split("=");
var elem_id = issue_info[0];
var vals = issue_info[1].split(',');
var start_date_elem = document.getElementById(elem_id + '_start_date_str');
if (!start_date_elem) {
//target not exists
return;
}
start_date_elem.innerHTML = vals[0];
var tooltip_start_date_elem = document.getElementById('tooltip_start_date_' + elem_id);
tooltip_start_date_elem.innerHTML = vals[0];
var due_date_elem = document.getElementById(elem_id + '_due_date_str');
due_date_elem.innerHTML = vals[2];
var tooltip_due_date_elem = document.getElementById('tooltip_due_date_' + elem_id);
tooltip_due_date_elem.innerHTML = vals[2];
var ev_elem = document.getElementById('ev_' + elem_id);
ev_elem.style.left = vals[4] + 'px';
ev_elem.style.width = (parseInt(vals[5])+100)+'px';
var todo_elem = document.getElementById('task_todo_' + elem_id);
todo_elem.style.width = vals[5] + 'px';
var late_elem = document.getElementById('task_late_' + elem_id);
if (late_elem) {
late_elem.style.width = vals[6] + 'px';
if (vals[6] == '0') {
late_elem.className = 'task task_none';
} else {
late_elem.className = 'task task_late';
}
}
var done_elem = document.getElementById('task_done_' + elem_id);
if (done_elem) {
done_elem.style.width = vals[7] + 'px';
if (vals[7] == '0') {
done_elem.className = 'task task_none';
} else {
done_elem.className = 'task task_done';
}
}
var task_elem = document.getElementById('task_task_' + elem_id);
if (task_elem) {
task_elem.style.left = (parseInt(vals[5])+5) + 'px';
}
var tooltip = document.getElementById("tt_" + elem_id);
tooltip.style.left = ev_elem.style.left;
//change calendar date
document.getElementById(elem_id+"_hidden_start_date").value = vals[1];
document.getElementById(elem_id+"_start_date").value = vals[1];
document.getElementById(elem_id+"_hidden_due_date").value = vals[3];
document.getElementById(elem_id+"_due_date").value = vals[3];
}
</script>
<table width="100%" style="border:0; border-collapse: collapse;">
<tr>
<td style="width:<%= subject_width %>px; padding:0px;">
......
end %>
</div>
</td>
<td style="width:225px; padding:0px;">
<div style="position:relative;height:<%= t_height + 24 %>px;width:225px;">
<div style="width:225px;height:<%= headers_height %>px;background: #eee;" class="gantt_hdr"></div>
<div style="width:225px;height:<%= t_height %>px;border-left: 1px solid #c0c0c0;overflow:hidden;" class="gantt_hdr"></div>
<%
top = headers_height + 8
@gantt.events.each do |i| %>
<div style="position: absolute;line-height:1.2em;height:16px;top:<%= top %>px;"><small>
<% if i.is_a? Issue %>
<span id="<%=i.id%>_start_date_str" style="font-size: 7pt;"><%= format_date(i.start_date) %></span>
<input type="hidden" size="12" id="<%=i.id%>_hidden_start_date" value="<%= i.start_date %>">
<input type="hidden" size="12" id="<%=i.id%>_start_date" value="<%= i.start_date %>"><%= calendar_for("#{i.id}_start_date") if i.is_a? Issue %>
<script type="text/javascript">
//<![CDATA[
new Form.Element.Observer('<%=i.id%>_start_date', 0.25,
function(element, value) {
if (value == document.getElementById('<%=i.id%>_hidden_start_date').value) {
return ;
}
new Ajax.Request('<%=url_for(:controller=>:issues, :action => :edit_gantt, :id=>i.id, :date_from=>@gantt.date_from.strftime("%Y-%m-%d"), :date_to=>@gantt.date_to.strftime("%Y-%m-%d"), :zoom=>zoom, :escape => false) %>', {asynchronous:true, evalScripts:true, onFailure:function(request){handle_failure(request.responseText)}, onSuccess:function(request){change_dates(request.responseText)}, parameters:'start_date=' + encodeURIComponent(value)});
})
//]]>
</script>
<span id="<%=i.id%>_due_date_str" style="font-size: 7pt;<%= "color: blue;" unless i.due_date%>">
<%= format_date(i.due_before) %>
</span>
<input type="hidden" size="12" id="<%=i.id%>_hidden_due_date" value="<%= i.due_date %>">
<input type="hidden" size="12" id="<%=i.id%>_due_date" value="<%= i.due_date %>"><%= calendar_for("#{i.id}_due_date") if i.due_date%>
<script type="text/javascript">
//<![CDATA[
new Form.Element.Observer('<%=i.id%>_due_date', 0.25,
function(element, value) {
if (value == document.getElementById('<%=i.id%>_hidden_due_date').value) {
return ;
}
new Ajax.Request('<%=url_for(:controller=>:issues, :action => :edit_gantt, :id=>i.id, :date_from=>@gantt.date_from.strftime("%Y-%m-%d"), :date_to=>@gantt.date_to.strftime("%Y-%m-%d"), :zoom=>zoom, :escape => false) %>', {asynchronous:true, evalScripts:true, onFailure:function(request){handle_failure(request.responseText)}, onSuccess:function(request){change_dates(request.responseText)}, parameters:'due_date=' + encodeURIComponent(value)});
})
//]]>
</script>
<% else %>
<span style="font-size: 7pt;"><%= format_date(i.start_date) %></span>
<% end %>
</small></div>
<% top = top + 20
end %>
</div>
</td>
<td>
<div style="position:relative;height:<%= t_height + 24 %>px;overflow:auto;">
<div style="position:relative;height:<%= t_height + 24 %>px;overflow:auto;" id="gantt-container">
<div style="width:<%= g_width-1 %>px;height:<%= headers_height %>px;background: #eee;" class="gantt_hdr">&nbsp;</div>
<%
#
......
left = 0
height = g_height + header_heigth - 1
wday = @gantt.date_from.cwday
dt = @gantt.date_from
(@gantt.date_to - @gantt.date_from + 1).to_i.times do
width = zoom - 1
%>
<div style="left:<%= left %>px;top:37px;width:<%= width %>px;height:<%= height %>px;font-size:0.7em;<%= "background:#f1f1f1;" if wday > 5 %>" class="gantt_hdr">
<%= day_name(wday).first %>
<%= "#{dt.day}<br>"if @gantt.zoom == 4 %><%= day_name(wday).first %>
</div>
<%
left = left + width+1
wday = wday + 1
dt = dt + 1
wday = 1 if wday > 7
end
end %>
......
#
top = headers_height + 10
@gantt.events.each do |i|
if i.is_a? Issue
i_start_date = (i.start_date >= @gantt.date_from ? i.start_date : @gantt.date_from )
i_end_date = (i.due_before <= @gantt.date_to ? i.due_before : @gantt.date_to )
i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor
i_done_date = (i_done_date <= @gantt.date_from ? @gantt.date_from : i_done_date )
i_done_date = (i_done_date >= @gantt.date_to ? @gantt.date_to : i_done_date )
i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
i_left = ((i_start_date - @gantt.date_from)*zoom).floor
i_width = ((i_end_date - i_start_date + 1)*zoom).floor - 2 # total width of the issue (- 2 for left and right borders)
d_width = ((i_done_date - i_start_date)*zoom).floor - 2 # done width
l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor - 2 : 0 # delay width
css = "task " + (i.leaf? ? 'leaf' : 'parent')
if i.is_a? Issue
i_left, i_width, l_width, d_width = get_position(i, @gantt.date_from, @gantt.date_to, zoom)
%>
<div style="top:<%= top %>px;left:<%= i_left %>px;width:<%= i_width %>px;" class="<%= css %> task_todo"><div class="left"></div>&nbsp;<div class="right"></div></div>
<% if l_width > 0 %>
<div style="top:<%= top %>px;left:<%= i_left %>px;width:<%= l_width %>px;" class="<%= css %> task_late">&nbsp;</div>
<% end %>
<% if d_width > 0 %>
<div style="top:<%= top %>px;left:<%= i_left %>px;width:<%= d_width %>px;" class="<%= css %> task_done">&nbsp;</div>
<% end %>
<div style="top:<%= top %>px;left:<%= i_left + i_width + 8 %>px;background:#fff;" class="<%= css %>">
<%= i.status.name %>
<%= (i.done_ratio).to_i %>%
</div>
<div class="tooltip" style="position: absolute;top:<%= top %>px;left:<%= i_left %>px;width:<%= i_width %>px;height:12px;">
<span class="tip">
<div id="ev_<%=i.id%>" style="position:absolute;left:<%= i_left %>px;top:<%= top %>px;padding-top:3px;height:18px;width:<%= i_width+100 %>px;" class="handle">
<div id="task_todo_<%=i.id%>" style="float:left:0px; width:<%= i_width %>px;" class="task task_todo">&nbsp;</div>
<div id="task_late_<%=i.id%>" style="float:left:0px; width:<%= l_width %>px;" class="<%= l_width == 0 ? 'task task_none' : 'task task_late' %>">&nbsp;</div>
<div id="task_done_<%=i.id%>" style="float:left:0px; width:<%= d_width %>px;" class="<%= d_width == 0 ? 'task task_none' : 'task task_done' %>">&nbsp;</div>
<div id="task_task_<%=i.id%>" style="position:absolute;left:<%= (i_width + 5) %>px;background:#fff;" class="task">
<%= h(i.status.name) %>
<%= (i.done_ratio).to_i %>%
<% if !i.due_date && i.fixed_version %>
-&nbsp;<strong><%= h(i.fixed_version.name) %></strong>
<% end %>
</div>
</div>
<%# === tooltip === %>
<div id="tt_<%=i.id%>" class="tooltip" style="position: absolute;top:<%= top+14 %>px;left:<%= i_left %>px;width:<%= i_width %>px;height:8px;">
<span class="tip" style="position: relative;top:-2px;">
<%= render_issue_tooltip i %>
</span></div>
<%= draggable_element("ev_#{i.id}",
:revert =>false, :scroll=>"'gantt-container'", :constraint => "'horizontal'", :snap=>zoom,
:onEnd=>"function( draggable, event ) {issue_moved(draggable.element);}"
) %>
<% else
i_left = ((i.start_date - @gantt.date_from)*zoom).floor
%>
config/routes.rb (working copy)
issues_views.connect 'projects/:project_id/issues.:format', :action => 'index'
issues_views.connect 'projects/:project_id/issues/new', :action => 'new'
issues_views.connect 'projects/:project_id/issues/gantt', :controller => 'gantts', :action => 'show'
issues_views.connect 'projects/:project_id/issues/edit_gantt/:id', :controller => 'gantts', :action => 'edit_gantt', :id => /\d+/
issues_views.connect 'projects/:project_id/issues/calendar', :controller => 'calendars', :action => 'show'
issues_views.connect 'projects/:project_id/issues/:copy_from/copy', :action => 'new'
issues_views.connect 'issues/:id', :action => 'show', :id => /\d+/
......
issues_actions.connect 'issues', :action => 'index'
issues_actions.connect 'projects/:project_id/issues', :action => 'create'
issues_actions.connect 'projects/:project_id/issues/gantt', :controller => 'gantts', :action => 'show'
issues_actions.connect 'projects/:project_id/issues/edit_gantt:id', :controller => 'gantts', :action => 'edit_gantt', :id => /\d+/
issues_actions.connect 'projects/:project_id/issues/calendar', :controller => 'calendars', :action => 'show'
issues_actions.connect 'issues/:id/quoted', :action => 'reply', :id => /\d+/
issues_actions.connect 'issues/:id/:action', :action => /edit|move|destroy/, :id => /\d+/
......
issues_actions.connect 'issues/:id.:format', :action => 'destroy', :id => /\d+/, :format => /xml/
end
issues_routes.connect 'issues/gantt', :controller => 'gantts', :action => 'show'
issues_routes.connect 'issues/edit_gantt/:id', :controller => 'gantts', :action => 'edit_gantt', :id => /\d+/
issues_routes.connect 'issues/calendar', :controller => 'calendars', :action => 'show'
issues_routes.connect 'issues/:action'
end
public/stylesheets/application.css (working copy)
.task_late { background:#f66 url(../images/task_late.png); border: 1px solid #f66; }
.task_done { background:#66f url(../images/task_done.png); border: 1px solid #66f; }
.task_todo { background:#aaa url(../images/task_todo.png); border: 1px solid #aaa; }
.task_none { background:transparent; border-style: none; }
.task_todo.parent { background: #888; border: 1px solid #888; height: 6px;}
.task_late.parent, .task_done.parent { height: 3px;}
(11-11/35)