Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort containers #1823

Closed
wants to merge 11 commits into from
16 changes: 16 additions & 0 deletions app/assets/javascripts/tracks_pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,21 @@ var TracksPages = {

/* fade flashes and alerts in automatically */
$(".alert").fadeOut(8000);
}, sort_container: function(container) {
function comparator(a, b) {
var contentA = $(a).attr('data-sort') || '';
var contentB = $(b).attr('data-sort') || '';
if (contentA > contentB) {
return 1;
}
if (contentB > contentA) {
return -1;
}
return 0;
}

var unsortedActions = container.children();
var sortedChildren = unsortedActions.sort(comparator);
container.append(sortedChildren);
}
};
12 changes: 12 additions & 0 deletions app/helpers/todos_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,18 @@ def date_field_tag(name, id, value = nil, options = {})
text_field_tag name, value, {"size" => 12, "id" => id, "class" => "Date", "autocomplete" => "off"}.update(options.stringify_keys)
end

def sort_key(todo)
# actions are sorted using {order("todos.due IS NULL, todos.due ASC, todos.created_at ASC")}
# the JavaScript frontend sorts using unicode/ascii
format = "%Y%m%d%H%M%S%L"
if todo.due?
sort_by_due = todo.due.strftime format
else
sort_by_due = "Z" * 17 # length of format string
end
sort_by_due + todo.created_at.strftime(format)
end

# === helpers for default layout

