Passed
Push — master ( bb68c7...408626 )
by Plamen
01:24
created

add/table_helper.js   B

Complexity

Total Complexity 52
Complexity/F 3.25

Size

Lines of Code 167
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 0
eloc 116
c 4
b 0
f 1
nc 1
dl 0
loc 167
rs 7.44
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.GetParent 0 6 3
B table_helper.BuildRequest 0 81 1
A table_helper.ProcessPaginationLinks 0 9 3
A table_helper.IePrior 0 12 4
A table_helper.RequestToUrl 0 17 5

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
        Sort(rq);
45
        Filter(rq);
46
        return rq;
47
        
48
        function Sort(rq){
49
            var thTags = document.getElementById(rq.tableId)
50
                    .getElementsByTagName("thead")[0]
51
                    .getElementsByTagName("th");
52
            var length = thTags.length;
53
            for(var i = 0; i < length; i++){
54
                var link = thTags[i].getElementsByTagName("a")[0];
55
                if(link){
56
                    var span = link.getElementsByTagName("span")[0];
57
                    if(span && sortBySpan(span, i)){
58
                        break;
59
                    }
60
                }
61
            }
62
            
63
            function sortBySpan(span, i){
64
                var order = span.innerHTML;
65
                if(order.length === 1){
66
                    rq.colNo = i;
67
                    rq.colOrd = order === rq.strDesc ? "desc" : "asc";
68
                }
69
                return rq.colNo === i;
70
            }
71
        }
72
        function Filter(rq){
73
            var r = getFilterFieldsByTableID(rq.tableId);
74
            if(r.filter !== null){
75
                rq.filter = r.filter;
76
            }
77
            if(r.filterBy !== null){
78
                rq.filterBy = r.filterBy;
79
            }
80
            
81
            function getFilterFieldsByTableID(tableID){
82
                var fields = {filterBy: null, filter: null};
83
                var filterDiv = getFilterDivByTableIDOrNull(tableID);
84
                if(filterDiv !== null){
85
                    setFilterBy(fields, filterDiv);
86
                    setFilterValue(fields, filterDiv);
87
                }
88
                return fields;
89
                
90
                function getFilterDivByTableIDOrNull(tableID){
91
                    if(document.getElementById(tableID).parentNode
92
                            .getElementsByTagName("div").length > 0
93
                            ){
94
                        var containerDivs = document.getElementById(tableID)
95
                                .parentNode.getElementsByTagName("div");
96
                        for(var i = 0; i < containerDivs.length; i++){
97
                            if(containerDivs[i].getAttribute("class") === "filter"){
98
                                return containerDivs[i];
99
                            }
100
                        }
101
102
                    }
103
                    return null;
104
                }
105
                
106
                function setFilterBy(fields, filterDiv){
107
                    var select = filterDiv.getElementsByTagName("select")[0];
108
                    if(select &&
109
                            select.options[select.selectedIndex].value !== "all"
110
                            ){
111
                        fields.filterBy = select.options[select.selectedIndex].value;
112
                    }
113
                }
114
                function setFilterValue(fields, filterDiv){
115
                    var textObj = filterDiv.getElementsByTagName("input")[0];
116
                    if(textObj && textObj.value && textObj.value.length !== 0){
117
                        fields.filter = encodeURIComponent(textObj.value.trim());
118
                    }
119
                }
120
            }
121
        }
122
    },
123
    IePrior: function(v){
124
        var rv = false;
125
        if(window.navigator.appName === 'Microsoft Internet Explorer'){
126
            var ua = window.navigator.userAgent;
127
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
128
            if(re.exec(ua) !== null){
129
                rv = parseFloat(RegExp.$1);
130
            }
131
            rv = rv < v ? true : false;
132
        }
133
        return rv;
134
    },
135
    GetParent: function(obj, objType){
136
        while(obj && obj.tagName !== objType.toUpperCase()){
137
            obj = obj.parentNode;
138
        }
139
        return obj;
140
    },
141
    ProcessPaginationLinks: function(tfoot){
142
        var pLinks = tfoot.querySelectorAll(".paging a");
143
        if(pLinks.length > 0){
144
            for(var j = 0; j < pLinks.length; j++){
145
                pLinks[j].setAttribute("href", "javascript:void(0);");
146
                pLinks[j].setAttribute("onclick", "return table.GoPage(this);");
147
            }
148
        }
149
    },
150
    RequestToUrl: function(rq){
151
        var url = location.pathname + ".json" + location.search;
152
        if(typeof rq === "object"){
153
            var getUrlVarName = {
154
                colNo: "col", colOrd: "ord", filter: "filter",
155
                filterBy: "filter-by", pageNo: "pg", exportType: "export",
156
                tableId: "table-id"
157
            };
158
            var flagFirst = location.search.length < 1 ? true : false;
159
            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...
160
                var clue = flagFirst === true ? "?" : "&";
161
                url += clue + getUrlVarName[r] + "=" + rq[r];
162
                flagFirst = false;
163
            }
164
        }
165
        return url;
166
    }
167
};
168