Passed
Push — master ( 1def37...7a9599 )
by Plamen
01:31
created

table_methods.BuildRequest   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 53
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 76
rs 8.5381

2 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 48 3
A 0 23 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
var table_methods = {
2
    helper: window.table_helper,
3
    BuildRequest: function(rq){
4
        Sort(rq);
5
        Filter(rq);
6
        
7
        function Sort(rq){
8
            function sortBySpan(span, i){
9
                var order = span.innerHTML;
10
                if(order.length === 1){
11
                    rq.colNo = i;
12
                    rq.colOrd = order === rq.strDesc ? "desc" : "asc";
13
                }
14
                return rq.colNo === i;
15
            }
16
            var thTags = document.getElementById(rq.tableId)
17
                    .getElementsByTagName("thead")[0]
18
                    .getElementsByTagName("th");
19
            var length = thTags.length;
20
            for(var i = 0; i < length; i++){
21
                var link = thTags[i].getElementsByTagName("a")[0];
22
                if(link){
23
                    var span = link.getElementsByTagName("span")[0];
24
                    if(span && sortBySpan(span, i)){
25
                        break;
26
                    }
27
                }
28
            }
29
        }
30
        function Filter(rq){
31
            function getFilterFieldsByTableID(tableID){
32
                var fields = {filterBy: null, filter: null};
33
                var filterDiv = getFilterDivByTableIDOrNull(tableID);
34
                if(filterDiv !== null){
35
                    setFilterBy(fields, filterDiv);
36
                    setFilterValue(fields, filterDiv);
37
                }
38
                return fields;
39
            }
40
            function getFilterDivByTableIDOrNull(tableID){
41
                if(document.getElementById(tableID).parentNode
42
                        .getElementsByTagName("div").length > 0
43
                        ){
44
                    var containerDivs = document.getElementById(tableID)
45
                            .parentNode.getElementsByTagName("div");
46
                    for(var i = 0; i < containerDivs.length; i++){
47
                        if(containerDivs[i].getAttribute("class") === "filter"){
48
                            return containerDivs[i];
49
                        }
50
                    }
51
52
                }
53
                return null;
54
            }
55
            function setFilterBy(fields, filterDiv){
56
                var select = filterDiv.getElementsByTagName("select")[0];
57
                if(select &&
58
                        select.options[select.selectedIndex].value !== "all"
59
                        ){
60
                    fields.filterBy = select.options[select.selectedIndex].value;
61
                }
62
            }
63
            function setFilterValue(fields, filterDiv){
64
                var textObj = filterDiv.getElementsByTagName("input")[0];
65
                if(textObj && textObj.value && textObj.value.length !== 0){
66
                    fields.filter = encodeURIComponent(textObj.value.trim());
67
                }
68
            }
69
70
            var r = getFilterFieldsByTableID(rq.tableId);
71
            if(r.filter !== null){
72
                rq.filter = r.filter;
73
            }
74
            if(r.filterBy !== null){
75
                rq.filterBy = r.filterBy;
76
            }
77
        }
78
    },
79
    Draw: {
80
        Section: function(tableContainer, dt, tSection){
81
            var section = tSection === "tfoot" ? "tfoot" : "tbody";
82
            var tSec = document.getElementById(tableContainer)
83
                    .getElementsByTagName(section)[0];
84
            Clear(tSec, this.helper.IePrior(9));
85
            for(var i = 0; i < dt.length; i++){
86
                var row = dt[i];
87
                var tRow = document.createElement("tr");
88
                Row(row, tRow);
89
                tSec.appendChild(tRow);
90
                if(section === "tfoot"){
91
                    this.helper.ProcessPaginationLinks(tSec);
92
                }
93
            }
94
            function Clear(tSection, iePrior9){
95
                if(iePrior9){
96
                    if(tSection.firstChild){
97
                        while(tSection.firstChild){
98
                            tSection.removeChild(tSection.firstChild);
99
                        }
100
                    }
101
                }else{
102
                    tSection.innerHTML = "";
103
                }
104
            }
105
            function Row(row, tRow){
106
                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...
107
                    var tCell = document.createElement("td");
108
                    if(typeof row[cell] === "string" ||
109
                            typeof row[cell] === "number"
110
                            ){
111
                        tCell.innerHTML = row[cell];
112
                    }else if(typeof row[cell] === "object"){
113
                        RowCellFromObject(row, cell, tCell);
114
                    }
115
                    tRow.appendChild(tCell);
116
                }
117
            }
118
            function RowCellFromObject(row, cell, tCell){
119
                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...
120
                    if(typeof row[cell][attr] === "string"){
121
                        tCell.innerHTML = row[cell][attr];
122
                    }else if(typeof row[cell][attr] === "object"){
123
                        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...
124
                            tCell.setAttribute(v, row[cell][attr][v]);
125
                        }
126
                    }
127
                }
128
            }
129
        },
130
        Run: function(tableContainer, d, instance){
131
132
            this.Section.call(instance, tableContainer, d.body);
133
            this.Section.call(instance, tableContainer, d.footer, "tfoot");
134
            if(instance.rq !== null){
135
                var hover = document.getElementById(instance.rq.tableId)
136
                        .getElementsByTagName("th")[instance.rq.colNo].lang;
137
                if(hover){
138
                    instance.helper.ColumnHover(tableContainer, instance.rq.colNo);
139
                }
140
            }
141
        }
142
    },
143
    Export: function(lnk, eType){
144
        var rq = {};
145
        rq.tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
146
        this.BuildRequest(rq);
147
        rq.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ?
148
                eType :
149
                "csv";
150
        window.open(this.helper.RequestToUrl(rq));
151
        return false;
152
    },
