2020-04-10 15:07:08 -04:00
|
|
|
import _ from './anime-client.js'
|
2018-09-14 11:56:48 -04:00
|
|
|
import { renderMangaSearchResults } from './template-helpers.js'
|
2018-08-22 12:54:06 -04:00
|
|
|
|
|
|
|
const search = (query) => {
|
2019-07-15 16:05:29 -04:00
|
|
|
_.show('.cssload-loader');
|
2020-08-25 16:02:15 -04:00
|
|
|
return _.get(_.url('/manga/search'), { query }, (searchResults, status) => {
|
2018-08-22 12:54:06 -04:00
|
|
|
searchResults = JSON.parse(searchResults);
|
2019-07-15 16:05:29 -04:00
|
|
|
_.hide('.cssload-loader');
|
2018-11-02 10:48:20 -04:00
|
|
|
_.$('#series-list')[ 0 ].innerHTML = renderMangaSearchResults(searchResults.data);
|
2018-08-22 12:54:06 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (_.hasElement('.manga #search')) {
|
2020-08-25 16:02:15 -04:00
|
|
|
let prevRequest = null
|
|
|
|
|
2019-07-10 13:32:05 -04:00
|
|
|
_.on('#search', 'input', _.throttle(250, (e) => {
|
2018-09-19 14:11:35 -04:00
|
|
|
let query = encodeURIComponent(e.target.value);
|
2018-08-22 12:54:06 -04:00
|
|
|
if (query === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-25 16:02:15 -04:00
|
|
|
if (prevRequest !== null) {
|
|
|
|
prevRequest.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
prevRequest = search(query);
|
2018-08-22 12:54:06 -04:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Javascript for editing manga, if logged in
|
|
|
|
*/
|
2018-11-02 10:48:20 -04:00
|
|
|
_.on('.manga.list', 'click', '.edit-buttons button', (e) => {
|
2018-08-22 12:54:06 -04:00
|
|
|
let thisSel = e.target;
|
|
|
|
let parentSel = _.closestParent(e.target, 'article');
|
2018-11-02 10:48:20 -04:00
|
|
|
let type = thisSel.classList.contains('plus-one-chapter') ? 'chapter' : 'volume';
|
2018-08-22 12:54:06 -04:00
|
|
|
let completed = parseInt(_.$(`.${type}s_read`, parentSel)[ 0 ].textContent, 10) || 0;
|
|
|
|
let total = parseInt(_.$(`.${type}_count`, parentSel)[ 0 ].textContent, 10);
|
|
|
|
let mangaName = _.$('.name', parentSel)[ 0 ].textContent;
|
|
|
|
|
|
|
|
if (isNaN(completed)) {
|
|
|
|
completed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the update data
|
|
|
|
let data = {
|
|
|
|
id: parentSel.dataset.kitsuId,
|
|
|
|
mal_id: parentSel.dataset.malId,
|
|
|
|
data: {
|
|
|
|
progress: completed
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// If the episode count is 0, and incremented,
|
|
|
|
// change status to currently reading
|
|
|
|
if (isNaN(completed) || completed === 0) {
|
2020-09-10 15:35:43 -04:00
|
|
|
data.data.status = 'CURRENT';
|
2018-08-22 12:54:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If you increment at the last chapter, mark as completed
|
|
|
|
if ((!isNaN(completed)) && (completed + 1) === total) {
|
2020-09-10 15:35:43 -04:00
|
|
|
data.data.status = 'COMPLETED';
|
2018-08-22 12:54:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the total count
|
|
|
|
data.data.progress = ++completed;
|
|
|
|
|
2019-07-15 16:05:29 -04:00
|
|
|
_.show('#loading-shadow');
|
2018-08-22 12:54:06 -04:00
|
|
|
|
2018-09-27 16:45:12 -04:00
|
|
|
_.ajax(_.url('/manga/increment'), {
|
2018-08-22 12:54:06 -04:00
|
|
|
data,
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST',
|
|
|
|
mimeType: 'application/json',
|
|
|
|
success: () => {
|
2020-09-10 15:35:43 -04:00
|
|
|
if (String(data.data.status).toUpperCase() === 'COMPLETED') {
|
2018-08-22 12:54:06 -04:00
|
|
|
_.hide(parentSel);
|
|
|
|
}
|
|
|
|
|
2019-07-15 16:05:29 -04:00
|
|
|
_.hide('#loading-shadow');
|
2018-08-22 12:54:06 -04:00
|
|
|
|
|
|
|
_.$(`.${type}s_read`, parentSel)[ 0 ].textContent = completed;
|
|
|
|
_.showMessage('success', `Successfully updated ${mangaName}`);
|
|
|
|
_.scrollToTop();
|
|
|
|
},
|
|
|
|
error: () => {
|
2019-07-15 16:05:29 -04:00
|
|
|
_.hide('#loading-shadow');
|
2018-08-22 12:54:06 -04:00
|
|
|
_.showMessage('error', `Failed to update ${mangaName}`);
|
|
|
|
_.scrollToTop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|