move js genreated for ajax calls into their own object to prevent name clashes

I only modified the js that is most likely to be used in cocurrent calls. This because we
start the development of Tracks 3 that will change all js.
This commit is contained in:
Reinier Balt 2014-08-10 13:18:33 +02:00
parent f0871738e9
commit 4cdbd9a451
14 changed files with 436 additions and 410 deletions

View file

@ -236,4 +236,10 @@ module ApplicationHelper
source_view_is_one_of(:project, :context) ? "#{@source_view}-#{eval("@#{@source_view}.id")}" : @source_view source_view_is_one_of(:project, :context) ? "#{@source_view}-#{eval("@#{@source_view}.id")}" : @source_view
end end
# create a unique object name which can be used in ajax calls returning js
# to prevent concurrent calls with same functions to overwrite each other functions
def unique_object_name_for(name)
"#{name}_#{SecureRandom.hex(5)}"
end
end end

View file

@ -392,15 +392,21 @@ module TodosHelper
return 'context' return 'context'
end end
# jquery animations are async, so first collect all animation steps that need
# to be run sequential in array animation, then execute them. All steps are
# functions which are passed a function as parameter that should execute the next
# animation steps.
# if the animation needs to be run inside the namespace of an object, set the
# object_name to the name of the object and this name will be prepended to each step
def render_animation(animation, object_name=nil) def render_animation(animation, object_name=nil)
html = "" object_name += "." unless object_name.nil? # add dot if object_name is given
animation.each do |step|
if step.present? # concatenate all steps into functions that call functions
html += (object_name.nil? ? step : "#{object_name}.#{step}") + "({ go: function() {\r\n" html = animation.map{ |step| "#{object_name}#{step}({ go: function() {" }.join("\r\n")
end # close all functions
end html += "}}) " * animation.size + ";"
html += "}}) " * animation.size
return html + ";" return html
end end
def reset_tab_index def reset_tab_index

View file

@ -1,21 +1,25 @@
replace_context_with_edit_form(); <%- object_name = unique_object_name_for("edit_context_#{@context.id}") %>
function replace_context_with_edit_form() { var <%=object_name%> = {
animate: function() {
<%=object_name%>.replace_context_with_edit_form();
},
replace_context_with_edit_form: function() {
$('div#<%=dom_id(@context)%>').fadeOut(250, function() { $('div#<%=dom_id(@context)%>').fadeOut(250, function() {
show_edit_form(); <%=object_name%>.show_edit_form();
set_focus(); <%=object_name%>.set_focus();
}); });
} },
show_edit_form: function() {
function show_edit_form() { $('div#<%=dom_id(@context, 'edit')%>').html(<%=object_name%>.html_for_edit_form());
$('div#<%=dom_id(@context, 'edit')%>').html(html_for_edit_form());
$('div#<%=dom_id(@context, 'edit')%>').fadeIn(500); $('div#<%=dom_id(@context, 'edit')%>').fadeIn(500);
} },
set_focus: function() {
function set_focus() {
$('input.context-name').focus(); $('input.context-name').focus();
},
html_for_edit_form: function() {
return "<%= escape_javascript(render(:partial => 'context_form', :object => @context)) %>";
} }
};
function html_for_edit_form() { <%= object_name %>.animate();
return "<%= escape_javascript(render(:partial => 'context_form', :object => @context)) %>"
}

View file

