File size: 5,648 Bytes
194b4ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

var dialog = (function () {

    var DIALOG_FADE_IN_SPEED = 250;
    var DIALOG_FADE_OUT_SPEED = 150;

    var $mainContainer = null;

    var onClose = null;

    function getContainer() {
        if (! $mainContainer) {
            $mainContainer = $('#dialogs-container');
        }
        return $mainContainer;
    }

    var mainLayout = (
        '<div id="{0}-modal" class="modal" tabindex="-1">' +
            '<div class="modal-header">' +
                '<h5 class="modal-title"></h5>' +
            '</div>' +
            '<div id="{0}-modal-content" class="modal-content"></div>' +
            '<div class="modal-footer"></div>' +
        '</div>'
    );

    function getContent(opt) {
        return mainLayout.format(opt.id);
    }

    function getButton(btnOpt) {
        var template = '<button {0} type="button" class="button {1}">{2}</button>';
        var id = btnOpt.id ? ' id="' + btnOpt.id + '-modal-btn" ' : '';
        var className = btnOpt.className ? btnOpt.className : '';
        var text = btnOpt.text;
        return template.format(id, className, text);
    }

    function close() {
        getContainer()
            .removeClass('open')
            .find('div.modal').fadeOut(DIALOG_FADE_OUT_SPEED);

        if (onClose && onClose.call) {
            onClose();
        }
    }

    function onFooterBtnClick(data) {
        if (data.action && data.action.call) {
            data.action.call(this, data.params || []);
        }
        if (! data.dontClose) {
            close();
        }
    }

    function show(opt) {

        onClose = null;
        close();

        if ((typeof opt).toLowerCase() === 'string') {
            opt = {
                content: opt
            };
        }

        if (! opt.id) {
            opt.id = +new Date;
        }

        onClose = (opt.onClose && opt.onClose.call) ? opt.onClose : null;

        var dialogId = '#{0}-modal'.format(opt.id);

        if (! getContainer().find(dialogId).length) {
            getContainer().append(getContent(opt));
        }

        $dialog = $(dialogId);

        if (opt.big || opt.imageSrc) {
            var windowHeight = $(window).height();
            var minHeight = parseInt(windowHeight * 0.8) - 100;
            var top = parseInt(windowHeight * 0.08);
            $dialog.css('top', top + 'px');
            if (! opt.dontStretch) {
                $dialog.find('.modal-content').css('min-height', minHeight);
            }
            $dialog.addClass('modal-big');

            if (opt.imageSrc) {
                $dialog.find('.modal-content').css({
                    'background-image': 'url(' + opt.imageSrc + ')',
                    'background-size': 'contain',
                    'background-repeat' : 'no-repeat',
                    'background-position': 'center center'
                });
            }
        }

        if (opt.maxWidth) {
            $dialog.css('max-width', opt.maxWidth);
        }

        if (opt.width) {
            $dialog.css('width', opt.width);
        }

        if (opt.top) {
            $dialog.css('top', opt.top);
        }

        $dialog
            .find('.modal-title').html(opt.title).end()
            .find('.modal-content').html(opt.content || opt.text || '').end()
            .bindClick(function (e) {
                e.stopPropagation();
            })
            .fadeIn(DIALOG_FADE_IN_SPEED);

        getContainer().addClass('open');

        if (! opt.title) {
            $dialog
                .find('.modal-header').hide().end()
                .find('.modal-content').css('padding', '30px 20px')
        }

        if (opt.removeWrapper) {
            $dialog.find('.modal-content *').first().unwrap();
        }
        $footer = $dialog.find('.modal-footer').first().empty();

        if (! opt.disableOverlay) {
            getContainer().bindClick(close);
        } else {
            getContainer().off('click');
        }

        if (! opt.buttons || ! opt.buttons.length) {
            opt.buttons = [{
                id: 'modal-close-button',
                text: 'Ok'
            }];
        }

        for (var i = 0, len = opt.buttons.length; i < len; i++) {
            if (! opt.buttons[i].id) {
                opt.buttons[i].id = Math.floor(Math.random() * 1000000);
            }
            $footer
                .append(getButton(opt.buttons[i]))
                .find('#' + opt.buttons[i].id + '-modal-btn')
                .bindClick(onFooterBtnClick, [opt.buttons[i]]);
        }

        return $dialog;
    }

    function showConfirm(text, action) {
        this.show({
            id: 'main-confirmation',
            title: 'Confirmation',
            content: text,
            buttons: [
                {
                    text: 'Yes',
                    className: 'danger',
                    action: action
                }, {
                    text: 'No'
                }
            ]
        });
    }

    function showImage(src, title, buttons, maxWidth) {
        this.show({
            id: 'image-dialog',
            imageSrc: '/sdapi/v1/eyemask/v1/static/images/' + src,
            big: true,
            maxWidth: maxWidth || null,
            title: title || null,
            buttons: buttons || null
        });
    }

    function showError(message) {
        this.show({
            id: 'error-dialog',
            title: 'Error',
            content: message,
        });
    }

    function hide() {
        close();
    }

    return {
        show: show,
        hide: hide,
        confirm: showConfirm,
        image: showImage,
        error: showError,
    };
})();