Completed
Push — master ( c3610f...1a80aa )
by Andreas
17:36
created

static/org.openpsa.widgets/ui.js   A

Complexity

Total Complexity 33
Complexity/F 2.06

Size

Lines of Code 181
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 122
dl 0
loc 181
rs 9.76
c 0
b 0
f 0
wmc 33
mnd 17
bc 17
fnc 16
bpm 1.0625
cpm 2.0625
noi 1

1 Function

Rating   Name   Duplication   Size   Complexity  
F ui.js ➔ enable_provider 0 26 27
1
const org_openpsa_layout = {
2
    clip_toolbar: function() {
3
        if ($('#org_openpsa_toolbar > ul.view_toolbar').length === 0) {
4
            // there seem to be no toolbar buttons, so we don't need to do anything
5
            return;
6
        }
7
        var container = $('#toolbar_dropdown').length > 0 ? $('#toolbar_dropdown') : $('<li class="enabled submenu"><a><i class="fa fa-caret-down"></i> <span class="toolbar_label">' + TOOLBAR_MORE_LABEL + '</span></a><ul class="midcom_toolbar"></ul></li>')
8
            .attr('id', 'toolbar_dropdown')
9
            .data('event_attached', false)
10
            .mouseover(function() {
11
                var self = $(this);
12
                if (self.data('timeout')) {
13
                    clearTimeout(self.data('timeout'));
14
                    self.removeData('timeout');
15
                    return;
16
                }
17
                self.addClass('expanded');
18
            })
19
            .mouseout(function() {
20
                var self = $(this);
21
                self.data('timeout', setTimeout(function() {
22
                    self.removeClass('expanded');
23
                    self.removeData('timeout');
24
                }, 500));
25
            })
26
            .css('display', 'none')
27
            .appendTo('#org_openpsa_toolbar > ul.view_toolbar'),
28
29
            dropdown = container.find('ul.midcom_toolbar'),
30
            toolbarWidth = $('#org_openpsa_toolbar').width() - ($('#org_openpsa_toolbar .navigation_toolbar').width() || 0),
31
            over = false;
32
33
        $('#org_openpsa_toolbar > .view_toolbar > li:not(#toolbar_dropdown)').each(function() {
34
            if (!over && ($(this).position().left + $(this).width() + container.width()) > toolbarWidth) {
35
                over = true;
36
            }
37
            if (over) {
38
                if (!container.data('event_attached')) {
39
                    dropdown.append($(this).detach());
40
                } else {
41
                    dropdown.prepend($(this).detach());
42
                }
43
            }
44
        });
45
        if (!over) {
46
            dropdown.children('li:not(#toolbar_dropdown)').each(function() {
47
                var item = $(this)
48
                    .clone()
49
                    .css('visibility', 'hidden')
50
                    .insertBefore(container),
51
52
                    positionLast = $('#org_openpsa_toolbar .view_toolbar li:last-child').position().left + $('#org_openpsa_toolbar .view_toolbar li:last-child').width();
53
54
                if (positionLast < toolbarWidth) {
55
                    $(this).remove();
56
                    item.css('visibility', 'visible');
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
57
                } else {
58
                    item.remove();
59
                    return false;
60
                }
61
            });
62
        }
63
64
        if (dropdown.children('li').length > 0) {
65
            container.css('display', 'inline-block');
66
        } else {
67
            container.css('display', 'none');
68
        }
69
        if (!container.data('event_attached')) {
70
            $(window).resize(function(){org_openpsa_layout.clip_toolbar();});
71
            container.data('event_attached', true);
72
            container.parent().addClass('clip-initialized');
73
        }
74
    },
75
76
    add_splitter: function() {
77
        $('<div></div>')
78
            .attr('id', 'template_openpsa2_resizer')
79
            .css('left', $('#leftframe').width())
80
            .appendTo('#container');
81
82
        $('#template_openpsa2_resizer').draggable({
83
            axis: 'x',
84
            containment: 'window',
85
            stop: function(event, ui) {
86
                var offset = Math.max((ui.offset.left), 0);
87
88
                $('#leftframe').css('width', offset + 'px');
89
90
                $.post(MIDCOM_PAGE_PREFIX + '__mfa/asgard/preferences/ajax/', {openpsa2_offset: offset + 2});
91
                $(window).trigger('resize');
92
            }
93
        });
94
    },
95
96
    initialize_search: function(providers, current) {
97
        if (typeof providers !== 'object' || providers.length === 0) {
98
            return;
99
        }
100
101
        var field = $('#org_openpsa_search_query'),
102
            selector = $('<ul id="org_openpsa_search_providers"></ul>'),
103
            li_class = '';
104
105
        if (typeof current !== 'string' || current === '') {
106
            current = providers[0].identifier;
107
        }
108
109
        function enable_provider(provider) {
110
            field
111
                .attr('placeholder', provider.placeholder || '')
112
                .focus();
113
            $('#org_openpsa_search_form').attr('action', provider.url);
114
115
            if (provider.autocomplete) {
116
                field.category_complete({
117
                    source: function (request, response) {
118
                        $.ajax({
119
                            url: provider.url + '/autocomplete/',
120
                            dataType: 'json',
121
                            data: {query: request.term},
122
                            success: response
123
                        });
124
                    },
125
                    select: function (event, ui) {
126
                        if (ui.item) {
127
                            location.href = ui.item.url;
128
                        }
129
                    },
130
                    minLength: 2,
131
                    autoFocus: true
132
                });
133
            }
134
        };
135
136
        providers.forEach(function(provider) {
137
            li_class = 'provider';
138
            if (current === provider.identifier) {
139
                li_class += ' current';
140
                enable_provider(provider);
141
            }
142
143
            $('<li class="' + li_class + '">' + provider.placeholder + '</li>')
144
                .data('provider', provider)
145
                .click(function() {
146
                    var old_item = $('#org_openpsa_search_providers .current'),
147
                        query = $('#org_openpsa_search_query');
148
149
                    if (old_item.data('provider').autocomplete) {
150
                        query.category_complete('destroy');
151
                    }
152
153
                    old_item.removeClass('current');
154
                    $(this).addClass('current');
155
156
                    enable_provider(provider);
157
158
                    $('#org_openpsa_search_trigger').click();
159
160
                    $.post(MIDCOM_PAGE_PREFIX + '__mfa/asgard/preferences/ajax/', {openpsa2_search_provider: provider.identifier});
161
                })
162
                .appendTo(selector);
163
        });
164
165
        var search = location.search.replace(/^.*?[\?|&]query=([^&]*).*/, '$1');
166
        if (search !== '' && search !== location.search) {
167
            field.val(decodeURIComponent(search));
168
        }
169
170
        selector.insertBefore(field);
171
172
        $('<div id="org_openpsa_search_trigger"><i class="fa fa-search"></i></div>')
173
            .click(function() {
174
                $('#org_openpsa_search_providers').toggle();
175
                $(this).toggleClass('focused');
176
            })
177
            .insertBefore(field);
178
179
        field.show();
180
    }
181
};
182