Skip to content

Instantly share code, notes, and snippets.

@timelf123
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save timelf123/816629a9633af1f90d3b to your computer and use it in GitHub Desktop.

Select an option

Save timelf123/816629a9633af1f90d3b to your computer and use it in GitHub Desktop.
(function($) {
$.fn.popover.Constructor.prototype.show = function() {
var fallback_position = 'top';
var e = $.Event('show.bs.' + this.type);
if (this.hasContent() && this.enabled) {
if (e.isDefaultPrevented()) {
return;
}
var $tip = this.tip();
this.setContent();
if (this.options.animation) {
$tip.addClass('fade');
}
var placement = (typeof this.options.placement == 'function') ?
this.options.placement.call(this, $tip[0], this.$element[0]) :
this.options.placement;
$tip.detach().css({ top: 0, left: 0, display: 'block' });
this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element);
var tp;
var pos = this.getPosition();
var actualWidth = $tip[0].offsetWidth;
var actualHeight = $tip[0].offsetHeight;
// Get positions
var tpt = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2};
var tpb = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2};
var tpl = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth};
var tpr = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width};
// Get position room
var hast = (tpt.top > $(window).scrollTop());
var hasb = ((tpb.top + actualHeight) < ($(window).scrollTop() + $(window).height()));
var hasl = (tpl.left > $(window).scrollLeft());
var hasr = ((tpr.left + actualWidth) < ($(window).scrollLeft() + $(window).width()));
switch (placement) {
case 'top':
if (!hast) {
placement = hasb ? 'bottom' : (hasr ? 'right' : (hasl ? 'left' : fallback_position));
}
break;
case 'bottom':
if (!hasb) {
placement = hast ? 'top' : (hasr ? 'right' : (hasl ? 'left' : fallback_position));
}
break;
case 'left':
if (!hasl) {
placement = hasr ? 'right' : (hast ? 'top' : (hasb ? 'bottom' : fallback_position));
}
break;
case 'right':
if (!hasr) {
placement = hasl ? 'left' : (hast ? 'top' : (hasb ? 'bottom' : fallback_position));
}
break;
}
switch (placement) {
case 'top':
tp = tpt;
break;
case 'bottom':
tp = tpb;
break;
case 'left':
tp = tpl;
break;
case 'right':
tp = tpr;
break;
}
this.applyPlacement(tp, placement);
this.$element.trigger('shown.bs.' + this.type);
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment