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

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

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,27 +56,29 @@
= 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 Manage items in bulk! Select an item by clicking its thumbnail.
Manage items in bulk! Select an item by clicking its thumbnail. .bulk-actions-form
.bulk-actions-form .bulk-actions-target-desc
.bulk-actions-target-desc %span.bulk-actions-target-desc-singular
%span.bulk-actions-target-desc-singular With the 1 selected item:
With the 1 selected item: %span.bulk-actions-target-desc-plural
%span.bulk-actions-target-desc-plural With the
With the %span.bulk-actions-target-count 0
%span.bulk-actions-target-count 0 selected items:
selected items: %ul.bulk-actions-options
%ul.bulk-actions-options %li
%li = form_tag user_closet_hangers_path(@user), method: :put, class: 'bulk-actions-move-all' do
%form = select_tag 'list_id', options_for_select(destination_options)
= select_tag 'destination', 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