TracksApp/tracks

View on GitHub
app/views/projects/update.js.erb

Summary

Maintainability
Test Coverage
<%-
  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 "<%= js_error_messages_for(@project) %>";
    }
  <%-
    else
  -%>
    animate: function() {
      TracksPages.page_inform('<%=t('projects.project_saved_status')%>');
      <% if source_view_is_one_of(:project_list, :review) -%>
        <%=object_name%>.update_project_list_page();
      <% else # assume source_view :project -%>
        <%=object_name%>.update_project_page();
      <% end %>
    },
    update_project_list_page: function() {
      <% if @state_changed -%>
        <%=object_name%>.remove_and_re_add_project();
      <% else -%>
        <%=object_name%>.replace_project_form_with_updated_project();
      <% end -%>
      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 %>);
      $('div.project-edit-current').removeClass('project-edit-current');
    },
    update_project_page: function() {
      <%=object_name%>.remove_project_edit_form();
      <%=object_name%>.update_and_show_project_settings();
      TracksForm.set_project_name_and_default_project_name("<%= escape_javascript(@project.name)%>");
      $("h2 span#project_name").html("<%= escape_javascript(@project.name)%>");
      <% if @project.default_context %>
        TracksForm.set_context_name_and_default_context_name("<%= escape_javascript(@project.default_context.name)%>");
      <% end %>
      <% if @project.default_tags %>
        TracksForm.set_tag_list_and_default_tag_list("<%= escape_javascript(@project.default_tags)%>");
      <% end %>
      TracksPages.update_sidebar(<%=object_name%>.html_for_sidebar());
    },
    remove_project_edit_form: function() {
      <%-
          # 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')%>').html("<!- empty ->");
      });
    },
    update_and_show_project_settings: function() {
      $('#<%=dom_id(@project, 'container')%>').html(<%=object_name%>.html_for_project_settings());
      $('#<%=dom_id(@project)%>').show();
    },
    replace_project_form_with_updated_project: function() {
      $('div#<%=dom_id(@project, 'container')%>').each(function(index, elem) {
        $(this).fadeOut(250, function() {
        <%
          # first add the updated project after the old one, then remove old one.
          # Using html() does not work, because it will replace the _content_ of
          # the container instead of the container itself, i.e. you will get
          # a container within a container which will break drag-and-drop sorting
        -%>
          $(this).after(<%=object_name%>.html_for_project_listing());
          $(this).remove();
          $('#<%=dom_id(@project, 'container')%>').fadeIn(500);
        })
      });
    },
    remove_and_re_add_project: function() {
      $('#<%=dom_id(@project, 'container')%>').slideUp(500, function() {
        $('#<%=dom_id(@project, 'container')%>').remove();
        $('#list-<%=@project.state%>-projects').append(<%=object_name%>.html_for_project_listing());
        });
    },
    <%
    # 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
    # render it into the function.
    -%>
    html_for_project_listing: function() {
      return "<%= source_view_is_one_of(:project_list, :review) ? js_render('project_listing', {:suppress_drag_handle => source_view_is(:review)}, @project) : "" %>";
    },
    html_for_sidebar: function() {
      return "<%= source_view_is(:project) ? escape_javascript(render(:file => 'sidebar/sidebar')) : "" %>";
    },
    html_for_project_settings: function() {
      return "<%= source_view_is(:project) ? js_render('project_settings', {}, @project) : "" %>";
    }
  <% end # if @saved -%>
}

<%=object_name%>.animate();