Conditions | 1 |
Paths | 1 |
Total Lines | 81 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var table_helper = { |
||
42 | BuildRequest: function(tableId){ |
||
43 | var rq = this.helper.RqObject.call(this, tableId); |
||
44 | Sort(rq); |
||
45 | Filter(rq); |
||
46 | return rq; |
||
47 | |||
48 | function Sort(rq){ |
||
49 | var thTags = document.getElementById(rq.tableId) |
||
50 | .getElementsByTagName("thead")[0] |
||
51 | .getElementsByTagName("th"); |
||
52 | var length = thTags.length; |
||
53 | for(var i = 0; i < length; i++){ |
||
54 | var link = thTags[i].getElementsByTagName("a")[0]; |
||
55 | if(link){ |
||
56 | var span = link.getElementsByTagName("span")[0]; |
||
57 | if(span && sortBySpan(span, i)){ |
||
58 | break; |
||
59 | } |
||
60 | } |
||
61 | } |
||
62 | |||
63 | function sortBySpan(span, i){ |
||
64 | var order = span.innerHTML; |
||
65 | if(order.length === 1){ |
||
66 | rq.colNo = i; |
||
67 | rq.colOrd = order === rq.strDesc ? "desc" : "asc"; |
||
68 | } |
||
69 | return rq.colNo === i; |
||
70 | } |
||
71 | } |
||
72 | function Filter(rq){ |
||
73 | var r = getFilterFieldsByTableID(rq.tableId); |
||
74 | if(r.filter !== null){ |
||
75 | rq.filter = r.filter; |
||
76 | } |
||
77 | if(r.filterBy !== null){ |
||
78 | rq.filterBy = r.filterBy; |
||
79 | } |
||
80 | |||
81 | function getFilterFieldsByTableID(tableID){ |
||
82 | var fields = {filterBy: null, filter: null}; |
||
83 | var filterDiv = getFilterDivByTableIDOrNull(tableID); |
||
84 | if(filterDiv !== null){ |
||
85 | setFilterBy(fields, filterDiv); |
||
86 | setFilterValue(fields, filterDiv); |
||
87 | } |
||
88 | return fields; |
||
89 | |||
90 | function getFilterDivByTableIDOrNull(tableID){ |
||
91 | if(document.getElementById(tableID).parentNode |
||
92 | .getElementsByTagName("div").length > 0 |
||
93 | ){ |
||
94 | var containerDivs = document.getElementById(tableID) |
||
95 | .parentNode.getElementsByTagName("div"); |
||
96 | for(var i = 0; i < containerDivs.length; i++){ |
||
97 | if(containerDivs[i].getAttribute("class") === "filter"){ |
||
98 | return containerDivs[i]; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | } |
||
103 | return null; |
||
104 | } |
||
105 | |||
106 | function setFilterBy(fields, filterDiv){ |
||
107 | var select = filterDiv.getElementsByTagName("select")[0]; |
||
108 | if(select && |
||
109 | select.options[select.selectedIndex].value !== "all" |
||
110 | ){ |
||
111 | fields.filterBy = select.options[select.selectedIndex].value; |
||
112 | } |
||
113 | } |
||
114 | function setFilterValue(fields, filterDiv){ |
||
115 | var textObj = filterDiv.getElementsByTagName("input")[0]; |
||
116 | if(textObj && textObj.value && textObj.value.length !== 0){ |
||
117 | fields.filter = encodeURIComponent(textObj.value.trim()); |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | }, |
||
123 | IePrior: function(v){ |
||
175 |
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: