Passed
Push — master ( 192c6a...bb68c7 )
by Plamen
01:28
created

add/table_methods.js   B

Complexity

Total Complexity 49
Complexity/F 3.06

Size

Lines of Code 196
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 1 Features 0
Metric Value
cc 0
eloc 143
nc 1
dl 0
loc 196
rs 8.48
c 8
b 1
f 0
wmc 49
mnd 4
bc 42
fnc 16
bpm 2.625
cpm 3.0625
noi 3

11 Functions

Rating   Name   Duplication   Size   Complexity  
A table_methods.GoPage.Run 0 8 1
A table_methods.Filter.GetTableId 0 7 3
A table_methods.Sort 0 25 3
A table_methods.LoadData.Run 0 21 3
A table_methods.GoPage.GetNo 0 16 4
A table_methods.LoadData.SetVisability 0 14 3
A table_methods.Filter.Run 0 17 2
A table_methods.ReloadData 0 5 1
A table_methods.Export 0 9 2
A table_methods.Draw.Run 0 12 3
A table_methods.Draw.Section 0 50 4

How to fix   Complexity   

Complexity

Complex classes like add/table_methods.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_methods = {
2
    helper: window.table_helper,
3
    Draw: {
4
        Section: function(tableContainer, dt, tSection){
5
            var section = tSection === "tfoot" ? "tfoot" : "tbody";
6
            var tSec = document.getElementById(tableContainer)
7
                    .getElementsByTagName(section)[0];
8
            Clear(tSec, this.helper.IePrior(9));
9
            for(var i = 0; i < dt.length; i++){
10
                var row = dt[i];
11
                var tRow = document.createElement("tr");
12
                Row(row, tRow);
13
                tSec.appendChild(tRow);
14
                if(section === "tfoot"){
15
                    this.helper.ProcessPaginationLinks(tSec);
16
                }
17
            }
18
            function Clear(tSection, iePrior9){
19
                if(iePrior9){
20
                    if(tSection.firstChild){
21
                        while(tSection.firstChild){
22
                            tSection.removeChild(tSection.firstChild);
23
                        }
24
                    }
25
                }else{
26
                    tSection.innerHTML = "";
27
                }
28
            }
29
            function Row(row, tRow){
30
                for(var cell in row){
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...
31
                    var tCell = document.createElement("td");
32
                    if(typeof row[cell] === "string" ||
33
                            typeof row[cell] === "number"
34
                            ){
35
                        tCell.innerHTML = row[cell];
36
                    }else if(typeof row[cell] === "object"){
37
                        RowCellFromObject(row, cell, tCell);
38
                    }
39
                    tRow.appendChild(tCell);
40
                }
41
            }
42
            function RowCellFromObject(row, cell, tCell){
43
                for(var attr in row[cell]){
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...
44
                    if(typeof row[cell][attr] === "string"){
45
                        tCell.innerHTML = row[cell][attr];
46
                    }else if(typeof row[cell][attr] === "object"){
47
                        for(var v in row[cell][attr]){
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...
48
                            tCell.setAttribute(v, row[cell][attr][v]);
49
                        }
50
                    }
51
                }
52
            }
53
        },
54
        Run: function(tableContainer, d, instance){
55
56
            this.Section.call(instance, tableContainer, d.body);
57
            this.Section.call(instance, tableContainer, d.footer, "tfoot");
58
            if(instance.rq !== null){
59
                var hover = document.getElementById(instance.rq.tableId)
60
                        .getElementsByTagName("th")[instance.rq.colNo].lang;
61
                if(hover){
62
                    instance.helper.ColumnHover(tableContainer, instance.rq.colNo);
63
                }
64
            }
65
        }
66
    },
67
    Export: function(lnk, eType){
68
        var tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
69
        var rq = this.helper.BuildRequest.call(this, tableId);
70
        rq.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ?
71
                eType :
72
                "csv";
73
        window.open(this.helper.RequestToUrl(rq));
74
        return false;
75
    },
76
    Filter: {
77
        Run: function(field){
78
            var tableId = this.Filter.GetTableId(field);
79
            if(tableId !== null){
80
                /*var exRq = this.rq;*/
81
                var rq = this.helper.BuildRequest.call(this, tableId);
82
                this.rq = rq;
83
                this.LoadData.Run(this);
84
                //@todo exRq match not work
85
                /*if(exRq === null
86
                        || typeof this.rq.filer === "undefined"
87
                        || this.rq.filter !== exRq.filter
88
                        || this.rq.filterBy !== exRq.filterBy
89
                        ){
90
                    this.LoadData.Run(this);
91
                }*/
92
            }
93
        },
94
        GetTableId: function(field){
95
            if(field.tagName.toLowerCase() !== "select"){
96
                return field.getAttribute("data-table-id");
97
            }
98
            var f = field.parentNode.parentNode.getElementsByTagName("input")[0];
99
            return '' === f.value ? null : f.getAttribute("data-table-id");
100
        }
101
    },
102
    GoPage: {
103
        Run: function(lnk){
104
            var tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
105
            var rq = this.helper.BuildRequest.call(this, tableId);
106
            rq.pageNo = this.GoPage.GetNo(lnk, rq.tableId);
107
            this.rq = rq;
108
            this.LoadData.Run(this);
109
            return false;
110
        },
111
        GetNo: function(lnk, tableId){
112
            //check & serve pagination jump links
113
            var jumpDir = lnk.innerHTML.trim().substr(0, 1);
114
            if(jumpDir === "+" || jumpDir === "-"){
115
                var current = document.getElementById(tableId)
116
                        .querySelector("tfoot .paging .a").innerHTML;
117
                var jump = lnk.innerHTML.replace("K", "000")
118
                        .replace("M", "000000000");
119
                var jumpPage = (parseInt(current) + parseInt(jump));
120
                lnk.parentNode.setAttribute("data-page", jumpPage);
121
                lnk.style.transform = "none";
122
            }
123
            return lnk.parentNode.hasAttribute("data-page") ?
124
                    lnk.parentNode.getAttribute("data-page") :
125
                    lnk.innerHTML;
126
        }
127
    },
128
    LoadData: {
129
        tail: null,
130
        Run: function(instance){
131
            if(this.tail !== null){
132
                this.tail.abort();
133
            }
134
            this.SetVisability(instance.rq.tableId, false);
135
            var inst = instance;
136
            var xmlhttp = window.XMLHttpRequest ?
137
                    new XMLHttpRequest() : //IE7+, Firefox, Chrome, Opera, Safari
138
                    new window.ActiveXObject("Microsoft.XMLHTTP");//IE6, IE5
139
            xmlhttp.onreadystatechange = function(){
140
                if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
141
                    var d = JSON.parse(xmlhttp.responseText);
142
                    inst.Draw.Run(inst.rq.tableId, d, inst);
143
                    inst.LoadData.SetVisability(inst.rq.tableId, true);
144
                    window.table.LoadEndCalback(inst.rq.tableId);
145
                }
146
            };
147
            xmlhttp.open("GET", instance.helper.RequestToUrl(inst.rq), true);
148
            xmlhttp.send();
149
            this.tail = xmlhttp; //put at tail to can abort later previous request
150
        },
151
        SetVisability: function(tableContainer, flag){
152
            var tbl = document.getElementById(tableContainer);
153
            if(flag === true){
154
                tbl.style.filter = "none";
155
                tbl.style.opacity = "1";
156
                tbl.style.cursor = "auto";
157
            }else if(flag === false){
158
                tbl.style.filter = "blur(1px)";
159
                tbl.style.opacity = "0.8";
160
                tbl.style.cursor = "wait";
161
            }else{
162
                console.error("table error in the flag value");
163
            }
164
        }
165
    },
166
    ReloadData: function(tableId){
167
        var rq = this.helper.BuildRequest.call(this, tableId);;
168
        this.rq = rq;
169
        this.LoadData.Run(this);
170
    },
171
    Sort: function(colNo, lnk){
172
        var tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
173
        var rq = this.helper.BuildRequest.call(this, tableId);
174
        if(Math.round(colNo) === rq.colNo){
175
            rq.colOrd = (rq.colOrd === "asc" ? "desc" : "asc");
176
        }else{
177
            rq.colNo = Math.round(colNo);
178
            rq.colOrd = "asc";
179
        }
180
        this.rq = rq;
181
        this.LoadData.Run(this);
182
        ClearAndSetNewSortArrow(rq);
183
184
        function ClearAndSetNewSortArrow(rq){
185
            var headSpans = document.getElementById(rq.tableId)
186
                    .getElementsByTagName("thead")[0]
187
                    .getElementsByTagName("span");
188
            var length = headSpans.length;
189
            for(var i = 0; i < length; i++){
190
                headSpans[i].innerHTML = "";
191
            }
192
            lnk.getElementsByTagName("span")[0].innerHTML =
193
                    (rq.colOrd === "desc" ? rq.strDesc : rq.strAsc);
194
        }
195
    }
196
};
197