summaryrefslogtreecommitdiff
path: root/assets/js/core/modal.js
blob: 792e73160d45fb17f68ef725a3e4c819c76262a7 (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
165
166
167
168
169
170
171
172
(function () {
    var isOpen = false;

    function onOverlayClick(e) {
        if (e.target.matches('#modal-overlay')) {
            e.stopPropagation();
            e.preventDefault();
            destroy();
        }
    }

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

    function onFormSubmit() {
        KB.trigger('modal.loading');
        submitForm();
    }

    function getForm() {
        return document.querySelector('#modal-content form:not(.js-modal-ignore-form)');
    }

    function submitForm() {
        var form = getForm();

        if (form) {
            var url = form.getAttribute('action');

            if (url) {
                KB.http.postForm(url, form).success(function (response) {
                    KB.trigger('modal.stop');

                    if (response) {
                        replace(response);
                    } else {
                        destroy();
                    }
                });
            }
        }
    }

    function afterRendering() {
        var formElement = KB.find('#modal-content form');
        if (formElement) {
            formElement.on('submit', onFormSubmit, false);
        }

        var autoFocusElement = document.querySelector('#modal-content input[autofocus]');
        if (autoFocusElement) {
            autoFocusElement.focus();
        }

        KB.render();
        _KB.datePicker();
        _KB.autoComplete();
        _KB.tagAutoComplete();
        _KB.get('Task').onPopoverOpened();
    }

    function replace(html) {
        var contentElement = KB.find('#modal-content');

        if (contentElement) {
            contentElement.replace(KB.dom('div')
                .attr('id', 'modal-content')
                .html(html)
                .build()
            );

            afterRendering();
        }
    }

    function create(html, width, overlayClickDestroy) {
        var closeButtonElement = KB.dom('a')
            .attr('href', '#')
            .attr('id', 'modal-close-button')
            .html('<i class="fa fa-times"></i>')
            .click(onCloseButtonClick)
            .build();

        var headerElement = KB.dom('div')
            .attr('id', 'modal-header')
            .add(closeButtonElement)
            .build();

        var contentElement = KB.dom('div')
            .attr('id', 'modal-content')
            .html(html)
            .build();

        var boxElement = KB.dom('div')
            .attr('id', 'modal-box')
            .style('width', width)
            .add(headerElement)
            .add(contentElement)
            .build();

        var overlayElement = KB.dom('div')
            .attr('id', 'modal-overlay')
            .add(boxElement)
            .build();

        if (overlayClickDestroy) {
            overlayElement.addEventListener('click', onOverlayClick, false);
        }

        document.body.appendChild(overlayElement);
        afterRendering();
    }

    function destroy() {
        isOpen = false;
        var overlayElement = KB.find('#modal-overlay');

        if (overlayElement) {
            overlayElement.remove();
        }
    }

    function getWidth(size) {
        var viewport = KB.utils.getViewportSize();

        switch (size) {
            case 'large':
                return viewport.width < 1350 ? '98%' : '1350px';
            case 'medium':
                return viewport.width < 1024 ? '70%' : '1024px';
        }

        return viewport.width < 800 ? '75%' : '800px';
    }

    KB.on('modal.close', function () {
        destroy();
    });

    KB.on('modal.submit', function () {
        submitForm();
    });

    KB.modal = {
        open: function (url, size, overlayClickDestroy) {
            _KB.get('Dropdown').close();
            destroy();

            if (typeof overlayClickDestroy === 'undefined') {
                overlayClickDestroy = true;
            }

            KB.http.get(url).success(function (response) {
                isOpen = true;
                create(response, getWidth(size), overlayClickDestroy);
            });
        },
        close: function () {
            destroy();
        },
        isOpen: function () {
            return isOpen;
        },
        replace: function (url) {
            KB.http.get(url).success(function (response) {
                replace(response);
            });
        },
        getForm: getForm
    };
}());