JIRA task mover plugin

Plugin to allow moving tasks in JIRA

As of 07/09/2018. See the latest version.

// ==UserScript==
// @name         JIRA task mover plugin
// @description  Plugin to allow moving tasks in JIRA
// @version      0.2
// @author       Max Schlüssel <[email protected]>
// @namespace    https://ghostkernel.org/
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    $(document).ready(function() {
        if(!$("body").is("#jira")) {
            return;
        }

        var issueRows = $(".issuerow");

        var draggedRow = null;
        var dropSequence = null;
        var insertAfter = false;

        issueRows.each(function() {
            var issueRow = $(this);
            var dragger = $("<td>");
            dragger.css("display", "block");
            dragger.css("background", "#EEE");
            dragger.css("width", "10px");
            dragger.css("height", "39px");
            dragger.css("border", "none");
            dragger.css("cursor", "move");
            issueRow.prepend(dragger);
            dropSequence = null;

            dragger.bind("mousedown.drag", function(e) {
                e.preventDefault();
                draggedRow = dragger.parent();
                draggedRow.css("background-color", "#EEE");
            });
        });

        $(document).bind("mousemove.drag", function(e) {
            e.preventDefault();
            if(draggedRow == null) {
                return;
            }
            var el = $(e.target);
            var hoveredRow = el.closest(".issuerow");
            if(hoveredRow.length > 0) {
                if(!draggedRow.is(hoveredRow)) {
                    draggedRow.detach();
                    if(e.offsetY < 20) {
                        insertAfter = false;
                        draggedRow.insertBefore(hoveredRow);
                    } else {
                        insertAfter = true;
                        draggedRow.insertAfter(hoveredRow);
                    }
                }
                dropSequence = draggedRow.index();
            }
        });

        $(document).bind("mouseup.drag", function(e) {
            e.preventDefault();

            if(draggedRow != null) {
                var moveUrl = draggedRow.find(".subtask-reorder a").first().attr("href");
                var currentSequence = moveUrl.substring(moveUrl.indexOf("&currentSubTaskSequence=") + "&currentSubTaskSequence=".length, moveUrl.indexOf("&subTaskSequence="));
                if(currentSequence != dropSequence) {
                    var id = moveUrl.substring(moveUrl.indexOf("id=") + "id=".length, moveUrl.indexOf("&currentSubTaskSequence="));
                    var newLink = "/secure/MoveIssueLink.jspa?id=" + id + "&currentSubTaskSequence=" + currentSequence + "&subTaskSequence=" + dropSequence;
                    window.location = newLink;
                }
                draggedRow.css("background-color", null);
                draggedRow = null;
            }
        });
    });
})();