Conditions | 13 |
Paths | 72 |
Total Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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:
Complex classes like et2_widget_timestamper.js ➔ ... ➔ et2_button.extend._insert_text 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 | /** |
||
70 | _insert_text: function() { |
||
71 | var text = "\n"; |
||
72 | var now = new Date(); |
||
73 | text += date(egw.preference('dateformat') + ' ' + (egw.preference("timeformat") === "12" ? "h:ia" : "H:i")+' ',now); |
||
74 | |||
75 | // Get properly formatted user name |
||
76 | var user = parseInt(egw.user('account_id')); |
||
77 | var accounts = egw.accounts('accounts'); |
||
78 | for(var j = 0; j < accounts.length; j++) |
||
79 | { |
||
80 | if(accounts[j].value === user) |
||
81 | { |
||
82 | text += accounts[j].label; |
||
83 | break; |
||
84 | } |
||
85 | } |
||
86 | text += ': '; |
||
87 | |||
88 | var input = this._get_input(this.target); |
||
89 | var scrollPos = input.scrollTop; |
||
90 | var browser = ((input.selectionStart || input.selectionStart == "0") ? |
||
91 | "standards" : (document.selection ? "ie" : false ) ); |
||
92 | |||
93 | var pos = 0; |
||
94 | var CK = CKEDITOR && CKEDITOR.instances[input.id] || false; |
||
95 | |||
96 | // Find cursor or selection |
||
97 | if (browser == "ie") |
||
98 | { |
||
99 | input.focus(); |
||
100 | var range = document.selection.createRange(); |
||
101 | range.moveStart ("character", -input.value.length); |
||
102 | pos = range.text.length; |
||
103 | } |
||
104 | else if (browser == "standards") |
||
105 | { |
||
106 | pos = input.selectionStart; |
||
107 | }; |
||
108 | |||
109 | // If CKEDitor, update it |
||
110 | if(CKEDITOR && CKEDITOR.instances[input.id]) |
||
111 | { |
||
112 | CKEDITOR.instances[input.id].insertText(text); |
||
113 | window.setTimeout(function() { |
||
114 | CKEDITOR.instances[input.id].focus(); |
||
115 | }, 10); |
||
116 | } |
||
117 | else |
||
118 | { |
||
119 | // Insert the text |
||
120 | var front = (input.value).substring(0, pos); |
||
121 | var back = (input.value).substring(pos, input.value.length); |
||
122 | input.value = front+text+back; |
||
123 | |||
124 | // Clean up a little |
||
125 | pos = pos + text.length; |
||
126 | if (browser == "ie") { |
||
127 | input.focus(); |
||
128 | var range = document.selection.createRange(); |
||
129 | range.moveStart ("character", -input.value.length); |
||
130 | range.moveStart ("character", pos); |
||
131 | range.moveEnd ("character", 0); |
||
132 | range.select(); |
||
133 | } |
||
134 | else if (browser == "standards") { |
||
135 | input.selectionStart = pos; |
||
136 | input.selectionEnd = pos; |
||
137 | input.focus(); |
||
138 | } |
||
139 | input.scrollTop = scrollPos; |
||
140 | } |
||
141 | }, |
||
142 | |||
170 | et2_register_widget(et2_timestamper, ["button-timestamp", "timestamper"]); |