Completed
Push — 16.1 ( 1ae5a9...01af42 )
by Nathan
30:29 queued 14:08
created

api/js/etemplate/et2_widget_timestamper.js   A

Complexity

Total Complexity 32
Complexity/F 5.33

Size

Lines of Code 157
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 0
c 4
b 1
f 0
nc 1
dl 0
loc 157
rs 9.6
wmc 32
mnd 3
bc 21
fnc 6
bpm 3.5
cpm 5.3333
noi 0
1
/**
2
 * EGroupware eTemplate2 - JS Timestamp button object
3
 *
4
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
5
 * @package etemplate
6
 * @subpackage api
7
 * @link http://www.egroupware.org
8
 * @author Nathan Gray
9
 * @copyright Nathan Gray 2017
10
 */
11
12
/*egw:uses
13
	et2_button;
14
*/
15
16
/**
17
 * Class which implements the "button-timestamper" XET-Tag
18
 *
19
 * Clicking the button puts the current time and current user at the end of
20
 * the provided field.
21
 *
22
 * @augments et2_button
23
 */
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
		return false;
63
	},
64
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
152
	_get_input: function _get_input(target)
153
	{
154
		var input = null;
155
		var widget = null;
156
157
		if (typeof target == 'string')
158
		{
159
			widget = this.getRoot().getWidgetById(target);
160
		}
161
		else if (target.instanceOf && target.instanceOf(et2_IInput))
162
		{
163
			widget = target;
164
		}
165
		else if(typeof target == 'string' && target.indexOf('#') < 0 && jQuery('#'+this.target).is('input'))
166
		{
167
			input = this.target;
168
		}
169
		if(widget)
170
		{
171
			return widget;
172
		}
173
		if(input.context)
174
		{
175
			input = input.get(0);
176
		}
177
		return input;
178
	}
179
});}).call(this);
180
et2_register_widget(et2_timestamper, ["button-timestamp", "timestamper"]);