Completed
Branch master (d2f96a)
by Rafael S.
02:27
created

docs/scripts/search.js   A

Complexity

Total Complexity 19
Complexity/F 1.9

Size

Lines of Code 82
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 44
mnd 9
bc 9
fnc 10
dl 0
loc 82
rs 10
bpm 0.9
cpm 1.9
noi 6
c 0
b 0
f 0
1
2
var searchAttr = 'data-search-mode';
3
function contains(a,m){
4
    return (a.textContent || a.innerText || "").toUpperCase().indexOf(m) !== -1;
5
};
6
7
//on search
8
document.getElementById("nav-search").addEventListener("keyup", function(event) {
9
    var search = this.value.toUpperCase();
10
11
    if (!search) {
12
        //no search, show all results
13
        document.documentElement.removeAttribute(searchAttr);
14
        
15
        document.querySelectorAll("nav > ul > li:not(.level-hide)").forEach(function(elem) {
16
            elem.style.display = "block";
17
        });
18
19
        if (typeof hideAllButCurrent === "function"){
20
            //let's do what ever collapse wants to do
21
            hideAllButCurrent();
22
        } else {
23
            //menu by default should be opened
24
            document.querySelectorAll("nav > ul > li > ul li").forEach(function(elem) {
25
                elem.style.display = "block";
26
            });
27
        }
28
    } else {
29
        //we are searching
30
        document.documentElement.setAttribute(searchAttr, '');
31
32
        //show all parents
33
        document.querySelectorAll("nav > ul > li").forEach(function(elem) {
34
            elem.style.display = "block";
35
        });
36
        //hide all results
37
        document.querySelectorAll("nav > ul > li > ul li").forEach(function(elem) {
38
            elem.style.display = "none";
39
        });
40
        //show results matching filter
41
        document.querySelectorAll("nav > ul > li > ul a").forEach(function(elem) {
42
            if (!contains(elem.parentNode, search)) {
43
                return;
44
            }
45
            elem.parentNode.style.display = "block";
46
        });
47
        //hide parents without children
48
        document.querySelectorAll("nav > ul > li").forEach(function(parent) {
49
            var countSearchA = 0;
50
            parent.querySelectorAll("a").forEach(function(elem) {
51
                if (contains(elem, search)) {
52
                    countSearchA++;
53
                }
54
            });
55
            
56
            var countUl = 0;
57
            var countUlVisible = 0;
58
            parent.querySelectorAll("ul").forEach(function(ulP) {
59
                // count all elements that match the search
60
                if (contains(ulP, search)) {
61
                    countUl++;
62
                }
63
                
64
                // count all visible elements
65
                var children = ulP.children
66
                for (i=0; i<children.length; i++) {
67
                    var elem = children[i];
68
                    if (elem.style.display != "none") {
69
                        countUlVisible++;
70
                    }
71
                }
72
            });
73
          
74
            if (countSearchA == 0 && countUl === 0){
75
                //has no child at all and does not contain text
76
                parent.style.display = "none";
77
            } else if(countSearchA == 0 && countUlVisible == 0){
78
                //has no visible child and does not contain text
79
                parent.style.display = "none";
80
            }
81
        });
82
    }
83
});