Passed
Push — master ( a2692a...8f3a27 )
by Plamen
01:26
created

table_helper.Draw.Section   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 25
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 33
rs 9.28

2 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 7 2
A 0 14 5
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: {
43
        Run: function(tableId){
44
            var rq = {tableId: tableId, strDesc: this.strDesc, strAsc: this.strAsc};
45
            var parent = this.helper.BuildRequest;
46
            parent.sort(rq);
47
            parent.filter(rq);
48
            return rq;
49
        },
50
        sort: function(rq){
51
            var thTags = document.getElementById(rq.tableId)
52
                        .getElementsByTagName("thead")[0]
53
                        .getElementsByTagName("th");
54
            var length = thTags.length;
55
            for(var i = 0; i < length; i++){
56
                var link = thTags[i].getElementsByTagName("a")[0];
57
                if(link){
58
                    var span = link.getElementsByTagName("span")[0];
59
                    if(span && sortBySpan(span, i)){
60
                        break;
61
                    }
62
                }
63
            }
64
65
            function sortBySpan(span, i){
66
                var order = span.innerHTML;
67
                if(order.length === 1){
68
                    rq.colNo = i;
69
                    rq.colOrd = order === rq.strDesc ? "desc" : "asc";
70
                }
71
                return rq.colNo === i;
72
            }
73
        },
74
        filter: function(rq){
75
            var r = getFilterFieldsByTableID(rq.tableId);
76
            if(r.filter !== null){
77
                rq.filter = r.filter;
78
            }
79
            if(r.filterBy !== null){
80
                rq.filterBy = r.filterBy;
81
            }
82
83
            function getFilterFieldsByTableID(tableID){
84
                var fields = {filterBy: null, filter: null};
85
                var filterDiv = getFilterDivByTableIDOrNull(tableID);
86
                if(filterDiv !== null){
87
                    setFilterBy(fields, filterDiv);
88
                    setFilterValue(fields, filterDiv);
89
                }
90
                return fields;
91
92
                function getFilterDivByTableIDOrNull(tableID){
93
                    if(document.getElementById(tableID).parentNode
94
                            .getElementsByTagName("div").length > 0
95
                            ){
96
                        var containerDivs = document.getElementById(tableID)
97
                                .parentNode.getElementsByTagName("div");
98
                        for(var i = 0; i < containerDivs.length; i++){
99
                            if(containerDivs[i].getAttribute("class") === "filter"){
100
                                return containerDivs[i];
101
                            }
102
                        }
103
104
                    }
105
                    return null;
106
                }
107
108
                function setFilterBy(fields, filterDiv){
109
                    var select = filterDiv.getElementsByTagName("select")[0];
110
                    if(select &&
111
                            select.options[select.selectedIndex].value !== "all"
112
                            ){
113
                        fields.filterBy = select.options[select.selectedIndex].value;
114
                    }
115
                }
116
                function setFilterValue(fields, filterDiv){
117
                    var textObj = filterDiv.getElementsByTagName("input")[0];
118
                    if(textObj && textObj.value && textObj.value.length !== 0){
119
                        fields.filter = encodeURIComponent(textObj.value.trim());
120
                    }
121
                }
122
            }
123
        }
124
    },
125
    Draw: {
126
        Section: function(tableContainer, dt, tSection){
127
            var section = tSection === "tfoot" ? "tfoot" : "tbody";
128
            var tSec = document.getElementById(tableContainer)
129
                        .getElementsByTagName(section)[0];
130
            tSec.innerHTML="";
131
            for(var i = 0; i < dt.length; i++){
132
                tSec.appendChild( Row(dt[i]) );
133
            }
134
            if(section === "tfoot"){
135
                this.helper.ProcessPaginationLinks(tSec);
136
            }
137
            function Row(row){
138
                var tRow = document.createElement("tr");
139
                for(var i = 0; i < row.length; i++){
140
                    tRow.appendChild(Cell(row[i]));
141
                }
142
                return tRow;
143
            }
144
            function Cell(cell){
145
                var tCell = document.createElement("td");
146
                if(typeof cell === "string" ||
147
                    typeof cell === "number"
148
                ){
149
                    tCell.innerHTML = cell;
150
                }else if(typeof cell === "object"){
151
                    tCell.innerHTML = cell[0];
152
                    for(var attr in cell[1]){
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...
153
                        tCell.setAttribute(attr, cell[1][attr]);
154
                    }
155
                }
156
                return tCell;
157
            }
158
        },
159
        Run: function(tableContainer, d, instance){
160
            this.Section.call(instance, tableContainer, d.body);
161
            this.Section.call(instance, tableContainer, d.footer, "tfoot");
162
            if(instance.rq !== null){
163
                var hover = document.getElementById(instance.rq.tableId)
164
                        .getElementsByTagName("th")[instance.rq.colNo].lang;
165
                if(hover){
166
                    instance.helper.ColumnHover(tableContainer, instance.rq.colNo);
167
                }
168
            }
169
        }
170
    },
