-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.Tipz.js
More file actions
594 lines (511 loc) · 17 KB
/
class.Tipz.js
File metadata and controls
594 lines (511 loc) · 17 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/**
Name: Tipz
Author: Juanma Cabello
Last modification date: 19/03/2010
Version: 1.2
Show a fancy tooltip from a DOMObject passed as a parameter.
Tipz LIBRARY IS MIT LICENSED
*/
// Members section
var Tipz = new Object();
Tipz.id = null;
Tipz.tipz = new Array();
Tipz.backgroundColor = '#000000';
Tipz.color = '#FFFFFF';
Tipz.opacity = 75;
Tipz.height = null;
Tipz.width = null;
Tipz.instances = 0;
Tipz.hasShadow = false;
Tipz.isSolid = false;
Tipz.position = 'bottom';
Tipz.delay = 0;
// Constructor section
Tipz.initialize = function(params) {
Tipz.id = params.id ? params.id : 'tipz';
Tipz.isSolid = params.isSolid ? params.isSolid : false;
if (Tipz.isSolid) {
Tipz.hasShadow = params.hasShadow ? params.hasShadow : false;
}
// Left and right positioning are disabled for IE
if (document.all) {
if (params.position == 'left' ||
params.position == 'right') {
Tipz.position = 'bottom';
} else {
Tipz.position = params.position;
}
} else {
Tipz.position = params.position ? params.position : 'bottom';
}
Tipz.backgroundColor = params.backgroundColor ? params.backgroundColor : '#000000';
Tipz.color = params.color ? params.color : '#FFFFFF';
Tipz.delay = params.delay ? params.delay : 0;
}
// Helpers section
/**
@name makeTopArrow
@description Create the arrow div when the tooltip is set to apear on the top.
The arrow is formed with 5 1 pixel height divs in stack.
Each one has 2 more pixels than the previous starting from 1 pixel, so
they form a little pyramid pointing to the top.
@return arrow The arrow div to be appended to the canvas.
*/
Tipz.makeTopArrow = function() {
// Arrow printing
var arrow = document.createElement('div');
arrow.className = 'tipz_arrow';
// First row
var firstRow = document.createElement('div');
firstRow.className = 'tipz_row';
firstRow.style.width = '1px';
firstRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(firstRow);
// Second row
var secondRow = document.createElement('div');
secondRow.className = 'tipz_row';
secondRow.style.width = '3px';
secondRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(secondRow);
// Third row
var thirdRow = document.createElement('div');
thirdRow.className = 'tipz_row';
thirdRow.style.width = '5px';
thirdRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(thirdRow);
// Fourth row
var fourthRow = document.createElement('div');
fourthRow.className = 'tipz_row';
fourthRow.style.width = '7px';
fourthRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(fourthRow);
// Fifth row
var fifthRow = document.createElement('div');
fifthRow.className = 'tipz_row';
fifthRow.style.width = '9px';
fifthRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(fifthRow);
return arrow;
}
/**
@name makeBottomArrow
@description Create the arrow div when the tooltip is set to apear on the bottom.
The arrow is formed with 5 1 pixel height divs in stack.
Each one has 2 less pixels than the previous starting from 9 pixel, so
they form a little pyramid pointing to the bottom.
@return arrow The arrow div to be appended to the canvas.
*/
Tipz.makeBottomArrow = function() {
// Arrow printing
var arrow = document.createElement('div');
arrow.className = 'tipz_arrow';
// Fifth row
var fifthRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
fifthRow.className = 'tipz_row_shadow';
} else {
fifthRow.className = 'tipz_row';
}
fifthRow.style.width = '9px';
fifthRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(fifthRow);
// Fourth row
var fourthRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
fourthRow.className = 'tipz_row_shadow';
} else {
fourthRow.className = 'tipz_row';
}
fourthRow.style.width = '7px';
fourthRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(fourthRow);
// Third row
var thirdRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
thirdRow.className = 'tipz_row_shadow';
} else {
thirdRow.className = 'tipz_row';
}
thirdRow.style.width = '5px';
thirdRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(thirdRow);
// Second row
var secondRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
secondRow.className = 'tipz_row_shadow';
} else {
secondRow.className = 'tipz_row';
}
secondRow.style.width = '3px';
secondRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(secondRow);
// First row
var firstRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
firstRow.className = 'tipz_row_shadow';
} else {
firstRow.className = 'tipz_row';
}
firstRow.style.width = '1px';
firstRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(firstRow);
return arrow;
}
/**
@name makeRightArrow
@description Create the arrow div when the tooltip is set to apear on the left.
The arrow is formed with 5 1 pixel width divs in line.
Each one has 2 less pixels than the previous starting from 9 pixel, so
they form a little pyramid pointing to the right.
@return arrow The arrow div to be appended to the canvas.
*/
Tipz.makeRightArrow = function() {
// Arrow printing
var arrow = document.createElement('div');
arrow.className = 'tipz_arrow';
// Fifth row
var fifthRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
fifthRow.className = 'tipz_column_shadow';
} else {
fifthRow.className = 'tipz_column';
}
fifthRow.style.height = '9px';
fifthRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(fifthRow);
// Fourth row
var fourthRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
fourthRow.className = 'tipz_column_shadow';
} else {
fourthRow.className = 'tipz_column';
}
fourthRow.style.marginTop = '1px';
fourthRow.style.height = '7px';
fourthRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(fourthRow);
// Third row
var thirdRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
thirdRow.className = 'tipz_column_shadow';
} else {
thirdRow.className = 'tipz_column';
}
thirdRow.style.marginTop = '2px';
thirdRow.style.height = '5px';
thirdRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(thirdRow);
// Second row
var secondRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
secondRow.className = 'tipz_column_shadow';
} else {
secondRow.className = 'tipz_column';
}
secondRow.style.marginTop = '3px';
secondRow.style.height = '3px';
secondRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(secondRow);
// First row
var firstRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
firstRow.className = 'tipz_column_shadow';
} else {
firstRow.className = 'tipz_column';
}
firstRow.style.marginTop = '4px';
firstRow.style.height = '1px';
firstRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(firstRow);
return arrow;
}
/**
@name makeLeftArrow
@description Create the arrow div when the tooltip is set to apear on the right.
The arrow is formed with 5 1 pixel width divs in line.
Each one has 2 more pixels than the previous starting from 1 pixel, so
they form a little pyramid pointing to the right.
@return arrow The arrow div to be appended to the canvas.
*/
Tipz.makeLeftArrow = function() {
// Arrow printing
var arrow = document.createElement('div');
arrow.className = 'tipz_arrow';
// First row
var firstRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
firstRow.className = 'tipz_column_shadow';
} else {
firstRow.className = 'tipz_column';
}
firstRow.style.marginTop = '4px';
firstRow.style.height = '1px';
firstRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(firstRow);
// Second row
var secondRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
secondRow.className = 'tipz_column_shadow';
} else {
secondRow.className = 'tipz_column';
}
secondRow.style.marginTop = '3px';
secondRow.style.height = '3px';
secondRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(secondRow);
// Third row
var thirdRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
thirdRow.className = 'tipz_column_shadow';
} else {
thirdRow.className = 'tipz_column';
}
thirdRow.style.marginTop = '2px';
thirdRow.style.height = '5px';
thirdRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(thirdRow);
// Fourth row
var fourthRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
fourthRow.className = 'tipz_column_shadow';
} else {
fourthRow.className = 'tipz_column';
}
fourthRow.style.marginTop = '1px';
fourthRow.style.height = '7px';
fourthRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(fourthRow);
// Fifth row
var fifthRow = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
fifthRow.className = 'tipz_column_shadow';
} else {
fifthRow.className = 'tipz_column';
}
fifthRow.style.height = '9px';
fifthRow.style.backgroundColor = Tipz.backgroundColor;
arrow.appendChild(fifthRow);
return arrow;
}
/**
@name fade
@description Make the transition effect
@return true
*/
Tipz.fade = function(startOpacity, endOpacity) {
// Get the DOM object which the transtition is for
var DOMObject = document.getElementById(Tipz.id);
// The method is being called with only one argument
if (typeof endOpacity == 'undefined') {
if (Tipz.isSolid) {
endOpacity = startOpacity;
} else {
endOpacity = Tipz.opacity;
}
startOpacity = 0;
}
// Set if the DOM object has to be removed at the ending of the fade
if (startOpacity > endOpacity) {
var remove = true;
} else {
var remove = false;
}
DOMObject.style.opacity = startOpacity/100;
var fade = function() {
if (startOpacity < endOpacity) {
startOpacity += 5;
// Standard
DOMObject.style.opacity = startOpacity/100;
// IE
DOMObject.style.filter = "alpha(opacity=" + startOpacity + ")";
// Mozilla
DOMObject.style.MozOpacity = startOpacity + "%";
}
else if (startOpacity >= endOpacity) {
startOpacity -= 5;
// Standard
DOMObject.style.opacity = startOpacity/100;
// IE
DOMObject.style.filter = "alpha(opacity=" + startOpacity + ")";
// Mozilla
DOMObject.style.MozOpacity = startOpacity + "%";
}
// If the transition is ended
if (startOpacity == endOpacity) {
clearInterval(interval);
// Only removes the DOM object if the transition is fadeOut
if (remove &&
document.getElementById(Tipz.id)) {
try {
document.body.removeChild(DOMObject);
} catch(e) {
}
}
}
}
var interval = setInterval(fade);
}
/**
@name getPosition
@description Get the position where the tip is needed to be
@params DOMObject The DOM object which the tip is for
@return position The position of the tip
*/
Tipz.getPosition = function(DOMObject) {
// A hash containing the offsetTop and offsetLeft
var position = {
offsetTop : 0,
offsetLeft : 0
};
// While the parentDOM has a parent node,
// it keeps adding pixels to the offsets
var positioning = function(parentDOM) {
while(parentDOM.parentNode) {
position.offsetTop += parentDOM.offsetTop;
position.offsetLeft += parentDOM.offsetLeft;
parentDOM = parentDOM.parentNode;
}
// Adding the offsets of the first DOM
position.offsetTop += DOMObject.offsetTop;
position.offsetLeft += DOMObject.offsetLeft;
// Return the position to be returned to the show method
return position;
}
// Getting the position
position = positioning(DOMObject.parentNode);
// Return the position
return position;
}
// Main methods section
/**
@name show
@description Prints the tooltip on screen.
All the tooltip is inside the canvas div.
There are two parts: the arrow and the content.
- The arrow.
- The content is the tip itself.
@params DOMObject The DOM object which the tooltip is for.
@params tip The text which is going to be shown in the tooltip.
@see Tipz.makeArrow()
@return true
*/
Tipz.show = function(DOMObject, tip) {
// Removes previous tooltip, just in case
if (document.getElementById(Tipz.id)) {
document.body.removeChild(document.getElementById(Tipz.id));
}
// Get the position and the dimension of the DOM object
var domHeight = DOMObject.offsetHeight;
var domWidth = DOMObject.offsetWidth;
// Get the position where the tip is going to be printed
var position = Tipz.getPosition(DOMObject);
var domTopPos = position.offsetTop;
var domLeftPos = position.offsetLeft;
// Canvas where the tooltip is going to be printed
var canvas = document.createElement('div');
canvas.id = Tipz.id;
canvas.className = 'tipz_canvas';
// Set opacity of the canvas
if (!Tipz.isSolid) {
canvas.style.opacity = Tipz.opacity;
}
if (Tipz.position == 'bottom') {
// Set the postition of the canvas
canvas.style.left = domLeftPos + (domWidth/2) - 3 + 'px';
canvas.style.top = domTopPos + domHeight + 'px';
// Create the arrow
var arrow = Tipz.makeTopArrow();
// Appending the arrow to the canvas
canvas.appendChild(arrow);
}
// The tip container
var content = document.createElement('div');
if (Tipz.isSolid && Tipz.hasShadow) {
content.className = 'tipz_content_shadow';
} else {
content.className = 'tipz_content';
}
content.style.backgroundColor = Tipz.backgroundColor;
content.style.color = Tipz.color;
// Append the tip to the container
content.innerHTML = tip;
// Right and left positioning are disabled for IE
if (Tipz.position == 'right' && !document.all) {
// Set the postition of the canvas
canvas.style.left = domLeftPos + domWidth + 'px';
canvas.style.top = domTopPos - 5 + 'px';
// Set the content style
content.style.cssFloat = 'left';
content.style.width = content.offsetWidth - 5 + 'px';
// Create the arrow
var arrow = Tipz.makeLeftArrow();
arrow.style.cssFloat = 'left';
// Appending the arrow to the canvas
canvas.appendChild(arrow);
}
// Append the container to the canvas
canvas.appendChild(content);
if (Tipz.position == 'top') {
// Set the postition of the canvas
canvas.style.left = domLeftPos + (domWidth/2) - 3 + 'px';
canvas.style.top = domTopPos - domHeight - 4 + 'px';
// Create the arrow
var arrow = Tipz.makeBottomArrow();
// Appending the arrow to the canvas
canvas.appendChild(arrow);
}
// Set the canvas hidden in order to get the position parameters
canvas.style.visibility = 'hidden';
// Append the canvas to the document
document.body.appendChild(canvas);
// Left and right positioning are disabled for IE
if (Tipz.position == 'left' && !document.all) {
// Set the postition of the canvas
canvas.style.left = domLeftPos - domWidth/1.2 + 'px';
canvas.style.top = domTopPos - 5 + 'px';
// Set the content style
content.style.cssFloat = 'left';
content.style.width = content.offsetWidth - 5 + 'px';
// Create the arrow
var arrow = Tipz.makeRightArrow();
arrow.style.cssFloat = 'left';
arrow.style.marginTop = (content.offsetHeight/2) - 4 + 'px';
// Appending the arrow to the canvas
canvas.appendChild(arrow);
}
if (Tipz.position != 'right') {
// Compensate the position
canvas.style.left = canvas.offsetLeft - (canvas.offsetWidth/2) + 'px';
} else {
// Set the arrow vertical position
arrow.style.marginTop = (content.offsetHeight/2) - 4 + 'px';
}
// Set the visibility to normal
canvas.style.visibility = '';
// Fade the tip in
Tipz.fade(100);
// Auxiliar var to keep the tip scope
//var thisTip = Tipz;
// Adding the hide to the DOM object onmouseout
DOMObject.onmouseout = function()
{
Tipz.hide();
}
// Adding the tip to the array of tips
Tipz.tipz.push(canvas);
// Increase the instances counter
Tipz.instances++;
return true;
}
/**
@name hide
@description Simply, delete the tooltip on screen
@return true
*/
Tipz.hide = function() {
// Fade the tip out
Tipz.fade(Tipz.opacity, 0);
// Decrease the instances number
Tipz.instances--;
return true;
}