forked from OpenNHP/opennhp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.popover.js
executable file
·248 lines (195 loc) · 6.55 KB
/
ui.popover.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
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
'use strict';
var $ = require('jquery');
var UI = require('./core');
var $w = $(window);
/**
* @reference https://github.com/nolimits4web/Framework7/blob/master/src/js/modals.js
* @license https://github.com/nolimits4web/Framework7/blob/master/LICENSE
*/
var Popover = function(element, options) {
this.options = $.extend({}, Popover.DEFAULTS, options || {});
this.$element = $(element);
this.active = null;
this.$popover = (this.options.target && $(this.options.target)) || null;
this.init();
this.events();
};
Popover.DEFAULTS = {
theme: undefined,
trigger: 'click',
content: '',
open: false,
target: undefined,
tpl: '<div class="am-popover">' +
'<div class="am-popover-inner"></div>' +
'<div class="am-popover-caret"></div></div>'
};
Popover.prototype.init = function() {
var me = this;
var $element = this.$element;
var $popover;
if (!this.options.target) {
this.$popover = this.getPopover();
this.setContent();
}
$popover = this.$popover;
$popover.appendTo($('body'));
this.sizePopover();
function sizePopover() {
me.sizePopover();
}
// TODO: 监听页面内容变化,重新调整位置
$element.on('open.popover.amui', function() {
$(window).on('resize.popover.amui', UI.utils.debounce(sizePopover, 50));
});
$element.on('close.popover.amui', function() {
$(window).off('resize.popover.amui', sizePopover);
});
this.options.open && this.open();
};
Popover.prototype.sizePopover = function sizePopover() {
var $element = this.$element;
var $popover = this.$popover;
if (!$popover || !$popover.length) {
return;
}
var popWidth = $popover.outerWidth();
var popHeight = $popover.outerHeight();
var $popCaret = $popover.find('.am-popover-caret');
var popCaretSize = ($popCaret.outerWidth() / 2) || 8;
// 取不到 $popCaret.outerHeight() 的值,所以直接加 8
var popTotalHeight = popHeight + 8; // $popCaret.outerHeight();
var triggerWidth = $element.outerWidth();
var triggerHeight = $element.outerHeight();
var triggerOffset = $element.offset();
var triggerRect = $element[0].getBoundingClientRect();
var winHeight = $w.height();
var winWidth = $w.width();
var popTop = 0;
var popLeft = 0;
var diff = 0;
var spacing = 2;
var popPosition = 'top';
$popover.css({left: '', top: ''}).removeClass('am-popover-left ' +
'am-popover-right am-popover-top am-popover-bottom');
$popCaret.css({left: '', top: ''});
if (popTotalHeight - spacing < triggerRect.top + spacing) {
// Popover on the top of trigger
popTop = triggerOffset.top - popTotalHeight - spacing;
} else if (popTotalHeight <
winHeight - triggerRect.top - triggerRect.height) {
// On bottom
popPosition = 'bottom';
popTop = triggerOffset.top + triggerHeight + popCaretSize + spacing;
} else { // On middle
popPosition = 'middle';
popTop = triggerHeight / 2 + triggerOffset.top - popHeight / 2;
}
// Horizontal Position
if (popPosition === 'top' || popPosition === 'bottom') {
popLeft = triggerWidth / 2 + triggerOffset.left - popWidth / 2;
diff = popLeft;
if (popLeft < 5) {
popLeft = 5;
}
if (popLeft + popWidth > winWidth) {
popLeft = (winWidth - popWidth - 20);
// console.log('left %d, win %d, popw %d', popLeft, winWidth, popWidth);
}
if (popPosition === 'top') {
// This is the Popover position, NOT caret position
// Popover on the Top of trigger, caret on the bottom of Popover
$popover.addClass('am-popover-top');
}
if (popPosition === 'bottom') {
$popover.addClass('am-popover-bottom');
}
diff = diff - popLeft;
$popCaret.css({left: (popWidth / 2 - popCaretSize + diff) + 'px'});
} else if (popPosition === 'middle') {
popLeft = triggerOffset.left - popWidth - popCaretSize;
$popover.addClass('am-popover-left');
if (popLeft < 5) {
popLeft = triggerOffset.left + triggerWidth + popCaretSize;
$popover.removeClass('am-popover-left').addClass('am-popover-right');
}
if (popLeft + popWidth > winWidth) {
popLeft = winWidth - popWidth - 5;
$popover.removeClass('am-popover-left').addClass('am-popover-right');
}
$popCaret.css({top: (popHeight / 2 - popCaretSize / 2) + 'px'});
}
// Apply position style
$popover.css({top: popTop + 'px', left: popLeft + 'px'});
};
Popover.prototype.toggle = function() {
return this[this.active ? 'close' : 'open']();
};
Popover.prototype.open = function() {
var $popover = this.$popover;
this.$element.trigger('open.popover.amui');
this.sizePopover();
$popover.show().addClass('am-active');
this.active = true;
};
Popover.prototype.close = function() {
var $popover = this.$popover;
this.$element.trigger('close.popover.amui');
$popover.
removeClass('am-active').
trigger('closed.popover.amui').
hide();
this.active = false;
};
Popover.prototype.getPopover = function() {
var uid = UI.utils.generateGUID('am-popover');
var theme = [];
if (this.options.theme) {
$.each(this.options.theme.split(','), function(i, item) {
theme.push('am-popover-' + $.trim(item));
});
}
return $(this.options.tpl).attr('id', uid).addClass(theme.join(' '));
};
Popover.prototype.setContent = function(content) {
content = content || this.options.content;
this.$popover && this.$popover.find('.am-popover-inner').empty().
html(content);
};
Popover.prototype.events = function() {
var eventNS = 'popover.amui';
var triggers = this.options.trigger.split(' ');
for (var i = triggers.length; i--;) {
var trigger = triggers[i];
if (trigger === 'click') {
this.$element.on('click.' + eventNS, $.proxy(this.toggle, this));
} else { // hover or focus
var eventIn = trigger == 'hover' ? 'mouseenter' : 'focusin';
var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout';
this.$element.on(eventIn + '.' + eventNS, $.proxy(this.open, this));
this.$element.on(eventOut + '.' + eventNS, $.proxy(this.close, this));
}
}
};
function Plugin(option) {
return this.each(function() {
var $this = $(this);
var data = $this.data('amui.popover');
var options = $.extend({},
UI.utils.parseOptions($this.attr('data-am-popover')),
typeof option == 'object' && option);
if (!data) {
$this.data('amui.popover', (data = new Popover(this, options)));
}
if (typeof option == 'string') {
data[option] && data[option]();
}
});
}
$.fn.popover = Plugin;
// Init code
UI.ready(function(context) {
$('[data-am-popover]', context).popover();
});
$.AMUI.popover = Popover;
module.exports = Popover;