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
|
|
|
/* Bulk pets form */
|
|
|
|
(function () {
|
2024-09-09 16:10:45 -07: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"),
|
|
|
|
bulk_load_queue;
|
|
|
|
|
|
|
|
$(document.body).addClass("js");
|
|
|
|
|
2024-09-09 18:56:39 -07:00
|
|
|
function petThumbnailUrl(pet_name) {
|
|
|
|
// if first character is "@", use the hash url
|
|
|
|
if (pet_name[0] == "@") {
|
|
|
|
return "https://pets.neopets.com/cp/" + pet_name.substr(1) + "/1/1.png";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "https://pets.neopets.com/cpn/" + pet_name + "/1/1.png";
|
|
|
|
}
|
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
bulk_load_queue = new (function BulkLoadQueue() {
|
|
|
|
var RECENTLY_SENT_INTERVAL_IN_SECONDS = 30;
|
|
|
|
var RECENTLY_SENT_MAX = 3;
|
|
|
|
var pets = [],
|
|
|
|
url = form.attr("action") + ".json",
|
|
|
|
recently_sent_count = 0,
|
|
|
|
loading = false;
|
|
|
|
|
|
|
|
function Pet(name) {
|
|
|
|
var el = $("#bulk-pets-submission-template")
|
|
|
|
.tmpl({ pet_name: name, pet_thumbnail: petThumbnailUrl(name) })
|
|
|
|
.appendTo(queue_el);
|
|
|
|
|
|
|
|
this.load = function () {
|
|
|
|
el.removeClass("waiting").addClass("loading");
|
|
|
|
var response_el = el.find("span.response");
|
|
|
|
pets.shift();
|
|
|
|
loading = true;
|
|
|
|
$.ajax({
|
|
|
|
complete: function (data) {
|
|
|
|
loading = false;
|
|
|
|
loadNextIfReady();
|
|
|
|
},
|
|
|
|
data: { name: name },
|
|
|
|
dataType: "json",
|
|
|
|
error: function (xhr) {
|
|
|
|
el.removeClass("loading").addClass("failed");
|
|
|
|
response_el.text(xhr.responseText);
|
|
|
|
},
|
|
|
|
success: function (data) {
|
|
|
|
var points = data.points;
|
|
|
|
el.removeClass("loading").addClass("loaded");
|
|
|
|
$("#bulk-pets-submission-success-template")
|
|
|
|
.tmpl({ points: points })
|
|
|
|
.appendTo(response_el);
|
|
|
|
},
|
|
|
|
type: "post",
|
|
|
|
url: url,
|
|
|
|
});
|
|
|
|
|
|
|
|
recently_sent_count++;
|
|
|
|
setTimeout(function () {
|
|
|
|
recently_sent_count--;
|
|
|
|
loadNextIfReady();
|
|
|
|
}, RECENTLY_SENT_INTERVAL_IN_SECONDS * 1000);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.add = function (name) {
|
|
|
|
name = name.replace(/^\s+|\s+$/g, "");
|
|
|
|
if (name.length) {
|
|
|
|
var pet = new Pet(name);
|
|
|
|
pets.push(pet);
|
|
|
|
if (pets.length == 1) loadNextIfReady();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function loadNextIfReady() {
|
|
|
|
if (!loading && recently_sent_count < RECENTLY_SENT_MAX && pets.length) {
|
|
|
|
pets[0].load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
names_el.keyup(function () {
|
|
|
|
var names = this.value.split("\n"),
|
|
|
|
x = names.length - 1,
|
|
|
|
i,
|
|
|
|
name;
|
|
|
|
for (i = 0; i < x; i++) {
|
|
|
|
bulk_load_queue.add(names[i]);
|
|
|
|
}
|
|
|
|
this.value = x >= 0 ? names[x] : "";
|
|
|
|
});
|
|
|
|
|
|
|
|
add_el.click(function () {
|
|
|
|
bulk_load_queue.add(names_el.val());
|
|
|
|
names_el.val("");
|
|
|
|
});
|
|
|
|
|
|
|
|
clear_el.click(function () {
|
|
|
|
queue_el.children("li.loaded, li.failed").remove();
|
|
|
|
});
|
2012-08-06 18:15:31 -07:00
|
|
|
})();
|