Passed
Push — master ( 506943...0f9777 )
by Plamen
01:27
created

table_methods.Draw.Section   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 13
rs 9.3333
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
                var res = null;
39
                if(document.getElementById(tableID).parentNode
40
                        .getElementsByTagName("div").length > 0
41
                ){
42
                    for(var i = 0; i < document.getElementById(tableID).parentNode.getElementsByTagName("div").length; i++){
43
                        if(document.getElementById(tableID).parentNode.getElementsByTagName("div")[i].getAttribute("class") === "filter"){
44
                            return document.getElementById(tableID).parentNode.getElementsByTagName("div")[i];
45
                        }
46
                    }
47
48
                }
49
                return res;
50
            }
51
            function setFilterBy(fields, filterDiv){
52
                var slctObj = filterDiv.getElementsByTagName("select")[0];
53
                if(slctObj && slctObj.options[slctObj.selectedIndex].value !== "all"){
54
                    fields.filterBy = slctObj.options[slctObj.selectedIndex].value;
55
                }
56
            }
57
            function setFilterValue(fields, filterDiv){
58
                var textObj = filterDiv.getElementsByTagName("input")[0];
59
                if(textObj && textObj.value && textObj.value.length !== 0){
60
                    fields.filter = encodeURIComponent(textObj.value.trim());
61
                }
62
            }
63
64
            var r = getFilterFieldsByTableID(rq.tableId);
65
            if(r.filter !== null){
66
                rq.filter = r.filter;
67
            }
68
            if(r.filterBy !== null){
69
                rq.filterBy = r.filterBy;
70
            }
71
        },
72
        Run: function(rq){
73
            this.Sort(rq);
74
            this.Filter(rq);
75
        }
76
    },
77
    ColumnHover: function(tableContainer, index){
78
        var rows = document.getElementById(tableContainer).rows;
79
        var upto = rows.length - 1;
80
        if(typeof index === "undefined"){
81
            ColumnHoverRelease(rows, upto);
82
        }else{
83
            for(var i = 0; i < upto; i++){
84
                rows[i].cells[index].setAttribute("lang", "col-hover");
85
            }
86
        }
87
        function ColumnHoverRelease(rows, upto){
88
            for(var i = 0; i < upto; i++){
89
                for(var j = 0; j < rows[i].cells.length; j++){
90
                    if(rows[i].cells[j].lang){
91
                        rows[i].cells[j].removeAttribute("lang");
92
                    }
93
                }
94
            }
95
        };
96
    },
97
    Draw: {
98
        Section: function(tableContainer, dt, tSection){
99
            var section = tSection === "tfoot" ? "tfoot" : "tbody";
100
            var tSec = document.getElementById(tableContainer)
101
                    .getElementsByTagName(section)[0];
102
            Clear(tSec, this.helper.IePrior(9));
103
            for(var i = 0; i < dt.length; i++){
104
                var row = dt[i];
105
                var tRow = document.createElement("tr");
106
                Row(row, tRow);
107
                tSec.appendChild(tRow);
108
                if(section === "tfoot"){
109
                    this.helper.ProcessPaginationLinks(tSec);
110
                }
111
            }
112
            function Clear(tSection, iePrior9){
113
                if(iePrior9){
114
                    if(tSection.firstChild){
115
                        while(tSection.firstChild){
116
                            tSection.removeChild(tSection.firstChild);
117
                        }
118
                    }
119
                }else{
120
                    tSection.innerHTML = "";
121
                }
122
            }
123
            function Row(row, tRow){
124
                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...
125
                    var tCell = document.createElement("td");
126
                    if(typeof row[cell] === "string" ||
127
                            typeof row[cell] === "number"
128
                            ){
129
                        tCell.innerHTML = row[cell];
130
                    }else if(typeof row[cell] === "object"){
131
                        RowCellFromObject(row, cell, tCell);
132
                    }
133
                    tRow.appendChild(tCell);
134
                }
135
            }
136
            function RowCellFromObject(row, cell, tCell){
137
                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...
138
                    if(typeof row[cell][attr] === "string"){
139
                        tCell.innerHTML = row[cell][attr];
140
                    }else if(typeof row[cell][attr] === "object"){
141
                        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...
142
                            tCell.setAttribute(v, row[cell][attr][v]);
143
                        }
144
                    }
145
                }
