summaryrefslogtreecommitdiff
path: root/assets/js/components/project-select-role.js
blob: 904d1dbd0251c55cf5d41e51fe6d34c081a0bd77 (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
KB.component('project-select-role', function (containerElement, options) {
    var isLoading = false;
    var isSuccess = false;
    var isError = false;
    var componentElement;

    function onChange(element) {
        isLoading = true;
        options.role = element.value;
        replaceComponentElement();
        updateRole();
    }

    function updateRole() {
        KB.http.postJson(options.url, {
            id: options.id,
            role: options.role
        }).success(function () {
            isLoading = false;
            isSuccess = true;
            replaceComponentElement();
        }).error(function () {
            isLoading = false;
            isSuccess = false;
            isError = true;
            replaceComponentElement();
        });
    }

    function replaceComponentElement() {
        KB.dom(componentElement).remove();
        componentElement = buildComponentElement();
        containerElement.appendChild(componentElement);
    }

    function buildComponentElement() {
        var roles = [];
        var container = KB.dom('div');

        for (var role in options.roles) {
            if (options.roles.hasOwnProperty(role)) {
                var item = {value: role, text: options.roles[role]};

                if (options.role === role) {
                    item.selected = 'selected';
                }

                roles.push(item);
            }
        }

        container.add(KB.dom('select').change(onChange).for('option', roles).build());

        if (isLoading) {
            container.text(' ');
            container.add(KB.dom('i').attr('class', 'fa fa-spinner fa-pulse fa-fw').build());
        } else if (isSuccess) {
            container.text(' ');
            container.add(KB.dom('i').attr('class', 'fa fa-check fa-fw icon-fade-out icon-success').build());
        } else if (isError) {
            container.text(' ');
            container.add(KB.dom('i').attr('class', 'fa fa-check fa-fw icon-fade-out icon-error').build());
        }

        return container.build();
    }

    this.render = function () {
        componentElement = buildComponentElement();
        containerElement.appendChild(componentElement);
    };
});