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 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
respond_to do |format|
format.js
@ -110,6 +111,13 @@ class ContextsController < ApplicationController
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
# actions, you'll get a warning dialogue. If you choose to go ahead, any
# 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
#
def order
list = params["list-contexts-hidden"] || params["list-contexts-active"]
list.each_with_index do |id, position|
current_user.contexts.update(id, :position => position + 1)
end
context_ids = params["container_context"]
@projects = current_user.contexts.update_positions( context_ids )
render :nothing => true
rescue
notify :error, $!
redirect_to :action => 'index'
end
protected

View file

@ -10,6 +10,13 @@ class User < ActiveRecord::Base
def find_by_params(params)
find(params['id'] || params['context_id']) || nil
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
has_many :projects,
:order => 'projects.position ASC',

View file

@ -1,35 +1,28 @@
<% context = context_form
@context = context-%>
<div id="<%= dom_id(context, 'edit') %>" class="edit-form" style="display:none;">
<% 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' %>
<label for="context_name">Context name</label><br/>
<%= text_field('context', 'name', :class => 'context-name') %><br/>
<% 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 -%>
<%= error_messages_for 'context' %>
<label for="context_hide">Hide from front page?</label>
<%= check_box('context', 'hide', :class => 'context-hide') %>
<input type="hidden" name="wants_render" value="true" />
<div class="submit_box">
<div class="widgets">
<button type="submit" class="positive" id="<%= dom_id(context, 'submit') %>" tabindex="15">
<%=image_tag("accept.png", :alt => "") %>
Update
</button>
<a href="javascript:void(0);" onclick="Element.toggle('<%= dom_id(context) %>');Element.toggle('<%= dom_id(context, 'edit') %>');" class="negative">
<%=image_tag("cancel.png", :alt => "") %>
Cancel
</a>
</div>
<label for="context_name">Context name</label><br/>
<%= text_field('context', 'name', :class => 'context-name') %><br/>
<label for="context_hide">Hide from front page?</label>
<%= check_box('context', 'hide', :class => 'context-hide') %>
<input type="hidden" name="wants_render" value="true" />
<div class="submit_box">
<div class="widgets">
<button type="submit" class="positive" id="<%= dom_id(context, 'submit') %>" tabindex="15">
<%=image_tag("accept.png", :alt => "") %>
Update
</button>
<a href="" onclick="" class="negative">
<%=image_tag("cancel.png", :alt => "") %>
Cancel
</a>
</div>
<br/><br/>
<% 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>
</div>
<br/><br/>
<% end %>

View file

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

View file

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

View file

@ -37,8 +37,3 @@ suppress_edit_button ||= false
<div id="<%= dom_id(project, 'edit') %>" class="edit-form" style="display:none;">
</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');
}
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 = {
// public
ensureVisibleWithEffectAppear: function(elemId){
@ -234,12 +223,22 @@ function askIfNewContextProvided() {
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();
project_display = $(ui.item).children('.project');
$.post('/projects/order',
row = $(ui.item).children('.sortable_row');
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"),
function(){project_display.effect('highlight', {}, 1000)},
function(){row.effect('highlight', {}, 1000)},
'script');
}
@ -403,7 +402,7 @@ $(document).ready(function() {
params = {_method: 'delete'};
$.post(this.href, params, null, 'script');
}
});
});
$('#toggle_project_new').click(function(evt){
TracksForm.toggle('toggle_project_new', 'project_new', 'project-form',
@ -418,10 +417,36 @@ $(document).ready(function() {
$(this).parents('.edit-form').find('form').clearForm();
});
$("#list-active-projects").sortable({handle: '.handle', update: update_project_order});
$("#list-hidden-projects").sortable({handle: '.handle', update: update_project_order});
$("#list-completed-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_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 */
enable_rich_interaction();
});