146
            }
147
        },
148
        Run: function(tableContainer, d, instance){
149
            
150
            this.Section.call(instance, tableContainer, d.body);
151
            this.Section.call(instance, tableContainer, d.footer, "tfoot");
152
            if(instance.rq !== null){
153
                var hover = document.getElementById(instance.rq.tableId)
154
                        .getElementsByTagName("th")[instance.rq.colNo].lang;
155
                if(hover){
156
                    instance.ColumnHover(tableContainer, instance.rq.colNo);
157
                }
158
            }
159
        }
160
    },
161
    Export: function(lnk, eType){
162
        var rq = {};
163
        rq.tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
164
        this.BuildRequest.Run(rq);
165
        rq.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ?
166
                eType :
167
                "csv";
168
        window.open(this.helper.RequestToUrl(rq));
169
        return false;
170
    },
171
    Filter: {
172
        Run: function(field){
173
            var crntTableId = this.Filter.GetTableId(field);
174
            if(crntTableId !== null){
175
                var exRq = this.rq;
176
                var rq = {};
177
                rq.tableId = crntTableId;
178
                rq.strDesc = this.strDesc;
179
                rq.strAsc = this.strAsc;
180
                this.BuildRequest.Run(rq);
181
                this.rq = rq;
182
                //@todo exRq match not work
183
                if(exRq === null 
184
                    || typeof this.rq.filer ==="undefined"
185
                    || this.rq.filter !== exRq.filter 
186
                    || this.rq.filterBy !== exRq.filterBy
187
                ){
188
                    this.LoadData.Run(this);
189
                }
190
            }
191
        },
192
        GetTableId: function(field){
193
            if(field.tagName.toLowerCase() !== "select"){
194
                return field.getAttribute("data-table-id");
195
            }
196
            var f = field.parentNode.parentNode.getElementsByTagName("input")[0];
197
            return '' === f.value ? null : f.getAttribute("data-table-id");
198
        }
199
    },
200
    Init: {
201
        Run: function(tableId){
202
            var tContainer = document.getElementById(tableId);
203
            if(!this.helper.IePrior(9)){
204
                this.Init.SetColumnsHoverEffect(tContainer, tableId);
205
            }
206
            var tfoot = tContainer.getElementsByTagName("tfoot")[0];
207
            this.helper.ProcessPaginationLinks(tfoot);
208
        },
209
        SetColumnsHoverEffect: function(tContainer, tableId){
210
            var tHcells = tContainer.rows[0].cells;
211
            for(var i = 0; i < tHcells.length; i++){
212
                if(tHcells[i].firstChild.tagName === "A"){
213
                    tHcells[i].firstChild.setAttribute("onmouseover", "table.ColumnHover('" + tableId + "'," + i + ");");
214
                    tHcells[i].firstChild.setAttribute("onmouseout", "table.ColumnHover('" + tableId + "');");
215
                }
216
            }
217
        }
218
    },
