summaryrefslogtreecommitdiff
path: root/assets/js/components/task-move-position.js
blob: 5e55971333c37934fa2586e0bd5b8b56d24bfa54 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
KB.component('task-move-position', function (containerElement, options) {

    function getSelectedValue(id) {
        var element = KB.dom(document).find('#' + id);

        if (element) {
            return parseInt(element.options[element.selectedIndex].value);
        }

        return null;
    }

    function getSwimlaneId() {
        var swimlaneId = getSelectedValue('form-swimlanes');
        return swimlaneId === null ? options.board[0].id : swimlaneId;
    }

    function getColumnId() {
        var columnId = getSelectedValue('form-columns');
        return columnId === null ? options.board[0].columns[0].id : columnId;
    }

    function getPosition() {
        var position = getSelectedValue('form-position');
        return position === null ? 1 : position;
    }

    function getPositionChoice() {
        var element = KB.find('input[name=positionChoice]:checked');

        if (element) {
            return element.value;
        }

        return 'before';
    }

    function onSwimlaneChanged() {
        var columnSelect = KB.dom(document).find('#form-columns');
        KB.dom(columnSelect).replace(buildColumnSelect());

        var taskSection = KB.dom(document).find('#form-tasks');
        KB.dom(taskSection).replace(buildTasks());
    }

    function onColumnChanged() {
        var taskSection = KB.dom(document).find('#form-tasks');
        KB.dom(taskSection).replace(buildTasks());
    }

    function onError(message) {
        KB.trigger('modal.stop');

        KB.find('#message-container')
            .replace(KB.dom('div')
                .attr('id', 'message-container')
                .attr('class', 'alert alert-error')
                .text(message)
                .build()
            );
    }

    function onSubmit() {
        var position = getPosition();
        var positionChoice = getPositionChoice();

        if (positionChoice === 'after') {
            position++;
        }

        KB.find('#message-container').replace(KB.dom('div').attr('id', 'message-container').build());

        KB.http.postJson(options.saveUrl, {
            "column_id": getColumnId(),
            "swimlane_id": getSwimlaneId(),
            "position": position
        }).success(function () {
            window.location.reload(true);
        }).error(function (response) {
            if (response) {
                onError(response.message);
            }
        });
    }

    function buildSwimlaneSelect() {
        var swimlanes = [];

        options.board.forEach(function(swimlane) {
            swimlanes.push({'value': swimlane.id, 'text': swimlane.name});
        });

        return KB.dom('select')
            .attr('id', 'form-swimlanes')
            .change(onSwimlaneChanged)
            .for('option', swimlanes)
            .build();
    }

    function buildColumnSelect() {
        var columns = [];
        var swimlaneId = getSwimlaneId();

        options.board.forEach(function(swimlane) {
            if (swimlaneId === swimlane.id) {
                swimlane.columns.forEach(function(column) {
                    columns.push({'value': column.id, 'text': column.title});
                });
            }
        });

        return KB.dom('select')
            .attr('id', 'form-columns')
            .change(onColumnChanged)
            .for('option', columns)
            .build();
    }

    function buildTasks() {
        var tasks = [];
        var swimlaneId = getSwimlaneId();
        var columnId = getColumnId();
        var container = KB.dom('div').attr('id', 'form-tasks');

        options.board.forEach(function(swimlane) {
            if (swimlaneId === swimlane.id) {
                swimlane.columns.forEach(function(column) {
                    if (columnId === column.id) {
                        column.tasks.forEach(function(task) {
                            tasks.push({'value': task.position, 'text': '#' + task.id + ' - ' + task.title});
                        });
                    }
                });
            }
        });

        if (tasks.length > 0) {
            container
                .add(KB.html.label(options.positionLabel, 'form-position'))
                .add(KB.dom('select').attr('id', 'form-position').for('option', tasks).build())
                .add(KB.html.radio(options.beforeLabel, 'positionChoice', 'before'))
                .add(KB.html.radio(options.afterLabel, 'positionChoice', 'after'))
            ;
        }

        return container.build();
    }

    this.render = function () {
        KB.on('modal.submit', onSubmit);

        var form = KB.dom('div')
            .on('submit', onSubmit)
            .add(KB.dom('div').attr('id', 'message-container').build())
            .add(KB.html.label(options.swimlaneLabel, 'form-swimlanes'))
            .add(buildSwimlaneSelect())
            .add(KB.html.label(options.columnLabel, 'form-columns'))
            .add(buildColumnSelect())
            .add(buildTasks())
            .build();

        containerElement.appendChild(form);
    };
});