Passed
Push — master ( 408626...9564ba )
by Plamen
01:39
created

add/table_helper.js   B

Complexity

Total Complexity 52
Complexity/F 3.25

Size

Lines of Code 165
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 0
eloc 116
nc 1
dl 0
loc 165
rs 7.44
c 5
b 0
f 1
wmc 52
mnd 3
bc 44
fnc 16
bpm 2.75
cpm 3.25
noi 1

7 Functions

Rating   Name   Duplication   Size   Complexity  
A table_helper.Init 0 20 2
A table_helper.ColumnHover 0 20 3
A table_helper.RequestToUrl 0 17 5
B table_helper.BuildRequest 0 24 5
A table_helper.GetParent 0 6 3
A table_helper.ProcessPaginationLinks 0 9 3
A table_helper.IePrior 0 12 4

How to fix   Complexity   

Complexity

Complex classes like add/table_helper.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
var table_helper = {
2
    Init: function(tableId){
3
        var tContainer = document.getElementById(tableId);
4
        if(!this.IePrior(9)){
5
            SetColumnsHoverEffect(tContainer, tableId);
6
        }
7
        var tfoot = tContainer.getElementsByTagName("tfoot")[0];
8
        this.ProcessPaginationLinks(tfoot);
9
        
10
        function SetColumnsHoverEffect(tContainer, tableId){
11
            var tHcells = tContainer.rows[0].cells;
12
            for(var i = 0; i < tHcells.length; i++){
13
                if(tHcells[i].firstChild.tagName === "A"){
14
                    tHcells[i].firstChild.setAttribute("onmouseover",
15
                            "table.ColumnHover('" + tableId + "'," + i + ");");
16
                    tHcells[i].firstChild.setAttribute("onmouseout",
17
                            "table.ColumnHover('" + tableId + "');");
18
                }
19
            }
20
        }
21
    },
22
    ColumnHover: function(tableContainer, index){
23
        var rows = document.getElementById(tableContainer).rows;
24
        var upto = rows.length - 1;
25
        if(typeof index === "undefined"){
26
            ColumnHoverRelease(rows, upto);
27
        }else{
28
            for(var i = 0; i < upto; i++){
29
                rows[i].cells[index].setAttribute("lang", "col-hover");
30
            }
31
        }
32
        function ColumnHoverRelease(rows, upto){
33
            for(var i = 0; i < upto; i++){
34
                for(var j = 0; j < rows[i].cells.length; j++){
35
                    if(rows[i].cells[j].lang){
36
                        rows[i].cells[j].removeAttribute("lang");
37
                    }
38
                }
39
            }
40
        };
41
    },
42
    BuildRequest: function(tableId){
43
        var rq = {tableId: tableId, strDesc: this.strAsc, strAsc: this.strAsc};
44
        (function (){ //Sort(rq);
45
            var thTags = document.getElementById(rq.tableId)
46
                    .getElementsByTagName("thead")[0]
47
                    .getElementsByTagName("th");
48
            var length = thTags.length;
49
            for(var i = 0; i < length; i++){
50
                var link = thTags[i].getElementsByTagName("a")[0];
51
                if(link){
52
                    var span = link.getElementsByTagName("span")[0];
53
                    if(span && sortBySpan(span, i)){
54
                        break;
55
                    }
56
                }
57
            }
58
            
59
            function sortBySpan(span, i){
60
                var order = span.innerHTML;
61
                if(order.length === 1){
62
                    rq.colNo = i;
63
                    rq.colOrd = order === rq.strDesc ? "desc" : "asc";
64
                }
65
                return rq.colNo === i;
66
            }
67
        })(rq);
68
        (function (rq){ // Filter(rq);
69
            var r = getFilterFieldsByTableID(rq.tableId);
70
            if(r.filter !== null){
71
                rq.filter = r.filter;
72
            }
73
            if(r.filterBy !== null){
74
                rq.filterBy = r.filterBy;
75
            }
76
            
77
            function getFilterFieldsByTableID(tableID){
78
                var fields = {filterBy: null, filter: null};
79
                var filterDiv = getFilterDivByTableIDOrNull(tableID);
80
                if(filterDiv !== null){
81
                    setFilterBy(fields, filterDiv);
82
                    setFilterValue(fields, filterDiv);
83
                }
84
                return fields;
85
                
86
                function getFilterDivByTableIDOrNull(tableID){
87
                    if(document.getElementById(tableID).parentNode
88
                            .getElementsByTagName("div").length > 0
89
                            ){
90
                        var containerDivs = document.getElementById(tableID)
91
                                .parentNode.getElementsByTagName("div");
92
                        for(var i = 0; i < containerDivs.length; i++){
93
                            if(containerDivs[i].getAttribute("class") === "filter"){
94
                                return containerDivs[i];
95
                            }
96
                        }
97
98
                    }
99
                    return null;
100
                }
101
                
102
                function setFilterBy(fields, filterDiv){
103
                    var select = filterDiv.getElementsByTagName("select")[0];
104
                    if(select &&
105
                            select.options[select.selectedIndex].value !== "all"
106
                            ){
107
                        fields.filterBy = select.options[select.selectedIndex].value;
108
                    }
109
                }
110
                function setFilterValue(fields, filterDiv){
111
                    var textObj = filterDiv.getElementsByTagName("input")[0];
112
                    if(textObj && textObj.value && textObj.value.length !== 0){
113
                        fields.filter = encodeURIComponent(textObj.value.trim());
114
                    }
115
                }
116
            }
117
        })(rq);
118
        return rq;
119
        
120
    },
121
    IePrior: function(v){
122
        var rv = false;
123
        if(window.navigator.appName === 'Microsoft Internet Explorer'){
124
            var ua = window.navigator.userAgent;
125
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
126
            if(re.exec(ua) !== null){
127
                rv = parseFloat(RegExp.$1);
128
            }
129
            rv = rv < v ? true : false;
130
        }
131
        return rv;
132
    },
133
    GetParent: function(obj, objType){
134
        while(obj && obj.tagName !== objType.toUpperCase()){
135
            obj = obj.parentNode;
136
        }
137
        return obj;
138
    },
139
    ProcessPaginationLinks: function(tfoot){
140
        var pLinks = tfoot.querySelectorAll(".paging a");
141
        if(pLinks.length > 0){
142
            for(var j = 0; j < pLinks.length; j++){
143
                pLinks[j].setAttribute("href", "javascript:void(0);");
144
                pLinks[j].setAttribute("onclick", "return table.GoPage(this);");
145
            }
146
        }
147
    },
148
    RequestToUrl: function(rq){
149
        var url = location.pathname + ".json" + location.search;
150
        if(typeof rq === "object"){
151
            var getUrlVarName = {
152
                colNo: "col", colOrd: "ord", filter: "filter",
153
                filterBy: "filter-by", pageNo: "pg", exportType: "export",
154
                tableId: "table-id"
155
            };
156
            var flagFirst = location.search.length < 1 ? true : false;
157
            for(var r in rq){
1 ignored issue
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
158
                var clue = flagFirst === true ? "?" : "&";
159
                url += clue + getUrlVarName[r] + "=" + rq[r];
160
                flagFirst = false;
161
            }
162
        }
163
        return url;
164
    }
165
};
166