UNPKG

lintel-contrib-popovers

Version:
2 lines 3.08 kB
!function(a){"use strict";function b(d,e){return this.each(function(){var f=a(this),g=a(e),h=a.extend({},b.defaults,g.data(),"object"==typeof d&&d),i=f.data("lt.popover");i?i.options=h:(i=new c(this,h,e),f.data("lt.popover",i)),"string"==typeof d?i[d](e):h.show&&i.toggle(e)})}var c=function(b,c,d){if(!d)throw new Error("Related target required to position popover.");this.$popover=a(b),this.$trigger=a(d),this.options=c||{},this.visible=!1};c.prototype.toggle=function(a){this.visible?(this.hide(),this.$trigger[0]!==a&&this.show(a)):this.show(a)},c.prototype.show=function(b){if(!this.visible){b&&(this.$trigger=a(b));var c=a.Event("show.lt.popover",{relatedTarget:b});if(this.$popover.trigger(c),!c.isDefaultPrevented()){this.visible=!0,a(document.body).addClass("popover-open"),this.$popover.addClass("open").addClass("popover-"+this.options.placement).attr("aria-hidden","false").offset(this.getOffset()),this.$popover.on("click.close.lt.popover",'[data-toggle="popover-close"]',a.proxy(this.hide,this)),this.optionsEscape(),this.popoverFocus(),this.options.onShow.call(this,this.$popover,this.$trigger);var d=a.Event("shown.lt.popover",{relatedTarget:b});this.$popover.trigger(d)}}},c.prototype.hide=function(b){if(this.visible){var c=a.Event("hide.lt.popover",{relatedTarget:this.$trigger&&this.$trigger[0]?this.$trigger[0]:b});if(this.$popover.trigger(c),!c.isDefaultPrevented()){this.visible=!1,a(document.body).removeClass("popover-open"),this.$popover.removeClass("open").removeClass("popover-top popover-right popover-bottom popover-left").attr("aria-hidden","true"),this.$popover.off("click.close.lt.popover"),this.optionsEscape(),this.restoreFocus(),this.options.onHide.call(this,this.$popover,this.$trigger);var d=a.Event("hidden.lt.popover",{relatedTarget:this.$trigger&&this.$trigger[0]?this.$trigger[0]:b});this.$popover.trigger(d)}}},c.prototype.getOffset=function(){var a=this.$trigger.offset(),b=this.options.placement,c=a.top,d=a.left,e=this.$trigger.outerHeight(),f=this.$trigger.outerWidth(),g=this.$popover.outerHeight(),h=this.$popover.outerWidth(),i={top:c,left:d};return("top"===b||"bottom"===b)&&(i.left=d+Math.floor(f/2)-Math.floor(h/2)),("right"===b||"left"===b)&&(i.top=c+Math.floor(e/2)-Math.floor(g/2)),"top"===b?i.top=c-g:"right"===b?i.left=d+f:"bottom"===b?i.top=c+e:"left"===b&&(i.left=d-h),i},c.prototype.optionsEscape=function(){this.visible&&this.options.esc?this.$popover.on("keydown.close.lt.popover",a.proxy(function(a){27===a.which&&this.hide()},this)):this.$popover.off("keydown.close.lt.popover")},c.prototype.popoverFocus=function(){this.$popover.focus()},c.prototype.restoreFocus=function(){this.$trigger&&this.$trigger.focus()},b.defaults={onShow:function(){},onHide:function(){},esc:!0,placement:"top",show:!0},a.fn.popover=b,a(document).on("click.lt.popover.data-attr",'[data-toggle="popover"]',function(c){var d=a(this),e=a(d.attr("data-target"))||a(d.attr("href")),f=e.data("lt.popover")?"toggle":a.extend(e.data(),d.data());d.is("a")&&c.preventDefault(),b.call(e[0],f,d[0])})}(jQuery); //# sourceMappingURL=popovers.min.js.map