2011-07-15 19:52:53 -07:00
|
|
|
(function () {
|
2011-07-22 11:02:04 -07:00
|
|
|
var body = $(document.body).addClass("js");
|
|
|
|
if(!body.hasClass("current-user")) return false;
|
|
|
|
|
|
|
|
var hangersElQuery = '#closet-hangers';
|
2011-07-15 22:09:04 -07:00
|
|
|
var hangersEl = $(hangersElQuery);
|
2011-07-15 19:52:53 -07:00
|
|
|
|
2011-07-15 20:39:19 -07:00
|
|
|
$.fn.disableForms = function () {
|
|
|
|
return this.data("formsDisabled", true).find("input").attr("disabled", "disabled").end();
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.enableForms = function () {
|
|
|
|
return this.data("formsDisabled", false).find("input").removeAttr("disabled").end();
|
|
|
|
}
|
|
|
|
|
2011-07-15 19:52:53 -07:00
|
|
|
$.fn.hasChanged = function () {
|
|
|
|
return this.data('previousValue') != this.val();
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.revertValue = function () {
|
2011-07-15 22:09:04 -07:00
|
|
|
return this.each(function () {
|
2011-07-15 19:52:53 -07:00
|
|
|
var el = $(this);
|
|
|
|
el.val(el.data('previousValue'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.storeValue = function () {
|
2011-07-15 22:09:04 -07:00
|
|
|
return this.each(function () {
|
2011-07-15 19:52:53 -07:00
|
|
|
var el = $(this);
|
|
|
|
el.data('previousValue', el.val());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-07-22 11:02:04 -07:00
|
|
|
function handleSaveError(xhr, action) {
|
2011-07-15 22:09:04 -07:00
|
|
|
try {
|
|
|
|
var data = $.parseJSON(xhr.responseText);
|
|
|
|
} catch(e) {
|
|
|
|
var data = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(typeof data.errors != 'undefined') {
|
|
|
|
$.jGrowl("Error " + action + ": " + data.errors.join(", "));
|
|
|
|
} else {
|
|
|
|
$.jGrowl("We had trouble " + action + " just now. Try again?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function objectRemoved(objectWrapper) {
|
|
|
|
objectWrapper.hide(250);
|
|
|
|
}
|
|
|
|
|
2011-07-15 20:39:19 -07:00
|
|
|
function submitUpdateForm(form) {
|
2011-07-15 19:52:53 -07:00
|
|
|
if(form.data('loading')) return false;
|
|
|
|
var input = form.children("input[type=number]");
|
|
|
|
if(input.hasChanged()) {
|
|
|
|
var objectWrapper = form.closest(".object").addClass("loading");
|
|
|
|
var span = objectWrapper.find("span").text(input.val());
|
2011-07-15 20:39:19 -07:00
|
|
|
var data = form.serialize(); // get data before disabling inputs
|
|
|
|
objectWrapper.disableForms();
|
2011-07-15 19:52:53 -07:00
|
|
|
form.data('loading', true);
|
|
|
|
$.ajax({
|
|
|
|
url: form.attr("action") + ".json",
|
|
|
|
type: "post",
|
2011-07-15 20:39:19 -07:00
|
|
|
data: data,
|
2011-07-15 19:52:53 -07:00
|
|
|
dataType: "json",
|
|
|
|
complete: function (data) {
|
2011-07-15 22:09:04 -07:00
|
|
|
if(input.val() == 0) {
|
|
|
|
objectRemoved(objectWrapper);
|
|
|
|
} else {
|
|
|
|
objectWrapper.removeClass("loading").enableForms();
|
|
|
|
}
|
2011-07-15 19:52:53 -07:00
|
|
|
form.data('loading', false);
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
input.storeValue();
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
input.revertValue();
|
|
|
|
span.text(input.val());
|
2011-07-15 22:09:04 -07:00
|
|
|
|
2011-07-22 11:02:04 -07:00
|
|
|
handleSaveError(xhr, "updating the quantity");
|
2011-07-15 19:52:53 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 22:09:04 -07:00
|
|
|
$(hangersElQuery + ' form.closet-hanger-update').live('submit', function (e) {
|
2011-07-15 19:52:53 -07:00
|
|
|
e.preventDefault();
|
2011-07-15 20:39:19 -07:00
|
|
|
submitUpdateForm($(this));
|
2011-07-15 19:52:53 -07:00
|
|
|
});
|
|
|
|
|
2011-07-15 22:09:04 -07:00
|
|
|
function quantityInputs() { return $(hangersElQuery + ' input[type=number]') }
|
|
|
|
|
|
|
|
quantityInputs().live('change', function () {
|
2011-07-15 20:39:19 -07:00
|
|
|
submitUpdateForm($(this).parent());
|
2011-07-15 19:52:53 -07:00
|
|
|
}).storeValue();
|
|
|
|
|
2011-07-15 22:09:04 -07:00
|
|
|
$(hangersElQuery + ' div.object').live('mouseleave', function () {
|
2011-07-15 20:39:19 -07:00
|
|
|
submitUpdateForm($(this).find('form.closet-hanger-update'));
|
|
|
|
});
|
|
|
|
|
2011-07-15 22:09:04 -07:00
|
|
|
$(hangersElQuery + " form.closet-hanger-destroy").live("submit", function (e) {
|
2011-07-15 20:39:19 -07:00
|
|
|
e.preventDefault();
|
|
|
|
var form = $(this);
|
2011-07-22 14:55:05 -07:00
|
|
|
var button = form.children("input[type=submit]").val("Removing…");
|
2011-07-15 20:39:19 -07:00
|
|
|
var objectWrapper = form.closest(".object").addClass("loading");
|
|
|
|
var data = form.serialize(); // get data before disabling inputs
|
|
|
|
objectWrapper.addClass("loading").disableForms();
|
|
|
|
$.ajax({
|
|
|
|
url: form.attr("action") + ".json",
|
|
|
|
type: "post",
|
|
|
|
data: data,
|
|
|
|
dataType: "json",
|
|
|
|
complete: function () {
|
|
|
|
button.val("Remove");
|
|
|
|
},
|
|
|
|
success: function () {
|
2011-07-15 22:09:04 -07:00
|
|
|
objectRemoved(objectWrapper);
|
2011-07-15 20:39:19 -07:00
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
objectWrapper.removeClass("loading").enableForms();
|
|
|
|
$.jGrowl("Error removing item. Try again?");
|
|
|
|
}
|
|
|
|
});
|
2011-07-15 19:52:53 -07:00
|
|
|
});
|
2011-07-15 22:09:04 -07:00
|
|
|
|
|
|
|
$('input, textarea').placeholder();
|
|
|
|
|
|
|
|
var itemsSearchForm = $("#closet-hangers-items-search[data-current-user-id]");
|
|
|
|
var itemsSearchField = itemsSearchForm.children("input[type=search]");
|
|
|
|
|
|
|
|
itemsSearchField.autocomplete({
|
|
|
|
select: function (e, ui) {
|
|
|
|
var item = ui.item;
|
|
|
|
itemsSearchField.addClass("loading");
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: "/user/" + itemsSearchForm.data("current-user-id") + "/items/" + item.id + "/closet_hanger",
|
|
|
|
type: "post",
|
|
|
|
data: {closet_hanger: {quantity: 1}, return_to: window.location.pathname + window.location.search},
|
|
|
|
complete: function () {
|
|
|
|
itemsSearchField.removeClass("loading");
|
|
|
|
},
|
|
|
|
success: function (html) {
|
|
|
|
var doc = $(html);
|
|
|
|
hangersEl.html( doc.find('#closet-hangers').html() );
|
|
|
|
quantityInputs().storeValue(); // since all the quantity inputs are new, gotta store initial value again
|
|
|
|
doc.find('.flash').hide().insertBefore(hangersEl).show(500).delay(5000).hide(250);
|
|
|
|
itemsSearchField.val("");
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2011-07-22 11:02:04 -07:00
|
|
|
handleSaveError(xhr, "adding the item");
|
2011-07-15 22:09:04 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
source: function (input, callback) {
|
|
|
|
$.getJSON("/items.json?q=" + input.term, function (data) {
|
|
|
|
var output = [];
|
|
|
|
var items = data.items;
|
|
|
|
for(var i in items) {
|
|
|
|
items[i].label = items[i].name;
|
|
|
|
output[output.length] = items[i];
|
|
|
|
}
|
|
|
|
callback(output);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}).data( "autocomplete" )._renderItem = function( ul, item ) {
|
|
|
|
return $( "<li></li>" )
|
|
|
|
.data( "item.autocomplete", item )
|
|
|
|
.append( "<a>Add <strong>" + item.label + "</strong>" )
|
|
|
|
.appendTo( ul );
|
|
|
|
}
|
2011-07-22 11:02:04 -07:00
|
|
|
|
|
|
|
var contactEl = $('#closet-hangers-contact');
|
|
|
|
var editContactLink = $('#edit-contact-link');
|
|
|
|
var contactForm = contactEl.children('form');
|
|
|
|
var cancelContactLink = $('#cancel-contact-link');
|
|
|
|
var contactFormUsername = contactForm.children('input[type=text]');
|
|
|
|
var editContactLinkUsername = $('#contact-link-has-value span');
|
|
|
|
|
|
|
|
function closeContactForm() {
|
|
|
|
contactEl.removeClass('editing');
|
|
|
|
}
|
|
|
|
|
|
|
|
editContactLink.click(function () {
|
|
|
|
contactEl.addClass('editing');
|
|
|
|
contactFormUsername.focus();
|
|
|
|
});
|
|
|
|
|
|
|
|
cancelContactLink.click(closeContactForm);
|
|
|
|
|
|
|
|
contactForm.submit(function (e) {
|
|
|
|
var data = contactForm.serialize();
|
|
|
|
contactForm.disableForms();
|
|
|
|
$.ajax({
|
|
|
|
url: contactForm.attr('action') + '.json',
|
|
|
|
type: 'post',
|
|
|
|
data: data,
|
|
|
|
dataType: 'json',
|
|
|
|
complete: function () {
|
|
|
|
contactForm.enableForms();
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
var newName = contactFormUsername.val();
|
|
|
|
if(newName.length > 0) {
|
|
|
|
editContactLink.addClass('has-value');
|
|
|
|
editContactLinkUsername.text(newName);
|
|
|
|
} else {
|
|
|
|
editContactLink.removeClass('has-value');
|
|
|
|
}
|
|
|
|
closeContactForm();
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
handleSaveError(xhr, 'saving Neopets username');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2011-07-22 14:55:05 -07:00
|
|
|
|
|
|
|
|
2011-07-22 15:06:46 -07:00
|
|
|
$('div.closet-hangers-group header').click(function () {
|
|
|
|
$(this).parent().toggleClass('hidden');
|
2011-07-22 14:55:05 -07:00
|
|
|
});
|
2011-07-15 19:52:53 -07:00
|
|
|
})();
|
|
|
|
|