-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltip-require.js
More file actions
370 lines (305 loc) · 9.45 KB
/
tooltip-require.js
File metadata and controls
370 lines (305 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* @module Tooltip
* @author javier.rocamora@gmail.com
* Attaches a tooltip to an element.
*/
define('Tooltip', function () {
"use strict";
/**
* Constructor for TooltipClass
* @class Every tooltip is an instance of TooltipClass
* TODO : Class could be in a separated file
*/
function TooltipClass (element) {
this.element = element;
// Default config
this.config = {
position: 'auto',
class: '',
orientation: '',
showOn: 'hover',
closeIcon: true
};
// Will contain the tooltip DOM Node
this.node = null;
this.arrow = null;
}
var arrowSize = 10; // px
/** Override default options. Probably there's a better way to do this
* @params {object} options An object with the options for the tooltip, possible options are:
* @config {string} [position='auto'] - 'auto' will position the tooltip (absolute) centered close to the element attached to
* Also normal position values are accepted (relative, absolute, static or fixed)
* @config {string} [class] - Extra class for custom styling
* @config {string} [orientation='top'] - top, bottom, left or right
* @config {string} [showOn='load'] - load|hover|click|... Load will show it from the beginning
* @config {boolean} [closeIcon=true] - If to show Close icon on the tooltip
* @config {boolean} [persistent=false] - If tooltip should stay when clicking outside. False by default, except for showOn=load
*/
TooltipClass.prototype.setOptions = function (options) {
var config = this.config;
if (options) {
if(options.class) {
config.class = options.class;
}
if(options.orientation) {
config.orientation = options.orientation;
}
if(options.position) {
config.position = options.position;
}
if(options.showOn) {
config.showOn = options.showOn;
}
if(options.closeIcon !== undefined) {
config.closeIcon = options.closeIcon;
}
if(options.text) {
config.text = options.text;
}
// Let's assume an onLoad should be persistent
if(options.showOn === "load") {
config.persistent = true;
}
if(options.persistent) {
config.persistent = options.persistent;
}
}
};
/**
* Creates the tooltip node, to be inserted on the DOM. Includes arrow and close icon
*/
TooltipClass.prototype.createTooltipNode = function (text) {
var tooltip = document.createElement("div"),
arrow = document.createElement("div"),
close = document.createElement("a"),
config = this.config,
self = this;
tooltip.className = config.orientation + " tooltip "+ config.class;
if (!text) {
if (!config.text) {
text = "This tooltip text must be set with title or data-tooltip attribute";
}
else {
text = config.text;
}
}
tooltip.innerHTML = text;
if (config.closeIcon) {
close.className = "close";
close.href = "#";
close.textContent = "✖";
close.addEventListener('click', function(evt) {
evt.preventDefault();
evt.stopPropagation();
self.hide();
}, false);
// == prepend
tooltip.insertBefore(close, tooltip.childNodes[0]);
}
arrow.className = "arrow";
tooltip.appendChild(arrow);
// Make it focusable
tooltip.tabIndex = -1;
this.node = tooltip;
this.arrow = arrow;
return tooltip;
};
/**
* Automatically position tooltip, depending on orientation
*/
TooltipClass.prototype.absolutePositioning = function () {
var element = this.element;
var left = element.offsetLeft,
top = element.offsetTop,
width = element.clientWidth,
height = element.clientHeight,
tooltipWidth = this.node.clientWidth,
tooltipHeight = this.node.clientHeight,
tooltipTop, tooltipLeft;
switch(this.config.orientation) {
case 'left':
tooltipLeft = left - tooltipWidth - arrowSize - 5;
tooltipTop = top - (tooltipHeight/2 - height/2);
break;
case 'right':
tooltipLeft = left + width + arrowSize + 5;
tooltipTop = top - (tooltipHeight/2 - height/2);
break;
case 'bottom':
tooltipTop = top + height + arrowSize + 5;
tooltipLeft = left - (tooltipWidth/2 - width/2);
break;
case 'top':
default:
tooltipTop = top - tooltipHeight - arrowSize - 5;
tooltipLeft = left - (tooltipWidth/2 - width/2);
break;
}
this.node.style.top = tooltipTop + "px" ;
this.node.style.left = tooltipLeft + "px" ;
};
/**
* Automatically position arrow for the tooltip, for tooltip position != 'auto'
* ONLY for orientation bottom/top. TODO for left/right
*/
TooltipClass.prototype.arrowAutoPositioning = function () {
var left = this.element.offsetLeft,
width = this.element.clientWidth;
if (this.config.orientation === "top" || this.config.orientation === "bottom") {
this.arrow.style.left = (left + width/2 - this.node.offsetLeft) + "px";
}
};
/**
* Attach event to the element to show the tooltip
*/
TooltipClass.prototype.attachEvents = function () {
var self = this,
element = this.element,
config = this.config;
// Create closure on event handlers, also useful for detaching the events when destroying the tooltip
self.listenerShow = function (evt) {
evt.preventDefault();
evt.stopPropagation();
self.show();
};
self.listenerHide = function (evt) {
evt.preventDefault();
evt.stopPropagation();
self.hide();
};
if (config.showOn === "hover") {
element.addEventListener('mouseover', this.listenerShow, false);
element.addEventListener('mouseout', this.listenerHide, false);
} else {
if(config.showOn !== "load" ) {
// Standard event
element.addEventListener(config.showOn, this.listenerShow, false);
}
if (!config.persistent) {
this.node.addEventListener('blur', this.listenerHide, true);
}
}
};
/**
* Show the tooltip. This creates the node every time is called.
*/
TooltipClass.prototype.destroyEvents = function () {
var config = this.config;
if (config.showOn === "hover") {
this.element.removeEventListener('mouseover', this.listenerShow, false);
this.element.removeEventListener('mouseout', this.listenerHide, false);
} else if (config.showOn === "focus") {
this.element.removeEventListener('focus', this.listenerShow, false);
this.element.removeEventListener('blur', this.listenerHide, false);
} else if (config.showOn !== "load") {
this.element.removeEventListener(config.showOn, this.listenerShow, false);
document.body.removeEventListener('click', this.bodyClickListener, false);
}
};
/**
* Show the tooltip. Uses visibility instead of display, to correct calculation of position.
*/
TooltipClass.prototype.show = function () {
this.node.style.visibility = "visible";
// Can't focus on an invisible element
this.node.focus();
};
/**
* Hides the tooltip. If was set on load, destroys the tooltip.
*/
TooltipClass.prototype.hide = function () {
if (this.config.showOn === "load") {
this.destroyEvents();
this.node.parentNode.removeChild(this.node);
} else {
this.node.style.visibility = "hidden";
}
};
/******** End of TooltipClass **********/
/*
* Tooltip Module: A factory that returns an object of type TooltipClass
*/
// Object module to return
var Tooltip = {};
// Keep a reference to all created tooltips
var existingTooltips = [];
/*
* Creates a tooltip next to an element
* @return {TooltipClass} - Tooltip object.
*/
Tooltip.create = function (element, options) {
if (!element) {
console.error("Tooltip: Invalid element, needs a DOM Node as 1st argument");
return null;
}
if (!isVisible(element)) {
console.error("Tooltip: Can't attach a tooltip to an invisible element ->", element );
return null;
}
// Does the tooltip already exist ?
var existing = element.parentNode.querySelector(".tooltip");
if (existing) {
console.log("Tooltip: Already existing tooltip on element. Caller should use Tooltip.destroy before adding new one ->", element );
existing.parentNode.removeChild(existing);
}
var tooltip = new TooltipClass(element);
tooltip.setOptions (options);
tooltip.createTooltipNode(element.getAttribute('title'));
// Attach to the element
element.parentNode.appendChild (tooltip.node);
if(tooltip.config.showOn === 'load') {
tooltip.show();
} else {
tooltip.hide();
}
tooltip.attachEvents();
if (tooltip.config.position === 'auto') {
tooltip.absolutePositioning();
} else {
tooltip.arrowAutoPositioning();
}
existingTooltips.push(tooltip);
return tooltip;
};
/*
* Destroys the tooltip and removes the events
*/
Tooltip.destroy = function (tooltip) {
nullifyTooltip(tooltip);
existingTooltips.pop(tooltip);
};
/* Separated function to avoid loop problems on the array */
function nullifyTooltip (tooltip) {
tooltip.destroyEvents();
if (tooltip.node.parentNode) {
tooltip.node.parentNode.removeChild(tooltip.node);
}
}
/*
* Helper function to remove all active tooltips
*/
Tooltip.destroyAll = function () {
existingTooltips.forEach(nullifyTooltip);
existingTooltips = [];
};
/**
* Check if element is visible on the page
*/
function isVisible(elem) {
return elem.offsetWidth > 0 || elem.offsetHeight > 0;
}
/* Init tooltip by default for elements with data-tooltip attributes
* data-tooltip is expected to have a hardcoded JSON object
*/
var elements = document.querySelectorAll ("[data-tooltip]");
for (var i= 0, len = elements.length; i < len; i++) {
var config = elements[i].getAttribute("data-tooltip");
if (config) {
config = JSON.parse(config);
} else {
config = undefined;
}
Tooltip.create(elements[i], config);
}
return Tooltip;
});