Passed
Push — master ( 39adec...e24893 )
by Plamen
01:26
created

table_methods.Draw.Section   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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