From acad0596be9d95e2779f26ea6b67dbcc6dd703cd Mon Sep 17 00:00:00 2001 From: Eric Allen Date: Fri, 2 Oct 2009 19:45:49 -0400 Subject: [PATCH] WIP: contexts page mostly working --- app/controllers/contexts_controller.rb | 19 +++++-- app/models/user.rb | 7 +++ app/views/contexts/_context_form.rhtml | 53 +++++++++---------- app/views/contexts/_context_listing.rhtml | 28 ++++------ app/views/contexts/edit.js.rjs | 4 ++ app/views/contexts/index.html.erb | 6 --- app/views/contexts/update.js.rjs | 5 +- app/views/projects/_project_listing.rhtml | 5 -- public/javascripts/application.js | 63 ++++++++++++++++------- 9 files changed, 104 insertions(+), 86 deletions(-) create mode 100644 app/views/contexts/edit.js.rjs diff --git a/app/controllers/contexts_controller.rb b/app/controllers/contexts_controller.rb index cda24148..07c84e3d 100644 --- a/app/controllers/contexts_controller.rb +++ b/app/controllers/contexts_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index ce96c267..6ce58aa8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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', diff --git a/app/views/contexts/_context_form.rhtml b/app/views/contexts/_context_form.rhtml index 68e3dde0..fc6d18c5 100644 --- a/app/views/contexts/_context_form.rhtml +++ b/app/views/contexts/_context_form.rhtml @@ -1,35 +1,28 @@ <% context = context_form @context = context-%> - - <%= render :partial => 'contexts/context_form', :object => context %> - \ No newline at end of file + + diff --git a/app/views/contexts/edit.js.rjs b/app/views/contexts/edit.js.rjs new file mode 100644 index 00000000..d9846e6c --- /dev/null +++ b/app/views/contexts/edit.js.rjs @@ -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 diff --git a/app/views/contexts/index.html.erb b/app/views/contexts/index.html.erb index 418e637d..46426579 100644 --- a/app/views/contexts/index.html.erb +++ b/app/views/contexts/index.html.erb @@ -8,12 +8,6 @@
« Hide form - <% apply_behavior '#toggle_context_new a:click', :prevent_default => true do |page| - page << "TracksForm.toggle('toggle_context_new', 'context_new', 'context-form', - '« Hide form', 'Hide new context form', - 'Create a new context »', 'Add a context');" - end - %>
diff --git a/app/views/contexts/update.js.rjs b/app/views/contexts/update.js.rjs index 33c633da..164dc5d2 100644 --- a/app/views/contexts/update.js.rjs +++ b/app/views/contexts/update.js.rjs @@ -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" \ No newline at end of file +page[dom_id(@context)].show diff --git a/app/views/projects/_project_listing.rhtml b/app/views/projects/_project_listing.rhtml index d3ff149b..fb159f13 100644 --- a/app/views/projects/_project_listing.rhtml +++ b/app/views/projects/_project_listing.rhtml @@ -37,8 +37,3 @@ suppress_edit_button ||= false
-<% if controller.action_name == 'create' %> - -<% end %> diff --git a/public/javascripts/application.js b/public/javascripts/application.js index cd4793b3..f16da017 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -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(); });