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]");
}
function getCheckedIds() {
var checkedIds = [];
getCheckboxes().filter(':checked').each(function() {
if (this.checked) checkedIds.push(this.id);
});
return checkedIds;
}
getCheckboxes().live("change", updateBulkActions);
function updateBulkActions() {
@ -372,12 +380,65 @@
$('.bulk-actions-target-count').text(checkedCount);
}
function maintainCheckboxes(fn) {
var checkedIds = [];
getCheckboxes().filter(':checked').each(function() {
if (this.checked) checkedIds.push(this.id);
$(".bulk-actions-move-all").bind("submit", function(e) {
// TODO: DRY
e.preventDefault();
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();
checkedIds.forEach(function(id) {

View file

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

View file

@ -15,6 +15,11 @@ class ClosetHangersController < ApplicationController
format.json { render :json => true }
end
elsif params[:ids]
ClosetHanger.transaction do
current_user.closet_hangers.where(id: params[:ids]).destroy_all
end
render json: true
else
@closet_hanger = current_user.closet_hangers.find params[:id]
@closet_hanger.destroy
@ -116,6 +121,16 @@ class ClosetHangersController < ApplicationController
end
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.attributes = params[:closet_hanger]
@item = @closet_hanger.item
@ -131,6 +146,7 @@ class ClosetHangersController < ApplicationController
closet_hanger_destroyed
end
end
end
def update_quantities
begin

View file

@ -71,6 +71,16 @@ class ClosetHanger < ActiveRecord::Base
possibly_null_closet_list.trading?
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)
self.class.verb(subject, owned?)
end

View file

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

View file

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