Completed
Push — master ( 6e5918...2d50a5 )
by Andreas
13:56
created

$(document).ready   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 10
c 2
b 0
f 0
1
$(document).ready(function() {
2
    $('body').on('click', '[data-dialog="delete"]', function(event) {
3
        event.preventDefault();
4
        var button = $(this),
5
            dialog = $('<div class="midcom-delete-dialog">'),
6
            spinner = $('<div class="spinner"><i class="fa fa-pulse fa-spinner"></i></div>'),
7
            text = button.data('dialog-text'),
8
            relocate = button.data('relocate'),
9
            action = button.attr('href') || button.data('action'),
10
            options = {
11
                title:  button.data('dialog-heading'),
12
                modal: true,
13
                width: 'auto',
14
                maxHeight: $(window).height(),
15
                buttons: [{
16
                    text: button.text().trim() || button.data('dialog-heading'),
17
                    click: function() {
18
                        if (relocate) {
19
                            $('<form action="' + action + '" method="post" class="midcom-dialog-delete-form">')
20
                                .append($('<input type="submit" name="' + button.data('form-id') + '">'))
21
                                .append($('<input type="hidden" name="referrer" value="' + location.pathname + '">'))
22
                                .hide()
23
                                .prependTo('body');
24
                            $('input[name="' + button.data('form-id') + '"]').click();
25
                        } else {
26
                            var params = {
27
                                referrer: location.pathname
28
                            };
29
                            params[button.data('form-id')] = 1;
30
31
                            $.post(action, params).done(function(message) {
32
                                button.trigger('dialogdeleted', [message]);
33
                                dialog.dialog("close");
34
                                if (   typeof window.parent.$ !== "undefined"
35
                                    && window.parent.$('#midcom-dialog').length > 0 ) {
36
                                    window.parent.$('#midcom-dialog').dialog('close');
37
                                }
38
                            });
39
                        }
40
                    }
41
                }, {
42
                    text: button.data('dialog-cancel-label'),
43
                    click: function() {
44
                        if (   typeof window.parent.$ !== "undefined"
45
                            && window.parent.$('#midcom-dialog').length > 0 ) {
46
                            window.parent.$('#midcom-dialog')
47
                                .nextAll('.ui-dialog-buttonpane')
48
                                .find('.ui-state-disabled')
49
                                .removeClass('ui-state-disabled');
50
                        }
51
                        $(this).dialog("close");
52
                    }
53
                }]
54
            };
55
56
        if ($('.midcom-delete-dialog').length > 0) {
57
            $('.midcom-delete-dialog').remove();
58
        }
59
60
        if (button.data('recursive')) {
61
            dialog.addClass('loading');
62
            options.buttons[0].disabled = true;
63
            $.getJSON(MIDCOM_PAGE_PREFIX + 'midcom-exec-midcom.helper.reflector/list-children.php',
64
                      {guid: button.data('guid')},
65
                      function (data) {
66
                          function render(items) {
67
                              var output = '';
68
                              $.each(items, function(i, item) {
69
                                  output += '<li class="leaf ' + item['class'] + '">' + item.icon + ' ' + item.title;
70
                                  if (item.children) {
71
                                      output += '<ul class="folder_list">';
72
                                      output += render(item.children);
73
                                      output += '</ul>';
74
                                  }
75
                                  output += '</li>';
76
                              });
77
                              return output;
78
                          }
79
80
                          if (data.length > 0) {
81
                              $('<ul class="folder_list">')
82
                                  .append($(render(data)))
83
                                  .appendTo($('#delete-child-list'));
84
                          } else {
85
                              dialog.find('p.warning').hide();
86
                          }
87
                          options.buttons[0].disabled = false;
88
89
                          dialog
90
                              .removeClass('loading')
91
                              .dialog('option', 'position', dialog.dialog('option', 'position'))
92
                              .dialog('option', 'buttons', options.buttons)
93
                              .focus();
94
                      });
95
        } else {
96
            text = '<p>' + text + '</p>';
97
        }
98
99
        dialog
100
            .css('min-width', '300px') // This should be handled by dialog's minWidth option, but that doesn't work with width: "auto"
101
                                       // Should be fixed in https://github.com/jquery/jquery-ui/commit/643b80c6070e2eba700a09a5b7b9717ea7551005
102
            .append($(text))
103
            .append(spinner)
104
            .appendTo($('body'))
105
            .dialog(options);
106
    });
107
108
    $('body').on('click', '[data-dialog="dialog"]', function(event) {
109
        event.preventDefault();
110
        if (!$(this).hasClass('active')) {
111
            create_dialog($(this), $(this).find('.toolbar_label').text() || $(this).attr('title') || '', $(this).attr('href'));
112
        }
113
    });
114
115
    $('body').on('click', '[data-dialog="confirm"]', function(event) {
116
        event.preventDefault();
117
        var button = $(this),
118
            dialog = $('<div class="midcom-confirm-dialog">'),
119
            options = {
120
                title:  button.data('dialog-heading'),
121
                modal: true,
122
                width: 'auto',
123
                maxHeight: $(window).height(),
124
                buttons: [{
125
                    text: button.data('dialog-confirm-label'),
126
                    click: function() {
127
                        button.closest('form').submit();
128
                    }
129
                }, {
130
                    text: button.data('dialog-cancel-label'),
131
                    click: function() {
132
                        $(this).dialog("close");
133
                    }
134
                }]
135
            };
136
137
        if ($('.midcom-confirm-dialog').length > 0) {
138
            $('.midcom-confirm-dialog').remove();
139
        }
140
141
        dialog
142
            .css('min-width', '300px') // This should be handled by dialog's minWidth option, but that doesn't work with width: "auto"
143
                                       // Should be fixed in https://github.com/jquery/jquery-ui/commit/643b80c6070e2eba700a09a5b7b9717ea7551005
144
            .append($('<p>' + button.data('dialog-text') + '</p>'))
145
            .appendTo($('body'))
146
            .dialog(options);
147
    });
148
    $('body').on('click', '.midcom-workflow-dialog .ui-dialog-buttonpane .ui-button', function() {
149
        $(this).closest('.ui-dialog-buttonpane').find('.ui-button')
150
            .addClass('ui-state-disabled');
151
    });
152
});
153
154
function create_dialog(control, title, url) {
155
    if ($('.midcom-workflow-dialog').length > 0) {
156
        $('.midcom-workflow-dialog .ui-dialog-content').dialog('close');
157
    }
158
159
    var dialog, iframe, spinner,
160
        config = {
161
            dialogClass: 'midcom-workflow-dialog',
162
            buttons: [],
163
            title: title,
164
            height:  590,
165
            width: 800,
166
            close: function() {
167
                control.removeClass('active');
168
                iframe.css('visibility', 'hidden');
169
                if (iframe[0].contentWindow) {
170
                    iframe[0].contentWindow.stop();
171
                }
172
            },
173
            open: function() {
174
                dialog.closest('.ui-dialog').focus();
175
            }};
176
177
    if (control.data('dialog-cancel-label')) {
178
        config.buttons.push({
179
            text: control.data('dialog-cancel-label'),
180
            click: function() {
181
                $(this).dialog( "close" );
182
            }
183
        });
184
    }
185
186
    if ($('#midcom-dialog').length > 0) {
187
        dialog = $('#midcom-dialog');
188
        iframe = dialog.find('> iframe');
189
        spinner = dialog.find('> i').show();
190
        config.height = dialog.dialog('option', 'height');
191
        config.width = dialog.dialog('option', 'width');
192
        if (   config.width > window.innerWidth
193
            || config.height > window.innerHeight) {
194
            config.position = { my: "center", at: "center", of: window, collision: 'flipfit' };
195
        }
196
    } else {
197
        spinner = $('<i class="fa fa-pulse fa-spinner"></i>');
198
        iframe = $('<iframe name="datamanager-dialog"'
199
                   + ' frameborder="0"'
200
                   + ' marginwidth="0"'
201
                   + ' marginheight="0"'
202
                   + ' width="100%"'
203
                   + ' height="100%"'
204
                   + ' scrolling="auto" />')
205
            .on('load', function() {
206
                spinner.hide();
207
                $(this).css('visibility', 'visible');
208
            });
209
210
        dialog = $('<div id="midcom-dialog"></div>')
211
            .append(spinner)
212
            .append(iframe)
213
            .insertAfter(control);
214
    }
215
216
    config.height = Math.min(config.height, window.innerHeight);
217
    config.width = Math.min(config.width, window.innerHeight);
218
219
    if (url) {
220
        iframe.attr('src', url);
221
    }
222
223
    control.addClass('active');
224
    if (   control.parent().attr('role') === 'gridcell'
225
        && control.closest('.jqgrow').hasClass('ui-state-highlight') === false) {
226
        //todo: find out why the click doesn't bubble automatically
227
        control.parent().trigger('click');
228
    }
229
    dialog
230
        .on('dialogcreate', function() {
231
            var maximized = false,
232
                saved_options = {};
233
            $(this).prevAll('.ui-dialog-titlebar').on('dblclick', function() {
234
                if (!maximized) {
235
                    saved_options.position = dialog.dialog('option', 'position');
236
                    saved_options.width = dialog.dialog('option', 'width');
237
                    saved_options.height = dialog.dialog('option', 'height');
238
                    dialog.dialog('option', {
239
                        width: '99%',
240
                        height: $(window).height(),
241
                        position: {my: 'center top', at: 'center top', of: window}
242
                    });
243
                    maximized = true;
244
                } else {
245
                    dialog.dialog('option', {
246
                        height: saved_options.height,
247
                        width: saved_options.width,
248
                        position: saved_options.position
249
                    });
250
                    maximized = false;
251
                }
252
            });
253
        })
254
        .dialog(config)
255
        .dialog("instance").uiDialog.draggable("option", "containment", false);
256
}
257