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)
@ -370,44 +371,47 @@ body.closet_hangers-index
span:after
content: ""
.bulk-actions
border-top: 1px solid $module-border-color
display: block
font-size: 85%
padding: .5em 1em
.bulk-actions
border-top: 1px solid $module-border-color
display: block
font-size: 85%
padding: .5em 1em
.bulk-actions-intro, .bulk-actions-target-desc-singular
.bulk-actions-intro, .bulk-actions-target-desc-singular
display: none
.bulk-actions-target-desc
display: inline-block
.bulk-actions-options
display: inline-block
list-style: none
> li
display: inline-block
margin-left: .75em
form
display: inline-block
&:not(:first-child)::before
content: " or "
display: inline-block
margin-right: .75em
&[data-target-count="0"]
.bulk-actions-intro
display: block
.bulk-actions-form
display: none
.bulk-actions-target-desc
display: inline-block
&[data-target-count="1"]
.bulk-actions-target-desc-singular
display: inline
.bulk-actions-options
display: inline-block
list-style: none
> li
display: inline-block
margin-left: .75em
&:not(:first-child)::before
content: " or "
display: inline-block
margin-right: .75em
&[data-target-count="0"]
.bulk-actions-intro
display: block
.bulk-actions-form
display: none
&[data-target-count="1"]
.bulk-actions-target-desc-singular
display: inline
.bulk-actions-target-desc-plural
display: none
.bulk-actions-target-desc-plural
display: none
#closet-hangers-contact
input[type=submit]

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,19 +121,30 @@ class ClosetHangersController < ApplicationController
end
def update
@closet_hanger = current_user.closet_hangers.find(params[:id])
@closet_hanger.attributes = params[:closet_hanger]
@item = @closet_hanger.item
unless @closet_hanger.quantity == 0 # save the hanger, new record or not
if @closet_hanger.save
closet_hanger_saved
else
closet_hanger_invalid
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
unless @closet_hanger.quantity == 0 # save the hanger, new record or not
if @closet_hanger.save
closet_hanger_saved
else
closet_hanger_invalid
end
else # delete the hanger since the user doesn't want it
@closet_hanger.destroy
closet_hanger_destroyed
end
else # delete the hanger since the user doesn't want it
@closet_hanger.destroy
closet_hanger_destroyed
end
end

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,27 +56,29 @@
= 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}
.bulk-actions-intro
Manage items in bulk! Select an item by clicking its thumbnail.
.bulk-actions-form
.bulk-actions-target-desc
%span.bulk-actions-target-desc-singular
With the 1 selected item:
%span.bulk-actions-target-desc-plural
With the
%span.bulk-actions-target-count 0
selected items:
%ul.bulk-actions-options
%li
%form
= select_tag 'destination', options_for_select(destination_options)
%button Move
%li
- unless public_perspective?
-# TODO: i18n
.bulk-actions{'data-target-count' => 0}
.bulk-actions-intro
Manage items in bulk! Select an item by clicking its thumbnail.
.bulk-actions-form
.bulk-actions-target-desc
%span.bulk-actions-target-desc-singular
With the 1 selected item:
%span.bulk-actions-target-desc-plural
With the
%span.bulk-actions-target-count 0
selected items:
%ul.bulk-actions-options
%li
= 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