static/org.openpsa.widgets/tab_functions.js   A
last analyzed

Complexity

Total Complexity 38
Complexity/F 1.81

Size

Lines of Code 186
Function Count 21

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 38
eloc 122
mnd 17
bc 17
fnc 21
dl 0
loc 186
rs 9.36
bpm 0.8095
cpm 1.8095
noi 1
c 0
b 0
f 0
1
const org_openpsa_widgets_tabs = {
2
    loaded_scripts: [],
3
    popstate: false,
4
    initialize: function(uiprefix) {
5
        org_openpsa_widgets_tabs.bind_events(uiprefix);
6
7
        var tabs = $('#tabs').tabs({
8
            beforeLoad: function(event, ui) {
9
                if (ui.tab.data("loaded")) {
10
                    event.preventDefault();
11
                    return;
12
                }
13
14
                ui.jqXHR.done(function() {
15
                    ui.tab.data("loaded", true);
16
                });
17
                ui.ajaxSettings.dataFilter = function(data, type) {
18
                    return org_openpsa_widgets_tabs.load_head_elements(data, type, event);
19
                };
20
            },
21
            load: function() {
22
                $(window).trigger('resize');
23
            },
24
            activate: function() {
25
                $(window).trigger('resize');
26
27
                var last_state = history.state,
28
                    data = {tab_id: tabs.tabs('option', 'active')};
29
30
                // skip if the last state was same than current
31
                if (!org_openpsa_widgets_tabs.popstate && (!last_state || last_state.tab_id !== data.tab_id)) {
32
                    history.pushState(data, $('body').data('title'), '?tab-' + data.tab_id);
33
                }
34
            },
35
            create: function() {
36
                if (window.hasOwnProperty('history')) {
37
                    window.onpopstate = org_openpsa_widgets_tabs.history_loader;
38
                }
39
            }
40
        });
41
    },
42
    history_loader: function() {
43
        var state = history.state,
44
            tab_id = 0;
45
46
        if (state) {
47
            tab_id = state.tab_id;
48
        }
49
50
        if ($('#tabs').tabs('option', 'active') != tab_id) {
51
            org_openpsa_widgets_tabs.popstate = true;
52
            $('#tabs').tabs('option', 'active', tab_id);
53
            org_openpsa_widgets_tabs.popstate = false;
54
        }
55
    },
56
    bind_events: function(uiprefix) {
57
        $('#tabs')
58
            .on('mousedown', '.ui-state-active a', function(event) {
59
                if (event.which !== 1) {
60
                    return;
61
                }
62
                location.href = this.href.replace(new RegExp('/' + uiprefix + '/'), '/');
63
            })
64
            .on('click', '.ui-tabs-panel a', org_openpsa_widgets_tabs.intercept_clicks)
65
66
            //since this is loaded in a tab - change the submit-function of
67
            // an occurring form - so the result will be loaded in the tab also
68
            .on("submit", 'form:not(.tab_escape)', function(event) {
69
                if ($(event.currentTarget).attr('onsubmit')) {
70
                    return;
71
                }
72
                var send_data = new FormData(this);
73
74
                $.ajax({
75
                    data: send_data,
76
                    processData: false,
77
                    contentType: false,
78
                    dataFilter: org_openpsa_widgets_tabs.load_head_elements,
79
                    type: this.method,
80
                    url: this.getAttribute('action'),
81
                    success: function(data) {
82
                        $(":not(.ui-tabs-hide) > .tab_div").html(data);
83
                        $(window).trigger('resize');
84
                    }
85
                });
86
                event.preventDefault();
87
            });
88
    },
89
    load_head_elements: function(data, type, event) {
90
        data = data.replace(/<\/HEAD_ELEMENTS>[\s\S]+?/m, '</HEAD_ELEMENTS>');
91
        var regex = /<HEAD_ELEMENTS>(.+?)<\/HEAD_ELEMENTS>/m;
92
        regex.exec(data);
93
        var head_elements = JSON.parse(RegExp.$1),
94
            inserted = [];
95
96
        data = data.slice(0, -(RegExp.$1.length + 31));
97
        head_elements.head_js.forEach(function(jscall) {
98
            if (   typeof jscall.url !== 'undefined'
99
                && $('script[src="' + jscall.url + '"]').length === 0
100
                && org_openpsa_widgets_tabs.loaded_scripts.indexOf(jscall.url) === -1) {
101
                org_openpsa_widgets_tabs.loaded_scripts.push(jscall.url);
102
                inserted.push(new Promise(function(resolve) {
103
                    var tag = document.createElement('script');
104
                    tag.src = jscall.url;
105
                    tag.async = false;
106
                    tag.onload = resolve;
107
                    document.head.appendChild(tag);
108
                }));
109
            }
110
            else if (   typeof jscall.content !== 'undefined'
111
                     && jscall.content.length > 0) {
112
                $('<script type="text/javascript">' + jscall.content + '</script>');
113
            }
114
        });
115
116
        var insertion_point = $('link[rel="stylesheet"]').first();
117
        head_elements.head_css.forEach(function(data) {
118
            if (   typeof data.type === 'undefined'
119
                || typeof data.href === 'undefined'
120
                || data.type !== 'text/css') {
121
                return;
122
            }
123
            if ($('link[href="' + data.href + '"]').length !== 0) {
124
                insertion_point = $('link[href="' + data.href + '"]');
125
            } else {
126
                var tag = '<link';
127
                $.each(data, function(key, value) {
128
                    tag += ' ' + key + '="' + value + '"';
129
                });
130
                tag += ' />';
131
132
                inserted.push(new Promise(function(resolve) {
133
                    tag = $(tag);
134
                    tag[0].onload = resolve;
135
                    insertion_point.after(tag);
136
                }));
137
138
                insertion_point = insertion_point.next();
139
            }
140
        });
141
142
        if (inserted.length > 0) {
143
            var active = $('#tabs > [aria-labelledby="key_' + $('#tabs').tabs('option', 'active') + '"]');
144
            Promise.all(inserted).then(function() {
145
                active.html(data);
146
                $(window).trigger('resize');
147
            });
148
            if (event) {
149
                event.preventDefault();
150
            }
151
            return;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Are you sure this return statement is not missing an argument? If this is intended, consider adding an explicit undefined like return undefined;.
Loading history...
152
        }
153
154
        return data;
155
    },
156
    intercept_clicks: function(event) {
157
        var target = $(event.currentTarget),
158
            href = event.currentTarget.href;
159
160
        if (target.attr('onclick')) {
161
            return;
162
        }
163
164
        if (!href.match(/\/uitab\//)) {
165
            return;
166
        }
167
        //for now, only links to plugins will be displayed inside the tab
168
        if (   !href.match(/\/__mfa\/org\.openpsa/)
169
            || target.hasClass('tab_escape')) {
170
            target.attr('href', href.replace(/\/uitab\/+/, '/'));
171
            return;
172
        }
173
174
        if (href.slice(href.length - 1, href.length) !== '#') {
175
            $.ajax({
176
                url: href,
177
                dataFilter: org_openpsa_widgets_tabs.load_head_elements,
178
                success: function(data) {
179
                    $(":not(.ui-tabs-hide) > .tab_div").html(data);
180
                }
181
            });
182
183
            event.preventDefault();
184
        }
185
    }
186
};
187