Popover.js
4.44 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
/* A rectangular panel that is absolutely positioned over other content
------------------------------------------------------------------------------------------------------------------------
Options:
- className (string)
- content (HTML string or jQuery element set)
- parentEl
- top
- left
- right (the x coord of where the right edge should be. not a "CSS" right)
- autoHide (boolean)
- show (callback)
- hide (callback)
*/
var Popover = Class.extend({
isHidden: true,
options: null,
el: null, // the container element for the popover. generated by this object
documentMousedownProxy: null, // document mousedown handler bound to `this`
margin: 10, // the space required between the popover and the edges of the scroll container
constructor: function(options) {
this.options = options || {};
},
// Shows the popover on the specified position. Renders it if not already
show: function() {
if (this.isHidden) {
if (!this.el) {
this.render();
}
this.el.show();
this.position();
this.isHidden = false;
this.trigger('show');
}
},
// Hides the popover, through CSS, but does not remove it from the DOM
hide: function() {
if (!this.isHidden) {
this.el.hide();
this.isHidden = true;
this.trigger('hide');
}
},
// Creates `this.el` and renders content inside of it
render: function() {
var _this = this;
var options = this.options;
this.el = $('<div class="fc-popover"/>')
.addClass(options.className || '')
.css({
// position initially to the top left to avoid creating scrollbars
top: 0,
left: 0
})
.append(options.content)
.appendTo(options.parentEl);
// when a click happens on anything inside with a 'fc-close' className, hide the popover
this.el.on('click', '.fc-close', function() {
_this.hide();
});
if (options.autoHide) {
$(document).on('mousedown', this.documentMousedownProxy = proxy(this, 'documentMousedown'));
}
},
// Triggered when the user clicks *anywhere* in the document, for the autoHide feature
documentMousedown: function(ev) {
// only hide the popover if the click happened outside the popover
if (this.el && !$(ev.target).closest(this.el).length) {
this.hide();
}
},
// Hides and unregisters any handlers
destroy: function() {
this.hide();
if (this.el) {
this.el.remove();
this.el = null;
}
$(document).off('mousedown', this.documentMousedownProxy);
},
// Positions the popover optimally, using the top/left/right options
position: function() {
var options = this.options;
var origin = this.el.offsetParent().offset();
var width = this.el.outerWidth();
var height = this.el.outerHeight();
var windowEl = $(window);
var viewportEl = getScrollParent(this.el);
var viewportTop;
var viewportLeft;
var viewportOffset;
var top; // the "position" (not "offset") values for the popover
var left; //
// compute top and left
top = options.top || 0;
if (options.left !== undefined) {
left = options.left;
}
else if (options.right !== undefined) {
left = options.right - width; // derive the left value from the right value
}
else {
left = 0;
}
if (viewportEl.is(window) || viewportEl.is(document)) { // normalize getScrollParent's result
viewportEl = windowEl;
viewportTop = 0; // the window is always at the top left
viewportLeft = 0; // (and .offset() won't work if called here)
}
else {
viewportOffset = viewportEl.offset();
viewportTop = viewportOffset.top;
viewportLeft = viewportOffset.left;
}
// if the window is scrolled, it causes the visible area to be further down
viewportTop += windowEl.scrollTop();
viewportLeft += windowEl.scrollLeft();
// constrain to the view port. if constrained by two edges, give precedence to top/left
if (options.viewportConstrain !== false) {
top = Math.min(top, viewportTop + viewportEl.outerHeight() - height - this.margin);
top = Math.max(top, viewportTop + this.margin);
left = Math.min(left, viewportLeft + viewportEl.outerWidth() - width - this.margin);
left = Math.max(left, viewportLeft + this.margin);
}
this.el.css({
top: top - origin.top,
left: left - origin.left
});
},
// Triggers a callback. Calls a function in the option hash of the same name.
// Arguments beyond the first `name` are forwarded on.
// TODO: better code reuse for this. Repeat code
trigger: function(name) {
if (this.options[name]) {
this.options[name].apply(this, Array.prototype.slice.call(arguments, 1));
}
}
});