diff --git a/bower.json b/bower.json index 69342c3..59d43b1 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-youtube-mb", - "version": "1.3.1", + "version": "1.3.2", "description": "Embed a YouTube player with a simple directive", "keywords": [ "angular", diff --git a/dist/angular-youtube-embed.min.js b/dist/angular-youtube-embed.min.js index 2d74588..5951885 100644 --- a/dist/angular-youtube-embed.min.js +++ b/dist/angular-youtube-embed.min.js @@ -2,4 +2,4 @@ angular-youtube-mb v1.3.1 https://github.com/brandly/angular-youtube-embed */ -angular.module("youtube-embed",[]).service("youtubeEmbedUtils",["$window","$rootScope",function(e,t){function r(e,t){return e.indexOf(t)>-1}function a(){t.$apply(function(){n.ready=!0})}var n={},i=/https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/gi,o=/t=(\d+)[ms]?(\d+)?s?/;return n.getIdFromURL=function(e){var t=e.replace(i,"$1");if(r(t,";")){var a=t.split(";");if(r(a[1],"%")){var n=decodeURIComponent(a[1]);t=("http://youtube.com"+n).replace(i,"$1")}else t=a[0]}else r(t,"#")&&(t=t.split("#")[0]);return t},n.getTimeFromURL=function(e){e=e||"";var t=e.match(o);if(!t)return 0;var a=t[0],n=t[1],i=t[2];return"undefined"!=typeof i?(i=parseInt(i,10),n=parseInt(n,10)):r(a,"m")?(n=parseInt(n,10),i=0):(i=parseInt(n,10),n=0),i+60*n},n.ready=!1,"undefined"==typeof YT?(e.onYouTubeIframeAPIReady=a,console.log("Unable to find YouTube iframe library on this page.")):YT.loaded?n.ready=!0:YT.ready(a),n}]).directive("youtubeVideo",["$window","youtubeEmbedUtils",function(e,t){var r=1,a={"-1":"unstarted",0:"ended",1:"playing",2:"paused",3:"buffering",5:"queued"},n="youtube.player.";return e.YTConfig={host:"https://www.youtube.com"},{restrict:"EA",scope:{videoId:"=?",videoUrl:"=?",player:"=?",playerVars:"=?",playerHeight:"=?",playerWidth:"=?"},link:function(e,i,o){function u(){var t=Array.prototype.slice.call(arguments);e.$apply(function(){e.$emit.apply(e,t)})}function d(t){var r=a[t.data];"undefined"!=typeof r&&u(n+r,e.player,t),e.$apply(function(){e.player.currentState=r})}function l(t){u(n+"ready",e.player,t)}function y(t){u(n+"error",e.player,t)}function p(){var t=angular.copy(e.playerVars);t.start=t.start||e.urlStartTime;var r=new YT.Player(c,{height:e.playerHeight,width:e.playerWidth,videoId:e.videoId,playerVars:t,events:{onReady:l,onStateChange:d,onError:y}});return r.id=c,r}function f(){(e.videoId||e.playerVars.list)&&(e.player&&"function"==typeof e.player.destroy&&e.player.destroy(),e.player=p())}e.utils=t;var c=o.playerId||i[0].id||"unique-youtube-embed-id-"+r++;i[0].id=c,e.playerHeight=e.playerHeight||390,e.playerWidth=e.playerWidth||640,e.playerVars=e.playerVars||{};var s=e.$watch(function(){return e.utils.ready&&("undefined"!=typeof e.videoUrl||"undefined"!=typeof e.videoId||"undefined"!=typeof e.playerVars.list)},function(t){t&&(s(),"undefined"!=typeof e.videoUrl?e.$watch("videoUrl",function(t){e.videoId=e.utils.getIdFromURL(t),e.urlStartTime=e.utils.getTimeFromURL(t),f()}):"undefined"!=typeof e.videoId?e.$watch("videoId",function(){e.urlStartTime=null,f()}):e.$watch("playerVars.list",function(){e.urlStartTime=null,f()}))});e.$watchCollection(["playerHeight","playerWidth"],function(){e.player&&e.player.setSize(e.playerWidth,e.playerHeight)}),e.$on("$destroy",function(){e.player&&e.player.destroy()})}}}]); \ No newline at end of file +angular.module("youtube-embed",[]).service("youtubeEmbedUtils",["$window","$rootScope",function(e,t){function r(e,t){return e.indexOf(t)>-1}function n(){t.$apply(function(){a.ready=!0})}var a={},i=/https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/gi,o=/t=(\d+)[ms]?(\d+)?s?/;return a.getIdFromURL=function(e){var t=e.replace(i,"$1");if(r(t,";")){var n=t.split(";");if(r(n[1],"%")){var a=decodeURIComponent(n[1]);t=("http://youtube.com"+a).replace(i,"$1")}else t=n[0]}else r(t,"#")&&(t=t.split("#")[0]);return t},a.getTimeFromURL=function(e){e=e||"";var t=e.match(o);if(!t)return 0;var n=t[0],a=t[1],i=t[2];return"undefined"!=typeof i?(i=parseInt(i,10),a=parseInt(a,10)):r(n,"m")?(a=parseInt(a,10),i=0):(i=parseInt(a,10),a=0),i+60*a},a.ready=!1,"undefined"==typeof YT?(e.onYouTubeIframeAPIReady=n,console.log("Unable to find YouTube iframe library on this page.")):YT.loaded?a.ready=!0:YT.ready(n),a}]).directive("youtubeVideo",["$window","youtubeEmbedUtils",function(e,t){var r=1,n={"-1":"unstarted",0:"ended",1:"playing",2:"paused",3:"buffering",5:"queued"},a="youtube.player.";return e.YTConfig={host:"https://www.youtube.com"},{restrict:"EA",scope:{videoId:"=?",videoUrl:"=?",player:"=?",playerVars:"=?",playerHeight:"=?",playerWidth:"=?"},link:function(e,i,o){function d(){var t=Array.prototype.slice.call(arguments);e.$apply(function(){e.$emit.apply(e,t)})}function u(t){var r=n[t.data];"undefined"!=typeof r&&d(a+r,e.player,t),e.$apply(function(){e.player.currentState=r})}function l(t){d(a+"ready",e.player,t)}function y(t){d(a+"error",e.player,t)}function p(){var t=angular.copy(e.playerVars);t.start=t.start||e.urlStartTime;var r=new YT.Player(s,{height:e.playerHeight,width:e.playerWidth,videoId:e.videoId,playerVars:t});return r.addEventListener("onReady",l),r.addEventListener("onStateChange",u),r.addEventListener("onError",y),r.id=s,r}function f(){(e.videoId||e.playerVars.list)&&(e.player&&"function"==typeof e.player.destroy&&e.player.destroy(),e.player=p())}e.utils=t;var s=o.playerId||i[0].id||"unique-youtube-embed-id-"+r++;i[0].id=s,e.playerHeight=e.playerHeight||390,e.playerWidth=e.playerWidth||640,e.playerVars=e.playerVars||{};var c=e.$watch(function(){return e.utils.ready&&("undefined"!=typeof e.videoUrl||"undefined"!=typeof e.videoId||"undefined"!=typeof e.playerVars.list)},function(t){t&&(c(),"undefined"!=typeof e.videoUrl?e.$watch("videoUrl",function(t){e.videoId=e.utils.getIdFromURL(t),e.urlStartTime=e.utils.getTimeFromURL(t),f()}):"undefined"!=typeof e.videoId?e.$watch("videoId",function(){e.urlStartTime=null,f()}):e.$watch("playerVars.list",function(){e.urlStartTime=null,f()}))});e.$watchCollection(["playerHeight","playerWidth"],function(){e.player&&e.player.setSize(e.playerWidth,e.playerHeight)}),e.$on("$destroy",function(){e.player&&e.player.destroy(),i.remove(),i=null})}}}]); \ No newline at end of file diff --git a/package.json b/package.json index d245440..fb88114 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "test": "gulp test" }, "dependencies": { - "angular": "" + "angular": "1.5.11" }, "devDependencies": { "coffee-script": "^1.7.1", diff --git a/src/angular-youtube-embed.js b/src/angular-youtube-embed.js index cb7592d..36dbff9 100644 --- a/src/angular-youtube-embed.js +++ b/src/angular-youtube-embed.js @@ -174,17 +174,17 @@ angular.module('youtube-embed', []) function createPlayer () { var playerVars = angular.copy(scope.playerVars); playerVars.start = playerVars.start || scope.urlStartTime; + var player = new YT.Player(playerId, { height: scope.playerHeight, width: scope.playerWidth, videoId: scope.videoId, - playerVars: playerVars, - events: { - onReady: onPlayerReady, - onStateChange: onPlayerStateChange, - onError: onPlayerError - } + playerVars: playerVars }); + + player.addEventListener('onReady',onPlayerReady); + player.addEventListener('onStateChange',onPlayerStateChange); + player.addEventListener('onError',onPlayerError); player.id = playerId; return player; @@ -246,6 +246,8 @@ angular.module('youtube-embed', []) scope.$on('$destroy', function () { scope.player && scope.player.destroy(); + element.remove(); + element = null; }); } };