src/javascript/modules/SearchForm.js   A
last analyzed

Size

Lines of Code 55

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
c 0
b 0
f 0
rs 10
nc 1
noi 5

1 Function

Rating   Name   Duplication   Size   Complexity  
A SearchForm.js ➔ ??? 0 8 1
1
class SearchForm {
2
3
    constructor(container) {
4
        this.container = container;
5
        this.input = this.container.querySelector('input[name="search"]');
6
        this.timeout = null; // init timeout variable to be used below in bindEvents
7
        this.time = 500; // in ms
8
9
        this.bindEvents();
10
    }
11
12
    bindEvents() {
13
        // listen for keystroke events
14
        this.input.addEventListener('keyup', (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
            // clear timeout if it has already been set.
16
            // this will prevent the previous task from executing
17
            // if it has been less than <MILLISECONDS>
18
            clearTimeout(this.timeout);
19
20
            // make a new timeout set to go off in {this.time}
21
            this.timeout = setTimeout(() => this.search(this.input.value), this.time);
22
        });
23
    }
24
25
    search(string) {
26
        let link = this.setUrlParameter(
27
            this.container.getAttribute('action'),
28
            'search',
29
            string
30
        );
31
32
        let moduleId = this.container.querySelector('input[name="moduleId"]');
33
34
        if (moduleId) {
35
            link = this.setUrlParameter(link, 'moduleId', moduleId.value);
36
        }
37
38
        window.location = link;
39
    }
40
41
    setUrlParameter(url, key, value) {
42
        let parts = url.split("#", 2), anchor = parts.length > 1 ? "#" + parts[1] : '';
43
        let query = (url = parts[0]).split("?", 2);
44
        if (query.length === 1)
45
            return url + "?" + key + "=" + value + anchor;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
46
47
        for (let params = query[query.length - 1].split("&"), i = 0; i < params.length; i++)
48
            if (params[i].toLowerCase().startsWith(key.toLowerCase() + "="))
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
49
                return params[i] = key + "=" + value, query[query.length - 1] = params.join("&"), query.join("?") + anchor;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
50
51
        return url + "&" + key + "=" + value + anchor
52
    }
53
}
54
55
export default SearchForm;