2024-02-18 20:34:24 -08:00
|
|
|
var DEBUG = document.location.search.substr(0, 6) == "?debug";
|
2010-11-05 17:09:03 -07:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
/* Needed items form */
|
|
|
|
(function () {
|
|
|
|
var UI = {};
|
2024-02-18 20:34:24 -08:00
|
|
|
UI.form = $("#needed-items-form");
|
|
|
|
UI.alert = $("#needed-items-alert");
|
|
|
|
UI.pet_name_field = $("#needed-items-pet-name-field");
|
|
|
|
UI.pet_thumbnail = $("#needed-items-pet-thumbnail");
|
|
|
|
UI.pet_header = $("#needed-items-pet-header");
|
|
|
|
UI.reload = $("#needed-items-reload");
|
|
|
|
UI.pet_items = $("#needed-items-pet-items");
|
|
|
|
UI.item_template = $("#item-template");
|
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
var current_request = { abort: function () {} };
|
|
|
|
function sendRequest(options) {
|
|
|
|
current_request = $.ajax(options);
|
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
function cancelRequest() {
|
2024-02-18 20:34:24 -08:00
|
|
|
if (DEBUG) console.log("Canceling request", current_request);
|
2012-08-06 18:15:31 -07:00
|
|
|
current_request.abort();
|
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
/* Pet */
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-12-30 13:02:57 -08:00
|
|
|
var last_successful_pet_name = null;
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
function loadPet(pet_name) {
|
|
|
|
// If there is a request in progress, kill it. Our new pet request takes
|
|
|
|
// priority, and, if I submit a name while the previous name is loading, I
|
|
|
|
// don't want to process both responses.
|
|
|
|
cancelRequest();
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
sendRequest({
|
2024-02-18 20:34:24 -08:00
|
|
|
url: UI.form.attr("action") + ".json",
|
|
|
|
dataType: "json",
|
|
|
|
data: { name: pet_name },
|
2012-08-06 18:15:31 -07:00
|
|
|
error: petError,
|
2024-02-18 20:34:24 -08:00
|
|
|
success: function (data) {
|
|
|
|
petSuccess(data, pet_name);
|
|
|
|
},
|
|
|
|
complete: petComplete,
|
2012-08-06 18:15:31 -07:00
|
|
|
});
|
2024-02-18 20:34:24 -08:00
|
|
|
|
|
|
|
UI.form.removeClass("failed").addClass("loading-pet");
|
2010-11-05 17:09:03 -07:00
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
function petComplete() {
|
2024-02-18 20:34:24 -08:00
|
|
|
UI.form.removeClass("loading-pet");
|
2012-08-06 18:15:31 -07:00
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
function petError(xhr) {
|
|
|
|
UI.alert.text(xhr.responseText);
|
2024-02-18 20:34:24 -08:00
|
|
|
UI.form.addClass("failed");
|
2012-08-06 18:15:31 -07:00
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
function petSuccess(data, pet_name) {
|
2012-12-30 13:02:57 -08:00
|
|
|
last_successful_pet_name = pet_name;
|
2024-02-18 20:34:24 -08:00
|
|
|
UI.pet_thumbnail.attr("src", petThumbnailUrl(pet_name));
|
2012-12-30 13:02:57 -08:00
|
|
|
UI.pet_header.empty();
|
2024-02-18 20:34:24 -08:00
|
|
|
$("#needed-items-pet-header-template")
|
|
|
|
.tmpl({ pet_name: pet_name })
|
|
|
|
.appendTo(UI.pet_header);
|
2012-08-06 18:15:31 -07:00
|
|
|
loadItems(data.query);
|
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
function petThumbnailUrl(pet_name) {
|
2024-08-29 22:45:39 -07:00
|
|
|
if (pet_name[0] == "@") {
|
|
|
|
// if first character is "@", use the hash url
|
|
|
|
return "https://pets.neopets.com/cp/" + pet_name.substr(1) + "/1/1.png";
|
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
return "https://pets.neopets.com/cpn/" + pet_name + "/1/1.png";
|
2012-08-06 18:15:31 -07:00
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
/* Items */
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
function loadItems(query) {
|
2024-02-18 20:34:24 -08:00
|
|
|
UI.form.addClass("loading-items");
|
2012-08-06 18:15:31 -07:00
|
|
|
sendRequest({
|
2024-02-18 20:34:24 -08:00
|
|
|
url: "/items/needed.json",
|
|
|
|
dataType: "json",
|
2012-08-06 18:15:31 -07:00
|
|
|
data: query,
|
2024-02-18 20:34:24 -08:00
|
|
|
success: itemsSuccess,
|
2012-08-06 18:15:31 -07:00
|
|
|
});
|
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
function itemsSuccess(items) {
|
2024-02-18 20:34:24 -08:00
|
|
|
if (DEBUG) {
|
2012-08-06 18:15:31 -07:00
|
|
|
// The dev server is missing lots of data, so sends me 2000+ needed
|
|
|
|
// items. We don't need that many for styling, so limit it to 100 to make
|
|
|
|
// my browser happier.
|
|
|
|
items = items.slice(0, 100);
|
2010-11-05 17:09:03 -07:00
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
UI.pet_items.empty();
|
|
|
|
UI.item_template.tmpl(items).appendTo(UI.pet_items);
|
2024-02-18 20:34:24 -08:00
|
|
|
|
|
|
|
UI.form.removeClass("loading-items").addClass("loaded");
|
2010-11-05 17:09:03 -07:00
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
UI.form.submit(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
loadPet(UI.pet_name_field.val());
|
|
|
|
});
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-12-30 13:02:57 -08:00
|
|
|
UI.reload.click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
loadPet(last_successful_pet_name);
|
2012-08-06 18:15:31 -07:00
|
|
|
});
|
2010-11-05 17:09:03 -07:00
|
|
|
})();
|
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
/* Bulk pets form */
|
|
|
|
(function () {
|
2024-02-18 20:34:24 -08:00
|
|
|
var form = $("#bulk-pets-form"),
|
|
|
|
queue_el = form.find("ul"),
|
|
|
|
names_el = form.find("textarea"),
|
|
|
|
add_el = $("#bulk-pets-form-add"),
|
|
|
|
clear_el = $("#bulk-pets-form-clear"),
|
2012-08-06 18:15:31 -07:00
|
|
|
bulk_load_queue;
|
|
|
|
|
2024-02-18 20:34:24 -08:00
|
|
|
$(document.body).addClass("js");
|
2010-11-05 17:09:03 -07:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
bulk_load_queue = new (function BulkLoadQueue() {
|
2013-07-02 20:50:34 -07:00
|
|
|
var RECENTLY_SENT_INTERVAL_IN_SECONDS = 30;
|
|
|
|
var RECENTLY_SENT_MAX = 3;
|
2024-02-18 20:34:24 -08:00
|
|
|
var pets = [],
|
|
|
|
url = form.attr("action") + ".json",
|
|
|
|
recently_sent_count = 0,
|
|
|
|
loading = false;
|
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
function Pet(name) {
|
2024-02-18 20:34:24 -08:00
|
|
|
var el = $("#bulk-pets-submission-template")
|
|
|
|
.tmpl({ pet_name: name })
|
|
|
|
.appendTo(queue_el);
|
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
this.load = function () {
|
2024-02-18 20:34:24 -08:00
|
|
|
el.removeClass("waiting").addClass("loading");
|
|
|
|
var response_el = el.find("span.response");
|
2013-07-02 14:10:24 -07:00
|
|
|
pets.shift();
|
|
|
|
loading = true;
|
2012-08-06 18:15:31 -07:00
|
|
|
$.ajax({
|
|
|
|
complete: function (data) {
|
2013-07-02 14:10:24 -07:00
|
|
|
loading = false;
|
|
|
|
loadNextIfReady();
|
2012-08-06 18:15:31 -07:00
|
|
|
},
|
2024-02-18 20:34:24 -08:00
|
|
|
data: { name: name },
|
|
|
|
dataType: "json",
|
2012-08-06 18:15:31 -07:00
|
|
|
error: function (xhr) {
|
2024-02-18 20:34:24 -08:00
|
|
|
el.removeClass("loading").addClass("failed");
|
2012-08-06 18:15:31 -07:00
|
|
|
response_el.text(xhr.responseText);
|
|
|
|
},
|
|
|
|
success: function (data) {
|
|
|
|
var points = data.points;
|
2024-02-18 20:34:24 -08:00
|
|
|
el.removeClass("loading").addClass("loaded");
|
|
|
|
$("#bulk-pets-submission-success-template")
|
|
|
|
.tmpl({ points: points })
|
|
|
|
.appendTo(response_el);
|
2012-08-06 18:15:31 -07:00
|
|
|
},
|
2024-02-18 20:34:24 -08:00
|
|
|
type: "post",
|
|
|
|
url: url,
|
2012-08-06 18:15:31 -07:00
|
|
|
});
|
2013-07-02 14:10:24 -07:00
|
|
|
|
|
|
|
recently_sent_count++;
|
|
|
|
setTimeout(function () {
|
|
|
|
recently_sent_count--;
|
|
|
|
loadNextIfReady();
|
|
|
|
}, RECENTLY_SENT_INTERVAL_IN_SECONDS * 1000);
|
2024-02-18 20:34:24 -08:00
|
|
|
};
|
2012-08-06 18:15:31 -07:00
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
this.add = function (name) {
|
2024-02-18 20:34:24 -08:00
|
|
|
name = name.replace(/^\s+|\s+$/g, "");
|
|
|
|
if (name.length) {
|
2012-08-06 18:15:31 -07:00
|
|
|
var pet = new Pet(name);
|
|
|
|
pets.push(pet);
|
2024-02-18 20:34:24 -08:00
|
|
|
if (pets.length == 1) loadNextIfReady();
|
2013-07-02 14:10:24 -07:00
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
};
|
2013-07-02 14:10:24 -07:00
|
|
|
|
|
|
|
function loadNextIfReady() {
|
2024-02-18 20:34:24 -08:00
|
|
|
if (!loading && recently_sent_count < RECENTLY_SENT_MAX && pets.length) {
|
2013-07-02 14:10:24 -07:00
|
|
|
pets[0].load();
|
2012-08-06 18:15:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
2010-11-05 17:09:03 -07:00
|
|
|
|
2012-08-06 18:15:31 -07:00
|
|
|
names_el.keyup(function () {
|
2024-02-18 20:34:24 -08:00
|
|
|
var names = this.value.split("\n"),
|
|
|
|
x = names.length - 1,
|
|
|
|
i,
|
|
|
|
name;
|
|
|
|
for (i = 0; i < x; i++) {
|
2012-08-06 18:15:31 -07:00
|
|
|
bulk_load_queue.add(names[i]);
|
|
|
|
}
|
2024-02-18 20:34:24 -08:00
|
|
|
this.value = x >= 0 ? names[x] : "";
|
2012-08-06 18:15:31 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
add_el.click(function () {
|
|
|
|
bulk_load_queue.add(names_el.val());
|
2024-02-18 20:34:24 -08:00
|
|
|
names_el.val("");
|
2012-08-06 18:15:31 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
clear_el.click(function () {
|
2024-02-18 20:34:24 -08:00
|
|
|
queue_el.children("li.loaded, li.failed").remove();
|
2012-08-06 18:15:31 -07:00
|
|
|
});
|
|
|
|
})();
|