static/midcom.workflow/dialog.js   A
last analyzed

Complexity

Total Complexity 37
Complexity/F 1.76

Size

Lines of Code 184
Function Count 21

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 113
c 0
b 0
f 0
dl 0
loc 184
rs 9.44
wmc 37
mnd 16
bc 16
fnc 21
bpm 0.7619
cpm 1.7619
noi 1

6 Functions

Rating   Name   Duplication   Size   Complexity  
A dialog.js ➔ add_dialog_button 0 12 3
A dialog.js ➔ add_post_button 0 16 3
A dialog.js ➔ add_item 0 7 2
A dialog.js ➔ close 0 7 2
A dialog.js ➔ refresh_opener 0 16 5
F dialog.js ➔ attach_to_parent_dialog 0 83 14
1
const dialog = window.frameElement ? window.parent.$(window.frameElement.parentNode) : null;
2
3
function refresh_opener(url, data) {
4
    if (url === undefined) {
5
        url = window.parent.location.href;
6
    }
7
    close(data);
8
9
    var button = window.parent.$('[data-dialog="dialog"][data-refresh-opener].active');
10
    if (button.length > 0) {
11
        if (   button.data('refresh-opener') === false
12
            && button.closest('.ui-tabs').length === 0) {
13
            return;
14
        }
15
        url = window.parent.location.href;
16
    }
17
    window.parent.location.href = url;
18
}
19
20
function close(data) {
21
    if (dialog) {
22
        dialog
23
            .trigger('dialogsaved', [data])
24
            .dialog('close');
25
    }
26
}
27
28
var extra_buttons = [];
29
function add_dialog_button(url, label, options) {
30
    var button = {
31
        text: label,
32
        'data-action': url,
33
        'class': 'dialog-extra-button',
34
        click: function(){}
35
    };
36
    $.each(options, function(key, value) {
37
        button[key] = value;
38
    });
39
    extra_buttons.push(button);
40
}
41
42
function add_post_button(url, label, options) {
43
    var button = {
44
        text: label,
45
        'class': 'dialog-extra-button',
46
        click: function() {
47
            var form = $('<form action="' + url + '" method="post"></form>'),
48
                dialog = window.parent.$('#midcom-dialog');
49
            $.each(options, function(key, value) {
50
                form.append($('<input type="hidden" name="' + key + '">').val(value));
51
            });
52
            form.appendTo('body').submit();
53
            dialog.dialog('option', 'buttons', []);
54
        }
55
    };
56
    extra_buttons.push(button);
57
}
58
59
function add_item(data) {
60
    if (dialog) {
61
        var widget_id = dialog.attr('id').replace(/_creation_dialog/, '');
62
        window.parent.midcom_helper_datamanager2_autocomplete.add_result_item(widget_id, data);
63
        close(data);
64
    }
65
}
66
67
function attach_to_parent_dialog(dialog) {
68
    let buttons = [];
69
    if (document.title) {
70
        dialog.dialog('option', 'title', document.title);
71
    }
72
    dialog.css('visibility', 'visible');
73
74
    $(window).on('unload', function() {
75
        dialog.nextAll('.ui-dialog-buttonpane').find('button')
76
            .prop('disabled', true)
77
            .addClass('ui-state-disabled');
78
    });
79
80
    if ($('.midcom-view-toolbar li').length > 0) {
81
        $('.midcom-view-toolbar li').each(function() {
82
            var btn = $(this).find('a'),
83
                options = {
84
                    click: function() {
85
                        btn.get(0).click();
86
                        btn.addClass('active');
87
                    }
88
                };
89
90
            add_dialog_button(btn.attr('href'), btn.text(), options);
91
        });
92
    }
93
94
    if ($('.datamanager2 .form_toolbar > *').length > 0) {
95
        $('.datamanager2 .form_toolbar > *').each(function() {
96
            var btn = $(this);
97
            buttons.push({
98
                text: btn.val() || btn.text(),
99
                click: function() {
100
                    if (btn.hasClass('cancel')) {
101
                        dialog.dialog('close');
102
                    } else {
103
                        btn.click();
104
                    }
105
                }
106
            });
107
        });
108
    }
109
    if (extra_buttons.length > 0) {
110
        buttons = extra_buttons.concat(buttons);
111
    }
112
113
    // This doesn't work under certain circumstances when flexbox is used somewhere in the page:
114
    // dialog.dialog('option', 'buttons', buttons);
115
    // @todo: The root of the problem seems to be that jquery can't set the content element
116
    // to a height of 0, so at some point this could be filed as a bug against their repo. Latest
117
    // stable (3.4.1) is affected. For now, we just copy the relevant part from jqueryui's
118
    // _createButtons method..
119
120
    var buttonset = dialog.nextAll('.ui-dialog-buttonpane').find('.ui-dialog-buttonset').empty();
121
122
    $.each(buttons, function (name, props) {
123
        var click, buttonOptions;
124
        props = $.isFunction(props) ? {click: props, text: name} : props;
125
126
        // Default to a non-submitting button
127
        props = $.extend({type: "button"}, props);
128
129
        // Change the context for the click callback to be the main element
130
        click = props.click;
131
        buttonOptions = {
132
            icon: props.icon,
133
            iconPosition: props.iconPosition,
134
            label: props.text
135
        };
136
137
        delete props.click;
138
        delete props.icon;
139
        delete props.iconPosition;
140
        delete props.text;
141
142
        $('<button></button>', props)
143
            .button(buttonOptions)
144
            .appendTo(buttonset)
145
            .on('click', function() {
146
                click.apply(dialog[0], arguments);
147
            });
148
    });
149
}
150
151
window.addEventListener('DOMContentLoaded', function() {
152
    if (dialog) {
153
        
154
        if (window.hasOwnProperty('DM_OBJECT_GUID') && DM_OBJECT_GUID) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable DM_OBJECT_GUID is declared in the current environment, consider using typeof DM_OBJECT_GUID === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
155
            let unlocker = function() {
156
                window.parent.$.get(MIDCOM_PAGE_PREFIX + 'midcom-unlock-' + DM_OBJECT_GUID);
157
                dialog.off('dialogclose', unlocker);
158
            };
159
            window.addEventListener("unload", function () {
160
                dialog.off('dialogclose', unlocker);
161
            });
162
            dialog.on('dialogclose', unlocker);
163
        }
164
165
        
166
        dialog.find(' > .spinner').hide();
167
        if (window.hasOwnProperty('$')) {
168
            $('body').on('submit', '.midcom-dialog-delete-form', function(e) {
169
                e.preventDefault();
170
                var form = $(this).detach().appendTo(dialog);
171
172
                form.find('input[name="referrer"]')
173
                    .val(window.parent.location.href);
174
175
                //somehow, the original submit button breaks when detaching
176
                form.append($('<input type="hidden" name="' + form.find('input[type="submit"]').attr('name') + '" value="x">'))
177
                    .submit();
178
            });
179
            attach_to_parent_dialog(dialog);
180
        }
181
    } else if (window.hasOwnProperty('$')) {
182
        $('.midcom-view-toolbar, .datamanager2 .form_toolbar').show();
183
    }
184
});
185