Completed
Push — 16.1 ( e62dd1...a78389 )
by Hadi
30:32 queued 12:24
created

egw.extend(ꞌtooltipꞌ)   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 24
rs 8.9713
1
/**
2
 * EGroupware clientside API 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 Andreas Stöckel (as AT stylite.de)
9
 * @author Ralf Becker <[email protected]>
10
 * @version $Id$
11
 */
12
13
/*egw:uses
14
	vendor.bower-asset.jquery.dist.jquery;
15
	egw_core;
16
*/
17
18
/**
19
 *
20
 * @param {string} _app application name object is instanciated for
21
 * @param {object} _wnd window object is instanciated for
22
 */
23
egw.extend('tooltip', egw.MODULE_WND_LOCAL, function(_app, _wnd)
24
{
25
	"use strict";
26
27
	var tooltip_div = null;
28
	var current_elem = null;
29
30
	var time_delta = 100;
31
	var show_delta = 0;
32
	var show_delay = 200;
33
34
	var x = 0;
35
	var y = 0;
36
37
	/**
38
	 * Removes the tooltip_div from the DOM if it does exist.
39
	 */
40
	function hide()
41
	{
42
		if (tooltip_div != null)
43
		{
44
			tooltip_div.remove();
45
			tooltip_div = null;
46
		}
47
	}
48
49
	/**
50
	 * Shows the tooltip at the current cursor position.
51
	 */
52
	function show()
53
	{
54
		if (tooltip_div && typeof x !== 'undefined' && typeof y !== 'undefined')
55
		{
56
			//Calculate the cursor_rectangle - this is a space the tooltip might
57
			//not overlap with
58
			var cursor_rect = {
59
				left: (x - 8),
60
				top: (y - 8),
61
				right: (x + 8),
62
				bottom: (y + 8)
63
			};
64
65
			//Calculate how much space is left on each side of the rectangle
66
			var window_width = jQuery(_wnd.document).width();
67
			var window_height = jQuery(_wnd.document).height();
68
			var space_left = {
69
				left: (cursor_rect.left),
70
				top: (cursor_rect.top),
71
				right: (window_width - cursor_rect.right),
72
				bottom: (window_height - cursor_rect.bottom)
73
			};
74
75
			//Get the width and the height of the tooltip
76
			var tooltip_width = tooltip_div.width();
77
			if (tooltip_width > 300) tooltip_width = 300;
78
			var tooltip_height = tooltip_div.height();
79
80
			if (space_left.right < tooltip_width) {
81
				tooltip_div.css('left', Math.max(0,cursor_rect.left - tooltip_width));
82
			} else if (space_left.left >= tooltip_width) {
83
				tooltip_div.css('left', cursor_rect.right);
84
			} else	{
85
				tooltip_div.css('left', cursor_rect.right);
86
				tooltip_div.css('max-width', space_left.right);
87
			}
88
89
			// tooltip does fit neither above nor below: put him vertical centered left or right of cursor
90
			if (space_left.bottom < tooltip_height && space_left.top < tooltip_height) {
91
				if (tooltip_height > window_height-20) {
92
					tooltip_div.css('max-height', tooltip_height=window_height-20);
93
				}
94
				tooltip_div.css('top', (window_height-tooltip_height)/2);
95
			} else if (space_left.bottom < tooltip_height) {
96
				tooltip_div.css('top', cursor_rect.top - tooltip_height);
97
			} else {
98
				tooltip_div.css('top', cursor_rect.bottom);
99
			}
100
101
			tooltip_div.fadeIn(100);
102
		}
103
	}
104
105
	/**
106
	 * Creates the tooltip_div with the given text.
107
	 *
108
	 * @param {string} _html
109
	 * @param {boolean} _isHtml if set to true content gets appended as html
110
	 */
111
	function prepare(_html, _isHtml)
112
	{
113
		// Free and null the old tooltip_div
114
		hide();
115
116
		//Generate the tooltip div, set it's text and append it to the body tag
117
		tooltip_div = jQuery(_wnd.document.createElement('div'));
118
		tooltip_div.hide();
119
		if (_isHtml)
120
		{
121
			tooltip_div.append(_html);
122
		}
123
		else
124
		{
125
			tooltip_div.text(_html)
126
		}
127
		tooltip_div.addClass("egw_tooltip");
128
		jQuery(_wnd.document.body).append(tooltip_div);
129
130
		//The tooltip should automatically hide when the mouse comes over it
131
		tooltip_div.mouseenter(function() {
132
				hide();
133
		});
134
	}
135
136
	/**
137
	 * showTooltipTimeout is used to prepare showing the tooltip.
138
	 */
139
	function showTooltipTimeout()
140
	{
141
		if (current_elem != null)
142
		{
143
			show_delta += time_delta;
144
			if (show_delta < show_delay)
145
			{
146
				//Repeat the call of timeout
147
				_wnd.setTimeout(showTooltipTimeout, time_delta);
148
			}
149
			else
150
			{
151
				show_delta = 0;
152
				show();
153
			}
154
		}
155
	}
156
157
	return {
158
		/**
159
		 * Binds a tooltip to the given DOM-Node with the given html.
160
		 * It is important to remove all tooltips from all elements which are
161
		 * no longer needed, in order to prevent memory leaks.
162
		 *
163
		 * @param _elem is the element to which the tooltip should get bound. It
164
		 * 	has to be a jQuery node.
165
		 * @param _html is the html code which should be shown as tooltip.
166
		 */
167
		tooltipBind: function(_elem, _html, _isHtml) {
168
			if (_html != '')
169
			{
170
				_elem.bind('mouseenter.tooltip', function(e) {
171
					if (_elem != current_elem)
172
					{
173
						//Prepare the tooltip
174
						prepare(_html, _isHtml);
175
176
						// Set the current element the mouse is over and
177
						// initialize the position variables
178
						current_elem = _elem;
179
						show_delta = 0;
180
						x = e.clientX;
181
						y = e.clientY;
182
183
						// Create the timeout for showing the timeout
184
						_wnd.setTimeout(showTooltipTimeout, time_delta);
185
					}
186
187
					return false;
188
				});
189
190
				_elem.bind('mouseleave.tooltip', function() {
191
					current_elem = null;
192
					show_delta = 0;
193
					if (tooltip_div)
194
					{
195
						tooltip_div.fadeOut(100);
196
					}
197
				});
198
199
				_elem.bind('mousemove.tooltip', function(e) {
200
					//Calculate the distance the mouse took since the last call of mousemove
201
					var dx = x - e.clientX;
202
					var dy = y - e.clientY;
203
					var movedist = Math.sqrt(dx * dx + dy * dy);
204
205
					//Block appereance of the tooltip on fast movements (with small movedistances)
206
					if (movedist > 2)
207
					{
208
						show_delta = 0;
209
					}
210
211
					x = e.clientX;
212
					y = e.clientY;
213
				});
214
			}
215
		},
216
217
		/**
218
		 * Unbinds the tooltip from the given DOM-Node.
219
		 *
220
		 * @param _elem is the element from which the tooltip should get
221
		 * removed. _elem has to be a jQuery node.
222
		 */
223
		tooltipUnbind: function(_elem) {
224
			if (current_elem == _elem)
225
			{
226
				hide();
227
				current_elem = null;
228
			}
229
230
			// Unbind all "tooltip" events from the given element
231
			_elem.unbind('.tooltip');
232
		}
233
	};
234
235
});
236
237