Professional Documents
Culture Documents
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
*/
this.init();
this._bindEvents();
};
Popover.DEFAULTS = {
theme: null,
trigger: 'click',
content: '',
open: false,
target: null,
tpl: '<div class="am-popover">' +
'<div class="am-popover-inner"></div>' +
'<div class="am-popover-caret"></div></div>'
};
Popover.prototype.init = function() {
var _this = 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() {
_this.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);
});
if (!$popover || !$popover.length) {
return;
}
// Horizontal Position
if (popPosition === 'top' || popPosition === 'bottom') {
popLeft = triggerWidth / 2 + triggerOffset.left - popWidth / 2;
diff = popLeft;
if (popLeft < 5) {
popLeft = 5;
}
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));
});
}
Popover.prototype.setContent = function(content) {
content = content || this.options.content;
this.$popover && this.$popover.find('.am-popover-inner')
.empty().html(content);
};
Popover.prototype._bindEvents = function() {
var eventNS = 'popover.amui';
var triggers = this.options.trigger.split(' ');
Popover.prototype.destroy = function() {
this.$element.off('.popover.amui').removeData('amui.popover');
this.$popover.remove();
};
UI.plugin('popover', Popover);
// Init code
UI.ready(function(context) {
$('[data-am-popover]', context).popover();
});
module.exports = Popover;
// TODO: 允许用户定义位置