Completed
Push — 16.1 ( 7ccc73...046888 )
by Nathan
64:46 queued 51:15
created

api/js/etemplate/et2_widget_timestamper.js   A

Complexity

Total Complexity 27
Complexity/F 4.5

Size

Lines of Code 147
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
c 2
b 0
f 0
nc 1
dl 0
loc 147
rs 10
wmc 27
mnd 3
bc 20
fnc 6
bpm 3.3333
cpm 4.5
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
		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"]);