def default_contexts_for_autocomplete
Expand Down
2 changes: 1 addition & 1 deletion app/views/todos/_todo.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ parameters += "&_tag_name=#{@tag_name}" if @source_view == 'tag'
# also make different caches per source_view to handle difference in showing [C] and [P]
cache [todo, current_user.date.strftime("%Y%m%d"), @source_view, current_user.prefs.verbose_action_descriptors] do
%>
<div id="<%= dom_id(todo) %>" class="item-container">
<div id="<%= dom_id(todo) %>" class="item-container" data-sort="<%= sort_key(todo) %>">
<div id="<%= dom_id(todo, 'line') %>" class="item-show">
<%= remote_star_icon(todo) %>
<%= remote_toggle_checkbox(todo) %>
Expand Down
6 changes: 4 additions & 2 deletions app/views/todos/_update_successful.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ remove_todo: function(next_steps) {
});
},
add_to_existing_container: function(next_steps) {
$('#<%= item_container_id(@todo) %>_items').append(<%=object_name%>.html_for_todo());
var container = $('#<%= item_container_id(@todo) %>_items');
container.append(<%=object_name%>.html_for_todo());
TracksPages.sort_container(container);
<% if source_view_is_one_of(:calendar) -%>
next_steps.go();
<% if (@target_context_count==1) || ( (@todo.deferred? || @todo.pending?) && @remaining_deferred_or_pending_count == 1) -%>
Expand All @@ -55,7 +57,7 @@ add_to_existing_container: function(next_steps) {
<% end -%>
},
replace_todo: function(next_steps) {
$('#<%= dom_id(@todo) %>').html(<%=object_name%>.html_for_todo());
$('#<%= dom_id(@todo) %>').replaceWith(<%=object_name%>.html_for_todo());
next_steps.go();
},
hide_container: function(next_steps) {
Expand Down
5 changes: 4 additions & 1 deletion app/views/todos/create.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@

function add_todo_to_existing_container(next_steps) {
$('#<%= empty_container_msg_div_id %>').hide();
$('#<%= item_container_id(@todo) %>_items').append(html_for_new_todo());
var container = $('#<%= item_container_id(@todo) %>_items');
container.append(html_for_new_todo());
TracksPages.sort_container(container);

$('#<%= item_container_id(@todo) %>').slideDown(500, function() {
$('#<%= dom_id(@todo) %>').effect('highlight', {}, 2000 );
next_steps.go();
Expand Down
12 changes: 9 additions & 3 deletions app/views/todos/destroy.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ function show_new_todo_if_todo_was_recurring() {
<% if @todo.from_recurring_todo? -%>
<% unless @new_recurring_todo.nil? || @new_recurring_todo.deferred? -%>
TodoItemsContainer.ensureVisibleWithEffectAppear("<%=item_container_id(@new_recurring_todo)%>");
$('#<%=item_container_id(@new_recurring_todo)%>_items').append(html_for_new_recurring_todo());
var container = $('#<%=item_container_id(@new_recurring_todo)%>_items');
container.append(html_for_new_recurring_todo());
TracksPages.sort_container(container);
$('#<%= dom_id(@new_recurring_todo, 'line')%>').effect('highlight', {}, 2000 );
TracksPages.page_inform("<%=t('todos.recurring_action_deleted')%>");
<% else -%>
Expand All @@ -75,11 +77,15 @@ function activate_pending_todos() {
if source_view_is_one_of(:project,:tag) -%>
$('#<%= dom_id(t) %>').fadeOut(400, function() {
$('#<%= dom_id(t) %>').remove();
$('#<%= item_container_id(t) %>_items').append("<%= html %>");
var container = $('#<%= item_container_id(t) %>_items');
container.append("<%= html %>");
TracksPages.sort_container(container);
<%= "$('#deferred_pending_container-empty-d').show();".html_safe if @remaining_deferred_or_pending_count==0 -%>
});
<% else -%>
$('#<%= item_container_id(t) %>_items').append("<%= html%>");
var container = $('#<%= item_container_id(t) %>_items');
container.append("<%= html%>");
TracksPages.sort_container(container);
<% end -%>
TodoItems.highlight_todo('#<%= dom_id(t, 'line')%>');
<% end -%>
Expand Down
6 changes: 4 additions & 2 deletions app/views/todos/remove_predecessor.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<% end -%>

function replace_updated_predecessor() {
$('#<%= dom_id(@predecessor) %>').html( html_for_predecessor() );
$('#<%= dom_id(@predecessor) %>').replaceWith( html_for_predecessor() );
}

function regenerate_predecessor_family() {
Expand All @@ -33,7 +33,9 @@ function update_successor() {
$('#c<%= @successor.context_id %>').fadeIn(500, function() {});
$('#no_todos_in_view').slideUp(100);
<% end -%>
$('#<%=item_container_id(@successor)%>').append(html_for_new_successor());
var container = $('#<%=item_container_id(@successor)%>_items');
container.append(html_for_new_successor());
TracksPages.sort_container(container);
$('#<%= dom_id(@successor, 'line')%>').effect('highlight', {}, 2000 ); <%
elsif @successor.deferred? -%>
$('#<%= dom_id(@successor)%>').html(html_for_new_successor()); <%
Expand Down
16 changes: 12 additions & 4 deletions app/views/todos/toggle_check.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ var <%= object_name %> = {
next_steps.go();
},
add_todo_to_container: function(next_steps) {
$('#<%= item_container_id(@todo) %>_items').append(<%=object_name%>.html_for_todo());
var container = $('#<%= item_container_id(@todo) %>_items');
container.append(<%=object_name%>.html_for_todo());
TracksPages.sort_container(container);
<% if should_make_context_visible -%>
$('#<%= item_container_id(@todo) %>').slideDown(500, function() {
$("#<%= empty_container_msg_div_id %>").slideUp(100);
Expand All @@ -82,7 +84,9 @@ var <%= object_name %> = {
<% # show new todo if the completed todo was recurring
if @todo.from_recurring_todo?
unless @new_recurring_todo.nil? || (@new_recurring_todo.deferred? && !source_view_is(:deferred)) -%>
$('#<%= item_container_id(@new_recurring_todo) %>_items').append(<%=object_name%>.html_for_recurring_todo());
var container = $('#<%= item_container_id(@new_recurring_todo) %>_items');
conainer.append(<%=object_name%>.html_for_recurring_todo());
TracksPages.sort_container(container);
$('#c<%= @new_recurring_todo.context_id %>').slideDown(500, function() {
<%=object_name%>.highlight_updated_recurring_todo(next_steps);
});
Expand Down Expand Up @@ -123,11 +127,15 @@ var <%= object_name %> = {
if source_view_is_one_of(:project,:tag) -%>
$('#<%= dom_id(t) %>').slideUp(400, function() {
$('#<%= dom_id(t) %>').remove();
$('#<%= item_container_id(t) %>_items').append("<%= html %>");
var container = $('#<%= item_container_id(t) %>_items');
container.append("<%= html %>");
TracksPages.sort_container(container);
<%= "$('#deferred_pending_container-empty-d').show();".html_safe if @remaining_deferred_or_pending_count==0 -%>
});
<% else -%>
$('#<%= item_container_id(t) %>_items').append("<%= html%>");
var container = $('#<%= item_container_id(t) %>_items');
container.append("<%= html%>");
TracksPages.sort_container(container);
<% end -%>
TodoItems.highlight_todo('#<%= dom_id(t)%>');
<% end -%>
Expand Down