-
Notifications
You must be signed in to change notification settings - Fork 32
/
angular-video-bg.min.js
1 lines (1 loc) · 5.15 KB
/
angular-video-bg.min.js
1
!function(){"use strict";function a(a,b,c){return{restrict:"EA",replace:!0,scope:{videoId:"=?",playlist:"=?",ratio:"=?",loop:"=?",mute:"=?",start:"=?",end:"=?",contentZIndex:"=?",allowClickEvents:"=?",mobileImage:"=?",playerCallback:"&?"},transclude:!0,template:"<div><div></div><div ng-transclude></div></div>",link:function(d,e){function f(a,b){var c;return function(){var d=this,e=arguments,f=function(){c=null,a.apply(d,e)};clearTimeout(c),c=setTimeout(f,b)}}function g(){var a=window.navigator.userAgent,b=a.indexOf("MSIE "),c=a.indexOf("Trident/"),d=a.indexOf("Edge/");if(b>0)return parseInt(a.substring(b+5,a.indexOf(".",b)),10);if(c>0){var e=a.indexOf("rv:");return parseInt(a.substring(e+3,a.indexOf(".",e)),10)}return d>0?parseInt(a.substring(d+5,a.indexOf(".",d)),10):!1}function h(a,b){var c=["top","right","bottom","left"],d=function(c,d){return c[d]=parseInt(b.call(v,a+"-"+d),10),c};return c.reduce(d,{})}function i(a,b){function c(){var a,c=Array.prototype.slice.call(arguments),d=0,e=function(b,c){return a[c]?b+a[c]:b};for(var f in b)b.hasOwnProperty(f)&&(a=b[f],d+=c.reduce(e,0));return d}return{width:a.width+c("left","right"),height:g()&&g()<12?a.height:a.height+c("top","bottom")}}function j(){var a=e.children().eq(1),b=!!a.children().length,c=Array.prototype.slice.call(e.parent().children());if(e.parent().css({position:"relative",overflow:"hidden"}),!b){e.css({position:"absolute",left:"0",top:"0"});var f=c.indexOf(e[0]);f>-1&&c.splice(f,1),a=angular.element(c)}a.css({position:"relative",zIndex:d.contentZIndex||99})}function k(){v=a.getComputedStyle(e.parent()[0]);var b=["width","height"],c=["border","margin"];return g()&&g()<12&&c.push("padding"),b=b.reduce(function(a,b){return a[b]=parseInt(v.getPropertyValue(b),10),a},{}),c=c.reduce(function(a,b){return a[b]=h(b,v.getPropertyValue),a},{}),i(b,c)}function l(){var a=parseInt(y.width/d.ratio,10),b=parseInt(y.height*d.ratio,10),c=y.height<a;return{width:c?y.width:b,height:c?a:y.height}}function m(){j(),y=k(),z=l()}function n(){var a={zIndex:1,position:"absolute",width:z.width+"px",height:z.height+"px",left:parseInt((y.width-z.width)/2,10)+"px",top:parseInt((y.height-z.height)/2,10)+"px"};d.allowClickEvents||(a.pointerEvents="none"),D.css(a)}function o(a){a=a||d,x.seekTo(a.start||0)}function p(a){var b,c;a=a||d,b=a.end?a.end-(a.start||0):d.start?x.getDuration()-a.start:x.getDuration(),c=1e3*b,B=setTimeout(function(){d.playlist?x.nextVideo():o(a)},c)}function q(){var a=d.playlist[x.getPlaylistIndex()];p(a)}function r(){E&&c(function(){E({player:x})}),d.playlist&&(x.loadPlaylist(A),d.loop&&x.setLoop(!0)),d.mute&&!x.isMuted()?x.mute():x.isMuted()&&x.unMute(),o(),d.$on("$destroy",function(){B&&clearTimeout(B),angular.element(a).off("resize",G),x.destroy()})}function s(a){if(a.data===YT.PlayerState.PLAYING&&(D.css("display","block"),!d.playlist&&d.loop&&p(),d.playlist&&d.loop&&q()),a.data===YT.PlayerState.UNSTARTED&&d.playlist){var b=d.playlist[x.getPlaylistIndex()],c=void 0===b.mute?d.mute:b.mute;F=b.mobileImage||d.mobileImage||"//img.youtube.com/vi/"+b.videoId+"/maxresdefault.jpg",u(F),D.css("display","none"),o(b),c||c&&d.mute?x.isMuted()||x.mute():c&&d.mute||x.isMuted()&&x.unMute()}}function t(){m();var a={autoplay:1,controls:0,iv_load_policy:3,cc_load_policy:0,modestbranding:1,playsinline:1,rel:0,showinfo:0,playlist:d.videoId};x=new YT.Player(w,{width:z.width,height:z.height,videoId:d.videoId,playerVars:a,events:{onReady:r,onStateChange:s}}),D=e.children().eq(0),D.css("display","none"),n()}function u(a){e.parent().css({backgroundImage:"url("+a+")",backgroundSize:"cover",backgroundPosition:"center center"})}var v,w,x,y,z,A,B,C=document.querySelector('script[src="//www.youtube.com/iframe_api"]'),D=e.children().eq(0),E=d.playerCallback,F=d.mobileImage||"//img.youtube.com/vi/"+d.videoId+"/maxresdefault.jpg";if(w="player"+Array.prototype.slice.call(document.querySelectorAll("div[video-id]")).indexOf(e[0]),D.attr("id",w),d.ratio=d.ratio||16/9,d.loop=void 0===d.loop?!0:d.loop,d.mute=void 0===d.mute?!0:d.mute,!d.videoId&&!d.playlist)throw new Error("Either video-id or playlist must be defined.");if(d.videoId&&d.playlist)throw new Error("Both video-id and playlist cannot be defined, please choose one or the other.");d.playlist&&(A=d.playlist.map(function(a){return a.videoId}));var G=f(function(){m(),n()},300);if(u(F),!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){var H;if(a.youTubeIframeAPIReady||(H=b.defer(),a.youTubeIframeAPIReady=H.promise,a.onYouTubeIframeAPIReady=function(){H.resolve()}),C)H&&H.resolve();else{var I=document.createElement("script");I.src="//www.youtube.com/iframe_api";var J=document.getElementsByTagName("script")[0];J.parentNode.insertBefore(I,J)}a.youTubeIframeAPIReady.then(t),angular.element(a).on("resize",G)}d.$watch("videoId",function(a,b){a&&b&&a!==b&&(clearTimeout(B),F=d.mobileImage||"//img.youtube.com/vi/"+a+"/maxresdefault.jpg",u(F),D.css("display","none"),x.loadVideoById(a))}),d.$watchCollection("playlist",function(a,b){a&&b&&a!==b&&(clearTimeout(B),A=a.map(function(a){return a.videoId}),x.loadPlaylist(A),d.loop&&x.setLoop(!0))})}}}angular.module("angularVideoBg",[]),angular.module("angularVideoBg").directive("videoBg",a),a.$inject=["$window","$q","$timeout"]}();