WIP: contexts page mostly working

This commit is contained in:
Eric Allen 2009-10-02 19:45:49 -04:00
parent eac798d0bf
commit acad0596be
9 changed files with 104 additions and 86 deletions

View file

@ -92,7 +92,8 @@ class ContextsController < ApplicationController
if @context.save if @context.save
if boolean_param('wants_render') if boolean_param('wants_render')
@context_state_changed = (@orgininal_context_hidden != @context.hidden?) @context_state_changed = ((@orgininal_context_hidden || false) != @context.hidden?)
puts "CHANGED: #{@original_context_hidden}, #{@context.hidden?}, #{(@orgininal_context_hidden == @context.hidden?)}"
@new_state = (@context.hidden? ? "hidden" : "active") if @context_state_changed @new_state = (@context.hidden? ? "hidden" : "active") if @context_state_changed
respond_to do |format| respond_to do |format|
format.js format.js
@ -110,6 +111,13 @@ class ContextsController < ApplicationController
end end
end end
def edit
@context = Context.find(params[:id])
respond_to do |format|
format.js
end
end
# Fairly self-explanatory; deletes the context If the context contains # Fairly self-explanatory; deletes the context If the context contains
# actions, you'll get a warning dialogue. If you choose to go ahead, any # actions, you'll get a warning dialogue. If you choose to go ahead, any
# actions in the context will also be deleted. # actions in the context will also be deleted.
@ -124,11 +132,12 @@ class ContextsController < ApplicationController
# Methods for changing the sort order of the contexts in the list # Methods for changing the sort order of the contexts in the list
# #
def order def order
list = params["list-contexts-hidden"] || params["list-contexts-active"] context_ids = params["container_context"]
list.each_with_index do |id, position| @projects = current_user.contexts.update_positions( context_ids )
current_user.contexts.update(id, :position => position + 1)
end
render :nothing => true render :nothing => true
rescue
notify :error, $!
redirect_to :action => 'index'
end end
protected protected

View file

@ -10,6 +10,13 @@ class User < ActiveRecord::Base
def find_by_params(params) def find_by_params(params)
find(params['id'] || params['context_id']) || nil find(params['id'] || params['context_id']) || nil
end end
def update_positions(context_ids)
context_ids.each_with_index do |id, position|
context = self.detect { |c| c.id == id.to_i }
raise "Context id #{id} not associated with user id #{@user.id}." if context.nil?
context.update_attribute(:position, position + 1)
end
end
end end
has_many :projects, has_many :projects,
:order => 'projects.position ASC', :order => 'projects.position ASC',

View file

@ -1,7 +1,6 @@
<% context = context_form <% context = context_form
@context = context-%> @context = context-%>
<div id="<%= dom_id(context, 'edit') %>" class="edit-form" style="display:none;"> <% form_remote_tag(:url => context_path(context), :html => {:id => dom_id(context, 'edit_form'), :class => "inline-form "+dom_id(context, 'edit_form')+"-edit-context-form edit-context-form", :method => :put}) do -%>
<% form_tag(context_path(context), {:id => dom_id(context, 'edit_form'), :class => "inline-form "+dom_id(context, 'edit_form')+"-edit-context-form edit-context-form", :method => :put}) do -%>
<%= error_messages_for 'context' %> <%= error_messages_for 'context' %>
<label for="context_name">Context name</label><br/> <label for="context_name">Context name</label><br/>
@ -17,7 +16,7 @@
<%=image_tag("accept.png", :alt => "") %> <%=image_tag("accept.png", :alt => "") %>
Update Update
</button> </button>
<a href="javascript:void(0);" onclick="Element.toggle('<%= dom_id(context) %>');Element.toggle('<%= dom_id(context, 'edit') %>');" class="negative"> <a href="" onclick="" class="negative">
<%=image_tag("cancel.png", :alt => "") %> <%=image_tag("cancel.png", :alt => "") %>
Cancel Cancel
</a> </a>
@ -26,10 +25,4 @@
<br/><br/> <br/><br/>
<% end %> <% end %>
<%= apply_behavior ".edit-context-form", make_remote_form(
:before => "this.up('div.edit-form').down('button.positive').startWaiting()",
:condition => "!(this.up('div.edit-form').down('button.positive')).isWaiting()"),
:external => true
@context = nil %>
</div>

View file

@ -20,22 +20,16 @@
<span class="grey">VISIBLE</span> <span class="grey">VISIBLE</span>
<% end %> <% end %>
<a class="delete_context_button" href="<%= formatted_context_path(context, :js) %>" title="delete the context '<%= context.name %>'"><%= image_tag( "blank.png", :title => "Delete context", :class=>"delete_item") %></a> <a class="delete_context_button" href="<%= formatted_context_path(context, :js) %>" title="delete the context '<%= context.name %>'"><%= image_tag( "blank.png", :title => "Delete context", :class=>"delete_item") %></a>
<%= apply_behavior "a.delete_context_button:click", { :prevent_default => true, :external => true} do |page, element| <%= link_to_remote(
page.confirming "'Are you sure that you want to ' + this.title + '?'" do image_tag( "blank.png", :title => "Edit context", :class=>"edit_item"),
element.up('.context').start_waiting :url => {:controller => 'contexts', :action => 'edit', :id => context.id},
page << remote_to_href(:method => 'delete') :method => 'get',
end :with => "'_source_view=#{@source_view}'",
end -%> :before => "$('#{dom_id(context)}').block({message:null});",
<a class="edit_context_button" href="#"><%= image_tag( "blank.png", :title => "Edit context", :class=>"edit_item") %></a> :complete => "$('#{dom_id(context)}').unblock();"
<%= apply_behavior 'a.edit_context_button:click', :prevent_default => true do |page, element| ) %>
element.up('.context').toggle
editform = element.up('.list').down('.edit-form')
editform.toggle
editform.visual_effect(:appear)
editform.down('input').focus
end
-%>
</div> </div>
</div> </div>
<%= render :partial => 'contexts/context_form', :object => context %> <div id="<%= dom_id(context, 'edit') %>" class="edit-form" style="display:none;">
</div>
</div> </div>

