diff --git a/app/controllers/application.rb b/app/controllers/application.rb index df5fcd1f..86b546f4 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -27,6 +27,7 @@ class ApplicationController < ActionController::Base helper_method :current_user, :prefs layout proc{ |controller| controller.mobile? ? "mobile" : "standard" } + exempt_from_layout /\.js\.erb$/ before_filter :set_session_expiration before_filter :set_time_zone diff --git a/app/helpers/todos_helper.rb b/app/helpers/todos_helper.rb index f11b5fbe..c9f431a3 100644 --- a/app/helpers/todos_helper.rb +++ b/app/helpers/todos_helper.rb @@ -14,26 +14,12 @@ module TodosHelper :id => dom_id(@todo, 'form'), :class => dom_id(@todo, 'form') + " inline-form edit_todo_form" }, &block ) - 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 end - - def set_behavior_for_star_icon - apply_behavior '.item-container a.star_item:click', - remote_to_href(:method => 'put', :with => "{ _source_view : '#{@source_view}' }"), - :prevent_default => true - end def remote_star_icon - str = link_to( image_tag_for_star(@todo), + link_to( image_tag_for_star(@todo), toggle_star_todo_path(@todo), :class => "icon star_item", :title => "star the action '#{@todo.description}'") - set_behavior_for_star_icon - str end def remote_edit_menu_item(parameters, todo) @@ -91,18 +77,9 @@ module TodosHelper :class => "recurring_icon", :title => recurrence_pattern_as_text(todo.recurring_todo)) end - def set_behavior_for_toggle_checkbox - parameters = "_source_view=#{@source_view}" - parameters += "&_tag_name=#{@tag_name}" if @source_view == 'tag' - apply_behavior '.item-container input.item-checkbox:click', - remote_function(:url => javascript_variable('this.value'), :method => 'put', - :with => "'#{parameters}'") - end def remote_toggle_checkbox - str = check_box_tag('item_id', toggle_check_todo_path(@todo), @todo.completed?, :class => 'item-checkbox') - set_behavior_for_toggle_checkbox - str + check_box_tag('item_id', toggle_check_todo_path(@todo), @todo.completed?, :class => 'item-checkbox') end def date_span diff --git a/app/views/contexts/_context.rhtml b/app/views/contexts/_context.rhtml index fb8f7408..41ea8e1c 100644 --- a/app/views/contexts/_context.rhtml +++ b/app/views/contexts/_context.rhtml @@ -37,11 +37,5 @@
<%= render :partial => "todos/todo", :collection => @not_done, :locals => { :parent_container_type => "context" } %> - <% if @not_done.empty? - # fix (hack) for #713 - set_behavior_for_star_icon - set_behavior_for_toggle_checkbox - end - -%> - \ No newline at end of file + diff --git a/app/views/layouts/standard.html.erb b/app/views/layouts/standard.html.erb index 89609e05..541dabbd 100644 --- a/app/views/layouts/standard.html.erb +++ b/app/views/layouts/standard.html.erb @@ -16,10 +16,12 @@ <% bundle :name => "tracks_js" do %> <%= javascript_include_tag *%w[ hoverIntent superfish application - calendar calendar-en calendar-setup accesskey-hints todo-items niftycube protoload flashobject ] %> <% end %> + <%= javascript_tag "var AUTH_TOKEN = #{form_authenticity_token.inspect};" if protect_against_forgery? %> + <%= javascript_tag "var SOURCE_VIEW = '#{@source_view}';" %> + <%= javascript_tag "var TAG_NAME = '#{@tag_name}';" if @tag_name %> <%= auto_discovery_link_tag(:rss, {:controller => "todos", :action => "index", :format => 'rss', :token => "#{current_user.token}"}, {:title => "RSS feed of next actions"}) %> diff --git a/app/views/projects/_project.rhtml b/app/views/projects/_project.rhtml index 1fc98ef5..14e12002 100644 --- a/app/views/projects/_project.rhtml +++ b/app/views/projects/_project.rhtml @@ -17,11 +17,5 @@ <%= render :partial => "todos/todo", :collection => @not_done, :locals => { :parent_container_type => "project" } %> - <% if @not_done.empty? - # fix (hack) for #713 - set_behavior_for_star_icon - set_behavior_for_toggle_checkbox - end - -%> diff --git a/app/views/todos/toggle_star.js.erb b/app/views/todos/toggle_star.js.erb new file mode 100644 index 00000000..0787920d --- /dev/null +++ b/app/views/todos/toggle_star.js.erb @@ -0,0 +1,3 @@ +<% if @saved -%> + $('div#line_todo_<%= @todo.id %> a.star_item img').toggleClass('starred_todo').toggleClass('unstarred_todo'); +<% end -%> diff --git a/app/views/todos/toggle_star.js.rjs b/app/views/todos/toggle_star.js.rjs deleted file mode 100644 index a36b8ed0..00000000 --- a/app/views/todos/toggle_star.js.rjs +++ /dev/null @@ -1,3 +0,0 @@ -if @saved - page[@todo].down('a.star_item').down('img').toggleClassName('starred_todo').toggleClassName('unstarred_todo') -end \ No newline at end of file diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 70ba0e32..1f27ff6c 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -93,15 +93,63 @@ var TodoBehavior = { }); } } -// uncomment the next four lines for easier debugging with FireBug -// Ajax.Responders.register({ -// onException: function(source, exception) { -// console.error(exception); -// } -// }); -/* fade flashes automatically */ -$(document).ready(function() { - $(".alert").fadeIn(8000); +/**************************************** + * Unobtrusive jQuery written by Eric Allen + ****************************************/ + +/* Set up authenticity token proplery */ +$(document).ajaxSend(function(event, request, settings) { + if ( settings.type == 'POST' ) { + settings.data = (settings.data ? settings.data + "&" : "") + + "authenticity_token=" + encodeURIComponent( AUTH_TOKEN ) + "&" + + "_source_view=" + encodeURIComponent( SOURCE_VIEW ); + request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + } + request.setRequestHeader("Accept", "text/javascript"); }); +function toggle_star_remote(ev){ + ev.preventDefault(); + $.post(this.href, {_method: 'put'}, null, 'script'); +} + +function toggle_checkbox_remote(ev){ + params = {_method: 'put'}; + if(typeof(TAG_NAME) !== 'undefined') + params._tag_name = TAG_NAME; + $.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 + */ +} + +function setup_container_toggles(){ + // bind handlers + // set to cookied state +} + +/* Unobtrusive jQuery behavior */ + +$(document).ready(function() { + /* fade flashes automatically */ + $(".alert").fadeIn(8000); + + /* set behavior for star icon */ + $(".item-container a.star_item"). + live('click', toggle_star_remote); + + /* set behavior for toggle checkboxes */ + $(".item-container input.item-checkbox"). + live('click', toggle_checkbox_remote); + + setup_container_toggles(); +});