bootstrap-popover-extension.js 10.3 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
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

(function ($) {
    "use strict";

    var _super = $.fn.popover;

    var Popover = function (element, options) {
        _super.Constructor.apply(this, arguments);
    };

    Popover.prototype = $.extend({}, _super.Constructor.prototype, {
        constructor: Popover,
        _super: function () {
            var args = $.makeArray(arguments);
            _super.Constructor.prototype[args.shift()].apply(this, args);
        },
        show: function () {
            var that = this;
            var e = $.Event('show.bs.' + this.type);
            var templateUrl = this.$element.attr('data-templateUrl');
            var onShow = function () {
                if (templateUrl) {
                    $.get(templateUrl, function (template) {
                        var tip = $(template);
                        that.options.compliler(tip[0])(that.options.scope);
                        that.options.scope.$apply();
                        that.$tip = tip;
                        that.onTipLoaded(that.$tip);
                    })
                } else {
                    that.onTipLoaded(that.tip());
                }
            }

            e.onShow = onShow;

            if ((this.hasContent() || templateUrl) && this.enabled) {
                this.$element.trigger(e);

                if (e.isDefaultPrevented())
                    return;

                onShow();
            }
        },
        onTipLoaded: function (tip) {
            var that = this;
            var $tip = tip;

            if (this.$element.attr('data-overwrite') !== 'true') {
                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;
            placement = placement.trim();

            var autoToken = /\s?auto?\s?/i;
            var autoPlace = autoToken.test(placement);
            if (autoPlace) {
                placement = placement.replace(autoToken, '') || 'rightTop';
            }

            $tip
              .detach()
              .css({ top: 0, left: 0, display: 'block' })
              .addClass(placement);

            this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element);

            var pos = this.getPosition();
            var actualWidth = $tip[0].offsetWidth;
            var actualHeight = $tip[0].offsetHeight;

            if (autoPlace) {
                var $parent = this.$element.parent();

                var orgPlacement = placement;
                var docScroll = document.documentElement.scrollTop || document.body.scrollTop;
                var parentWidth = this.options.container === 'body' ? window.innerWidth : $parent.outerWidth();
                var parentHeight = this.options.container === 'body' ? window.innerHeight : $parent.outerHeight();
                var parentLeft = this.options.container === 'body' ? 0 : $parent.offset().left;
                var parentTop = this.options.container === 'body' ? 0 : $parent.offset().top;
                parentTop += docScroll;

                var placementOrder = placement.split(' ');

                placement = this.getPlacement({
                    placementOrder: placementOrder,
                    pos: pos,
                    actualWidth: actualWidth,
                    actualHeight: actualHeight,
                    parentWidth: parentWidth,
                    parentHeight: parentHeight,
                    parentTop: parentTop,
                    parentLeft: parentLeft,
                    useOptimizedPlacementAlgorithm: this.options.useOptimizedPlacementAlgorithm
                });

                $tip
                  .removeClass(orgPlacement)
                  .addClass(placement);
            }

            var calculatedOffset = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight);

            this.applyPlacement(calculatedOffset, placement);
            this.hoverState = null;

            var complete = function () {
                that.$element.trigger('shown.bs.' + that.type, $tip);
            };

            $.support.transition && this.$tip.hasClass('fade') ?
              $tip
                .one($.support.transition.end, complete)
                .emulateTransitionEnd(150) :
              complete();
        },
        getCalculatedOffset: function (placement, pos, actualWidth, actualHeight) {
            return placement === 'bottom' ? { top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2 } :
                placement === 'top' ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2 } :
                placement === 'left' ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
                placement === 'right' ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width } :
                placement === 'topLeft' ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - (actualWidth * .25) } :
                placement === 'topRight' ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - (actualWidth * .75) } :
                placement === 'rightTop' ? { top: pos.top + pos.height / 2 - (actualHeight * .25), left: pos.left + pos.width } :
                placement === 'rightBottom' ? { top: pos.top + pos.height / 2 - (actualHeight * .75), left: pos.left + pos.width } :
                placement === 'bottomLeft' ? { top: pos.top + pos.height, left: pos.left + pos.width / 2 - (actualWidth * .25) } :
                placement === 'bottomRight' ? { top: pos.top + pos.height, left: pos.left + pos.width / 2 - (actualWidth * .75) } :
                placement === 'leftTop' ? { top: pos.top + pos.height / 2 - (actualHeight * .25), left: pos.left - actualWidth } :
                /*placement === 'leftBottom' ?*/ { top: pos.top + pos.height / 2 - (actualHeight * .75), left: pos.left - actualWidth };
        },
        getPlacement: function (placementInfo) {
            placementInfo.placementOrder.splice(placementInfo.placementOrder.length, 0
            , 'rightTop'
            , 'rightBottom'
            , 'leftTop'
            , 'leftBottom'
            , 'bottomLeft'
            , 'bottomRight'
            , 'topLeft'
            , 'topRight');


            var placementOrder = placementInfo.placementOrder;
            var pos = placementInfo.pos;
            var actualWidth = placementInfo.actualWidth;
            var actualHeight = placementInfo.actualHeight;
            var parentTop = placementInfo.parentTop;
            var parentLeft = placementInfo.parentLeft;
            var parentWidth = placementInfo.parentWidth;
            var parentHeight = placementInfo.parentHeight;

            var minOffsetRate = 100;
            var minOffsetRateIndex = -1;
            for (var i = 0; i < placementOrder.length; i++) {
                var placement = placementOrder[i];
                var tl = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight);
                var canDisplay = tl.top >= parentTop && tl.left >= parentLeft && tl.left + actualWidth <= parentLeft + parentWidth && tl.top + actualHeight <= parentTop + parentHeight;

                if (canDisplay) {
                    return placement;
                } else if (placementInfo.useOptimizedPlacementAlgorithm) {
                    // calculate the offset rate (area of popup outside parent area/popup area)
                    var offsetRate = 0;
                    if (tl.left < parentLeft) {
                        offsetRate += (parentLeft - tl.left) / actualWidth;
                    }
                    if (tl.left + actualWidth > parentLeft + parentWidth) {
                        offsetRate += (tl.left + actualWidth - parentLeft - parentWidth) / actualWidth;
                    }
                    if (tl.top < parentTop) {
                        offsetRate += (parentTop - tl.top) / actualHeight;
                    }
                    if (tl.top + actualHeight > parentTop + parentHeight) {
                        offsetRate += (tl.top + actualHeight - parentTop - parentHeight) / actualHeight;
                    }
                    // find the minimal offset rate
                    if (offsetRate < minOffsetRate) {
                        minOffsetRate = offsetRate;
                        minOffsetRateIndex = i;
                    }
                }
            }

            return placementOrder[placementInfo.useOptimizedPlacementAlgorithm ? minOffsetRateIndex : 0];
        },
        getPosition: function ($element) {
            $element = $element || this.$element

            var el = $element[0]
            var isBody = el.tagName === 'BODY'

            var parentDeclare = this.$element.attr('data-placement-target');
            if (parentDeclare) {
                var parents = this.$element.parents(parentDeclare);

                if (parents.length > 0) {
                    el = parents[0];
                }
            }

            if (!el) {
                el = this.$element[0];
            }

            var elRect    = el.getBoundingClientRect()
            if (elRect.width == null) {
                // width and height are missing in IE8, so compute them manually; see https://github.com/twbs/bootstrap/issues/14093
                elRect = $.extend({}, elRect, { width: elRect.right - elRect.left, height: elRect.bottom - elRect.top })
            }
            var elOffset  = isBody ? { top: 0, left: 0 } : $element.offset()
            var scroll    = { scroll: isBody ? document.documentElement.scrollTop || document.body.scrollTop : $element.scrollTop() }
            var outerDims = isBody ? { width: $(window).width(), height: $(window).height() } : null

            return $.extend({}, elRect, scroll, outerDims, elOffset)
        }
    });

    $.fn.popover = $.extend(function (option) {
        return this.each(function () {
            var $this = $(this)
              , data = $this.data('bs.popover')
              , options = typeof option === 'object' && option;
            if (!data)
                $this.data('bs.popover', (data = new Popover(this, options)));
            if (typeof option === 'string')
                data[option]();
        });
    }, _super);
})(jQuery);