-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjQuery.JamCity.min.js
More file actions
9 lines (9 loc) · 4.87 KB
/
jQuery.JamCity.min.js
File metadata and controls
9 lines (9 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
/**
*
* jquery.JamCity : v. 2.0.0
* https://github.com/bluetidepro/jquery.JamCity
* Copyright (c) 2012 Zach Reed (http://iamzachreed.com).
* MIT licensed
*
*/
(function(e,t,n,r){"use strict";function o(t,n){this.element=t;this.settings=e.extend({},s,n);this._defaults=s;this._name=i;this.init()}var i="JamCity",s={apiKey:"",username:"",fetch:5,contentType:"recentTracks",artSize:"medium",cssTooltips:true,cssThemeClass:null,_blankLinks:true,refreshResults:true,refreshResultsInterval:6e5,noAlbumArtImg:"http://placehold.it/250x250&text=No Art",noResults:"Sorry, nothing was found...",songsFetched:function(){},htmlBuilt:function(){}};o.prototype={init:function(){var e=this.settings,t=this.element;this.validate(e);this.fetchSongs(t,e)},validate:function(e){if(!e.apiKey){this.debug("Please enter a valid API key.");return false}if(!e.username){this.debug("Please enter a valid username.");return false}return true},buildLastfmUrl:function(e){var t="http://ws.audioscrobbler.com/2.0/?method=user.get"+e.contentType+"&user="+e.username+"&api_key="+e.apiKey+"&limit="+e.fetch+"&format=json";return t},buildInfoUrl:function(e,t,n){var r="http://ws.audioscrobbler.com/2.0/?method="+e+".getinfo&api_key="+t.apiKey+"&mbid="+n+"&format=json";return r},fetchSongs:function(t,n){var r=this,i={recentTracks:"track",lovedTracks:"track",topAlbums:"album",topTracks:"track",topArtists:"artist",weeklyAlbumChart:"album",weeklyArtistChart:"artist",weeklyTrackChart:"track"},s=[],o=i[n.contentType],u=n.contentType.toLowerCase(),a="http://ws.audioscrobbler.com/2.0/?method=user.get"+n.contentType+"&user="+n.username+"&api_key="+n.apiKey+"&limit="+n.fetch+"&format=json";e.ajax({url:a,type:"GET",context:this,async:false,error:function(){},dataType:"json",success:function(i){if(i[u]&&i[u].total==="0"){s.push("jamCityError")}else{var a=i[u][o],f=a.length-1;e(a).each(function(e,t){var i=r.templateData(n,t);s.push(i);if(i.item_nowplaying){f=f-1}if(e===f){return false}})}if(e.isFunction(n.songsFetched)){n.songsFetched.call(this);r.buildHTML(s,t,n)}if(n.refreshResults){if(n.refreshResultsInterval<1e4){n.refreshResultsInterval=1e4}setTimeout(function(){e(t).find("#jamCityWrapper").remove();r.fetchSongs(t,n)},n.refreshResultsInterval)}}})},templateData:function(t,n){var r=[];r.item=n;r.item_url=n.url;r.item_mbid=n.mbid;r.item_nowplaying=null;r.item_playcount=null;switch(t.contentType){case"recentTracks":if(n["@attr"]){r.item_nowplaying=n["@attr"]["nowplaying"]}r.item_name="'"+n.name+"' by "+n.artist["#text"];r.item_album=n.album;r.item_image=n.image;break;case"lovedTracks":r.item_name="'"+n.name+"' by "+n.artist.name;r.item_album=n.album;r.item_image=n.image;break;case"topAlbums":r.item_name="'"+n.name+"' by "+n.artist.name;r.item_playcount=n.playcount;r.item_image=n.image;break;case"topTracks":r.item_playcount=n.playcount;r.item_image=n.image;r.item_name="'"+n.name+"' by "+n.artist.name;break;case"topArtists":r.item_image=n.image;r.item_name=n.name;r.item_playcount=n.playcount;break;case"weeklyAlbumChart":r.item_name="'"+n.name+"' by "+n.artist["#text"];r.item_playcount=n.playcount;if(r.item_mbid){var i=this.buildInfoUrl("album",t,r.item_mbid);e.ajax({url:i,type:"GET",async:false,dataType:"json",success:function(e){r.item_image=e.album.image}})}break;case"weeklyArtistChart":r.item_playcount=n.playcount;if(r.item_mbid){var s=this.buildInfoUrl("artist",t,r.item_mbid);e.ajax({url:s,type:"GET",async:false,dataType:"json",success:function(e){r.item_image=e.artist.image}})}r.item_name=n.name;break;case"weeklyTrackChart":r.item_image=n.image;r.item_name="'"+n.name+"' by "+n.artist["#text"];r.item_playcount=n.playcount;break;default:}return r},buildHTML:function(t,n,r){var i=this,s=null;e(n).append('<ul id="jamCityWrapper" class="'+r.cssThemeClass+" jamCitySize_"+r.artSize+'"></ul>');if(t=="jamCityError"){e(n).find("#jamCityWrapper").addClass("jamCityError").append("<li>"+r.noResults+"</li>")}else{e.each(t,function(t,i){if(i.item_image){if(i.item_image[4]){s=i.item_image[4]["#text"]}else if(i.item_image[3]){s=i.item_image[3]["#text"]}else if(i.item_image[2]){s=i.item_image[2]["#text"]}else if(i.item_image[1]){s=i.item_image[1]["#text"]}else{s=r.noAlbumArtImg}}else{s=r.noAlbumArtImg}e(n).find("#jamCityWrapper").append('<li style="z-index: '+(500-t)+'">'+'<a href="'+i.item_url+'" '+(r.cssTooltips&&i.item_name?'data-tip="'+i.item_name+(i.item_playcount?" ("+i.item_playcount+" plays)":"")+'"':"")+'class="case'+(i.item_nowplaying?" nowplaying":"")+'"'+(r._blankLinks?' target="_blank"':"")+">"+'<div class="overlay"></div>'+'<div class="cover"><img src="'+s+'"/></div>'+'<div class="vinyl"></div>'+"</a>"+"</li>");if(t===r.fetch-1){return false}})}if(e.isFunction(r.htmlBuilt)){r.htmlBuilt.call(this)}},debug:function(e){if(typeof console=="object"){console.log(e)}else if(typeof opera=="object"){opera.postError(e)}}};e.fn[i]=function(t){return this.each(function(){if(!e.data(this,"plugin_"+i)){e.data(this,"plugin_"+i,new o(this,t))}})}})(jQuery,window,document)