From c429ce64d31255124d7d8633045152940500ea2b Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 25 Aug 2020 16:06:00 -0400 Subject: [PATCH] Missing pieces of previous commit --- public/es/anon.js | 5 ++++- public/es/scripts.js | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/public/es/anon.js b/public/es/anon.js index 12a6db00..4e6ae6f5 100644 --- a/public/es/anon.js +++ b/public/es/anon.js @@ -260,7 +260,7 @@ function ajaxSerialize(data) { * * @param {string} url - the url to request * @param {Object} config - the configuration object - * @return {void} + * @return {XMLHttpRequest} */ AnimeClient.ajax = (url, config) => { // Set some sane defaults @@ -321,6 +321,8 @@ AnimeClient.ajax = (url, config) => { } else { request.send(config.data); } + + return request }; /** @@ -329,6 +331,7 @@ AnimeClient.ajax = (url, config) => { * @param {string} url * @param {object|function} data * @param {function} [callback] + * @return {XMLHttpRequest} */ AnimeClient.get = (url, data, callback = null) => { if (callback === null) { diff --git a/public/es/scripts.js b/public/es/scripts.js index 56e000ff..96ea1f5f 100644 --- a/public/es/scripts.js +++ b/public/es/scripts.js @@ -260,7 +260,7 @@ function ajaxSerialize(data) { * * @param {string} url - the url to request * @param {Object} config - the configuration object - * @return {void} + * @return {XMLHttpRequest} */ AnimeClient.ajax = (url, config) => { // Set some sane defaults @@ -321,6 +321,8 @@ AnimeClient.ajax = (url, config) => { } else { request.send(config.data); } + + return request }; /** @@ -329,6 +331,7 @@ AnimeClient.ajax = (url, config) => { * @param {string} url * @param {object|function} data * @param {function} [callback] + * @return {XMLHttpRequest} */ AnimeClient.get = (url, data, callback = null) => { if (callback === null) { @@ -589,7 +592,7 @@ const search = (query) => { AnimeClient.show('.cssload-loader'); // Do the api search - AnimeClient.get(AnimeClient.url('/anime-collection/search'), { query }, (searchResults, status) => { + return AnimeClient.get(AnimeClient.url('/anime-collection/search'), { query }, (searchResults, status) => { searchResults = JSON.parse(searchResults); // Hide the loader @@ -601,13 +604,19 @@ const search = (query) => { }; if (AnimeClient.hasElement('.anime #search')) { + let prevRequest = null; + AnimeClient.on('#search', 'input', AnimeClient.throttle(250, (e) => { const query = encodeURIComponent(e.target.value); if (query === '') { return; } - search(query); + if (prevRequest !== null) { + prevRequest.abort(); + } + + prevRequest = search(query); })); } @@ -675,7 +684,7 @@ AnimeClient.on('body.anime.list', 'click', '.plus-one', (e) => { const search$1 = (query) => { AnimeClient.show('.cssload-loader'); - AnimeClient.get(AnimeClient.url('/manga/search'), { query }, (searchResults, status) => { + return AnimeClient.get(AnimeClient.url('/manga/search'), { query }, (searchResults, status) => { searchResults = JSON.parse(searchResults); AnimeClient.hide('.cssload-loader'); AnimeClient.$('#series-list')[ 0 ].innerHTML = renderMangaSearchResults(searchResults.data); @@ -683,13 +692,19 @@ const search$1 = (query) => { }; if (AnimeClient.hasElement('.manga #search')) { + let prevRequest = null; + AnimeClient.on('#search', 'input', AnimeClient.throttle(250, (e) => { let query = encodeURIComponent(e.target.value); if (query === '') { return; } - search$1(query); + if (prevRequest !== null) { + prevRequest.abort(); + } + + prevRequest = search$1(query); })); }