171
    LoadData: {
172
        tail: null,
173
        Run: function(instance){
174
            if(this.tail !== null){
175
                this.tail.abort();
176
            }
177
            SetVisability(instance.rq.tableId, false);
178
            var inst = instance;
179
            var xmlhttp = window.XMLHttpRequest ?
180
                    new XMLHttpRequest() : //IE7+, Firefox, Chrome, Opera, Safari
181
                    new window.ActiveXObject("Microsoft.XMLHTTP");//IE6, IE5
182
            xmlhttp.onreadystatechange = function(){
183
                if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
184
                    var d = JSON.parse(xmlhttp.responseText);
185
                    inst.helper.Draw.Run(inst.rq.tableId, d, inst);
186
                    SetVisability(inst.rq.tableId, true);
187
                    window.table.LoadEndCalback(inst.rq.tableId);
188
                }
189
            };
190
            xmlhttp.open("GET", instance.helper.RequestToUrl(inst.rq), true);
191
            xmlhttp.send();
192
            this.tail = xmlhttp; //put at tail to can abort later previous request
193
            
194
            function SetVisability(tableContainer, flag){
195
                var tbl = document.getElementById(tableContainer);
196
                if(flag === true){
197
                    tbl.style.filter = "none";
198
                    tbl.style.opacity = "1";
199
                    tbl.style.cursor = "auto";
200
                }else if(flag === false){
201
                    tbl.style.filter = "blur(1px)";
202
                    tbl.style.opacity = "0.8";
203
                    tbl.style.cursor = "wait";
204
                }else{
205
                    console.error("table error in the flag value");
206
                }
207
            }
208
        }
209
    },
210
    IePrior: function(v){
211
        var rv = false;
212
        if(window.navigator.appName === 'Microsoft Internet Explorer'){
213
            var ua = window.navigator.userAgent;
214
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
215
            if(re.exec(ua) !== null){
216
                rv = parseFloat(RegExp.$1);
217
            }
218
            rv = rv < v ? true : false;
219
        }
220
        return rv;
221
    },
222
    GetParent: function(obj, objType){
223
        while(obj && obj.tagName !== objType.toUpperCase()){
224
            obj = obj.parentNode;
225
        }
226
        return obj;
227
    },
228
    ProcessPaginationLinks: function(tfoot){
229
        var pLinks = tfoot.querySelectorAll(".paging a");
230
        if(pLinks.length > 0){
231
            for(var j = 0; j < pLinks.length; j++){
232
                pLinks[j].setAttribute("href", "javascript:void(0);");
233
                pLinks[j].setAttribute("onclick", "return table.GoPage(this);");
234
            }
235
        }
236
    },
237
    RequestToUrl: function(rq){
238
        var url = location.pathname + ".json" + location.search;
239
        var getUrlVarName = {
240
            colNo: "col", colOrd: "ord", filter: "filter",
241
            filterBy: "filter-by", pageNo: "pg", exportType: "export",
242
            tableId: "table-id"
243
        };
244
        var flagFirst = location.search.length < 1 ? true : false;
245
        //lets remove rq.strAsc and rq.srtDesc
246
        //var request = Object.assign({}, rq); //EC6+
247
        var request = JSON.parse(JSON.stringify(rq)); //EC5
248
        delete(request.strAsc); delete(request.strDesc);
249
        for(var r in request){
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...
250
            var clue = flagFirst === true ? "?" : "&";
251
            url += clue + getUrlVarName[r] + "=" + rq[r];
252
            flagFirst = false;
253
        }
254
        return url;
255
    }
256
};
257