| Conditions | 1 |
| Paths | 384 |
| Total Lines | 146 |
| 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:
| 1 | /** |
||
| 24 | var et2_timestamper = (function(){ "use strict"; return et2_button.extend([], |
||
| 25 | { |
||
| 26 | attributes: { |
||
| 27 | target: { |
||
| 28 | name: "Target field", |
||
| 29 | type: "string", |
||
| 30 | default: et2_no_init, |
||
| 31 | description: "Which field to place the timestamp in" |
||
| 32 | }, |
||
| 33 | image: { |
||
| 34 | default: "timestamp" |
||
| 35 | }, |
||
| 36 | background_image: { |
||
| 37 | default: true |
||
| 38 | } |
||
| 39 | }, |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor |
||
| 43 | * |
||
| 44 | * @memberOf et2_button |
||
| 45 | */ |
||
| 46 | init: function() { |
||
| 47 | this._super.apply(this, arguments); |
||
| 48 | }, |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Overwritten to maintain an internal clicked attribute |
||
| 52 | * |
||
| 53 | * @param _ev |
||
| 54 | * @returns {Boolean} |
||
| 55 | */ |
||
| 56 | click: function(_ev) { |
||
| 57 | // ignore click on readonly button |
||
| 58 | if (this.options.readonly) return false; |
||
| 59 | |||
| 60 | this._insert_text(); |
||
| 61 | |||
| 62 | if (!this._super.apply(this, arguments)) |
||
| 63 | { |
||
| 64 | return false; |
||
| 65 | } |
||
| 66 | |||
| 67 | return true; |
||
| 68 | }, |
||
| 69 | |||
| 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 | |||
| 143 | _get_input: function _get_input(target) |
||
| 144 | { |
||
| 145 | var input = null; |
||
| 146 | var widget = null; |
||
| 147 | if(jQuery('#'+this.target).is('input')) |
||
| 148 | { |
||
| 149 | input = this.target; |
||
| 150 | } |
||
| 151 | else if (typeof target == 'string') |
||
| 152 | { |
||
| 153 | var widget = this.getRoot().getWidgetById(target); |
||
| 154 | } |
||
| 155 | else if (target.instanceOf && target.instanceOf(et2_IInput)) |
||
| 156 | { |
||
| 157 | widget = target; |
||
| 158 | } |
||
| 159 | if(widget) |
||
| 160 | { |
||
| 161 | input = widget.input ? widget.input : widget.getDOMNode(); |
||
| 162 | } |
||
| 163 | if(input.context) |
||
| 164 | { |
||
| 165 | input = input.get(0); |
||
| 166 | } |
||
| 167 | return input; |
||
| 168 | } |
||
| 169 | });}).call(this); |
||
| 170 | et2_register_widget(et2_timestamper, ["button-timestamp", "timestamper"]); |