-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrhapsody.min.js
executable file
·1 lines (1 loc) · 4.87 KB
/
rhapsody.min.js
1
!function(a,b,c){"use strict";if(a&&b&&b.ajax&&c){var d=c.stringify;c.stringify=function(a){return d(a,function(a,b){return"genre"===a?{id:b.id,name:b.name}:b})};var e=function(a,b,c){var d=a[b];a[b]=function(){return c.length===arguments.length?c.apply(this,arguments):"function"==typeof d?d.apply(this,arguments):void 0}},f="rhapsody.member.accessToken",g="rhapsody.member.refreshToken",h=function(a){for(var b in a)this[b]=a[b]},i={init:function(a){this.api.consumerKey=a.consumerKey,this.api.version=a.version||this.api.version,this.api.catalog=a.catalog||this.api.catalog;var c=a.player||"player-frame";if(c&&"string"==typeof c){var d=this,e=b("#"+c);if(0===e.length)b(function(){var e=b("<iframe></iframe>").attr("id",c).attr("name",c).attr("src","http://api.rhapsody.com/v1.1/player/index.html?apikey="+a.consumerKey).attr("frameborder","no").attr("style","display:none;").appendTo(b(document.body)).load(function(){d.player.win=e.get(0)})});else{if(!(e.get(0)instanceof HTMLIFrameElement))throw new Error('The element "'+c+'" is not an HTMLIFrameElement.');d.player.win=e.get(0)}}},api:{host:"api.rhapsody.com",catalog:"US",version:"v2.1",endpoint:function(a){return(a?"https://":"http://")+[this.host,this.version].join("/")},headers:function(a){var b={};return a&&i.member.accessToken&&(b.Authorization="Bearer "+i.member.accessToken),b},dataType:function(){return"json"},get:function(a,c,d){var e={apikey:this.consumerKey};b.ajax({type:"GET",dataType:this.dataType(),data:e,headers:this.headers(a),url:this.endpoint(a)+c,success:function(a,b,c){d(a)},error:function(a){d({status:a.status,error:a.statusText,response:a.responseJSON})}})},post:function(a,c,d,e){d||(d={}),b.ajax({type:d._method||"POST",data:d,dataType:this.dataType(),headers:this.headers(a),url:this.endpoint(a)+c+(a?"":"?apikey="+this.consumerKey),success:function(a,b,c){e(a)},error:function(a){e({status:a.status,error:a.statusText,response:a.responseJSON})}})},put:function(a,b,c,d){c._method="PUT",this.post.call(this,a,b,c,d)},del:function(a,b,c,d){c._method="DELETE",this.post.call(this,a,b,c,d)}},member:new function(){var b=new h({accessToken:a.localStorage[f],refreshToken:a.localStorage[g]});return b},player:{frameReady:!1,ready:!1,auth:function(){i.api.consumerKey&&i.member.accessToken&&i.windows(this.win).post("auth",{consumerKey:i.api.consumerKey,accessToken:i.member.accessToken})},play:function(a){return i.previewer.pause(),i.windows(this.win).post("play",a),this},pause:function(){return i.windows(this.win).post("pause"),this},next:function(){i.windows(this.win).post("playNext")},previous:function(){i.windows(this.win).post("playPrevious")},queue:function(a){return i.windows(this.win).post("queue",a),this},clearQueue:function(){i.windows(this.win).post("clearQueue")},toggleShuffle:function(){i.windows(this.win).post("toggleShuffle")},toggleRepeat:function(){i.windows(this.win).post("toggleRepeat")},seek:function(a){i.windows(this.win).post("seek",a)},setVolume:function(a){i.windows(this.win).post("setVolume",a)},on:function(a,b){var c=this;return window.addEventListener("message",function(d){if("playerframeready"===d.data.type?c.frameReady=!0:"ready"===d.data.type?c.ready=!0:"playsessionexpired"===d.data.type&&(c.paused=!1,c.playing=!1),c.frameReady&&c.ready&&!c.authed&&(c.authed=!0,c.auth()),d.data.type===a){if(d.data.data&&d.data.data.id){d.data.data.id=d.data.data.id.replace("tra","Tra");var e=d.data.data.code,f="PlayStarted"===e||"PlayComplete"!==e&&"Paused"!==e&&"BufferEmpty"!==e&&"NetworkDropped"!==e&&"PlayInterrupted"!==e&&"IdleTimeout"!==e,g="Paused"===e||"NetworkDropped"===e||"PlayInterrupted"===e||"IdleTimeout"===e;c.playing=d.data.data.playing=f,c.paused=d.data.data.paused=g,c.currentTrack=c.playing||c.paused?d.data.data.id:null}b.call(this,d.data)}}),this}},previewer:{play:function(){return this},pause:function(){return this}},windows:function(a){return{post:function(b,c){if(!a)throw new Error("An iframe was not found at that reference.");a.contentWindow.postMessage({method:b,args:i.util.jsonClean(c||{})},"*")}}},on:function(a,b){window.addEventListener(a,b)},util:{secondsToTime:function(a){if(!isNaN(a)){var b=Math.floor(a/60),c=Math.floor(a)%60;return b+":"+(10>c?"0"+c:c)}return"0:00"},jsonClean:function(a){return c.parse(c.stringify(a,function(a,b){return"genre"===a?{id:b.id,name:b.name}:b}))}}};e(h.prototype,"set",function(b){b&&b.accessToken&&b.refreshToken&&(this.accessToken=a.localStorage[f]=b.accessToken,this.refreshToken=a.localStorage[g]=b.refreshToken,i.player.auth())}),e(h.prototype,"unset",function(){this.accessToken=this.refreshToken=null,a.localStorage.removeItem(f),a.localStorage.removeItem(g)}),e(h.prototype,"load",function(){return this.accessToken=a.localStorage[f],this.refreshToken=a.localStorage[g],this}),e(h.prototype,"signedIn",function(){return null!=this.accessToken&&null!=this.refreshToken}),i.player.on("playevent",function(a){}).on("playtimer",function(a){}),a.Rhapsody=i,a.Member=h}}(window,jQuery,JSON);