angular-cached-resource
Version:
An AngularJS module to interact with RESTful resources, even when browser is offline
8 lines • 5.67 kB
JavaScript
/**
* angular-strap
* @version v2.0.1 - 2014-04-10
* @link http://mgcrea.github.io/angular-strap
* @author Olivier Louvignes (olivier@mg-crea.com)
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
;angular.module("mgcrea.ngStrap.tooltip",["mgcrea.ngStrap.helpers.dimensions"]).provider("$tooltip",function(){var e=this.defaults={animation:"am-fade",prefixClass:"tooltip",prefixEvent:"tooltip",container:!1,placement:"top",template:"tooltip/tooltip.tpl.html",contentTemplate:!1,trigger:"hover focus",keyboard:!1,html:!1,show:!1,title:"",type:"",delay:0};this.$get=["$window","$rootScope","$compile","$q","$templateCache","$http","$animate","$timeout","dimensions","$$rAF",function(t,o,n,i,a,r,l,s,c,u){function p(t,i){function a(){return"body"===p.container?c.offset(t[0]):c.position(t[0])}function r(e,t,o,n){var i,a=e.split("-");switch(a[0]){case"right":i={top:t.top+t.height/2-n/2,left:t.left+t.width};break;case"bottom":i={top:t.top+t.height,left:t.left+t.width/2-o/2};break;case"left":i={top:t.top+t.height/2-n/2,left:t.left-o};break;default:i={top:t.top-n,left:t.left+t.width/2-o/2}}if(!a[1])return i;if("top"===a[0]||"bottom"===a[0])switch(a[1]){case"left":i.left=t.left;break;case"right":i.left=t.left+t.width-o}else if("left"===a[0]||"right"===a[0])switch(a[1]){case"top":i.top=t.top-n;break;case"bottom":i.top=t.top+t.height}return i}var s={},p=s.$options=angular.extend({},e,i);s.$promise=h(p.template);var m=s.$scope=p.scope&&p.scope.$new()||o.$new();p.delay&&angular.isString(p.delay)&&(p.delay=parseFloat(p.delay)),p.title&&(s.$scope.title=p.title),m.$hide=function(){m.$$postDigest(function(){s.hide()})},m.$show=function(){m.$$postDigest(function(){s.show()})},m.$toggle=function(){m.$$postDigest(function(){s.toggle()})},s.$isShown=m.$isShown=!1;var v,y;p.contentTemplate&&(s.$promise=s.$promise.then(function(e){var t=angular.element(e);return h(p.contentTemplate).then(function(e){var o=f('[ng-bind="content"]',t[0]);return o.length||(o=f('[ng-bind="title"]',t[0])),o.removeAttr("ng-bind").html(e),t[0].outerHTML})}));var w,b,k,S;return s.$promise.then(function(e){angular.isObject(e)&&(e=e.data),p.html&&(e=e.replace(g,'ng-bind-html="')),e=d.apply(e),k=e,w=n(e),s.init()}),s.init=function(){p.delay&&angular.isNumber(p.delay)&&(p.delay={show:p.delay,hide:p.delay}),"self"===p.container?S=t:p.container&&(S=f(p.container));var e=p.trigger.split(" ");angular.forEach(e,function(e){"click"===e?t.on("click",s.toggle):"manual"!==e&&(t.on("hover"===e?"mouseenter":"focus",s.enter),t.on("hover"===e?"mouseleave":"blur",s.leave),"hover"!==e&&t.on($?"touchstart":"mousedown",s.$onFocusElementMouseDown))}),p.show&&m.$$postDigest(function(){"focus"===p.trigger?t[0].focus():s.show()})},s.destroy=function(){for(var e=p.trigger.split(" "),o=e.length;o--;){var n=e[o];"click"===n?t.off("click",s.toggle):"manual"!==n&&(t.off("hover"===n?"mouseenter":"focus",s.enter),t.off("hover"===n?"mouseleave":"blur",s.leave),"hover"!==n&&t.off($?"touchstart":"mousedown",s.$onFocusElementMouseDown))}b&&(b.remove(),b=null),m.$destroy()},s.enter=function(){return clearTimeout(v),y="in",p.delay&&p.delay.show?void(v=setTimeout(function(){"in"===y&&s.show()},p.delay.show)):s.show()},s.show=function(){m.$emit(p.prefixEvent+".show.before",s);var e=p.container?S:null,o=p.container?null:t;b&&b.remove(),b=s.$element=w(m,function(){}),b.css({top:"0px",left:"0px",display:"block"}).addClass(p.placement),p.animation&&b.addClass(p.animation),p.type&&b.addClass(p.prefixClass+"-"+p.type),l.enter(b,e,o,function(){m.$emit(p.prefixEvent+".show",s)}),s.$isShown=m.$isShown=!0,m.$$phase||m.$root.$$phase||m.$digest(),u(s.$applyPlacement),p.keyboard&&("focus"!==p.trigger?(s.focus(),b.on("keyup",s.$onKeyUp)):t.on("keyup",s.$onFocusKeyUp))},s.leave=function(){return clearTimeout(v),y="out",p.delay&&p.delay.hide?void(v=setTimeout(function(){"out"===y&&s.hide()},p.delay.hide)):s.hide()},s.hide=function(e){return s.$isShown?(m.$emit(p.prefixEvent+".hide.before",s),l.leave(b,function(){m.$emit(p.prefixEvent+".hide",s)}),s.$isShown=m.$isShown=!1,m.$$phase||m.$root.$$phase||m.$digest(),p.keyboard&&null!==b&&b.off("keyup",s.$onKeyUp),e&&"focus"===p.trigger?t[0].blur():void 0):void 0},s.toggle=function(){s.$isShown?s.leave():s.enter()},s.focus=function(){b[0].focus()},s.$applyPlacement=function(){if(b){var e=a(),t=b.prop("offsetWidth"),o=b.prop("offsetHeight"),n=r(p.placement,e,t,o);n.top+="px",n.left+="px",b.css(n)}},s.$onKeyUp=function(e){27===e.which&&s.hide()},s.$onFocusKeyUp=function(e){27===e.which&&t[0].blur()},s.$onFocusElementMouseDown=function(e){e.preventDefault(),e.stopPropagation(),s.$isShown?t[0].blur():t[0].focus()},s}function f(e,t){return angular.element((t||document).querySelectorAll(e))}function h(e){return i.when(a.get(e)||r.get(e)).then(function(t){return angular.isObject(t)?(a.put(e,t.data),t.data):t})}var d=String.prototype.trim,$="createTouch"in t.document,g=/ng-bind="/gi;return p}]}).directive("bsTooltip",["$window","$location","$sce","$tooltip","$$rAF",function(e,t,o,n,i){return{restrict:"EAC",scope:!0,link:function(e,t,a){var r={scope:e};angular.forEach(["template","contentTemplate","placement","container","delay","trigger","keyboard","html","animation","type"],function(e){angular.isDefined(a[e])&&(r[e]=a[e])}),angular.forEach(["title"],function(t){a[t]&&a.$observe(t,function(n,a){e[t]=o.trustAsHtml(n),angular.isDefined(a)&&i(function(){l&&l.$applyPlacement()})})}),a.bsTooltip&&e.$watch(a.bsTooltip,function(t,o){angular.isObject(t)?angular.extend(e,t):e.title=t,angular.isDefined(o)&&i(function(){l&&l.$applyPlacement()})},!0);var l=n(t,r);e.$on("$destroy",function(){l.destroy(),r=null,l=null})}}}]);