Remove ajax_auth.js lib, by merging it in where needed

It's only actually used in two JS files, so rather than doing a weird
global `$.ajaxSetup` call, let's just inline it into the small handful
of AJAX calls that actually care.
This commit is contained in:
Emi Matchu 2024-09-20 19:10:26 -07:00
parent f20a1b5398
commit 31619071af
6 changed files with 22 additions and 24 deletions

View file

@ -1,20 +0,0 @@
(function () {
var CSRFProtection;
var token = $('meta[name="csrf-token"]').attr("content");
if (token) {
CSRFProtection = function (xhr, settings) {
var sendToken =
typeof settings.useCSRFProtection === "undefined" || // default to true
settings.useCSRFProtection;
if (sendToken) {
xhr.setRequestHeader("X-CSRF-Token", token);
}
};
} else {
CSRFProtection = $.noop;
}
$.ajaxSetup({
beforeSend: CSRFProtection,
});
})();

View file

@ -1,4 +1,11 @@
(function () {
function addCSRFToken(xhr) {
const token = document
.querySelector('meta[name="csrf-token"]')
?.getAttribute("content");
xhr.setRequestHeader("X-CSRF-Token", token);
}
var hangersInitCallbacks = [];
function onHangersInit(callback) {
@ -285,6 +292,7 @@
type: "post",
data: data,
dataType: "json",
beforeSend: addCSRFToken,
complete: function (data) {
if (quantityEl.val() == 0) {
objectRemoved(objectWrapper);
@ -389,6 +397,7 @@
type: "post",
data: data,
dataType: "json",
beforeSend: addCSRFToken,
complete: function () {
button.val("Remove");
},
@ -465,6 +474,7 @@
url: form.attr("action"),
type: form.attr("method"),
data: data,
beforeSend: addCSRFToken,
success: function (html) {
var doc = $(html);
hangersEl.html(doc.find("#closet-hangers").html());
@ -501,6 +511,7 @@
url: form.attr("action") + ".json?" + $.param({ ids: hangerIds }),
type: "delete",
dataType: "json",
beforeSend: addCSRFToken,
success: function () {
objectRemoved(hangerEls);
},
@ -567,6 +578,7 @@
closet_hanger: closetHanger,
return_to: window.location.pathname + window.location.search,
},
beforeSend: addCSRFToken,
complete: function () {
itemsSearchField.removeClass("loading");
},
@ -711,6 +723,7 @@
type: "post",
data: data,
dataType: "json",
beforeSend: addCSRFToken,
complete: function () {
contactForm.enableForms();
},
@ -731,6 +744,7 @@
type: "POST",
data: { neopets_connection: { neopets_username: newUsername } },
dataType: "json",
beforeSend: addCSRFToken,
success: function (connection) {
var newOption = $("<option/>", {
text: newUsername,

View file

@ -37,6 +37,12 @@
pets.shift();
loading = true;
$.ajax({
beforeSend: (xhr) => {
const token = document
.querySelector('meta[name="csrf-token"]')
?.getAttribute("content");
xhr.setRequestHeader("X-CSRF-Token", token);
},
complete: function (data) {
loading = false;
loadNextIfReady();

View file

@ -152,8 +152,7 @@
- content_for :javascripts do
= include_javascript_libraries :jquery, :jquery_tmpl
= javascript_include_tag 'ajax_auth', 'jquery.ui', 'jquery.jgrowl',
defer: true
= javascript_include_tag 'jquery.ui', 'jquery.jgrowl', defer: true
- content_for :javascripts_body do
= javascript_include_tag 'closet_hangers/index', defer: true

View file

@ -122,7 +122,7 @@
- content_for :javascripts do
= include_javascript_libraries :jquery, :jquery_tmpl
= javascript_include_tag 'ajax_auth', 'jquery.timeago', defer: true
= javascript_include_tag 'jquery.timeago', defer: true
- content_for :javascripts_body do
= javascript_include_tag 'outfits/new', defer: true

View file

@ -53,7 +53,6 @@
- content_for :javascripts do
= include_javascript_libraries :jquery, :jquery_tmpl
= javascript_include_tag 'ajax_auth', defer: true
- content_for :javascripts_body do
= javascript_include_tag 'pets/bulk', defer: true