2015-06-24 16:01:35 -04:00
|
|
|
/**
|
|
|
|
* Javascript for editing anime, if logged in
|
|
|
|
*/
|
2016-02-08 20:21:41 -05:00
|
|
|
((_) => {
|
2015-11-13 11:33:27 -05:00
|
|
|
|
2016-02-01 09:49:18 -05:00
|
|
|
'use strict';
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
// Action to increment episode count
|
2016-02-08 20:21:41 -05:00
|
|
|
_.on('body.anime.list', 'click', '.plus_one', function(e) {
|
|
|
|
let this_sel = this;
|
|
|
|
let parent_sel = this.parentElement;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-08 20:21:41 -05:00
|
|
|
let watched_count = parseInt(_.$('.completed_number', parent_sel)[0].textContent, 10);
|
|
|
|
let total_count = parseInt(_.$('.total_number', parent_sel)[0].textContent, 10);
|
|
|
|
let title = _.$('.name a', parent_sel)[0].textContent;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
// Setup the update data
|
2016-02-01 09:49:18 -05:00
|
|
|
let data = {
|
2016-02-08 20:21:41 -05:00
|
|
|
id: parent_sel.id,
|
2015-06-24 16:01:35 -04:00
|
|
|
increment_episodes: true
|
|
|
|
};
|
|
|
|
|
|
|
|
// If the episode count is 0, and incremented,
|
|
|
|
// change status to currently watching
|
2016-02-01 09:49:18 -05:00
|
|
|
if (isNaN(watched_count) || watched_count === 0) {
|
|
|
|
data.status = 'currently-watching';
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If you increment at the last episode, mark as completed
|
2016-02-01 09:49:18 -05:00
|
|
|
if (( ! isNaN(watched_count)) && (watched_count + 1) === total_count) {
|
2015-06-24 16:01:35 -04:00
|
|
|
delete data.increment_episodes;
|
2016-02-01 09:49:18 -05:00
|
|
|
data.status = 'completed';
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// okay, lets actually make some changes!
|
2016-02-08 20:21:41 -05:00
|
|
|
_.ajax(_.url('/anime/update'), {
|
2015-11-13 11:33:27 -05:00
|
|
|
data: data,
|
|
|
|
dataType: 'json',
|
2016-02-02 11:34:03 -05:00
|
|
|
type: 'POST',
|
2015-11-13 11:33:27 -05:00
|
|
|
mimeType: 'application/json',
|
2016-02-02 11:34:03 -05:00
|
|
|
success: (res) => {
|
|
|
|
if (res.status === 'completed') {
|
2016-02-08 20:21:41 -05:00
|
|
|
this.parentElement.addAttribute('hidden', 'hidden');
|
2016-02-02 11:34:03 -05:00
|
|
|
}
|
|
|
|
|
2016-02-08 20:21:41 -05:00
|
|
|
_.showMessage('success', `Sucessfully updated ${title}`);
|
|
|
|
_.$('.completed_number', parent_sel)[0].textContent = ++watched_count;
|
|
|
|
_.scrollToTop();
|
2016-02-02 11:34:03 -05:00
|
|
|
},
|
|
|
|
error: (xhr, errorType, error) => {
|
|
|
|
console.error(error);
|
2016-02-08 20:21:41 -05:00
|
|
|
_.showMessage('error', `Failed to updated ${title}. `);
|
|
|
|
_.scrollToTop();
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-08 20:21:41 -05:00
|
|
|
})(AnimeClient);
|