Total Complexity | 45 |
Complexity/F | 3 |
Lines of Code | 186 |
Function Count | 15 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 11 | ||
Bugs | 2 | Features | 0 |
Complex classes like add/table_methods.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
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 | if(section === "tfoot"){ |
||
12 | this.helper.ProcessPaginationLinks(tSec); |
||
13 | } |
||
14 | } |
||
15 | function Row(row){ |
||
16 | var tRow = document.createElement("tr"); |
||
17 | for(var cell in row){ |
||
1 ignored issue
–
show
|
|||
18 | var tCell = document.createElement("td"); |
||
19 | if(typeof row[cell] === "string" || |
||
20 | typeof row[cell] === "number" |
||
21 | ){ |
||
22 | tCell.innerHTML = row[cell]; |
||
23 | }else if(typeof row[cell] === "object"){ |
||
24 | RowCellFromObject(row, cell, tCell); |
||
25 | } |
||
26 | tRow.appendChild(tCell); |
||
27 | } |
||
28 | return tRow; |
||
29 | } |
||
30 | function RowCellFromObject(row, cell, tCell){ |
||
31 | for(var attr in row[cell]){ |
||
1 ignored issue
–
show
|
|||
32 | if(typeof row[cell][attr] === "string"){ |
||
33 | tCell.innerHTML = row[cell][attr]; |
||
34 | }else if(typeof row[cell][attr] === "object"){ |
||
35 | for(var v in row[cell][attr]){ |
||
1 ignored issue
–
show
|
|||
36 | tCell.setAttribute(v, row[cell][attr][v]); |
||
37 | } |
||
38 | } |
||
39 | } |
||
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 |
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: