summaryrefslogtreecommitdiff
path: root/assets/js/src/Subtask.js
blob: 7670095ea71a7f6f593e02ee97d8b516989c5a45 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function Subtask(app) {
    this.app = app;
}

Subtask.prototype.listen = function() {
    var self = this;

    this.dragAndDrop();

    $(document).on("click", ".subtask-toggle-status", function(e) {
        e.preventDefault();
        var el = $(this);

        $.ajax({
            cache: false,
            url: el.attr("href"),
            success: function(data) {
                if (el.hasClass("subtask-refresh-table")) {
                    $(".subtasks-table").replaceWith(data);
                } else {
                    el.replaceWith(data);
                }

                self.dragAndDrop();
            }
        });
    });

    $(document).on("click", ".subtask-toggle-timer", function(e) {
        e.preventDefault();
        var el = $(this);

        $.ajax({
            cache: false,
            url: el.attr("href"),
            success: function(data) {
                $(".subtasks-table").replaceWith(data);
                self.dragAndDrop();
            }
        });
    });
};

Subtask.prototype.dragAndDrop = function() {
    var self = this;

    $(".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");
            self.savePosition(subtask.data("subtask-id"), subtask.index() + 1);
        },
        start: function(event, ui) {
            ui.item.addClass("draggable-item-selected");
        }
    }).disableSelection();
};

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

    this.app.showLoadingIcon();

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