@ -1,28 +1,31 @@
<%-
object_name = unique_object_name_for("update_context_#{@context.id}")
-%>
var <%=object_name%> = {
<% unless @saved -%> <% unless @saved -%>
TracksPages.show_edit_errors(html_for_error_messages()); animate: function() {
TracksPages.show_edit_errors(<%=object_name%>.html_for_error_messages());
function html_for_error_messages() { },
html_for_error_messages: function() {
return "<%= escape_javascript(get_list_of_error_messages_for(@context)) %>"; return "<%= escape_javascript(get_list_of_error_messages_for(@context)) %>";
} }
<% else -%> <% else -%>
animate: function() {
TracksPages.page_notify('notice', '<%= t('contexts.save_status_message') %>', 5); TracksPages.page_notify('notice', '<%= t('contexts.save_status_message') %>', 5);
<% if @state_changed -%> <% if @state_changed -%>
remove_and_re_add_context(); <%=object_name%>.remove_and_re_add_context();
update_container_states(); <%=object_name%>.update_container_states();
<% else -%> <% else -%>
replace_context_form_with_updated_context(); <%=object_name%>.replace_context_form_with_updated_context();
<% end -%> <% end -%>
},
function remove_and_re_add_context() { remove_and_re_add_context: function() {
$('#<%=dom_id(@context, 'container')%>').slideUp(500, function() { $('#<%=dom_id(@context, 'container')%>').slideUp(500, function() {
$('#<%=dom_id(@context, 'container')%>').remove(); $('#<%=dom_id(@context, 'container')%>').remove();
$('#list-contexts-<%=@new_state%>').append(html_for_context_listing()); $('#list-contexts-<%=@new_state%>').append(<%=object_name%>.html_for_context_listing());
}); });
} },
replace_context_form_with_updated_context: function() {
function replace_context_form_with_updated_context() {
$('#<%=dom_id(@context, 'container')%>').fadeOut(250, function() { $('#<%=dom_id(@context, 'container')%>').fadeOut(250, function() {
<% <%
# first add the updated context after the old one, then remove old one # first add the updated context after the old one, then remove old one
@ -30,18 +33,18 @@
# the container instead of the container itself, i.e. you will get # the container instead of the container itself, i.e. you will get
# a container within a container which will break drag-and-drop sorting # a container within a container which will break drag-and-drop sorting
-%> -%>
$('#<%=dom_id(@context, 'container')%>').after(html_for_context_listing()); $('#<%=dom_id(@context, 'container')%>').after(<%=object_name%>.html_for_context_listing());
$('#<%=dom_id(@context, 'container')%>').remove(); $('#<%=dom_id(@context, 'container')%>').remove();
$('#<%=dom_id(@context, 'container')%>').fadeIn(500); $('#<%=dom_id(@context, 'container')%>').fadeIn(500);
}); });
} },
update_container_states: function() {
function update_container_states() {
ContextListPage.update_all_states_count(<%=@active_contexts.count%>, <%=@hidden_contexts.count%>, <%=@closed_contexts.count%>); ContextListPage.update_all_states_count(<%=@active_contexts.count%>, <%=@hidden_contexts.count%>, <%=@closed_contexts.count%>);
} },
html_for_context_listing: function() {
function html_for_context_listing() {
return "<%= escape_javascript(render(:partial => 'context_listing', :object => @context))%>"; return "<%= escape_javascript(render(:partial => 'context_listing', :object => @context))%>";
} }
<% end -%> <% end -%>
}
<%=object_name%>.animate();

View file

