Completed
Push — master ( 693283...8cba26 )
by Craig
06:26
created

Zikula.Users.LiveSearch.js ➔ initUserLiveSearch   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
cc 2
nc 2
nop 1
rs 8.8571

6 Functions

Rating   Name   Duplication   Size   Complexity  
A Zikula.Users.LiveSearch.js ➔ ... ➔ $(?!?).autocomplete.source 0 5 1
A Zikula.Users.LiveSearch.js ➔ ... ➔ $(?!?).autocomplete.response 0 7 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 ➔ ... ➔ $(?!?).click 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
        source: function (request, response) {
22
            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...
23
                response(data);
24
            });
25
        },
26
        response: function(event, ui) {
27
            if (ui.content.length === 0) {
28
                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...
29
            } else {
30
                jQuery('#' + fieldName + 'LiveSearch .empty-message').remove();
31
            }
32
        },
33
        focus: function(event, ui) {
34
            jQuery('#' + fieldName + 'Selector').val(ui.item.uname);
35
36
            return false;
37
        },
38
        select: function(event, ui) {
39
            jQuery('#' + fieldName).val(ui.item.uid);
40
            jQuery('#' + fieldName + 'Avatar').html(ui.item.avatar);
41
42
            return false;
43
        }
44
    })
45
    .autocomplete('instance')._renderItem = function(ul, item) {
46
        return jQuery('<div class="suggestion">')
47
            .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>')
48
            .appendTo(ul);
49
    };
50
}
51