153
    Filter: {
154
        Run: function(field){
155
            var crntTableId = this.Filter.GetTableId(field);
156
            if(crntTableId !== null){
157
                /*var exRq = this.rq;*/
158
                var rq = {};
159
                rq.tableId = crntTableId;
160
                rq.strDesc = this.strDesc;
161
                rq.strAsc = this.strAsc;
162
                this.BuildRequest(rq);
163
                this.rq = rq;
164
                this.LoadData.Run(this);
165
                //@todo exRq match not work
166
                /*if(exRq === null
167
                        || typeof this.rq.filer === "undefined"
168
                        || this.rq.filter !== exRq.filter
169
                        || this.rq.filterBy !== exRq.filterBy
170
                        ){
171
                    this.LoadData.Run(this);
172
                }*/
173
            }
174
        },
175
        GetTableId: function(field){
176
            if(field.tagName.toLowerCase() !== "select"){
177
                return field.getAttribute("data-table-id");
178
            }
179
            var f = field.parentNode.parentNode.getElementsByTagName("input")[0];
180
            return '' === f.value ? null : f.getAttribute("data-table-id");
181
        }
182
    },
183
    GoPage: {
184
        Run: function(lnk){
185
            var rq = {};
186
            rq.tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
187
            rq.strDesc = this.strDesc;
188
            rq.strAsc = this.strAsc;
189
            this.BuildRequest(rq);
190
            rq.pageNo = this.GoPage.GetNo(lnk, rq.tableId);
191
            this.rq = rq;
192
            this.LoadData.Run(this);
193
            return false;
194
        },
195
        GetNo: function(lnk, tableId){
196
            //check & serve pagination jump links
197
            var jumpDir = lnk.innerHTML.trim().substr(0, 1);
198
            if(jumpDir === "+" || jumpDir === "-"){
199
                var current = document.getElementById(tableId)
200
                        .querySelector("tfoot .paging .a").innerHTML;
201
                var jump = lnk.innerHTML.replace("K", "000")
202
                        .replace("M", "000000000");
203
                var jumpPage = (parseInt(current) + parseInt(jump));
204
                lnk.parentNode.setAttribute("data-page", jumpPage);
205
                lnk.style.transform = "none";
206
            }
207
            return lnk.parentNode.hasAttribute("data-page") ?
208
                    lnk.parentNode.getAttribute("data-page") :
209
                    lnk.innerHTML;
210
        }
211
    },
212
    LoadData: {
213
        tail: null,
214
        Run: function(instance){
215
            if(this.tail !== null){
216
                this.tail.abort();
217
            }
218
            this.SetVisability(instance.rq.tableId, false);
219
            var inst = instance;
220
            var xmlhttp = window.XMLHttpRequest ?
221
                    new XMLHttpRequest() : //IE7+, Firefox, Chrome, Opera, Safari
222
                    new window.ActiveXObject("Microsoft.XMLHTTP");//IE6, IE5
223
            xmlhttp.onreadystatechange = function(){
224
                if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
225
                    var d = JSON.parse(xmlhttp.responseText);
226
                    inst.Draw.Run(inst.rq.tableId, d, inst);
227
                    inst.LoadData.SetVisability(inst.rq.tableId, true);
228
                    window.table.LoadEndCalback(inst.rq.tableId);
229
                }
230
            };
231
            xmlhttp.open("GET", instance.helper.RequestToUrl(inst.rq), true);
232
            xmlhttp.send();
233
            this.tail = xmlhttp; //put at tail to can abort later previous request
234
        },
235
        SetVisability: function(tableContainer, flag){
236
            var tbl = document.getElementById(tableContainer);
237
            if(flag === true){
238
                tbl.style.filter = "none";
239
                tbl.style.opacity = "1";
240
                tbl.style.cursor = "auto";
241
            }else if(flag === false){
242
                tbl.style.filter = "blur(1px)";
243
                tbl.style.opacity = "0.8";
244
                tbl.style.cursor = "wait";
245
            }else{
246
                console.error("table error in the flag value");
247
            }
248
        }
249
    },
250
    ReloadData: function(tableId){
251
        var inst = this;
252
        var rq = this.helper.RqObject.call(inst, tableId);
253
        this.BuildRequest(rq);
254
        this.rq = rq;
255
        this.LoadData.Run(this);
256
    },
257
    Sort: function(colNo, lnk){
258
        var rq = {};
259
        rq.tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
260
        rq.strDesc = this.strDesc;
261
        rq.strAsc = this.strAsc;
262
        this.BuildRequest(rq);
263
        if(Math.round(colNo) === rq.colNo){
264
            rq.colOrd = (rq.colOrd === "asc" ? "desc" : "asc");
265
        }else{
266
            rq.colNo = Math.round(colNo);
267
            rq.colOrd = "asc";
268
        }
269
        this.rq = rq;
270
        this.LoadData.Run(this);
271
        ClearAndSetNewSortArrow(rq);
272
273
        function ClearAndSetNewSortArrow(rq){
274
            var headSpans = document.getElementById(rq.tableId)
275
                    .getElementsByTagName("thead")[0]
276
                    .getElementsByTagName("span");
277
            var length = headSpans.length;
278
            for(var i = 0; i < length; i++){
279
                headSpans[i].innerHTML = "";
280
            }
281
            lnk.getElementsByTagName("span")[0].innerHTML =
282
                    (rq.colOrd === "desc" ? rq.strDesc : rq.strAsc);
283
        }
284
    }
285
};
286