View file

@ -0,0 +1,4 @@
page[dom_id(@context, 'edit')].replace_html :partial => 'context_form', :locals => { :context_form => @context }
page[@context].hide
page[dom_id(@context, 'edit')].show
page[dom_id(@context, 'edit_form')].find('input.context-name').focus

View file

@ -8,12 +8,6 @@
<div id="toggle_context_new" class="hide_form"> <div id="toggle_context_new" class="hide_form">
<a title="Hide new context form" accesskey="n">&laquo; Hide form</a> <a title="Hide new context form" accesskey="n">&laquo; Hide form</a>
<% apply_behavior '#toggle_context_new a:click', :prevent_default => true do |page|
page << "TracksForm.toggle('toggle_context_new', 'context_new', 'context-form',
'&laquo; Hide form', 'Hide new context form',
'Create a new context &#187;', 'Add a context');"
end
%>
</div> </div>
<div id="context_new" class="context_new" style="display:block"> <div id="context_new" class="context_new" style="display:block">

View file

@ -6,7 +6,4 @@ if @context_state_changed
else else
page.replace_html dom_id(@context, 'container'), :partial => 'context_listing', :object => @context page.replace_html dom_id(@context, 'container'), :partial => 'context_listing', :object => @context
end end
page.sortable "list-contexts-active", get_listing_sortable_options page[dom_id(@context)].show
page.sortable "list-contexts-hidden", get_listing_sortable_options
page.hide "busy"

View file

@ -37,8 +37,3 @@ suppress_edit_button ||= false
<div id="<%= dom_id(project, 'edit') %>" class="edit-form" style="display:none;"> <div id="<%= dom_id(project, 'edit') %>" class="edit-form" style="display:none;">
</div> </div>
</div> </div>
<% if controller.action_name == 'create' %>
<script>
new Effect.Appear('<%= dom_id(project) %>');
</script>
<% end %>

View file

@ -140,17 +140,6 @@ function toggle_checkbox_remote(ev){
$.post(this.value, params, null, 'script'); $.post(this.value, params, null, 'script');
} }
function set_behavior_for_tag_edit_todo(){
/*
apply_behavior 'form.edit_todo_form', make_remote_form(
:method => :put,
:before => "todoSpinner = this.down('button.positive'); todoSpinner.startWaiting()",
:loaded => "todoSpinner.stopWaiting()",
:condition => "!(this.down('button.positive').isWaiting())"),
:prevent_default => true
*/
}
todoItems = { todoItems = {
// public // public
ensureVisibleWithEffectAppear: function(elemId){ ensureVisibleWithEffectAppear: function(elemId){
@ -234,12 +223,22 @@ function askIfNewContextProvided() {
return confirm('New context "' + givenContextName + '" will be also created. Are you sure?'); return confirm('New context "' + givenContextName + '" will be also created. Are you sure?');
} }
function update_project_order(event, ui){ function update_order(event, ui){
container = $(ui.item).parent(); container = $(ui.item).parent();
project_display = $(ui.item).children('.project'); row = $(ui.item).children('.sortable_row');
$.post('/projects/order',
url = '';
if(row.hasClass('context'))
url = '/contexts/order';
else if(row.hasClass('project'))
url = '/projects/order';
else {
console.log("Bad sortable list");
return;
}
$.post(url,
container.sortable("serialize"), container.sortable("serialize"),
function(){project_display.effect('highlight', {}, 1000)}, function(){row.effect('highlight', {}, 1000)},
'script'); 'script');
} }
@ -418,10 +417,36 @@ $(document).ready(function() {
$(this).parents('.edit-form').find('form').clearForm(); $(this).parents('.edit-form').find('form').clearForm();
}); });
$("#list-active-projects").sortable({handle: '.handle', update: update_project_order}); $("#list-active-projects").sortable({handle: '.handle', update: update_order});
$("#list-hidden-projects").sortable({handle: '.handle', update: update_project_order}); $("#list-hidden-projects").sortable({handle: '.handle', update: update_order});
$("#list-completed-projects").sortable({handle: '.handle', update: update_project_order}); $("#list-completed-projects").sortable({handle: '.handle', update: update_order});
/* Contexts behavior */
$('#toggle_context_new').click(function(evt){
TracksForm.toggle('toggle_context_new', 'context_new', 'context-form',
'« Hide form', 'Hide new context form',
'Create a new context »', 'Add a context');
});
$('a.delete_project_button').live('click', function(evt){
evt.preventDefault();
if(confirm("Are you sure that you want to "+this.title+"?")){
$(this).parents('.context').block({message: null});
params = {_method: 'delete'};
$.post(this.href, params, null, 'script');
}
});
$("#list-contexts-active").sortable({handle: '.handle', update: update_order});
$("#list-contexts-hidden").sortable({handle: '.handle', update: update_order});
/*
<%= apply_behavior 'a.edit_context_button:click', :prevent_default => true do |page, element|
element.up('.context').toggle
editform = element.up('.list').down('.edit-form')
editform.toggle
editform.visual_effect(:appear)
editform.down('input').focus
end
-%>
*/
/* Gets called from some AJAX callbacks, too */ /* Gets called from some AJAX callbacks, too */
enable_rich_interaction(); enable_rich_interaction();
}); });