modify todo controller to store multiple next actions

This commit is contained in:
Reinier Balt 2010-07-14 20:20:55 +02:00
parent c9ce82d533
commit 4400c42d7c
5 changed files with 246 additions and 121 deletions

View file

@ -49,6 +49,10 @@ class TodosController < ApplicationController
def create def create
@source_view = params['_source_view'] || 'todo' @source_view = params['_source_view'] || 'todo'
@tag_name = params['_tag_name'] @tag_name = params['_tag_name']
unless params[:todo][:multiple_todos].nil?
create_multiple
else
p = TodoCreateParamsHelper.new(params, prefs) p = TodoCreateParamsHelper.new(params, prefs)
p.parse_dates() unless mobile? p.parse_dates() unless mobile?
tag_list = p.tag_list tag_list = p.tag_list
@ -122,6 +126,61 @@ class TodosController < ApplicationController
end end
end end
end end
end
def create_multiple
if project_specified_by_name(params[:project_name])
project = current_user.projects.find_or_create_by_name(params[:project_name])
@new_project_created = project.new_record_before_save?
@project_id = project.id
end
if context_specified_by_name(params[:context_name])
context = current_user.contexts.find_or_create_by_name(params[:context_name])
@new_context_created = context.new_record_before_save?
@not_done_todos = [] if @new_context_created
@context_id = context.id
end
tag_list = params[:tag_list]
@todos = []
params[:todo][:multiple_todos].split("\n").map do |line|
@todo = current_user.todos.build(
:description => line)
@todo.project_id = @project_id
@todo.context_id = @context_id
puts "TODO: #{@todo.description}, #{@todo.project_id}, #{@todo.context_id}"
@saved = @todo.save
puts "NOT SAVED" unless @saved
unless (@saved == false) || tag_list.blank?
@todo.tag_with(tag_list)
@todo.tags.reload
end
@todos << @todo
@not_done_todos << @todo if @new_context_created
end
respond_to do |format|
format.html { redirect_to :action => "index" }
format.js do
determine_down_count if @saved
@contexts = current_user.contexts.find(:all) if @new_context_created
@projects = current_user.projects.find(:all) if @new_project_created
@initial_context_name = params['default_context_name']
@initial_project_name = params['default_project_name']
@default_tags = @todos[0].project.default_tags unless @todos[0].project.nil?
render :action => 'create_multiple'
end
format.xml do
if @saved
head :created, :location => context_url(@todos[0].context)
else
render :xml => @todos[0].errors.to_xml, :status => 422
end
end
end
end
def edit def edit
@projects = current_user.projects.find(:all) @projects = current_user.projects.find(:all)
@ -1144,4 +1203,20 @@ class TodosController < ApplicationController
end end
end end
private
def project_specified_by_name(project_name)
return false unless params['project_id'].blank?
return false if project_name.blank?
return false if project_name == 'None'
true
end
def context_specified_by_name(context_name)
return false unless params['context_id'].blank?
return false if context_name.blank?
true
end
end end

View file

@ -18,7 +18,7 @@
:html=> { :id=>'todo-form-new-action', :name=>'todo', :class => 'inline-form' }, :html=> { :id=>'todo-form-new-action', :name=>'todo', :class => 'inline-form' },
:before => "$('#todo_new_action_submit').block({message:null})", :before => "$('#todo_new_action_submit').block({message:null})",
:complete => "$('#todo_new_action_submit').unblock()", :complete => "$('#todo_new_action_submit').unblock()",
:condition => "askIfNewContextProvided()") do -%> :condition => "askIfNewContextProvided('')") do -%>
<div id="status"><%= error_messages_for("item", :object_name => 'action') %></div> <div id="status"><%= error_messages_for("item", :object_name => 'action') %></div>
@ -71,33 +71,41 @@
</div> </div>
<div id="todo_multi_add" style="display:none"> <div id="todo_multi_add" style="display:none">
<form id="todo-form-multi-new-acion" class="inline-form"> <% form_remote_tag(
:url => todos_path, :method => :post,
:html=> { :id=>'todo-form-multi-new-action', :name=>'todo', :class => 'inline-form' },
:before => "$('#todo_multi_new_action_submit').block({message:null})",
:complete => "$('#todo_multi_new_action_submit').unblock()",
:condition => "askIfNewContextProvided('multi_')") do -%>
<label for="todo_notes">Multiple next actions (one on each line)</label> <label for="todo_notes">Multiple next actions (one on each line)</label>
<%= text_area( "todo", "multiple_todos", "cols" => 29, "rows" => 6, "tabindex" => 2) %> <%= text_area( "todo", "multiple_todos", "cols" => 29, "rows" => 6, "tabindex" => 2) %>
<input id="default_project_name_id" name="default_project_name" type="hidden" value="<%=@initial_project_name-%>" />
<label for="todo_project_name">Project for all actions</label> <label for="todo_project_name">Project for all actions</label>
<input id="todo_project_name" name="project_name" autocomplete="off" tabindex="3" size="30" type="text" value="<%= @initial_project_name %>" /> <input id="multi_todo_project_name" name="project_name" autocomplete="off" tabindex="3" size="30" type="text" value="<%= @initial_project_name %>" />
<div class="page_name_auto_complete" id="project_list" style="display:none"></div> <div class="page_name_auto_complete" id="project_list" style="display:none"></div>
<input id="default_context_name_id" name="default_context_name" type="hidden" value="<%=@initial_context_name-%>" /> <input id="default_context_name_id" name="default_context_name" type="hidden" value="<%=@initial_context_name-%>" />
<label for="todo_context_name">Context for all actions</label> <label for="todo_context_name">Context for all actions</label>
<input id="todo_context_name" name="context_name" autocomplete="off" tabindex="4" size="30" type="text" value="<%= @initial_context_name %>" /> <input id="multi_todo_context_name" name="context_name" autocomplete="off" tabindex="4" size="30" type="text" value="<%= @initial_context_name %>" />
<div class="page_name_auto_complete" id="context_list" style="display:none"></div> <div class="page_name_auto_complete" id="context_list" style="display:none"></div>
<label for="tag_list">Tags for all actions (sep. with commas)</label> <label for="tag_list">Tags for all actions (sep. with commas)</label>
<%= text_field_tag "tag_list", @default_tags, :size => 30, :tabindex => 5 %> <%= text_field_tag "multi_tag_list", @default_tags, :name=>:tag_list, :size => 30, :tabindex => 5 %>
<%= content_tag("div", "", :id => "tag_list_auto_complete", :class => "auto_complete") %> <%= content_tag("div", "", :id => "tag_list_auto_complete", :class => "auto_complete") %>
<div class="submit_box"> <div class="submit_box">
<div class="widgets"> <div class="widgets">
<button type="submit" class="positive" id="todo_multi_new_action_submit" tabindex="8"> <button type="submit" class="positive" id="todo_multi_new_action_submit" tabindex="8">
<%= image_tag("accept.png", :alt => "") %>Add action <%= image_tag("accept.png", :alt => "") %>Add actions
</button> </button>
</div> </div>
</div> </div>
</form> <% end -%>
</div> </div>

