2011-07-15 19:52:53 -07:00
|
|
|
(function () {
|
2011-07-25 11:06:07 -07:00
|
|
|
/*
|
|
|
|
|
|
|
|
Hanger groups
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
var hangerGroups = [];
|
|
|
|
|
|
|
|
$('div.closet-hangers-group').each(function () {
|
|
|
|
var el = $(this);
|
|
|
|
hangerGroups[hangerGroups.length] = {
|
|
|
|
label: el.find('span.verb').text(),
|
|
|
|
owned: (el.attr('data-owned') == 'true')
|
|
|
|
};
|
2011-07-29 07:52:04 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
$('div.closet-hangers-group span.toggle').live('click', function () {
|
|
|
|
$(this).closest('.closet-hangers-group').toggleClass('hidden');
|
2011-07-25 11:06:07 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Hanger forms
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
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");
|
2011-07-29 07:52:04 -07:00
|
|
|
var newQuantity = input.val();
|
|
|
|
var span = objectWrapper.find("span").text(newQuantity);
|
|
|
|
span.parent().attr('class', 'quantity quantity-' + newQuantity);
|
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
|
|
|
|
2011-07-25 11:06:07 -07:00
|
|
|
/*
|
|
|
|
|
|
|
|
Search, autocomplete
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-07-15 22:09:04 -07:00
|
|
|
$('input, textarea').placeholder();
|
|
|
|
|
|
|
|
var itemsSearchForm = $("#closet-hangers-items-search[data-current-user-id]");
|
2011-07-25 11:06:07 -07:00
|
|
|
var itemsSearchField = itemsSearchForm.children("input[name=q]");
|
2011-07-15 22:09:04 -07:00
|
|
|
|
|
|
|
itemsSearchField.autocomplete({
|
|
|
|
select: function (e, ui) {
|
2011-07-25 11:06:07 -07:00
|
|
|
if(ui.item.is_item) {
|
|
|
|
// Let the autocompleter finish up this search before starting a new one
|
|
|
|
setTimeout(function () { itemsSearchField.autocomplete("search", ui.item) }, 0);
|
|
|
|
} else {
|
|
|
|
var item = ui.item.item;
|
|
|
|
var group = ui.item.group;
|
2011-07-15 22:09:04 -07:00
|
|
|
|
2011-07-25 11:06:07 -07:00
|
|
|
itemsSearchField.addClass("loading");
|
|
|
|
|
|
|
|
var closetHanger = {
|
|
|
|
quantity: 1,
|
|
|
|
owned: group.owned
|
|
|
|
};
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: "/user/" + itemsSearchForm.data("current-user-id") + "/items/" + item.id + "/closet_hanger",
|
|
|
|
type: "post",
|
|
|
|
data: {closet_hanger: closetHanger, 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) {
|
|
|
|
handleSaveError(xhr, "adding the item");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-07-15 22:09:04 -07:00
|
|
|
},
|
|
|
|
source: function (input, callback) {
|
2011-07-25 11:06:07 -07:00
|
|
|
if(typeof input.term == 'string') { // user-typed query
|
|
|
|
$.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;
|
|
|
|
items[i].is_item = true;
|
|
|
|
output[output.length] = items[i];
|
|
|
|
}
|
|
|
|
callback(output);
|
|
|
|
});
|
|
|
|
} else { // item was chosen, now choose a group to insert
|
|
|
|
var groupInserts = [];
|
|
|
|
for(var i in hangerGroups) {
|
|
|
|
groupInserts[groupInserts.length] = {
|
|
|
|
group: hangerGroups[i],
|
|
|
|
item: input.term,
|
|
|
|
label: input.term.label
|
|
|
|
}
|
2011-07-15 22:09:04 -07:00
|
|
|
}
|
2011-07-25 11:06:07 -07:00
|
|
|
callback(groupInserts);
|
|
|
|
}
|
2011-07-15 22:09:04 -07:00
|
|
|
}
|
2011-07-25 11:06:07 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
var autocompleter = itemsSearchField.data("autocomplete");
|
|
|
|
|
|
|
|
autocompleter._renderItem = function( ul, item ) {
|
|
|
|
var li = $("<li></li>").data("item.autocomplete", item);
|
|
|
|
if(item.is_item) { // these are items from the server
|
|
|
|
li.append("<a>Add <strong>" + item.label + "</strong>");
|
|
|
|
} else { // these are group inserts
|
|
|
|
li.append("<a>I <strong>" + item.group.label + "</strong> the <strong>" + item.item.label + "</strong>");
|
|
|
|
}
|
|
|
|
return li.appendTo(ul);
|
2011-07-15 22:09:04 -07:00
|
|
|
}
|
2011-07-22 11:02:04 -07:00
|
|
|
|
2011-07-25 11:06:07 -07:00
|
|
|
/*
|
|
|
|
|
|
|
|
Contact Neopets username form
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-07-22 11:02:04 -07:00
|
|
|
var contactEl = $('#closet-hangers-contact');
|
2011-07-26 15:41:15 -07:00
|
|
|
var editContactLink = $('.edit-contact-link');
|
2011-07-22 11:02:04 -07:00
|
|
|
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-29 07:52:04 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Hanger list controls
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
$('input[type=submit][data-confirm]').live('click', function (e) {
|
|
|
|
if(!confirm(this.getAttribute('data-confirm'))) e.preventDefault();
|
|
|
|
});
|
2011-07-15 19:52:53 -07:00
|
|
|
})();
|
|
|
|
|