From 42b5c332b2c24c8dcc2581e0cd130ef930959d99 2016-04-17 07:40:39 From: Jean-Philippe Lang Date: 2016-04-17 07:40:39 Subject: [PATCH] Lists can be reordered with drag and drop (#12909). git-svn-id: http://svn.redmine.org/redmine/trunk@15336 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/controllers/custom_fields_controller.rb b/app/controllers/custom_fields_controller.rb index 3c2cf7c..ab0d501 100644 --- a/app/controllers/custom_fields_controller.rb +++ b/app/controllers/custom_fields_controller.rb @@ -54,11 +54,19 @@ class CustomFieldsController < ApplicationController def update if @custom_field.update_attributes(params[:custom_field]) - flash[:notice] = l(:notice_successful_update) call_hook(:controller_custom_fields_edit_after_save, :params => params, :custom_field => @custom_field) - redirect_back_or_default edit_custom_field_path(@custom_field) + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_update) + redirect_back_or_default edit_custom_field_path(@custom_field) + } + format.js { render :nothing => true } + end else - render :action => 'edit' + respond_to do |format| + format.html { render :action => 'edit' } + format.js { render :nothing => true, :status => 422 } + end end end diff --git a/app/controllers/enumerations_controller.rb b/app/controllers/enumerations_controller.rb index 05ca515..feb3603 100644 --- a/app/controllers/enumerations_controller.rb +++ b/app/controllers/enumerations_controller.rb @@ -57,10 +57,18 @@ class EnumerationsController < ApplicationController def update if @enumeration.update_attributes(params[:enumeration]) - flash[:notice] = l(:notice_successful_update) - redirect_to enumerations_path + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_update) + redirect_to enumerations_path + } + format.js { render :nothing => true } + end else - render :action => 'edit' + respond_to do |format| + format.html { render :action => 'edit' } + format.js { render :nothing => true, :status => 422 } + end end end diff --git a/app/controllers/issue_statuses_controller.rb b/app/controllers/issue_statuses_controller.rb index 6d574fe..e49878f 100644 --- a/app/controllers/issue_statuses_controller.rb +++ b/app/controllers/issue_statuses_controller.rb @@ -51,10 +51,18 @@ class IssueStatusesController < ApplicationController def update @issue_status = IssueStatus.find(params[:id]) if @issue_status.update_attributes(params[:issue_status]) - flash[:notice] = l(:notice_successful_update) - redirect_to issue_statuses_path(:page => params[:page]) + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_update) + redirect_to issue_statuses_path(:page => params[:page]) + } + format.js { render :nothing => true } + end else - render :action => 'edit' + respond_to do |format| + format.html { render :action => 'edit' } + format.js { render :nothing => true, :status => 422 } + end end end diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index 2732a89..2de185b 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -72,10 +72,18 @@ class RolesController < ApplicationController def update if @role.update_attributes(params[:role]) - flash[:notice] = l(:notice_successful_update) - redirect_to roles_path(:page => params[:page]) + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_update) + redirect_to roles_path(:page => params[:page]) + } + format.js { render :nothing => true } + end else - render :action => 'edit' + respond_to do |format| + format.html { render :action => 'edit' } + format.js { render :nothing => true, :status => 422 } + end end end diff --git a/app/controllers/trackers_controller.rb b/app/controllers/trackers_controller.rb index 91784d1..2b93753 100644 --- a/app/controllers/trackers_controller.rb +++ b/app/controllers/trackers_controller.rb @@ -59,12 +59,22 @@ class TrackersController < ApplicationController def update @tracker = Tracker.find(params[:id]) if @tracker.update_attributes(params[:tracker]) - flash[:notice] = l(:notice_successful_update) - redirect_to trackers_path(:page => params[:page]) - return + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_update) + redirect_to trackers_path(:page => params[:page]) + } + format.js { render :nothing => true } + end + else + respond_to do |format| + format.html { + edit + render :action => 'edit' + } + format.js { render :nothing => true, :status => 422 } + end end - edit - render :action => 'edit' end def destroy diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f091037..4d59348 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -468,6 +468,16 @@ module ApplicationHelper :title => l(:label_sort_lowest), :class => 'icon-only icon-move-bottom') end + def reorder_handle(object, options={}) + data = { + :reorder_url => options[:url] || url_for(object), + :reorder_param => options[:param] || object.class.name.underscore + } + content_tag('span', '', + :class => "sort-handle ui-icon ui-icon-arrowthick-2-n-s", + :data => data) + end + def breadcrumb(*args) elements = args.flatten elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'breadcrumb') : nil diff --git a/app/views/custom_fields/_index.html.erb b/app/views/custom_fields/_index.html.erb index b0d3c27..7a5d372 100644 --- a/app/views/custom_fields/_index.html.erb +++ b/app/views/custom_fields/_index.html.erb @@ -1,4 +1,4 @@ - +
@@ -7,7 +7,6 @@ <% end %> - @@ -21,8 +20,8 @@ <% end %> - diff --git a/app/views/custom_fields/index.html.erb b/app/views/custom_fields/index.html.erb index e446413..cf2193d 100644 --- a/app/views/custom_fields/index.html.erb +++ b/app/views/custom_fields/index.html.erb @@ -9,3 +9,7 @@ <% else %>

<%= l(:label_no_data) %>

<% end %> + +<%= javascript_tag do %> + $(function() { $("table.custom_fields tbody").positionedItems(); }); +<% end %> \ No newline at end of file diff --git a/app/views/enumerations/index.html.erb b/app/views/enumerations/index.html.erb index d1fb919..5f1d2ea 100644 --- a/app/views/enumerations/index.html.erb +++ b/app/views/enumerations/index.html.erb @@ -5,12 +5,11 @@ <% enumerations = klass.shared %> <% if enumerations.any? %> -
<%=l(:field_name)%> <%=l(:field_field_format)%><%=l(:field_is_for_all)%> <%=l(:label_used_by)%><%=l(:button_sort)%>
<%= checked_image custom_field.is_for_all? %> <%= l(:label_x_projects, :count => custom_field.projects.count) if custom_field.is_a? IssueCustomField and !custom_field.is_for_all? %><%= reorder_links('custom_field', {:action => 'update', :id => custom_field, :back_url => back_url}, :put) %> + <%= reorder_handle(custom_field, :url => custom_field_path(custom_field), :param => 'custom_field') %> <%= delete_link custom_field_path(custom_field) %>
+
- <% enumerations.each do |enumeration| %> @@ -18,8 +17,10 @@ - - + <% end %>
<%= l(:field_name) %> <%= l(:field_is_default) %> <%= l(:field_active) %><%=l(:button_sort)%>
<%= link_to enumeration, edit_enumeration_path(enumeration) %> <%= checked_image enumeration.is_default? %> <%= checked_image enumeration.active? %><%= reorder_links('enumeration', {:action => 'update', :id => enumeration}, :put) %><%= delete_link enumeration_path(enumeration) %> + <%= reorder_handle(enumeration, :url => enumeration_path(enumeration), :param => 'enumeration') %> + <%= delete_link enumeration_path(enumeration) %> +
@@ -30,3 +31,7 @@ <% end %> <% html_title(l(:label_enumerations)) -%> + +<%= javascript_tag do %> + $(function() { $("table.enumerations tbody").positionedItems(); }); +<% end %> \ No newline at end of file diff --git a/app/views/issue_statuses/index.html.erb b/app/views/issue_statuses/index.html.erb index 59d6846..8608402 100644 --- a/app/views/issue_statuses/index.html.erb +++ b/app/views/issue_statuses/index.html.erb @@ -5,14 +5,13 @@

<%=l(:label_issue_status_plural)%>

- +
<% if Issue.use_status_for_done_ratio? %> <% end %> - @@ -23,8 +22,8 @@ <% end %> - @@ -33,3 +32,7 @@
<%=l(:field_status)%><%=l(:field_done_ratio)%><%=l(:field_is_closed)%><%=l(:button_sort)%>
<%= status.default_done_ratio %><%= checked_image status.is_closed? %><%= reorder_links('issue_status', {:action => 'update', :id => status, :page => params[:page]}, :put) %> + <%= reorder_handle(status) %> <%= delete_link issue_status_path(status) %>
<% html_title(l(:label_issue_status_plural)) -%> + +<%= javascript_tag do %> + $(function() { $("table.issue_statuses tbody").positionedItems(); }); +<% end %> diff --git a/app/views/roles/index.html.erb b/app/views/roles/index.html.erb index f9855f1..8f05964 100644 --- a/app/views/roles/index.html.erb +++ b/app/views/roles/index.html.erb @@ -5,22 +5,17 @@

<%=l(:label_role_plural)%>

- +
- <% for role in @roles %> - "> + <%= role.builtin? ? "builtin" : "givable" %>"> - @@ -30,3 +25,7 @@
<%=l(:label_role)%><%=l(:button_sort)%>
<%= content_tag(role.builtin? ? 'em' : 'span', link_to(role.name, edit_role_path(role))) %> - <% unless role.builtin? %> - <%= reorder_links('role', {:action => 'update', :id => role, :page => params[:page]}, :put) %> - <% end %> - + <%= reorder_handle(role) unless role.builtin? %> <%= link_to l(:button_copy), new_role_path(:copy => role), :class => 'icon icon-copy' %> <%= delete_link role_path(role) unless role.builtin? %>
<% html_title(l(:label_role_plural)) -%> + +<%= javascript_tag do %> + $(function() { $("table.roles tbody").positionedItems({items: ".givable"}); }); +<% end %> \ No newline at end of file diff --git a/app/views/trackers/index.html.erb b/app/views/trackers/index.html.erb index 9dd253e..5f40cf0 100644 --- a/app/views/trackers/index.html.erb +++ b/app/views/trackers/index.html.erb @@ -5,11 +5,10 @@

<%=l(:label_tracker_plural)%>

- +
- @@ -23,10 +22,8 @@ <% end %> - @@ -35,3 +32,7 @@
<%=l(:label_tracker)%> <%=l(:button_sort)%>
- <%= reorder_links('tracker', {:action => 'update', :id => tracker, :page => params[:page]}, :put) %> - + <%= reorder_handle(tracker) %> <%= delete_link tracker_path(tracker) %>
<% html_title(l(:label_tracker_plural)) -%> + +<%= javascript_tag do %> + $(function() { $("table.trackers tbody").positionedItems(); }); +<% end %> diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 3409bc6..f791add 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -590,6 +590,45 @@ function beforeShowDatePicker(input, inst) { $(input).datepicker("option", "defaultDate", default_date); } +(function($){ + $.fn.positionedItems = function(sortableOptions, options){ + var settings = $.extend({ + firstPosition: 1 + }, options ); + + return this.sortable($.extend({ + handle: ".sort-handle", + helper: function(event, ui){ + ui.children().each(function(){ + $(this).width($(this).width()); + }); + return ui; + }, + update: function(event, ui) { + var sortable = $(this); + var url = ui.item.find(".sort-handle").data("reorder-url"); + var param = ui.item.find(".sort-handle").data("reorder-param"); + var data = {}; + data[param] = {position: ui.item.index() + settings['firstPosition']}; + $.ajax({ + url: url, + type: 'put', + dataType: 'script', + data: data, + success: function(data){ + sortable.children(":even").removeClass("even").addClass("odd"); + sortable.children(":odd").removeClass("odd").addClass("even"); + }, + error: function(jqXHR, textStatus, errorThrown){ + alert(jqXHR.status); + sortable.sortable("cancel"); + } + }); + }, + }, sortableOptions)); + } +}( jQuery )); + function initMyPageSortable(list, url) { $('#list-'+list).sortable({ connectWith: '.block-receiver', diff --git a/test/functional/trackers_controller_test.rb b/test/functional/trackers_controller_test.rb index f3d7a1c..a2664f2 100644 --- a/test/functional/trackers_controller_test.rb +++ b/test/functional/trackers_controller_test.rb @@ -152,7 +152,7 @@ class TrackersControllerTest < ActionController::TestCase def test_move_lower tracker = Tracker.find_by_position(1) - put :update, :id => 1, :tracker => { :move_to => 'lower' } + put :update, :id => 1, :tracker => { :position => '2' } assert_equal 2, tracker.reload.position end