View file

@ -0,0 +1,42 @@
if @saved
page.hide 'status'
status_message = 'Added new next action'
status_message += 's' if @todos.size > 1
status_message = 'Added new project / ' + status_message if @new_project_created
status_message = 'Added new context / ' + status_message if @new_context_created
page.notify :notice, status_message, 5.0
page['badge_count'].replace_html @down_count
# reset form and set focus to first field
page.send :record, "$('#todo-form-multi-new-action').clearForm();$('#todo-form-multi-new-action input:text:first').focus();"
# set defaults of form
page.send :record, "$('#multi_todo_context_name').val('#{@initial_context_name}');"
page.send :record, "$('#multi_todo_project_name').val('#{@initial_project_name}');"
page.send :record, "$('#multi_tag_list').val('#{@default_tags}');"
if should_show_new_item()
if @new_context_created
page.insert_html :top, 'display_box', :partial => 'contexts/context', :locals => { :context => @todo.context, :collapsible => true }
else
page.call "todoItems.ensureVisibleWithEffectAppear", "c#{@todo.context_id}" if source_view_is_one_of(:todo, :deferred, :tag)
@todos.each do |todo|
page.insert_html :bottom, item_container_id(todo), :partial => 'todos/todo', :locals => { :todo => todo, :parent_container_type => parent_container_type, :source_view => @source_view }
page.visual_effect :highlight, dom_id(todo), :duration => 3
end
page[empty_container_msg_div_id].hide unless empty_container_msg_div_id.nil?
end
if (source_view_is :project and @todo.pending?) or (source_view_is :deferred)
page['tickler-empty-nd'].hide # For some reason this does not work: page['tickler-empty-nd'].hide if (@todo.pending? or (source_view_is :deferred))
end
end
# make sure the behavior of the new/updated todo is enabled
page << "enable_rich_interaction();"
else
page.show 'status'
page.replace_html 'status', "#{error_messages_for('todo', :object_name => 'action')}"
end

View file

@ -11,7 +11,7 @@ var TracksForm = {
toggleLink.text(hideLinkText).attr('title', hideLinkTitle); toggleLink.text(hideLinkText).attr('title', hideLinkTitle);
$('#'+formId+' input:text:first').focus(); $('#'+formId+' input:text:first').focus();
} }
toggleLink.parent.toggleClass('hide_form'); toggleLink.parent().toggleClass('hide_form');
}, },
hide_all_recurring: function () { hide_all_recurring: function () {
$.each(['daily', 'weekly', 'monthly', 'yearly'], function(){ $.each(['daily', 'weekly', 'monthly', 'yearly'], function(){
@ -137,8 +137,8 @@ function setup_container_toggles(){
}); });
} }
function askIfNewContextProvided() { function askIfNewContextProvided(source) {
var givenContextName = $('#todo_context_name').val(); var givenContextName = $('#'+source+'todo_context_name').val();
var contextNames = []; var contextNames = [];
var contextNamesRequest = $.ajax({url: relative_to_root('contexts.autocomplete'), var contextNamesRequest = $.ajax({url: relative_to_root('contexts.autocomplete'),
async: false, async: false,

View file

@ -825,7 +825,7 @@ input#go_to_project, input#context_hide {
float: right; float: right;
} }
#todo-form-new-action .submit_box, #project_form .submit_box, #context_form .submit_box, #todo-form-multi-new-acion .submit_box { #todo-form-new-action .submit_box, #project_form .submit_box, #context_form .submit_box, #todo-form-multi-new-action .submit_box {
height: 25px; height: 25px;
padding: 5px 0; padding: 5px 0;
text-align: center; text-align: center;