2015-07-14 13:45:54 -07:00

9 lines
9.5 KiB
JavaScript

/**
* angular-strap
* @version v2.2.1 - 2015-03-10
* @link http://mgcrea.github.io/angular-strap
* @author Olivier Louvignes (olivier@mg-crea.com)
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
"use strict";angular.module("mgcrea.ngStrap.tooltip",["mgcrea.ngStrap.helpers.dimensions"]).provider("$tooltip",function(){var t=this.defaults={animation:"am-fade",customClass:"",prefixClass:"tooltip",prefixEvent:"tooltip",container:!1,target:!1,placement:"top",template:"tooltip/tooltip.tpl.html",contentTemplate:!1,trigger:"hover focus",keyboard:!1,html:!1,show:!1,title:"",type:"",delay:0,autoClose:!1,bsEnabled:!0,viewport:{selector:"body",padding:0}};this.$get=["$window","$rootScope","$compile","$q","$templateCache","$http","$animate","$sce","dimensions","$$rAF","$timeout",function(e,o,n,i,a,r,l,s,c,f,u){function p(i,a){function r(){M.$emit(K.prefixEvent+".show",H)}function p(){if(M.$emit(K.prefixEvent+".hide",H),B===Y){if(R&&"focus"===K.trigger)return i[0].blur();A()}}function b(){var t=K.trigger.split(" ");angular.forEach(t,function(t){"click"===t?i.on("click",H.toggle):"manual"!==t&&(i.on("hover"===t?"mouseenter":"focus",H.enter),i.on("hover"===t?"mouseleave":"blur",H.leave),"button"===N&&"hover"!==t&&i.on($?"touchstart":"mousedown",H.$onFocusElementMouseDown))})}function y(){for(var t=K.trigger.split(" "),e=t.length;e--;){var o=t[e];"click"===o?i.off("click",H.toggle):"manual"!==o&&(i.off("hover"===o?"mouseenter":"focus",H.enter),i.off("hover"===o?"mouseleave":"blur",H.leave),"button"===N&&"hover"!==o&&i.off($?"touchstart":"mousedown",H.$onFocusElementMouseDown))}}function k(){"focus"!==K.trigger?B.on("keyup",H.$onKeyUp):i.on("keyup",H.$onFocusKeyUp)}function E(){"focus"!==K.trigger?B.off("keyup",H.$onKeyUp):i.off("keyup",H.$onFocusKeyUp)}function C(){u(function(){B.on("click",x),w.on("click",H.hide),z=!0},0,!1)}function S(){z&&(B.off("click",x),w.off("click",H.hide),z=!1)}function x(t){t.stopPropagation()}function D(t){t=t||K.target||i;var o=t[0],n="BODY"===o.tagName,a=o.getBoundingClientRect(),r={};for(var l in a)r[l]=a[l];null===r.width&&(r=angular.extend({},r,{width:a.right-a.left,height:a.bottom-a.top}));var s=n?{top:0,left:0}:c.offset(o),f={scroll:n?document.documentElement.scrollTop||document.body.scrollTop:t.prop("scrollTop")||0},u=n?{width:document.documentElement.clientWidth,height:e.innerHeight}:null;return angular.extend({},r,f,u,s)}function T(t,e,o,n){var i,a=t.split("-");switch(a[0]){case"right":i={top:e.top+e.height/2-n/2,left:e.left+e.width};break;case"bottom":i={top:e.top+e.height,left:e.left+e.width/2-o/2};break;case"left":i={top:e.top+e.height/2-n/2,left:e.left-o};break;default:i={top:e.top-n,left:e.left+e.width/2-o/2}}if(!a[1])return i;if("top"===a[0]||"bottom"===a[0])switch(a[1]){case"left":i.left=e.left;break;case"right":i.left=e.left+e.width-o}else if("left"===a[0]||"right"===a[0])switch(a[1]){case"top":i.top=e.top-n;break;case"bottom":i.top=e.top+e.height}return i}function P(t,e){var o=B[0],n=o.offsetWidth,i=o.offsetHeight,a=parseInt(c.css(o,"margin-top"),10),r=parseInt(c.css(o,"margin-left"),10);isNaN(a)&&(a=0),isNaN(r)&&(r=0),t.top=t.top+a,t.left=t.left+r,c.setOffset(o,angular.extend({using:function(t){B.css({top:Math.round(t.top)+"px",left:Math.round(t.left)+"px"})}},t),0);var l=o.offsetWidth,s=o.offsetHeight;if("top"===e&&s!==i&&(t.top=t.top+i-s),!/top-left|top-right|bottom-left|bottom-right/.test(e)){var f=F(e,t,l,s);if(f.left?t.left+=f.left:t.top+=f.top,c.setOffset(o,t),/top|right|bottom|left/.test(e)){var u=/top|bottom/.test(e),p=u?2*f.left-n+l:2*f.top-i+s,h=u?"offsetWidth":"offsetHeight";O(p,o[h],u)}}}function F(t,e,o,n){var i={top:0,left:0},a=K.viewport&&d(K.viewport.selector||K.viewport);if(!a)return i;var r=K.viewport&&K.viewport.padding||0,l=D(a);if(/right|left/.test(t)){var s=e.top-r-l.scroll,c=e.top+r-l.scroll+n;s<l.top?i.top=l.top-s:c>l.top+l.height&&(i.top=l.top+l.height-c)}else{var f=e.left-r,u=e.left+r+o;f<l.left?i.left=l.left-f:u>l.width&&(i.left=l.left+l.width-u)}return i}function O(t,e,o){var n=d(".tooltip-arrow, .arrow",B[0]);n.css(o?"left":"top",50*(1-t/e)+"%").css(o?"top":"left","")}function A(){clearTimeout(W),H.$isShown&&null!==B&&(K.autoClose&&S(),K.keyboard&&E()),V&&(V.$destroy(),V=null),B&&(B.remove(),B=H.$element=null)}var H={},N=i[0].nodeName.toLowerCase(),K=H.$options=angular.extend({},t,a);H.$promise=g(K.template);var M=H.$scope=K.scope&&K.scope.$new()||o.$new();if(K.delay&&angular.isString(K.delay)){var U=K.delay.split(",").map(parseFloat);K.delay=U.length>1?{show:U[0],hide:U[1]}:U[0]}H.$id=K.id||i.attr("id")||"",K.title&&(M.title=s.trustAsHtml(K.title)),M.$setEnabled=function(t){M.$$postDigest(function(){H.setEnabled(t)})},M.$hide=function(){M.$$postDigest(function(){H.hide()})},M.$show=function(){M.$$postDigest(function(){H.show()})},M.$toggle=function(){M.$$postDigest(function(){H.toggle()})},H.$isShown=M.$isShown=!1;var W,j;K.contentTemplate&&(H.$promise=H.$promise.then(function(t){var e=angular.element(t);return g(K.contentTemplate).then(function(t){var o=d('[ng-bind="content"]',e[0]);return o.length||(o=d('[ng-bind="title"]',e[0])),o.removeAttr("ng-bind").html(t),e[0].outerHTML})}));var q,B,I,L,V;H.$promise.then(function(t){angular.isObject(t)&&(t=t.data),K.html&&(t=t.replace(v,'ng-bind-html="')),t=m.apply(t),I=t,q=n(t),H.init()}),H.init=function(){K.delay&&angular.isNumber(K.delay)&&(K.delay={show:K.delay,hide:K.delay}),"self"===K.container?L=i:angular.isElement(K.container)?L=K.container:K.container&&(L=d(K.container)),b(),K.target&&(K.target=angular.isElement(K.target)?K.target:d(K.target)),K.show&&M.$$postDigest(function(){"focus"===K.trigger?i[0].focus():H.show()})},H.destroy=function(){y(),A(),M.$destroy()},H.enter=function(){return clearTimeout(W),j="in",K.delay&&K.delay.show?void(W=setTimeout(function(){"in"===j&&H.show()},K.delay.show)):H.show()},H.show=function(){if(K.bsEnabled&&!H.$isShown){M.$emit(K.prefixEvent+".show.before",H);var t,e;K.container?(t=L,e=L[0].lastChild?angular.element(L[0].lastChild):null):(t=null,e=i),B&&A(),V=H.$scope.$new(),B=H.$element=q(V,function(){}),B.css({top:"-9999px",left:"-9999px",display:"block",visibility:"hidden"}),K.animation&&B.addClass(K.animation),K.type&&B.addClass(K.prefixClass+"-"+K.type),K.customClass&&B.addClass(K.customClass),e?e.after(B):t.prepend(B),H.$isShown=M.$isShown=!0,h(M),H.$applyPlacement();var o=l.enter(B,t,e,r);o&&o.then&&o.then(r),h(M),f(function(){B&&B.css({visibility:"visible"})}),K.keyboard&&("focus"!==K.trigger&&H.focus(),k()),K.autoClose&&C()}},H.leave=function(){return clearTimeout(W),j="out",K.delay&&K.delay.hide?void(W=setTimeout(function(){"out"===j&&H.hide()},K.delay.hide)):H.hide()};var R,Y;H.hide=function(t){if(H.$isShown){M.$emit(K.prefixEvent+".hide.before",H),R=t,Y=B;var e=l.leave(B,p);e&&e.then&&e.then(p),H.$isShown=M.$isShown=!1,h(M),K.keyboard&&null!==B&&E(),K.autoClose&&null!==B&&S()}},H.toggle=function(){H.$isShown?H.leave():H.enter()},H.focus=function(){B[0].focus()},H.setEnabled=function(t){K.bsEnabled=t},H.setViewport=function(t){K.viewport=t},H.$applyPlacement=function(){if(B){var e=K.placement,o=/\s?auto?\s?/i,n=o.test(e);n&&(e=e.replace(o,"")||t.placement),B.addClass(K.placement);var a=D(),r=B.prop("offsetWidth"),l=B.prop("offsetHeight");if(n){var s=e,c=K.container?d(K.container):i.parent(),f=D(c);s.indexOf("bottom")>=0&&a.bottom+l>f.bottom?e=s.replace("bottom","top"):s.indexOf("top")>=0&&a.top-l<f.top&&(e=s.replace("top","bottom")),("right"===s||"bottom-left"===s||"top-left"===s)&&a.right+r>f.width?e="right"===s?"left":e.replace("left","right"):("left"===s||"bottom-right"===s||"top-right"===s)&&a.left-r<f.left&&(e="left"===s?"right":e.replace("right","left")),B.removeClass(s).addClass(e)}var u=T(e,a,r,l);P(u,e)}},H.$onKeyUp=function(t){27===t.which&&H.$isShown&&(H.hide(),t.stopPropagation())},H.$onFocusKeyUp=function(t){27===t.which&&(i[0].blur(),t.stopPropagation())},H.$onFocusElementMouseDown=function(t){t.preventDefault(),t.stopPropagation(),H.$isShown?i[0].blur():i[0].focus()};var z=!1;return H}function h(t){t.$$phase||t.$root&&t.$root.$$phase||t.$digest()}function d(t,e){return angular.element((e||document).querySelectorAll(t))}function g(t){return b[t]?b[t]:b[t]=r.get(t,{cache:a}).then(function(t){return t.data})}var m=String.prototype.trim,$="createTouch"in e.document,v=/ng-bind="/gi,w=angular.element(e.document),b={};return p}]}).directive("bsTooltip",["$window","$location","$sce","$tooltip","$$rAF",function(t,e,o,n,i){return{restrict:"EAC",scope:!0,link:function(t,e,a){var r={scope:t};angular.forEach(["template","contentTemplate","placement","container","delay","trigger","keyboard","html","animation","backdropAnimation","type","customClass","id"],function(t){angular.isDefined(a[t])&&(r[t]=a[t])}),e.attr("data-target")&&(r.target=e.attr("data-target")),t.hasOwnProperty("title")||(t.title=""),a.$observe("title",function(e){if(angular.isDefined(e)||!t.hasOwnProperty("title")){var n=t.title;t.title=o.trustAsHtml(e),angular.isDefined(n)&&i(function(){l&&l.$applyPlacement()})}}),a.bsTooltip&&t.$watch(a.bsTooltip,function(e,o){angular.isObject(e)?angular.extend(t,e):t.title=e,angular.isDefined(o)&&i(function(){l&&l.$applyPlacement()})},!0),a.bsShow&&t.$watch(a.bsShow,function(t){l&&angular.isDefined(t)&&(angular.isString(t)&&(t=!!t.match(/true|,?(tooltip),?/i)),t===!0?l.show():l.hide())}),a.bsEnabled&&t.$watch(a.bsEnabled,function(t){l&&angular.isDefined(t)&&(angular.isString(t)&&(t=!!t.match(/true|1|,?(tooltip),?/i)),l.setEnabled(t===!1?!1:!0))}),a.viewport&&t.$watch(a.viewport,function(t){l&&angular.isDefined(t)&&l.setViewport(t)});var l=n(e,r);t.$on("$destroy",function(){l&&l.destroy(),r=null,l=null})}}}]);
//# sourceMappingURL=tooltip.min.js.map