Completed
Push — master ( e9293e...9e79e0 )
by Andreas
19:17
created

static/midcom.workflow/dialog.js   A

Complexity

Total Complexity 31
Complexity/F 1.63

Size

Lines of Code 166
Function Count 19

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 104
dl 0
loc 166
rs 9.92
c 0
b 0
f 0
wmc 31
mnd 12
bc 12
fnc 19
bpm 0.6314
cpm 1.6315
noi 0

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