1
0
Fork 0
forked from OpenNeo/impress

oh huh. looks like remove/move are ready.

This commit is contained in:
Matchu 2015-09-26 19:55:09 -07:00
parent 6bba2ae288
commit e6a2b978f1
6 changed files with 167 additions and 73 deletions

View file

@ -364,6 +364,14 @@
return $(hangersElQuery + " input[type=checkbox]"); return $(hangersElQuery + " input[type=checkbox]");
} }
function getCheckedIds() {
var checkedIds = [];
getCheckboxes().filter(':checked').each(function() {
if (this.checked) checkedIds.push(this.id);
});
return checkedIds;
}
getCheckboxes().live("change", updateBulkActions); getCheckboxes().live("change", updateBulkActions);
function updateBulkActions() { function updateBulkActions() {
@ -372,12 +380,65 @@
$('.bulk-actions-target-count').text(checkedCount); $('.bulk-actions-target-count').text(checkedCount);
} }
function maintainCheckboxes(fn) { $(".bulk-actions-move-all").bind("submit", function(e) {
var checkedIds = []; // TODO: DRY
getCheckboxes().filter(':checked').each(function() { e.preventDefault();
if (this.checked) checkedIds.push(this.id); var form = $(this);
var data = form.serializeArray();
data.push({name: "return_to", value: window.location.pathname + window.location.search});
var checkedBoxes = getCheckboxes().filter(':checked');
checkedBoxes.each(function() {
data.push({name: "ids[]", value: $(this).closest('.object').attr('data-id')});
}); });
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: data,
success: function (html) {
var doc = $(html);
maintainCheckboxes(function() {
hangersEl.html( doc.find('#closet-hangers').html() );
hangersInit();
});
doc.find('.flash').hide().insertBefore(hangersEl).show(500).delay(5000).hide(250);
itemsSearchField.val("");
},
error: function (xhr) {
handleSaveError(xhr, "moving these items");
}
});
});
$(".bulk-actions-remove-all").bind("submit", function(e) {
e.preventDefault();
var form = $(this);
var hangerIds = [];
var checkedBoxes = getCheckboxes().filter(':checked');
var hangerEls = $();
checkedBoxes.each(function() {
hangerEls = hangerEls.add($(this).closest('.object'));
});
hangerEls.each(function() {
hangerIds.push($(this).attr('data-id'));
});
$.ajax({
url: form.attr("action") + ".json?" + $.param({ids: hangerIds}),
type: "delete",
dataType: "json",
success: function () {
objectRemoved(hangerEls);
},
error: function () {
$.jGrowl("Error removing items. Try again?");
}
});
});
function maintainCheckboxes(fn) {
var checkedIds = getCheckedIds();
fn(); fn();
checkedIds.forEach(function(id) { checkedIds.forEach(function(id) {

View file

@ -86,13 +86,14 @@ body.closet_hangers-index
input input
width: 30em width: 30em
.bulk-actions
display: none
text-align: center
#closet-hangers #closet-hangers
clear: both clear: both
text-align: center text-align: center
.bulk-actions
display: none
.object .object
.quantity .quantity
+opacity(.75) +opacity(.75)
@ -390,6 +391,9 @@ body.closet_hangers-index
display: inline-block display: inline-block
margin-left: .75em margin-left: .75em
form
display: inline-block
&:not(:first-child)::before &:not(:first-child)::before
content: " or " content: " or "
display: inline-block display: inline-block

View file

@ -15,6 +15,11 @@ class ClosetHangersController < ApplicationController
format.json { render :json => true } format.json { render :json => true }
end end
elsif params[:ids]
ClosetHanger.transaction do
current_user.closet_hangers.where(id: params[:ids]).destroy_all
end
render json: true
else else
@closet_hanger = current_user.closet_hangers.find params[:id] @closet_hanger = current_user.closet_hangers.find params[:id]
@closet_hanger.destroy @closet_hanger.destroy
@ -116,6 +121,16 @@ class ClosetHangersController < ApplicationController
end end
def update def update
if params[:ids]
ClosetHanger.transaction do
@closet_hangers = current_user.closet_hangers.includes(:list).find params[:ids]
@closet_hangers.each do |h|
h.possibly_null_list_id = params[:list_id]
h.save!
end
end
redirect_back!(user_closet_hangers_path(current_user))
else
@closet_hanger = current_user.closet_hangers.find(params[:id]) @closet_hanger = current_user.closet_hangers.find(params[:id])
@closet_hanger.attributes = params[:closet_hanger] @closet_hanger.attributes = params[:closet_hanger]
@item = @closet_hanger.item @item = @closet_hanger.item
@ -131,6 +146,7 @@ class ClosetHangersController < ApplicationController
closet_hanger_destroyed closet_hanger_destroyed
end end
end end
end
def update_quantities def update_quantities
begin begin

View file

@ -71,6 +71,16 @@ class ClosetHanger < ActiveRecord::Base
possibly_null_closet_list.trading? possibly_null_closet_list.trading?
end end
def possibly_null_list_id=(list_id_or_owned)
if list_id_or_owned.to_s == 'true' || list_id_or_owned.to_s == 'false'
self.list_id = nil
self.owned = list_id_or_owned
else
self.list_id = list_id_or_owned
# owned is set in the set_owned_by_list hook
end
end
def verb(subject=:someone) def verb(subject=:someone)
self.class.verb(subject, owned?) self.class.verb(subject, owned?)
end end

View file

@ -56,8 +56,7 @@
= link_to t('.import_from.neopets_user'), new_neopets_user_path = link_to t('.import_from.neopets_user'), new_neopets_user_path
= link_to t('.export_to.petpage'), petpage_user_closet_hangers_path(@user) = link_to t('.export_to.petpage'), petpage_user_closet_hangers_path(@user)
#closet-hangers{:class => public_perspective? ? nil : 'current-user'} - unless public_perspective?
- unless public_perspective?
-# TODO: i18n -# TODO: i18n
.bulk-actions{'data-target-count' => 0} .bulk-actions{'data-target-count' => 0}
.bulk-actions-intro .bulk-actions-intro
@ -72,11 +71,14 @@
selected items: selected items:
%ul.bulk-actions-options %ul.bulk-actions-options
%li %li
%form = form_tag user_closet_hangers_path(@user), method: :put, class: 'bulk-actions-move-all' do
= select_tag 'destination', options_for_select(destination_options) = select_tag 'list_id', options_for_select(destination_options)
%button Move %button Move
%li %li
= form_tag user_closet_hangers_path(@user), method: :delete, class: 'bulk-actions-remove-all' do
%button Remove all %button Remove all
#closet-hangers{:class => public_perspective? ? nil : 'current-user'}
- [true, false].each do |owned| - [true, false].each do |owned|
.closet-hangers-group{'data-owned' => owned.to_s, :id => "closet-hangers-group-#{owned}"} .closet-hangers-group{'data-owned' => owned.to_s, :id => "closet-hangers-group-#{owned}"}
%header %header

View file

@ -63,6 +63,7 @@ OpenneoImpressItems::Application.routes.draw do
resources :closet_hangers, :only => [:index, :update, :destroy], :path => 'closet' do resources :closet_hangers, :only => [:index, :update, :destroy], :path => 'closet' do
collection do collection do
get :petpage get :petpage
put :update
delete :destroy delete :destroy
end end
end end