-
Notifications
You must be signed in to change notification settings - Fork 1
/
nhpup_1.1.js
133 lines (116 loc) · 4.54 KB
/
nhpup_1.1.js
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
/*
--------------------------------------------------------------------------
Code for link-hover text boxes
By Nicolas Honing
Usage: <a onmouseover="nhpup.popup('popup text' [, {'class': 'myclass', 'width': 300}])">a link</a>
The configuration dict with CSS class and width is optional - default is class .pup and width of 200px.
You can style the popup box via CSS, targeting its ID #pup.
You can escape " in the popup text with ".
Tutorial and support at http://nicolashoening.de?twocents&nr=8
--------------------------------------------------------------------------
*/
nhpup = {
pup: null, // This is out popup box, represented by a div
identifier: "pup", // Name of ID and class of the popup box
minMargin: 15, // Set how much minimal space there should be (in pixels)
// between the popup and everything else (borders, mouse)
default_width: 200, // Will be set to width from css in document.ready
move: false, // Move it around with the mouse? we are only ready for that when the mouse event is set up.
// Besides, having this turned off intially is resource-friendly.
/*
Write message, show popup w/ custom width if necessary,
make sure it disappears on mouseout
*/
popup: function(p_msg, p_config)
{
// do track mouse moves and update position
this.move = true;
// restore defaults
this.pup.removeClass()
.addClass(this.identifier)
.width(this.default_width);
// custom configuration
if (typeof p_config != 'undefined') {
if ('class' in p_config) {
this.pup.addClass(p_config['class']);
}
if ('width' in p_config) {
this.pup.width(p_config['width']);
}
}
// Write content and display
// Alan: Só mostra se checkbox estiver marcado.
if (document.getElementById('checkajuda').checked == true) {
this.pup.html(p_msg).show();
}
// Make sure popup goes away on mouse out and we stop the constant
// positioning on mouse moves.
// The event obj needs to be gotten from the virtual
// caller, since we use onmouseover='nhpup.popup(p_msg)'
var t = this.getTarget(arguments.callee.caller.arguments[0]);
$(t).unbind('mouseout').bind('mouseout',
function(e){
nhpup.pup.hide();
nhpup.move = false;
}
);
},
// set the target element position
setElementPos: function(x, y)
{
// Call nudge to avoid edge overflow. Important tweak: x+10, because if
// the popup is where the mouse is, the hoverOver/hoverOut events flicker
var x_y = this.nudge(x + 10, y);
// remember: the popup is still hidden
this.pup.css('top', x_y[1] + 'px')
.css('left', x_y[0] + 'px');
},
/* Avoid edge overflow */
nudge: function(x,y)
{
var win = $(window);
// When the mouse is too far on the right, put window to the left
var xtreme = $(document).scrollLeft() + win.width() - this.pup.width() - this.minMargin;
if(x > xtreme) {
x -= this.pup.width() + 2 * this.minMargin;
}
x = this.max(x, 0);
// When the mouse is too far down, move window up
if((y + this.pup.height()) > (win.height() + $(document).scrollTop())) {
y -= this.pup.height() + this.minMargin;
}
return [ x, y ];
},
/* custom max */
max: function(a,b)
{
if (a>b) return a;
else return b;
},
/*
Get the target (element) of an event.
Inspired by quirksmode
*/
getTarget: function(e)
{
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
return targ;
}
};
/* Prepare popup and define the mouseover callback */
jQuery(document).ready(function(){
// create default popup on the page
$('body').append('<div id="' + nhpup.identifier + '" class="' + nhpup.identifier + '" style="position:abolute; display:none; z-index:200;"></div>');
nhpup.pup = $('#' + nhpup.identifier);
// set dynamic coords when the mouse moves
$(document).mousemove(function(e){
if (nhpup.move){
nhpup.setElementPos(e.pageX, e.pageY);
}
});
});