-
Notifications
You must be signed in to change notification settings - Fork 4
/
polyplayer.vendor.min.js
1 lines (1 loc) · 11.9 KB
/
polyplayer.vendor.min.js
1
!function(t){var e=(function(){function e(t){return new e.fn.init(t)}function i(e,i,n){if(!n.contentWindow.postMessage)return!1;var r=n.getAttribute("src").split("?")[0],s=JSON.stringify({method:e,value:i});"//"===r.substr(0,2)&&(r=t.location.protocol+r),n.contentWindow.postMessage(s,r)}function n(t){var e,i;try{e=JSON.parse(t.data),i=e.event||e.method}catch(n){}if("ready"!=i||d||(d=!0),t.origin!=h)return!1;var r=e.value,o=e.data,a=""===a?null:e.player_id,l=s(i,a),u=[];return l?(void 0!==r&&u.push(r),o&&u.push(o),a&&u.push(a),u.length>0?l.apply(null,u):l.call()):!1}function r(t,e,i){i?(u[i]||(u[i]={}),u[i][t]=e):u[t]=e}function s(t,e){return e?u[e][t]:u[t]}function o(t,e){if(e&&u[e]){if(!u[e][t])return!1;u[e][t]=null}else{if(!u[t])return!1;u[t]=null}return!0}function a(e){"//"===e.substr(0,2)&&(e=t.location.protocol+e);for(var i=e.split("/"),n="",r=0,s=i.length;s>r&&3>r;r++)n+=i[r],2>r&&(n+="/");return n}function l(t){return!!(t&&t.constructor&&t.call&&t.apply)}var u={},d=!1,h=(Array.prototype.slice,"");return e.fn=e.prototype={element:null,init:function(t){return"string"==typeof t&&(t=document.getElementById(t)),this.element=t,h=a(this.element.getAttribute("src")),this},api:function(t,e){if(!this.element||!t)return!1;var n=this,s=n.element,o=""!==s.id?s.id:null,a=l(e)?null:e,u=l(e)?e:null;return u&&r(t,u,o),i(t,a,s),n},addEvent:function(t,e){if(!this.element)return!1;var n=this,s=n.element,o=""!==s.id?s.id:null;return r(t,e,o),"ready"!=t?i("addEventListener",t,s):"ready"==t&&d&&e.call(null,o),n},removeEvent:function(t){if(!this.element)return!1;var e=this,n=e.element,r=""!==n.id?n.id:null,s=o(t,r);"ready"!=t&&s&&i("removeEventListener",t,n)}},e.fn.init.prototype=e.fn,t.addEventListener?t.addEventListener("message",n,!1):t.attachEvent("onmessage",n),t.Froogaloop=t.$f=e}(),function s(t){var e={provider:null,videoId:null,videoUrl:null,prefetchInfo:!1,container:document.body};if(_.extend(e,t),e.videoUrl){var i=this._parseUrl(e.videoUrl);_.extend(e,i)}return e.container instanceof Element||(e.container=document.querySelector(e.container)),e.playerId=s._generatePlayerId(),new this._providers[e.provider](e)});e.prototype._providers={youtube:null,vimeo:null,soundcloud:null},e.prototype._parseUrl=function(t){var e={provider:null,videoUrl:null,videoId:null};try{var i=/^(https?:\/\/)?(www.)?([a-z0-9\-]+)\.[a-z]+(\/(.*))?/i.exec(t);if(null===i)throw"Invalid url";var n=i[3].toLowerCase(),r=i[5]||"";if(!(n in this._providers))throw"Unknown provider";if(e.provider=n,e.videoUrl=t,"youtube"===n){var s=/v=([A-Za-z0-9\-_]+)/.exec(t);if(null===s)throw"YouTube requires a URL containing the video ID (v)";e.videoId=s[1]}else if("vimeo"===n){if(!/^[0-9]+$/.test(r))throw"Vimeo must be a numeric video url";e.videoId=parseInt(r,10)}else if("soundcloud"===n){if(/^[0-9a-zA-Z-_]+\/sets\/[0-9a-zA-Z-_]+$/i.test(r))throw"Soundcloud sets are not implemented yet";if(!/^[0-9a-zA-Z-_]+\/[0-9a-zA-Z-_]+$/i.test(r))throw"This is not a valid url to a song on Soundcloud"}}catch(o){throw o}return e},e._lastPlayerId=0,e._generatePlayerId=function(){return"polyplayer_"+this._lastPlayerId++},e.states={LOADING:0,READY:1,PLAYING:2,PAUSED:3,FINISHED:4,STOPPED:5};var i=Backbone.Collection.extend({model:function(t,i){if(!t.container){var n=document.createElement("div");i.collection._container.appendChild(n),t.container=n}return new e(t)},initialize:function(t,e){var i=e.container||document.body;i instanceof Element||(i=document.querySelector(i)),this._container=i,this.on("finish",this._onFinish,this)},loopMode:0,_currentPlayer:null,setPlayer:function(t,e){var i=this.getCurrentPlayer();return null!=i&&i.stop(),this._currentPlayer=t,this.trigger("playerChange",t),e&&t.play(),t},setPlayerById:function(t,e){this.setPlayer(this.get(t),e)},getCurrentPlayer:function(){return this.get(this._currentPlayer)},getOrSetCurrentPlayer:function(){var t=this.getCurrentPlayer();return null==t?this.nextPlayer():t},play:function(){this.getOrSetCurrentPlayer().play()},pause:function(){this.getCurrentPlayer().pause()},stop:function(){this.getCurrentPlayer().stop()},seek:function(t){this.getCurrentPlayer().seek(t)},seekTo:function(t){this.getCurrentPlayer().seekTo(t)},getState:function(){return this.getCurrentPlayer().getState()},getCurrentPosition:function(){return this.getCurrentPlayer().getCurrentPosition()},getDetails:function(t){this.getCurrentPlayer().getDetails(t)},nextPlayer:function(t){var e=this.getCurrentPlayer(),i=0,t=!!t;return null!==e&&(i=this.indexOf(e)+1),!t&&i>=this.length?void 0:(i%=this.length,this.setPlayer(this.models[i],!0))},previousPlayer:function(t){var e=this.getCurrentPlayer(),i=0,t=!!t;if(null!==e&&(i=this.indexOf(e)-1),0>i){if(!t)return;i+=this.length}return this.setPlayer(this.models[i],!0)},randomPlayer:function(){return this.setPlayer(this.models[Math.floor(this.length*Math.random())],!0)},_onFinish:function(){var t=this.loopMode;switch(t){case i.loopModes.NEXT:this.nextPlayer(!1);break;case i.loopModes.LOOP:this.nextPlayer(!0);break;case i.loopModes.RANDOM:this.randomPlayer()}}});i.loopModes={NO:0,NEXT:1,LOOP:2,RANDOM:3};var n=Backbone.Model.extend({defaults:{rawDetails:null,details:null,videoId:null,videoUrl:null,playerId:null,state:e.states.LOADING,currentPosition:null,duration:null},_setState:function(t){var i=e.states[t.toUpperCase()];this.set("state",i),this.trigger("stateChange",i)},_setCurrentPosition:function(t){this.set("currentPosition",t),this.trigger("playProgress",{currentPosition:t,relativePosition:t/this.get("duration")})},seek:function(t){var e=this;this.getDetails(function(i){e.seekTo(t*i.duration)})},getDetails:function(t){if(this.get("state")===e.states.LOADING)return this.on("ready",_.bind(this.getDetails,this,t)),void 0;var i=this.get("details");if(null!==i)return t(i);var n=this;this._fetchDetails(function(e){n.set("details",e),t(e)})},getCurrentPosition:function(){return this.get("currentPosition")},getDuration:function(){return this.get("duration")},getState:function(){return this.get("state")}});e.prototype._providers.soundcloud=n.extend({initialize:function(t){this.set({playerId:t.playerId,url:t.videoUrl,videoId:t.videoId});var e=this;e.widget=null,this._loadScript(function(){var i=document.createElement("iframe");i.setAttribute("id",t.playerId),i.setAttribute("src","https://w.soundcloud.com/player/?url="+encodeURIComponent(t.videoUrl)),t.container.appendChild(i);var n=e.widget=SC.Widget(i);n.bind(SC.Widget.Events.READY,_.bind(e._onReady,e)),n.bind(SC.Widget.Events.PAUSE,_.bind(e._onPause,e)),n.bind(SC.Widget.Events.PLAY,_.bind(e._onPlay,e)),n.bind(SC.Widget.Events.FINISH,_.bind(e._onFinish,e)),n.bind(SC.Widget.Events.PLAY_PROGRESS,_.bind(e._onPlayProgress,e))})},play:function(){this.widget.play()},pause:function(){this.widget.pause()},seekTo:function(t){this.widget.seekTo(t),this._setCurrentPosition(t)},stop:function(){this.pause(),this.seek(0),this._setCurrentPosition(0);var t=this;setTimeout(function(){t._setState("stopped"),t.trigger("stop")},100)},_loadScript:function(e){if("SC"in t&&SC)return e(),void 0;var i=document.createElement("script");i.src="https://w.soundcloud.com/player/api.js",i.onload=function(){e()},document.body.appendChild(i)},_fetchDetails:function(t){this.widget.getCurrentSound(_.bind(function(e){this.set("rawDetails",e);var i={duration:e.duration,id:e.id,title:e.title,createdAt:new Date(e.created_at),thumbnails:[{width:100,height:100,url:e.artwork_url}]};this.set("details",i),t(i)},this))},_onReady:function(){var t=this;this.widget.getDuration(function(e){t.set("duration",e),t._setState("ready"),t.trigger("ready")})},_onPause:function(){this._setState("paused"),this.trigger("pause")},_onPlay:function(){this._setState("playing"),this.trigger("play")},_onFinish:function(){this.stop(),this._setState("finished"),this.trigger("finish")},_onPlayProgress:function(t){this.set("currentPosition",t.currentPosition),this.trigger("playProgress",t)}}),e.prototype._providers.youtube=n.extend({initialize:function(e){this.set({playerId:e.playerId,url:e.videoUrl,videoId:e.videoId});var i=document.createElement("div");i.setAttribute("id",e.playerId),e.container.appendChild(i);var n=this,r=function(){n.player=new YT.Player(e.playerId,{videoId:e.videoId,events:{onStateChange:function(t){n._setStateById(t.data)},onReady:function(){n._onReady()}}})};if("YT"in t)r();else{var s=document.createElement("script");s.src="https://www.youtube.com/iframe_api",document.body.appendChild(s),s.onload=function(){YT.ready(r)}}this.on("play",this._setInterval),this.on("pause",this._clearInterval),this.on("finish",this._clearInterval)},_onReady:function(){this.set("duration",1e3*this.player.getDuration()),this._setState("ready"),this.trigger("ready")},play:function(){this.get("state")===e.states.STOPPED&&this.seekTo(0),this.player.playVideo()},pause:function(){this.player.pauseVideo()},stop:function(){this.player.stopVideo(),this.pause(),this._setCurrentPosition(0),this._setState("stopped"),this.trigger("stop")},seekTo:function(t){if(this._seekValue=t,this._setCurrentPosition(t),!this._seekTimeoutSet){this._seekTimeoutSet=!0;var e=this;setTimeout(function(){var t=e._seekValue;e.player.seekTo(t/1e3,!0),e._seekTimeoutSet=!1},500)}},_seekValue:null,_seekTimeoutSet:!1,_setInterval:function(){this._clearInterval();var t=this;this._intervalId=setInterval(function(){var e=t.player.getCurrentTime();t._setCurrentPosition(1e3*e)},1e3)},_clearInterval:function(){clearInterval(this._intervalId),this._intervalId=null},_fetchDetails:function(t){var e=this,i=new XMLHttpRequest;i.onload=function(i){var n=i.target.responseText;try{n=JSON.parse(n).entry}catch(r){throw r}e.set("rawDetails",n);var s={title:n.title.$t,duration:1e3*parseInt(n.media$group.yt$duration.seconds),thumbnails:[],createdAt:new Date(n.published.$t)};_.each(n.media$group.media$thumbnail,function(t){s.thumbnails.push({height:t.height,width:t.width,url:t.url})}),e.set("details",s),t(s)},i.open("get","https://gdata.youtube.com/feeds/api/videos/"+this.get("videoId")+"?v=2&alt=json",!0),i.send()},_setStateById:function(t){var e={"-1":"loading",0:"finished",1:"playing",2:"paused",3:null,5:"ready"},i=e[t];null!==i&&(this._setState(i),"playing"===i?this.trigger("play"):"paused"==i?this.trigger("pause"):"finished"==i&&this.trigger("finish"))}}),e.prototype._providers.vimeo=n.extend({initialize:function(t){this.set({playerId:t.playerId,url:t.videoUrl,videoId:t.videoId});var e=document.createElement("iframe");e.setAttribute("id",t.playerId),e.setAttribute("src","http://player.vimeo.com/video/"+t.videoId+"?api=1&player_id="+t.playerId),t.container.appendChild(e);var i=this.player=$f(e),n=this;i.addEvent("ready",function(){i.api("getDuration",function(t){n.set("duration",parseInt(1e3*t)),n._setState("ready"),n.trigger("ready")}),i.addEvent("playProgress",function(t){n.trigger("progress",{relativePosition:t.percent})}),i.addEvent("play",function(){n._setState("playing"),n.trigger("play")}),i.addEvent("pause",function(){n._setState("paused"),n.trigger("pause")}),i.addEvent("finish",function(){n.stop(),n._setState("finished"),n.trigger("finish")}),i.addEvent("playProgress",function(t){n.set("currentPosition",1e3*t.seconds),n.trigger("playProgress",{currentPosition:1e3*t.seconds,relativePosition:t.percent})})})},play:function(){this.player.api("play")},pause:function(){this.player.api("pause")},seekTo:function(t){this.player.api("seekTo",t/1e3),this._setCurrentPosition(t)},stop:function(){this.pause(),this.seek(0),this._setCurrentPosition(0),this._setState("stopped"),this.trigger("stop")},_fetchDetails:function(t){var e=this,i=new XMLHttpRequest;i.onload=function(i){var n=i.target.responseText;try{n=JSON.parse(n)[0]}catch(r){throw r}e.set("rawDetails",n);var s=/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/.exec(n.upload_date),o=new Date(s[1],s[2]-1,s[3],s[4],s[5],s[6]),a={title:n.title,duration:1e3*parseInt(n.duration),thumbnails:[{width:100,height:100,url:n.thumbnail_small},{width:200,height:200,url:n.thumbnail_medium},{width:640,height:640,url:n.thumbnail_large}],createdAt:o};e.set("details",a),t(a)},i.open("get","http://vimeo.com/api/v2/video/"+this.get("videoId")+".json",!0),i.send()}});var r={};r.Player=e,r.Playlist=i,t.PP=r}(window);