Completed
Push — master ( ea2870...892b1b )
by Andreas
13:43
created

static/midcom.workflow/workflow.js   B

Complexity

Total Complexity 44
Complexity/F 2.1

Size

Lines of Code 266
Function Count 21

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 0
eloc 197
nc 96
dl 0
loc 266
rs 8.8798
c 4
b 1
f 0
wmc 44
mnd 2
bc 45
fnc 21
bpm 2.1427
cpm 2.0952
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C workflow.js ➔ create_dialog 0 103 9

How to fix   Complexity   

Complexity

Complex classes like static/midcom.workflow/workflow.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
        var pane = $(this).closest('.ui-dialog-buttonpane'),
150
            iframe = pane.prevAll('#midcom-dialog').find('iframe'),
151
            disabler = function() {
152
                pane.find('.ui-button')
153
                    .addClass('ui-state-disabled');
154
            };
155
156
        if (!$(this).hasClass('dialog-extra-button') && $('form.datamanager2', iframe.contents()).length > 0) {
157
            $('form.datamanager2', iframe.contents()).on('submit', disabler);
158
        } else {
159
            disabler();
160
        }
161
    });
162
});
163
164
function create_dialog(control, title, url) {
165
    if ($('.midcom-workflow-dialog').length > 0) {
166
        $('.midcom-workflow-dialog .ui-dialog-content').dialog('close');
167
    }
168
169
    var dialog, iframe, spinner,
170
        config = {
171
            dialogClass: 'midcom-workflow-dialog',
172
            buttons: [],
173
            title: title,
174
            height:  590,
175
            width: 800,
176
            close: function() {
177
                control.removeClass('active');
178
                iframe.css('visibility', 'hidden');
179
                if (iframe[0].contentWindow) {
180
                    iframe[0].contentWindow.stop();
181
                }
182
            },
183
            open: function() {
184
                dialog.closest('.ui-dialog').focus();
185
            }};
186
187
    if (control.data('dialog-cancel-label')) {
188
        config.buttons.push({
189
            text: control.data('dialog-cancel-label'),
190
            click: function() {
191
                $(this).dialog( "close" );
192
            }
193
        });
194
    }
195
196
    if ($('#midcom-dialog').length > 0) {
197
        dialog = $('#midcom-dialog');
198
        iframe = dialog.find('> iframe');
199
        spinner = dialog.find('> i').show();
200
        config.height = dialog.dialog('option', 'height');
201
        config.width = dialog.dialog('option', 'width');
202
        if (   config.width > window.innerWidth
203
            || config.height > window.innerHeight) {
204
            config.position = { my: "center", at: "center", of: window, collision: 'flipfit' };
205
        }
206
    } else {
207
        spinner = $('<i class="fa fa-pulse fa-spinner"></i>');
208
        iframe = $('<iframe name="datamanager-dialog"'
209
                   + ' frameborder="0"'
210
                   + ' marginwidth="0"'
211
                   + ' marginheight="0"'
212
                   + ' width="100%"'
213
                   + ' height="100%"'
214
                   + ' scrolling="auto" />')
215
            .on('load', function() {
216
                spinner.hide();
217
                $(this).css('visibility', 'visible');
218
            });
219
220
        dialog = $('<div id="midcom-dialog"></div>')
221
            .append(spinner)
222
            .append(iframe)
223
            .insertAfter(control);
224
    }
225
226
    config.height = Math.min(config.height, window.innerHeight);
227
    config.width = Math.min(config.width, window.innerHeight);
228
229
    if (url) {
230
        iframe.attr('src', url);
231
    }
232
233
    control.addClass('active');
234
    if (   control.parent().attr('role') === 'gridcell'
235
        && control.closest('.jqgrow').hasClass('ui-state-highlight') === false) {
236
        //todo: find out why the click doesn't bubble automatically
237
        control.parent().trigger('click');
238
    }
239
    dialog
240
        .on('dialogcreate', function() {
241
            var maximized = false,
242
                saved_options = {};
243
            $(this).prevAll('.ui-dialog-titlebar').on('dblclick', function() {
244
                if (!maximized) {
245
                    saved_options.position = dialog.dialog('option', 'position');
246
                    saved_options.width = dialog.dialog('option', 'width');
247
                    saved_options.height = dialog.dialog('option', 'height');
248
                    dialog.dialog('option', {
249
                        width: '99%',
250
                        height: $(window).height(),
251
                        position: {my: 'center top', at: 'center top', of: window}
252
                    });
253
                    maximized = true;
254
                } else {
255
                    dialog.dialog('option', {
256
                        height: saved_options.height,
257
                        width: saved_options.width,
258
                        position: saved_options.position
259
                    });
260
                    maximized = false;
261
                }
262
            });
263
        })
264
        .dialog(config)
265
        .dialog("instance").uiDialog.draggable("option", "containment", false);
266
}
267