Passed
Push — master ( 96fe94...16192a )
by Plamen
01:31
created

table_methods.Draw.Section   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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