Passed
Push — master ( 366cac...e5a823 )
by Plamen
01:32
created

table_methods.Sort   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 23
c 2
b 0
f 0
nc 3
nop 2
dl 0
loc 28
rs 9.328

1 Function

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