summaryrefslogtreecommitdiff
path: root/assets/js/components/subtask-drag-and-drop.js
blob: f704376a39d007db19f1764eaa4751229860de6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
KB.on('dom.ready', function() {

    function savePosition(subtaskId, position) {
        var url = $(".subtasks-table").data("save-position-url");

        $.ajax({
            cache: false,
            url: url,
            contentType: "application/json",
            type: "POST",
            processData: false,
            data: JSON.stringify({
                "subtask_id": subtaskId,
                "position": position
            })
        });
    }

    $(".draggable-row-handle").mouseenter(function() {
        $(this).parent().parent().addClass("draggable-item-hover");
    }).mouseleave(function() {
        $(this).parent().parent().removeClass("draggable-item-hover");
    });

    $(".subtasks-table tbody").sortable({
        forcePlaceholderSize: true,
        handle: "td:first i",
        helper: function(e, ui) {
            ui.children().each(function() {
                $(this).width($(this).width());
            });

            return ui;
        },
        stop: function(event, ui) {
            var subtask = ui.item;
            subtask.removeClass("draggable-item-selected");
            savePosition(subtask.data("subtask-id"), subtask.index() + 1);
        },
        start: function(event, ui) {
            ui.item.addClass("draggable-item-selected");
        }
    }).disableSelection();
});