gazza911 / Re-order episodes

// ==UserScript==
// @name         Re-order episodes
// @version      0.61
// @description  Proof of Concept for re-ordering episodes
// @match        https://www.tvmaze.com/shows/*/episodes
// @require      https://code.jquery.com/ui/1.12.1/jquery-ui.js
// @updateURL https://openuserjs.org/meta/gazza911/Re-order_episodes.meta.js
// @grant        none
// @license      MIT
//
// ==/UserScript==

/*

    THIS IS NOT INTENDED TO MAKE ANY CHANGES ON TVMAZE
    THIS IS PURELY SO THAT IT CAN BE SEEN HOW EPISODES COULD BE RE-ORDERED

    IT ALSO GIVES AN OPPORTUNITY TO IDENTIFY & RESOLVE ANY ISSUES THAT COULD ARISE

*/

var seasonList = {};

$(document).ready(function(){
    $('<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">').appendTo("head");
    $("section.season").each(function(){
        var season = $(this).prev().attr("id");
        seasonList[season] = $(this).find("article.episode-row").length;
    });
    $("section.season").sortable({
        items: "article.episode-row",
        connectWith: "section.season",
        start: function(e, ui) {
            ui.item.startSeason = $(this).prev().attr("id");
            ui.item.startPos = seasonList[ui.item.startSeason] - ui.item.index() + 1;
        },
        update: function (event, ui) {
            var endSeason = ui.item.parent("section.season").prev().attr("id");
            var newIndex, adjustedEnd, adjustedStart;
            if (endSeason != ui.item.startSeason) {
                // Using connectWith will make multiple calls to the update function
                if (ui.sender == null) {
                    // Previous season
                    seasonList[ui.item.startSeason]--;
                    
                    if (ui.item.startSeason != "Specials") {
                        adjustedStart = seasonList[ui.item.startSeason] - ui.item.startPos + 1;

                        $(this).children("article.episode-row:lt(" + adjustedStart + ")").each(function(){
                            var $div = $(this).children("div:first");
                            var number = $div.text();
                            number = parseInt(number) - 1;
                            $div.text(number);
                        });
                    }
                }
                else {
                    // New season
                    seasonList[endSeason]++;
                    
                    if (endSeason != "Specials") {
                        newIndex = seasonList[endSeason] - ui.item.index() + 1;
                        ui.item[0].children[0].innerText = newIndex;
                        adjustedEnd = seasonList[endSeason] - newIndex;
    
                        $(this).children("article.episode-row:lt(" + adjustedEnd + ")").each(function(){
                            var $div = $(this).children("div:first");
                            var number = $div.text();
                            number = parseInt(number) + 1;
                            $div.text(number);
                        });
                    }
                    else {
                        ui.item[0].children[0].innerText = "Special";
                    }
                }
            }
            else if (ui.item.startSeason != "Specials") {
                newIndex = seasonList[endSeason] - ui.item.index() + 1;
                adjustedEnd = seasonList[endSeason] - newIndex;
                adjustedStart = seasonList[endSeason] - ui.item.startPos;
                var adjustment = 1;
                ui.item[0].children[0].innerText = newIndex;
                if (adjustedStart > adjustedEnd) {
                    var temp = adjustedStart + 1;
                    adjustedStart = adjustedEnd + 1;
                    adjustedEnd = temp;
                    adjustment = -1;
                }
                $(this).children("article.episode-row").slice(adjustedStart, adjustedEnd).each(function(){
                    var $div = $(this).children("div:first");
                    var number = $div.text();
                    number = parseInt(number) + adjustment;
                    $div.text(number);

                });
           }
        }
    });
});