@ -1,4 +1,10 @@
<% if @saved -%> <% unless @saved -%>
TracksPages.show_errors(html_for_error_messages());
function html_for_error_messages() {
return "<%= escape_javascript(get_list_of_error_messages_for(@project)) %>";
}
<% else -%>
<% if @go_to_project -%> <% if @go_to_project -%>
redirect_to("<%= project_path(@project) -%>") redirect_to("<%= project_path(@project) -%>")
<% else -%> <% else -%>
@ -10,9 +16,6 @@
clear_form(); clear_form();
TracksPages.page_notify('notice', "Created new project '<%= @project.name%>'", 5); TracksPages.page_notify('notice', "Created new project '<%= @project.name%>'", 5);
<% end -%> <% end -%>
<% else -%>
TracksPages.show_errors(html_for_error_messages());
<% end -%>
/* TODO: make this generic for all pages with lists */ /* TODO: make this generic for all pages with lists */
function hide_empty_message() { function hide_empty_message() {
@ -36,10 +39,8 @@ function update_active_projects_container() {
ProjectListPage.update_state_count('active', <%=@active_projects_count%>); ProjectListPage.update_state_count('active', <%=@active_projects_count%>);
} }
function html_for_error_messages() {
return "<%= escape_javascript(get_list_of_error_messages_for(@project)) %>";
}
function html_for_project_listing() { function html_for_project_listing() {
return "<%= @saved ? escape_javascript(render(:partial => 'project_listing', :object => @project)) : "" %>"; return "<%= @saved ? escape_javascript(render(:partial => 'project_listing', :object => @project)) : "" %>";
} }
<% end -%>

View file

@ -3,27 +3,29 @@
selector_edit = "div.project-edit-current " + selector_edit unless @source_view=="project" selector_edit = "div.project-edit-current " + selector_edit unless @source_view=="project"
selector_project = "div##{dom_id(@project)}" selector_project = "div##{dom_id(@project)}"
selector_project = "div.project-edit-current " + selector_project unless @source_view=="project" selector_project = "div.project-edit-current " + selector_project unless @source_view=="project"
object_name = unique_object_name_for("edit_project_#{@project.id}")
-%> -%>
var <%=object_name%> = {
function html_for_edit_form() { html_for_edit_form: function() {
return "<%= escape_javascript(render(:partial => 'project_form', :object => @project)) %>"; return "<%= escape_javascript(render(:partial => 'project_form', :object => @project)) %>";
} },
show_edit_form: function() {
function show_edit_form() { $('<%= selector_edit %>').html(<%=object_name%>.html_for_edit_form());
$('<%= selector_edit %>').html(html_for_edit_form());
$('<%= selector_edit %>').fadeIn(500); $('<%= selector_edit %>').fadeIn(500);
$('span#project_name').editable('disable'); $('span#project_name').editable('disable');
} },
set_focus: function() {
function set_focus() {
$('input.project-name').focus(); $('input.project-name').focus();
} },
replace_project_with_edit_form: function() {
function replace_project_with_edit_form() {
$('<%= selector_project %>').fadeOut(250, function() { $('<%= selector_project %>').fadeOut(250, function() {
show_edit_form(); <%=object_name%>.show_edit_form();
set_focus(); <%=object_name%>.set_focus();
}); });
},
animate: function() {
<%=object_name%>.replace_project_with_edit_form();
}
} }
replace_project_with_edit_form(); <%=object_name%>.animate();

View file

@ -1,35 +1,39 @@
<% if @saved -%> <%-
object_name = unique_object_name_for("update_project_#{@project.id}")
-%>
var <%=object_name%> = {
<% unless @saved -%>
animate: function(){
TracksPages.show_edit_errors(<%=object_name%>.html_for_error_messages());
},
html_for_error_messages: function() {
return "<%= escape_javascript(get_list_of_error_messages_for(@project)) %>";
}
<%-
else
-%>
animate: function() {
TracksPages.page_notify('notice', '<%=t('projects.project_saved_status')%>', 5); TracksPages.page_notify('notice', '<%=t('projects.project_saved_status')%>', 5);
<% if source_view_is_one_of(:project_list, :review) -%> <% if source_view_is_one_of(:project_list, :review) -%>
update_project_list_page(); <%=object_name%>.update_project_list_page();
<% else # assume source_view :project <% else # assume source_view :project -%>
-%> <%=object_name%>.update_project_page();
update_project_page();
<% end %> <% end %>
<% else -%> },
TracksPages.show_edit_errors(html_for_error_messages()); update_project_list_page: function() {
<% end %>
<% if @saved
# only add these js functions if the project is saved
-%>
function update_project_list_page() {
<% if @state_changed -%> <% if @state_changed -%>
remove_and_re_add_project(); <%=object_name%>.remove_and_re_add_project();
<% else -%> <% else -%>
replace_project_form_with_updated_project(); <%=object_name%>.replace_project_form_with_updated_project();
<% end -%> <% end -%>
ProjectListPage.update_all_states_count(<%=@active_projects_count%>, <%=@hidden_projects_count%>, <%=@completed_projects_count%>); ProjectListPage.update_all_states_count(<%=@active_projects_count%>, <%=@hidden_projects_count%>, <%=@completed_projects_count%>);
ProjectListPage.show_or_hide_all_state_containers(<%= @show_active_projects %>, <%= @show_hidden_projects %>, <%= @show_completed_projects %>); ProjectListPage.show_or_hide_all_state_containers(<%= @show_active_projects %>, <%= @show_hidden_projects %>, <%= @show_completed_projects %>);
TracksForm.set_project_name_and_default_project_name("<%= escape_javascript(@project.name)%>"); TracksForm.set_project_name_and_default_project_name("<%= escape_javascript(@project.name)%>");
$('div.project-edit-current').removeClass('project-edit-current'); $('div.project-edit-current').removeClass('project-edit-current');
} },
update_project_page: function() {
function update_project_page() { <%=object_name%>.remove_project_edit_form();
remove_project_edit_form(); <%=object_name%>.update_and_show_project_settings();
update_and_show_project_settings();
TracksForm.set_project_name("<%= escape_javascript(@project.name)%>"); TracksForm.set_project_name("<%= escape_javascript(@project.name)%>");
$("h2 span#project_name").html("<%= escape_javascript(@project.name)%>"); $("h2 span#project_name").html("<%= escape_javascript(@project.name)%>");
<% if @project.default_context %> <% if @project.default_context %>
@ -38,24 +42,22 @@ function update_project_page() {
<% if @project.default_tags %> <% if @project.default_tags %>
TracksForm.set_tag_list_and_default_tag_list("<%= escape_javascript(@project.default_tags)%>"); TracksForm.set_tag_list_and_default_tag_list("<%= escape_javascript(@project.default_tags)%>");
<% end %> <% end %>
TracksPages.update_sidebar(html_for_sidebar()); TracksPages.update_sidebar(<%=object_name%>.html_for_sidebar());
} },
remove_project_edit_form: function() {
function remove_project_edit_form() {
<%- <%-
# do not remove() edit form as this will remove the DIV that is needed to replace with the new form, so only empty the DIV # do not remove() edit form as this will remove the DIV
# that is needed to replace with the new form, so only empty the DIV
-%> -%>
$('#<%=dom_id(@project, 'edit')%>').hide(500, function() { $('#<%=dom_id(@project, 'edit')%>').hide(500, function() {
$('#<%=dom_id(@project, 'edit')%>').html("<!- empty ->"); $('#<%=dom_id(@project, 'edit')%>').html("<!- empty ->");
}); });
} },
update_and_show_project_settings: function() {
function update_and_show_project_settings() { $('#<%=dom_id(@project, 'container')%>').html(<%=object_name%>.html_for_project_settings());
$('#<%=dom_id(@project, 'container')%>').html(html_for_project_settings());
$('#<%=dom_id(@project)%>').show(); $('#<%=dom_id(@project)%>').show();
} },
replace_project_form_with_updated_project: function() {
function replace_project_form_with_updated_project() {
$('div#<%=dom_id(@project, 'container')%>').each(function(index, elem) { $('div#<%=dom_id(@project, 'container')%>').each(function(index, elem) {
$(this).fadeOut(250, function() { $(this).fadeOut(250, function() {
<% <%
@ -64,39 +66,33 @@ function replace_project_form_with_updated_project() {
# the container instead of the container itself, i.e. you will get # the container instead of the container itself, i.e. you will get
# a container within a container which will break drag-and-drop sorting # a container within a container which will break drag-and-drop sorting
-%> -%>
$(this).after(html_for_project_listing()); $(this).after(<%=object_name%>.html_for_project_listing());
$(this).remove(); $(this).remove();
$('#<%=dom_id(@project, 'container')%>').fadeIn(500); $('#<%=dom_id(@project, 'container')%>').fadeIn(500);
})}); })
} });
},
function remove_and_re_add_project() { remove_and_re_add_project: function() {
$('#<%=dom_id(@project, 'container')%>').slideUp(500, function() { $('#<%=dom_id(@project, 'container')%>').slideUp(500, function() {
$('#<%=dom_id(@project, 'container')%>').remove(); $('#<%=dom_id(@project, 'container')%>').remove();
$('#list-<%=@project.state%>-projects').append(html_for_project_listing()); $('#list-<%=@project.state%>-projects').append(<%=object_name%>.html_for_project_listing());
}); });
} },
<% <%
# the following functions return empty string if rendering the partial is not # the following functions return empty string if rendering the partial is not
# necessary, for example the sidebar is not on the project list page, so do not # necessary, for example the sidebar is not on the project list page, so do not
# render it into the function. # render it into the function.
-%> -%>
function html_for_project_listing() { html_for_project_listing: function() {
return "<%= source_view_is_one_of(:project_list, :review) ? escape_javascript(render(:partial => 'project_listing', :object => @project, :locals=>{:suppress_drag_handle => source_view_is(:review)} )) : "" %>"; return "<%= source_view_is_one_of(:project_list, :review) ? escape_javascript(render(:partial => 'project_listing', :object => @project, :locals=>{:suppress_drag_handle => source_view_is(:review)} )) : "" %>";
} },
html_for_sidebar: function() {
function html_for_sidebar() {
return "<%= source_view_is(:project) ? escape_javascript(render(:file => 'sidebar/sidebar')) : "" %>"; return "<%= source_view_is(:project) ? escape_javascript(render(:file => 'sidebar/sidebar')) : "" %>";
} },
html_for_project_settings: function() {
function html_for_project_settings() {
return "<%= source_view_is(:project) ? escape_javascript(render(:partial => 'project_settings', :object => @project )) : "" %>"; return "<%= source_view_is(:project) ? escape_javascript(render(:partial => 'project_settings', :object => @project )) : "" %>";
} }
<% end # if @saved -%>
<% end # if @saved
-%>
function html_for_error_messages() {
return "<%= escape_javascript(get_list_of_error_messages_for(@project)) %>";
} }
<%=object_name%>.animate();

View file

@ -1,6 +1,11 @@
$('#edit-recurring-todo').html(html_for_edit_form()); <% object_name = unique_object_name_for("edit_rec_todo_#{@recurring_todo.id}") -%>
var <%=object_name%> = {
animate: function() {
$('#edit-recurring-todo').html(<%=object_name%>.html_for_edit_form());
$('#edit-recurring-todo').dialog( "open" ); $('#edit-recurring-todo').dialog( "open" );
},
function html_for_edit_form() { html_for_edit_form: function() {
return "<%= escape_javascript(render(:partial => 'edit_form')) %>"; return "<%= escape_javascript(render(:partial => 'edit_form')) %>";
} }
}
<%=object_name%>.animate();

View file

@ -1,51 +1,51 @@
<%- if @saved -%> <%- unless @saved -%>
TracksPages.set_page_badge(<%= @down_count %>);
remove_old_and_add_updated_recurring_todo();
inform_if_new_todo_created();
<%- else -%>
TracksPages.page_notify('error', '<%= t('todos.error_completing_todo', :description => @recurring_todo.description) %>', 8); TracksPages.page_notify('error', '<%= t('todos.error_completing_todo', :description => @recurring_todo.description) %>', 8);
<%- end -%> <%- else
object_name = unique_object_name_for("toggle_check_rec")
function inform_if_new_todo_created() { -%>
<%- unless @new_recurring_todo.nil? -%> var <%=object_name%> = {
animate: function() {
TracksPages.set_page_badge(<%= @down_count %>);
<%=object_name%>.remove_old_and_add_updated_recurring_todo();
<%= "#{object_name}.inform_if_new_todo_created();" if @new_recurring_todo -%>
},
inform_if_new_todo_created: function() {
TracksPages.page_notify('notice', '<%= t('todos.new_related_todo_created') %>', 5); TracksPages.page_notify('notice', '<%= t('todos.new_related_todo_created') %>', 5);
<%- end -%> },
} remove_old_and_add_updated_recurring_todo: function() {
function remove_old_and_add_updated_recurring_todo() {
$('#<%=dom_id(@recurring_todo)%>').slideUp(1000, function() { $('#<%=dom_id(@recurring_todo)%>').slideUp(1000, function() {
$('#<%=dom_id(@recurring_todo)%>').remove(); $('#<%=dom_id(@recurring_todo)%>').remove();
show_empty_messages(); <%=object_name%>.show_empty_messages();
<%- if @recurring_todo.completed? -%> <%- if @recurring_todo.completed? -%>
add_recurring_todo_to_completed_container(); <%=object_name%>.add_recurring_todo_to_completed_container();
<%- else -%> <%- else -%>
add_recurring_todo_to_active_container(); <%=object_name%>.add_recurring_todo_to_active_container();
<%- end -%> <%- end -%>
}); });
} },
add_recurring_todo_to_completed_container: function() {
function add_recurring_todo_to_completed_container() { $('#completed_recurring_todos_container').append(<%=object_name%>.html_for_recurring_todo());
$('#completed_recurring_todos_container').append(html_for_recurring_todo());
$('#<%= dom_id(@recurring_todo)%>').effect('highlight', {}, 2000 ); $('#<%= dom_id(@recurring_todo)%>').effect('highlight', {}, 2000 );
$('#completed-empty-nd').hide(); $('#completed-empty-nd').hide();
} },
add_recurring_todo_to_active_container: function() {
function add_recurring_todo_to_active_container() { $('#recurring_todos_container').append(<%=object_name%>.html_for_recurring_todo());
$('#recurring_todos_container').append(html_for_recurring_todo());
$('#<%= dom_id(@recurring_todo)%>').effect('highlight', {}, 2000 ); $('#<%= dom_id(@recurring_todo)%>').effect('highlight', {}, 2000 );
$('#recurring-todos-empty-nd').hide(); $('#recurring-todos-empty-nd').hide();
} },
show_empty_messages: function() {
function show_empty_messages() {
<%- if @active_remaining == 0 -%> <%- if @active_remaining == 0 -%>
$('#recurring-todos-empty-nd').show(); $('#recurring-todos-empty-nd').show();
<%- end -%> <%- end -%>
<%- if @completed_remaining == 0 -%> <%- if @completed_remaining == 0 -%>
$('#completed-empty-nd').show(); $('#completed-empty-nd').show();
<%- end -%> <%- end -%>
} },
html_for_recurring_todo: function() {
function html_for_recurring_todo() {
return "<%= @saved ? escape_javascript(render(:partial => @recurring_todo)) : "" %>"; return "<%= @saved ? escape_javascript(render(:partial => @recurring_todo)) : "" %>";
} }
}
<%=object_name%>.animate();
<%- end -%>

View file

@ -1,28 +1,40 @@
hide_todo(); <%- object_name = unique_object_name_for("edit_todo_#{@todo.id}") %>
replace_placeholder_with_form();
fill_dependency_array();
enable_rich_interaction();
function hide_todo() { function add_spec(id, spec) {
$('#<%= dom_id(@todo, 'line') %>').hide(); <%-
# do this outside of object_name to fill spec_of_todo in
# global namespace, not in namespace of object. Otherwise
# spec_of_todo is not available for the js attached to the
# edit form
-%>
spec_of_todo[id] = spec;
} }
function replace_placeholder_with_form() { var <%=object_name%> = {
$('#<%=dom_id(@todo, 'edit')%>').html(html_for_edit_form()); animate: function() {
<%=object_name%>.hide_todo();
<%=object_name%>.replace_placeholder_with_form();
<%=object_name%>.fill_dependency_array();
enable_rich_interaction();
},
hide_todo: function() {
$('#<%= dom_id(@todo, 'line') %>').hide();
},
replace_placeholder_with_form: function() {
$('#<%=dom_id(@todo, 'edit')%>').html(<%=object_name%>.html_for_edit_form());
$('#<%=dom_id(@todo, 'edit')%>').show(); $('#<%=dom_id(@todo, 'edit')%>').show();
$('#<%=dom_id(@todo, 'form')%> input#todo_description').focus(); $('#<%=dom_id(@todo, 'form')%> input#todo_description').focus();
} },
fill_dependency_array: function() {
function fill_dependency_array() {
spec_of_todo = new Array(); spec_of_todo = new Array();
<% <%
@todo.predecessors.each do |dep| -%> @todo.predecessors.each do |dep| -%>
spec_of_todo['<%=dep.id%>'] = "<%= escape_javascript(dep.specification)%>"; add_spec('<%=dep.id%>', "<%= escape_javascript(dep.specification)%>");
<% end -%> <% end -%>
} },
html_for_edit_form: function() {
function html_for_edit_form() {
return "<%= escape_javascript(render(:partial => 'todos/edit_form', :object => @todo)) %>" return "<%= escape_javascript(render(:partial => 'todos/edit_form', :object => @todo)) %>"
} }
}
<%=object_name%>.animate();

View file

@ -1,8 +1,8 @@
<% unless @saved -%> <% unless @saved -%>
TracksPages.page_notify('error', "<%= t('todos.error_toggle_complete') %>", 5); TracksPages.page_notify('error', "<%= t('todos.error_toggle_complete') %>", 5);
<% else <% else
# create a unique obejct name to prevent concurrent toggles to overwrite each other functions # create a unique object name to prevent concurrent toggles to overwrite each other functions
object_name = "toggle_check_#{SecureRandom.hex(5)}" object_name = unique_object_name_for("toggle_check")
-%> -%>
var <%= object_name %> = { var <%= object_name %> = {
animate: function() { animate: function() {

View file

@ -1,16 +1,17 @@
<%-
object_name = unique_object_name_for("update_context_#{@context.id}")
-%>
var <%=object_name%> = {
<% unless @saved -%> <% unless @saved -%>
animate: function() {
TracksPages.show_edit_errors(html_for_error_messages()); TracksPages.show_edit_errors(html_for_error_messages());
},
function html_for_error_messages() { html_for_error_messages: function() {
return "<%= escape_javascript(get_list_of_error_messages_for(@todo)) %>"; return "<%= escape_javascript(get_list_of_error_messages_for(@todo)) %>";
} }
<% else -%>
<% else animate: function() {
<%-
# jquery animations are async, so first collect all animation steps that need
# to be run sequential,then execute them. All steps are functions which are
# passed a function as parameter that will execute the next animation steps
animation = [] animation = []
animation << "remove_todo" if update_needs_to_remove_todo_from_container animation << "remove_todo" if update_needs_to_remove_todo_from_container
if replace_with_updated_todo if replace_with_updated_todo
@ -22,26 +23,25 @@
animation << "highlight_updated_todo" animation << "highlight_updated_todo"
animation << "update_empty_container" if source_view_is_one_of(:tag, :todo, :deferred, :project, :context) animation << "update_empty_container" if source_view_is_one_of(:tag, :todo, :deferred, :project, :context)
animation << "update_predecessors" animation << "update_predecessors"
%> -%>
TracksPages.page_notify('notice', '<%=escape_javascript @status_message%>', 5); TracksPages.page_notify('notice', '<%=escape_javascript @status_message%>', 5);
TracksPages.set_page_badge(<%= @down_count %>); TracksPages.set_page_badge(<%= @down_count %>);
<%= render_animation(animation) %> <%= render_animation(animation, object_name) %>
},
function remove_todo(next_steps) { remove_todo: function(next_steps) {
$('#<%= dom_id(@todo) %>').fadeOut(400, function() { $('#<%= dom_id(@todo) %>').fadeOut(400, function() {
$('#<%= dom_id(@todo) %>').remove(); $('#<%= dom_id(@todo) %>').remove();
<% if source_view_is :calendar <% if source_view_is :calendar
# in calendar view it is possible to have a todo twice on the page # in calendar view it is possible to have a todo on the page twice
-%> -%>
$('#<%= dom_id(@todo) %>').remove(); $('#<%= dom_id(@todo) %>').remove();
<% end %> <% end -%>
<%= show_empty_message_in_source_container -%> <%= show_empty_message_in_source_container -%>
next_steps.go(); next_steps.go();
}); });
} },
add_to_existing_container: function(next_steps) {
function add_to_existing_container(next_steps) { $('#<%= item_container_id(@todo) %>_items').append(<%=object_name%>.html_for_todo());
$('#<%= item_container_id(@todo) %>_items').append(html_for_todo());
<% if source_view_is_one_of(:calendar) -%> <% if source_view_is_one_of(:calendar) -%>
next_steps.go(); next_steps.go();
<% if (@target_context_count==1) || ( (@todo.deferred? || @todo.pending?) && @remaining_deferred_or_pending_count == 1) -%> <% if (@target_context_count==1) || ( (@todo.deferred? || @todo.pending?) && @remaining_deferred_or_pending_count == 1) -%>
@ -65,53 +65,44 @@ function add_to_existing_container(next_steps) {
<% end -%> <% end -%>
<% end -%> <% end -%>
<% end -%> <% end -%>
} },
replace_todo: function(next_steps) {
function replace_todo(next_steps) { $('#<%= dom_id(@todo) %>').html(<%=object_name%>.html_for_todo());
$('#<%= dom_id(@todo) %>').html(html_for_todo());
next_steps.go(); next_steps.go();
} },
hide_container: function(next_steps) {
function hide_container(next_steps) {
$('#<%= item_container_id(@original_item) %>').fadeOut(400, function(){ next_steps.go(); }); $('#<%= item_container_id(@original_item) %>').fadeOut(400, function(){ next_steps.go(); });
<%= "$('#deferred_pending_container_empty-nd').slideDown(400);".html_safe if source_view_is(:deferred) && @down_count == 0 %> <%= "$('#deferred_pending_container_empty-nd').slideDown(400);".html_safe if source_view_is(:deferred) && @down_count == 0 %>
} },
highlight_updated_todo: function(next_steps) {
function highlight_updated_todo(next_steps) {
$('#<%= dom_id(@todo)%>').effect('highlight', {}, 2000, function(){ }); $('#<%= dom_id(@todo)%>').effect('highlight', {}, 2000, function(){ });
next_steps.go(); next_steps.go();
} },
update_empty_container: function(next_steps) {
function update_empty_container(next_steps) {
<% if should_show_empty_container -%> <% if should_show_empty_container -%>
$('div#no_todos_in_view').slideDown(400, function(){ next_steps.go(); }); $('div#no_todos_in_view').slideDown(400, function(){ next_steps.go(); });
<% else -%> <% else -%>
$('div#no_todos_in_view').fadeOut(100, function(){ next_steps.go(); }); $('div#no_todos_in_view').fadeOut(100, function(){ next_steps.go(); });
<% end -%> <% end -%>
} },
update_badge_count: function() {
function update_badge_count() {
<% <%
count = source_view_is(:context) ? @remaining_in_context : @down_count count = source_view_is(:context) ? @remaining_in_context : @down_count
count = @project_changed ? @remaining_undone_in_project : count count = @project_changed ? @remaining_undone_in_project : count
-%> -%>
TracksPages.set_page_badge(<%= count %>); TracksPages.set_page_badge(<%= count %>);
} },
insert_new_container_with_updated_todo: function(next_steps) {
function insert_new_container_with_updated_todo(next_steps) { $('#display_box').prepend(<%=object_name%>.html_for_new_container());
$('#display_box').prepend(html_for_new_container());
$('#<%= item_container_id(@todo) %>').fadeIn(500, function() { next_steps.go(); }); $('#<%= item_container_id(@todo) %>').fadeIn(500, function() { next_steps.go(); });
} },
html_for_todo: function() {
function html_for_todo() {
return "<%= escape_javascript(render(:partial => @todo, :locals => { :parent_container_type => parent_container_type })) %>"; return "<%= escape_javascript(render(:partial => @todo, :locals => { :parent_container_type => parent_container_type })) %>";
} },
html_for_new_container: function() {
function html_for_new_container() {
return "<%= ( @new_context_created || @new_project_created ) ? escape_javascript(render(:partial => @new_container, :locals => { :settings => {:collapsible => true }})) : "" %>"; return "<%= ( @new_context_created || @new_project_created ) ? escape_javascript(render(:partial => @new_container, :locals => { :settings => {:collapsible => true }})) : "" %>";
} },
update_predecessors: function(next_steps) {
function update_predecessors(next_steps) {
regenerate_predecessor_family(); regenerate_predecessor_family();
<% if @removed_predecessors <% if @removed_predecessors
@removed_predecessors.each do |p| -%> @removed_predecessors.each do |p| -%>
@ -121,9 +112,8 @@ function update_predecessors(next_steps) {
<% end -%> <% end -%>
<% end -%> <% end -%>
next_steps.go(); next_steps.go();
} },
regenerate_predecessor_family: function() {
function regenerate_predecessor_family() {
<% <%
parents = @todo.predecessors.to_a parents = @todo.predecessors.to_a
until parents.empty? until parents.empty?
@ -135,5 +125,6 @@ function regenerate_predecessor_family() {
end end
-%> -%>
} }
<% end # if @saved -%>
<% end %> }
<%=object_name%>.animate();