Completed
Push — master ( 4ff74f...939158 )
by Askupa
02:11
created

assets/js/src/search.js   A

Complexity

Total Complexity 10
Complexity/F 1.67

Size

Lines of Code 49
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 0
c 1
b 1
f 0
nc 4
dl 0
loc 49
rs 10
wmc 10
mnd 2
bc 9
fnc 6
bpm 1.5
cpm 1.6666
noi 2

3 Functions

Rating   Name   Duplication   Size   Complexity  
A Amarkal.settings.search.onKeyup 0 22 3
A Amarkal.settings.search.init 0 6 1
A Amarkal.settings.search.find 0 17 1
1
/**
2
 * The search class is used internally to search through the fields and find those
3
 * that match the user's query.
4
 */
5
Amarkal.settings.search = {
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ 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...
6
    $fields: null,
7
    $input: null,
8
    init: function() {
9
        var _this = this;
10
        this.$fields = $('.amarkal-settings-field');
11
        this.$input = $('#settings-search');
12
        this.$input.on('keyup',function(){_this.onKeyup();});
13
    },
14
    onKeyup: function(e) {
0 ignored issues
show
Unused Code introduced by
The parameter e 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...
15
        var query = this.$input.val().toLowerCase(),
16
            $matches;
17
    
18
        // The user's search query is longer than 1 character
19
        if(query.length > 1) {
20
            
21
            $matches = this.find(query);
22
            
23
            this.$fields.hide();
24
            $matches.show();
25
26
            // Textual user feedback
27
            $('#settings-search-results').text($matches.length ? $matches.length+' settings found' : 'Nothing found');
28
        }
29
30
        // The user's search query is not longer than 1 character
31
        else {
32
            this.$fields.show();
33
            $('#settings-search-results').text('');
34
        }
35
    },
36
    find: function(query) {
37
        var matches  = [];
38
        
39
        this.$fields.each(function(){
40
            var $field = $(this),
41
                title  = $field.attr('data-title').toLowerCase(),
42
                description = $field.find('.description').text();
43
44
            // Check query against the field's title
45
            if(title.match(query) || description.match(query)){
46
                matches.push($field);
47
            }
48
        });
49
50
        // Convert the matches array to a jQuery
51
        return $(matches).map(function(){return this.toArray();});
52
    }
53
};