Completed
Push — master ( 60399b...890f86 )
by Craig
06:15
created

Zikula.Users.LiveSearch.js ➔ initUserLiveSearch   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 49
rs 9.2258
c 2
b 2
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A Zikula.Users.LiveSearch.js ➔ ... ➔ $(?!?).click 0 5 1
A Zikula.Users.LiveSearch.js ➔ ... ➔ $(?!?).autocomplete.source 0 5 1
A Zikula.Users.LiveSearch.js ➔ ... ➔ $(?!?).autocomplete.response 0 6 2
A Zikula.Users.LiveSearch.js ➔ ... ➔ $(?!?).autocomplete.autocomplete._renderItem 0 5 1
A Zikula.Users.LiveSearch.js ➔ ... ➔ $(?!?).autocomplete.select 0 6 1
A Zikula.Users.LiveSearch.js ➔ ... ➔ $(?!?).autocomplete.focus 0 5 1
A Zikula.Users.LiveSearch.js ➔ ... ➔ $(?!?).autocomplete.open 0 5 1
1
// Copyright Zikula Foundation, licensed MIT.
2
3
/**
4
 * Initialises a user field with auto completion.
5
 */
6
function initUserLiveSearch(fieldName)
7
{
8
    jQuery('#' + fieldName + 'ResetVal').click( function (event) {
9
        event.preventDefault();
10
        jQuery('#' + fieldName).val('');
11
        jQuery('#' + fieldName + 'Selector').val('');
12
    }).removeClass('hidden');
13
14
    if (jQuery('#' + fieldName + 'LiveSearch').length < 1) {
15
        return;
16
    }
17
    jQuery('#' + fieldName + 'LiveSearch').removeClass('hidden');
18
19
    jQuery('#' + fieldName + 'Selector').autocomplete({
20
        minLength: 1,
21
        open: function(event, ui) {
0 ignored issues
show
Unused Code introduced by
The parameter ui is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
22
            jQuery(this).autocomplete('widget').css({
23
                width: (jQuery(this).outerWidth() + 'px')
24
            });
25
        },
26
        source: function (request, response) {
27
            jQuery.getJSON(Routing.generate('zikulausersmodule_livesearch_getusers', { fragment: request.term }), function(data) {
0 ignored issues
show
Bug introduced by
The variable Routing seems to be never declared. If this is a global, consider adding a /** global: Routing */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
28
                response(data);
29
            });
30
        },
31
        response: function(event, ui) {
32
            jQuery('#' + fieldName + 'LiveSearch .empty-message').remove();
33
            if (ui.content.length === 0) {
34
                jQuery('#' + fieldName + 'LiveSearch').append('<div class="empty-message">' + Translator.__('No results found!') + '</div>');
0 ignored issues
show
Bug introduced by
The variable Translator seems to be never declared. If this is a global, consider adding a /** global: Translator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
35
            }
36
        },
37
        focus: function(event, ui) {
38
            jQuery('#' + fieldName + 'Selector').val(ui.item.uname);
39
40
            return false;
41
        },
42
        select: function(event, ui) {
43
            jQuery('#' + fieldName).val(ui.item.uid);
44
            jQuery('#' + fieldName + 'Avatar').html(ui.item.avatar);
45
46
            return false;
47
        }
48
    })
49
    .autocomplete('instance')._renderItem = function(ul, item) {
50
        return jQuery('<div class="suggestion">')
51
            .append('<div class="media"><div class="media-left"><a href="javascript:void(0)">' + item.avatar + '</a></div><div class="media-body"><p class="media-heading">' + item.uname + '</p></div></div>')
52
            .appendTo(ul);
53
    };
54
}
55