summaryrefslogtreecommitdiff
path: root/assets/js/components/confirm-buttons.js
blob: da31df55a74b4c2b62b11c31337420c6ae289583 (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
KB.component('confirm-buttons', function (containerElement, options) {
    var isLoading = false;

    function onSubmit() {
        isLoading = true;
        KB.find('#modal-confirm-button').replace(buildButton());
        KB.http.get(options.url);
    }

    function onCancel() {
        KB.trigger('modal.close');
    }

    function onStop() {
        isLoading = false;
        KB.find('#modal-confirm-button').replace(buildButton());
    }

    function buildButton() {
        var button = KB.dom('button')
            .click(onSubmit)
            .attr('id', 'modal-confirm-button')
            .attr('type', 'button')
            .attr('class', 'btn btn-red');

        if (isLoading) {
            button
                .disable()
                .add(KB.dom('i').attr('class', 'fa fa-spinner fa-pulse').build())
                .text(' ')
            ;
        }

        if (options.tabindex) {
            button.attr('tabindex', options.tabindex);
        }

        return button
            .text(options.submitLabel)
            .build();
    }

    this.render = function () {
        KB.on('modal.stop', onStop);
        KB.on('modal.close', function () {
            KB.removeListener('modal.stop', onStop);
        });

        var element = KB.dom('div')
            .attr('class', 'form-actions')
            .add(buildButton())
            .text(' ' + options.orLabel + ' ')
            .add(KB.dom('a').attr('href', '#').click(onCancel).text(options.cancelLabel).build())
            .build();

        containerElement.appendChild(element);
    };
});