219
    GoPage: {
220
        Run: function(lnk){
221
            var rq = {};
222
            rq.tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
223
            rq.strDesc = this.strDesc;
224
            rq.strAsc = this.strAsc;
225
            this.BuildRequest.Run(rq);
226
            rq.pageNo = this.GoPage.GetNo(lnk, rq.tableId);
227
            this.rq = rq;
228
            this.LoadData.Run(this);
229
            return false;
230
        },
231
        GetNo: function(lnk, tableId){
232
            //check & serve pagination jump links
233
            var jumpDir = lnk.innerHTML.trim().substr(0, 1);
234
            if(jumpDir === "+" || jumpDir === "-"){
235
                var current = document.getElementById(tableId)
236
                        .querySelector("tfoot .paging .a").innerHTML;
237
                var jump = lnk.innerHTML.replace("K", "000").replace("M", "000000000");
238
                var jumpPage = (parseInt(current) + parseInt(jump));
239
                lnk.parentNode.setAttribute("data-page", jumpPage);
240
                lnk.style.transform = "none";
241
            }
242
            return lnk.parentNode.hasAttribute("data-page") ?
243
                    lnk.parentNode.getAttribute("data-page") :
244
                    lnk.innerHTML;
245
        }
246
    },
247
    LoadData: {
248
        tail: null,
249
        Run: function(instance){
250
            if(this.tail !== null){
251
                this.tail.abort();
252
            }
253
            this.SetVisability(instance.rq.tableId, false);
254
            var inst = instance;
255
            var xmlhttp = window.XMLHttpRequest ?
256
                    new XMLHttpRequest() : //IE7+, Firefox, Chrome, Opera, Safari
257
                    new window.ActiveXObject("Microsoft.XMLHTTP");//IE6, IE5
258
            xmlhttp.onreadystatechange = function(){
259
                if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
260
                    var d = JSON.parse(xmlhttp.responseText);
261
                    inst.Draw.Run(inst.rq.tableId, d, inst);
262
                    inst.LoadData.SetVisability(inst.rq.tableId, true);
263
                    table.LoadEndCalback(inst.rq.tableId);
0 ignored issues
show
Bug introduced by
The variable table seems to be never declared. If this is a global, consider adding a /** global: table */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
264
                }
265
            };
266
            xmlhttp.open("GET", instance.helper.RequestToUrl(inst.rq), true);
267
            xmlhttp.send();
268
            this.tail = xmlhttp; //put at tail to can abort later previous request
269
        },
270
        SetVisability: function(tableContainer, flag){
271
            var tbl = document.getElementById(tableContainer);
272
            if(flag === true){
273
                tbl.style.filter = "none";
274
                tbl.style.opacity = "1";
275
                tbl.style.cursor = "auto";
276
            }else if(flag === false){
277
                tbl.style.filter = "blur(1px)";
278
                tbl.style.opacity = "0.8";
279
                tbl.style.cursor = "wait";
280
            }else{
281
                console.error("table error in the flag value");
282
            }
283
        }
284
    },
285
    ReloadData: function(tableId){
286
        var inst = this;
287
        var rq = this.helper.RqObject.call(inst, tableId);
288
        this.BuildRequest.Run(rq);
289
        this.rq = rq;
290
        this.LoadData.Run(this);
291
    },
292
    Sort: function(colNo, lnk){
293
        var rq = {};
294
        rq.tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
295
        rq.strDesc = this.strDesc;
296
        rq.strAsc = this.strAsc;
297
        this.BuildRequest.Run(rq);
298
        if(Math.round(colNo) === rq.colNo){
299
            rq.colOrd = (rq.colOrd === "asc" ? "desc" : "asc");
300
        }else{
301
            rq.colNo = Math.round(colNo);
302
            rq.colOrd = "asc";
303
        }
304
        this.rq = rq;
305
        this.LoadData.Run(this);
306
        /* Clear and add new sort arrow */
307
        var headSpans = document.getElementById(rq.tableId)
308
                        .getElementsByTagName("thead")[0]
309
                        .getElementsByTagName("span");
310
        var length = headSpans.length;
311
        for(var i = 0; i < length; i++){
312
            headSpans[i].innerHTML = "";
313
        }
314
        lnk.getElementsByTagName("span")[0].innerHTML = (rq.colOrd === "desc" ?
315
                                                            this.strDesc : 
316
                                                            this.strAsc
317
                                                        );
318
    }
319
};
320