| Conditions | 18 |
| Paths | 576 |
| Total Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 1 | 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 | /** |
||
| 65 | _insert_text: function() { |
||
| 66 | var text = ""; |
||
| 67 | var now = new Date(); |
||
| 68 | text += date(egw.preference('dateformat') + ' ' + (egw.preference("timeformat") === "12" ? "h:ia" : "H:i")+' ',now); |
||
| 69 | |||
| 70 | // Get properly formatted user name |
||
| 71 | var user = parseInt(egw.user('account_id')); |
||
| 72 | var accounts = egw.accounts('accounts'); |
||
| 73 | for(var j = 0; j < accounts.length; j++) |
||
| 74 | { |
||
| 75 | if(accounts[j].value === user) |
||
| 76 | { |
||
| 77 | text += accounts[j].label; |
||
| 78 | break; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | text += ': '; |
||
| 82 | |||
| 83 | var widget = this._get_input(this.target); |
||
| 84 | var input = widget.input ? widget.input : widget.getDOMNode(); |
||
| 85 | if(input.context) |
||
| 86 | { |
||
| 87 | input = input.get(0); |
||
| 88 | } |
||
| 89 | |||
| 90 | var scrollPos = input.scrollTop; |
||
| 91 | var browser = ((input.selectionStart || input.selectionStart == "0") ? |
||
| 92 | "standards" : (document.selection ? "ie" : false ) ); |
||
| 93 | |||
| 94 | var pos = 0; |
||
| 95 | var CK = CKEDITOR && CKEDITOR.instances[input.id] || false; |
||
| 96 | |||
| 97 | // Find cursor or selection |
||
| 98 | if (browser == "ie") |
||
| 99 | { |
||
| 100 | input.focus(); |
||
| 101 | var range = document.selection.createRange(); |
||
| 102 | range.moveStart ("character", -input.value.length); |
||
| 103 | pos = range.text.length; |
||
| 104 | } |
||
| 105 | else if (browser == "standards") |
||
| 106 | { |
||
| 107 | pos = input.selectionStart; |
||
| 108 | }; |
||
| 109 | |||
| 110 | // If CKEDitor, update it |
||
| 111 | if(CKEDITOR && CKEDITOR.instances[input.id]) |
||
| 112 | { |
||
| 113 | CKEDITOR.instances[input.id].insertText(text); |
||
| 114 | window.setTimeout(function() { |
||
| 115 | CKEDITOR.instances[input.id].focus(); |
||
| 116 | }, 10); |
||
| 117 | } |
||
| 118 | else |
||
| 119 | { |
||
| 120 | // Insert the text |
||
| 121 | var front = (input.value).substring(0, pos); |
||
| 122 | var back = (input.value).substring(pos, input.value.length); |
||
| 123 | input.value = front+text+back; |
||
| 124 | |||
| 125 | // Clean up a little |
||
| 126 | pos = pos + text.length; |
||
| 127 | if (browser == "ie") { |
||
| 128 | input.focus(); |
||
| 129 | var range = document.selection.createRange(); |
||
| 130 | range.moveStart ("character", -input.value.length); |
||
| 131 | range.moveStart ("character", pos); |
||
| 132 | range.moveEnd ("character", 0); |
||
| 133 | range.select(); |
||
| 134 | } |
||
| 135 | else if (browser == "standards") { |
||
| 136 | input.selectionStart = pos; |
||
| 137 | input.selectionEnd = pos; |
||
| 138 | input.focus(); |
||
| 139 | } |
||
| 140 | input.scrollTop = scrollPos; |
||
| 141 | input.focus(); |
||
| 142 | } |
||
| 143 | // If on a tab, switch to that tab so user can see it |
||
| 144 | var tab = widget; |
||
| 145 | while(tab._parent && tab._type != 'tabbox') |
||
| 146 | { |
||
| 147 | tab = tab._parent; |
||
| 148 | } |
||
| 149 | if (tab._type == 'tabbox') tab.activateTab(widget); |
||
| 150 | }, |
||
| 151 | |||
| 180 | et2_register_widget(et2_timestamper, ["button-timestamp", "timestamper"]); |