jquery.showinfo.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. var AIRTIME_API_VERSION = "1.1";
  2. (function($){
  3. $.fn.airtimeShowSchedule = function(options) {
  4. var defaults = {
  5. updatePeriod: 20, //seconds
  6. sourceDomain: "http://localhost/", //where to get show status from
  7. text: {onAirToday:"On air today"},
  8. showLimit: 5
  9. };
  10. options = $.extend(true, defaults, options);
  11. options.sourceDomain = addEndingBackslash(options.sourceDomain);
  12. return this.each(function() {
  13. var obj = $(this);
  14. var sd;
  15. getServerData();
  16. function updateWidget(){
  17. var currentShow = sd.getCurrentShow();
  18. var nextShows = sd.getNextShows();
  19. var shows = currentShow.length == 0 ? nextShows : currentShow.concat(nextShows);
  20. tableString = "";
  21. tableString += "<h3>" + options.text.onAirToday + "</h3>";
  22. tableString += "<table width='100%' border='0' cellspacing='0' cellpadding='0' class='widget widget now-playing-list small'>"+
  23. "<tbody>";
  24. for (var i=0; i<shows.length; i++){
  25. tableString +=
  26. "<tr>" +
  27. "<td class='time'>"+shows[i].getRange()+"</td>";
  28. var url = shows[i].getURL();
  29. if (url.length > 0) {
  30. tableString += "<td><a href='" + shows[i].getURL() + "'>" + shows[i].getName() + "</a></td></tr>";
  31. } else {
  32. tableString += "<td>" + shows[i].getName() + "</td></tr>";
  33. }
  34. }
  35. tableString += "</tbody></table>";
  36. obj.empty();
  37. obj.append(tableString);
  38. }
  39. function processData(data){
  40. checkWidgetVersion(data);
  41. sd = new ScheduleData(data);
  42. updateWidget();
  43. }
  44. function airtimeScheduleJsonpError(jqXHR, textStatus, errorThrown){
  45. }
  46. function getServerData(){
  47. $.ajax({url: options.sourceDomain + "api/live-info/",
  48. data: {type:"endofday",limit: options.showLimit},
  49. dataType: "jsonp",
  50. success:function(data) {
  51. processData(data);
  52. },
  53. error: airtimeScheduleJsonpError});
  54. setTimeout(getServerData, options.updatePeriod*1000);
  55. }
  56. });
  57. };
  58. })(jQuery);
  59. (function($){
  60. $.fn.airtimeLiveInfo = function(options) {
  61. var defaults = {
  62. updatePeriod: 5, //seconds
  63. sourceDomain: "http://localhost/", //where to get show status from
  64. text: {onAirNow:"On Air Now", offline:"Offline", current:"Current", next:"Next"}
  65. };
  66. options = $.extend(true, defaults, options);
  67. options.sourceDomain = addEndingBackslash(options.sourceDomain);
  68. return this.each(function() {
  69. var obj = $(this);
  70. var sd = null;
  71. getServerData();
  72. //refresh the UI to update the elapsed/remaining time
  73. setInterval(updateWidget, 1000);
  74. function updateWidget(){
  75. if (sd == null){
  76. return;
  77. }
  78. var currentShow = sd.getCurrentShow();
  79. var nextShows = sd.getNextShows();
  80. var showStatus = options.text.offline;
  81. var currentShowName = "";
  82. var timeElapsed = "";
  83. var timeRemaining = "";
  84. var nextShowName = "";
  85. var nextShowRange = "";
  86. if (currentShow.length > 0){
  87. showStatus = options.text.onAirNow;
  88. currentShowName = currentShow[0].getName();
  89. timeElapsed = sd.getShowTimeElapsed(currentShow[0]);
  90. timeRemaining = sd.getShowTimeRemaining(currentShow[0]);
  91. }
  92. if (nextShows.length > 0){
  93. nextShowName = nextShows[0].getName();
  94. nextShowRange = nextShows[0].getRange();
  95. }
  96. obj.empty();
  97. obj.append("<h4>"+showStatus+" &gt;&gt;</h4>");
  98. obj.append("<ul class='widget now-playing-bar'>" +
  99. "<li class='current'>"+options.text.current+": "+currentShowName+
  100. "<span id='time-elapsed' class='time-elapsed'>"+timeElapsed+"</span>" +
  101. "<span id='time-remaining' class='time-remaining'>"+timeRemaining+"</span>"+
  102. "</li>" +
  103. "<li class='next'>"+options.text.next+": "+nextShowName+"<span>"+nextShowRange+"</span></li>" +
  104. "</ul>");
  105. }
  106. function processData(data){
  107. checkWidgetVersion(data);
  108. sd = new ScheduleData(data);
  109. }
  110. function airtimeScheduleJsonpError(jqXHR, textStatus, errorThrown){
  111. }
  112. function getServerData(){
  113. $.ajax({url: options.sourceDomain + "api/live-info/",
  114. data: {type:"interval",limit:"5"},
  115. dataType: "jsonp",
  116. success: function(data) {
  117. processData(data);
  118. },
  119. error: airtimeScheduleJsonpError});
  120. setTimeout(getServerData, options.updatePeriod*1000);
  121. }
  122. });
  123. };
  124. })(jQuery);
  125. (function($){
  126. $.fn.airtimeLiveTrackInfo = function(options) {
  127. var defaults = {
  128. updatePeriod: 5, //seconds
  129. sourceDomain: "http://localhost/", //where to get show status from
  130. text: {onAirNow:"On Air Now", offline:"Offline", current:"Current", next:"Next"}
  131. };
  132. options = $.extend(true, defaults, options);
  133. options.sourceDomain = addEndingBackslash(options.sourceDomain);
  134. return this.each(function() {
  135. var obj = $(this);
  136. var sd = null;
  137. getServerData();
  138. //refresh the UI to update the elapsed/remaining time
  139. setInterval(updateWidget, 1000);
  140. function updateWidget(){
  141. if (sd == null){
  142. return;
  143. }
  144. var currentShow = sd.getCurrentShow();
  145. var nextShows = sd.getNextShows();
  146. var showStatus = options.text.offline;
  147. var currentShowName = "";
  148. var timeElapsed = "";
  149. var timeRemaining = "";
  150. var nextShowName = "";
  151. var nextShowRange = "";
  152. if (currentShow.length > 0){
  153. showStatus = options.text.onAirNow;
  154. currentShowName = currentShow[0].getName();
  155. timeElapsed = sd.getShowTimeElapsed(currentShow[0]);
  156. timeRemaining = sd.getShowTimeRemaining(currentShow[0]);
  157. }
  158. if (nextShows.length > 0){
  159. nextShowName = nextShows[0].getName();
  160. nextShowRange = nextShows[0].getRange();
  161. }
  162. obj.empty();
  163. obj.append("<span id='status-current-show' style='display:inline'>"+showStatus+" &gt;&gt;&nbsp;"+currentShowName+"</span>" +
  164. "<span class='current' id='time-elapsed' class='time-elapsed'>"+timeElapsed+"</span>" +
  165. "<span class='current' id='time-remaining' class='time-remaining'>"+timeRemaining+"</span>");
  166. obj.append("<ul class='widget now-playing-bar'>" +
  167. "<li class='current track-metadata'>"+options.text.current+": "+sd.currentTrack.getTitle()+"</li>" +
  168. "<li class='next track-metadata'>"+options.text.next+": "+sd.nextTrack.getTitle()+"</span></li>" +
  169. "</ul>");
  170. }
  171. function processData(data){
  172. checkWidgetVersion(data);
  173. sd = new ScheduleData(data);
  174. }
  175. function airtimeScheduleJsonpError(jqXHR, textStatus, errorThrown){
  176. }
  177. function getServerData(){
  178. $.ajax({url: options.sourceDomain + "api/live-info/",
  179. data: {type:"interval",limit:"5"},
  180. dataType: "jsonp",
  181. success: function(data) {
  182. processData(data);
  183. },
  184. error: airtimeScheduleJsonpError});
  185. setTimeout(getServerData, options.updatePeriod*1000);
  186. }
  187. });
  188. };
  189. })(jQuery);
  190. (function($){
  191. $.fn.airtimeWeekSchedule = function(options) {
  192. var defaults = {
  193. sourceDomain: "http://localhost/", //where to get show status from
  194. updatePeriod: 600,
  195. dowText:{monday:"Monday", tuesday:"Tuesday", wednesday:"Wednesday",thursday:"Thursday", friday:"Friday", saturday:"Saturday",sunday:"Sunday", nextmonday:"Next Monday", nexttuesday:"Next Tuesday",nextwednesday:"Next Wednesday", nextthursday:"Next Thursday",nextfriday:"Next Friday", nextsaturday:"Next Saturday", nextsunday:"NextSunday"},
  196. miscText: {time:"Time", programName:"Program Name", details:"Details", readMore:"Read More"}
  197. };
  198. options = $.extend(true, defaults, options);
  199. options.sourceDomain = addEndingBackslash(options.sourceDomain);
  200. return this.each(function() {
  201. var obj = $(this);
  202. obj.empty();
  203. obj.attr("class", "ui-tabs");
  204. var dow = ["monday", "tuesday", "wednesday", "thursday", "friday","saturday", "sunday", "nextmonday", "nexttuesday", "nextwednesday","nextthursday", "nextfriday", "nextsaturday", "nextsunday"];
  205. var date = new Date();
  206. //subtract 1 because javascript date function returns
  207. //sunday as 0 based, but we want Monday to be 0-based.
  208. var todayInt = (date.getDay()-1);
  209. if (todayInt < 0)
  210. todayInt += 7;
  211. var html = '<ul>';
  212. for (var i=0; i<dow.length; i++){
  213. html += '<li'+ (i==todayInt?' class="ui-tabs-selected ui-state-active"':'')+'><a href="#'+dow[i]+'">'+options.dowText[dow[i]]+'</a></li>';
  214. }
  215. html += '</ul>';
  216. for (var i=0; i<dow.length; i++){
  217. html += '<div id="'+dow[i]+'" class="ui-tabs-hide"></div>'
  218. }
  219. obj.append(html);
  220. getServerData();
  221. function updateWidget(data){
  222. for (var i=0; i<dow.length; i++){
  223. var html =
  224. '<table class="widget widget now-playing-list">'+
  225. '<colgroup>'+
  226. '<col width="150" />'+
  227. '<col width="350" />'+
  228. '<col width="240" />'+
  229. '</colgroup>'+
  230. '<thead>'+
  231. '<tr>'+
  232. '<td>'+options.miscText.time+'</td>'+
  233. '<td>'+options.miscText.programName+'</td>'+
  234. '<td>'+options.miscText.details+'</td>'+
  235. '</tr>'+
  236. '</thead>'+
  237. '<tfoot>'+
  238. '<tr>'+
  239. '<td></td>'+
  240. '</tr>'+
  241. '</tfoot>'+
  242. '<tbody>';
  243. var daySchedule = data[dow[i]];
  244. for (var j=0; j<daySchedule.length; j++){
  245. var url = daySchedule[j].url;
  246. html +=
  247. '<tr>'+
  248. '<td>'+getTime(daySchedule[j].start_timestamp)+ " - " + getTime(daySchedule[j].end_timestamp)+'</td>'+
  249. '<td>'+
  250. '<h4>'+daySchedule[j].name+'</h4>'+
  251. '</td>'+
  252. '<td>'+
  253. '<ul>'+
  254. '<li>'+(url.length > 0 ? '<a href="'+url+'">'+options.miscText.readMore+'</a>':'')+'</li>'+
  255. '</ul>'+
  256. '</td>'+
  257. '</tr>';
  258. }
  259. html +=
  260. '</tbody>'+
  261. '</table>';
  262. $("#"+dow[i]).empty();
  263. $("#"+dow[i]).append(html);
  264. }
  265. }
  266. function processData(data){
  267. checkWidgetVersion(data);
  268. updateWidget(data);
  269. }
  270. function airtimeScheduleJsonpError(jqXHR, textStatus, errorThrown){
  271. }
  272. function getServerData(){
  273. $.ajax({ url: options.sourceDomain + "api/week-info/", dataType:"jsonp", success:function(data){
  274. processData(data);
  275. }, error:airtimeScheduleJsonpError});
  276. setTimeout(getServerData, options.updatePeriod*1000);
  277. }
  278. });
  279. };
  280. })(jQuery);
  281. function addEndingBackslash(str){
  282. if (str.charAt(str.length-1) != '/')
  283. return str+'/';
  284. else return str;
  285. }
  286. /* ScheduleData class BEGIN */
  287. function ScheduleData(data){
  288. this.data = data;
  289. this.estimatedSchedulePosixTime;
  290. this.currentShow = new Array();
  291. if(data.currentShow != undefined) {
  292. for (var i=0; i< data.currentShow.length; i++){
  293. this.currentShow[i] = new Show(data.currentShow[i]);
  294. }
  295. }
  296. this.nextShows = new Array();
  297. if(data.nextShow != undefined) {
  298. for (var i=0; i< data.nextShow.length; i++) {
  299. this.nextShows[i] = new Show(data.nextShow[i]);
  300. }
  301. }
  302. this.currentTrack = new AudioTrack(data.current);
  303. this.nextTrack = new AudioTrack(data.next);
  304. this.schedulePosixTime = convertDateToPosixTime(data.schedulerTime);
  305. //this.schedulePosixTime += parseInt(data.timezoneOffset, 10)*1000;
  306. var date = new Date();
  307. this.localRemoteTimeOffset = date.getTime() - this.schedulePosixTime;
  308. }
  309. ScheduleData.prototype.secondsTimer = function(){
  310. var date = new Date();
  311. this.estimatedSchedulePosixTime = date.getTime() - this.localRemoteTimeOffset;
  312. }
  313. ScheduleData.prototype.getCurrentShow = function(){
  314. return this.currentShow;
  315. }
  316. ScheduleData.prototype.getNextShows = function() {
  317. return this.nextShows;
  318. }
  319. ScheduleData.prototype.getShowTimeElapsed = function(show) {
  320. this.secondsTimer();
  321. var showStart = convertDateToPosixTime(show.getStartTimestamp());
  322. return convertToHHMMSS(this.estimatedSchedulePosixTime - showStart);
  323. };
  324. ScheduleData.prototype.getShowTimeRemaining = function(show) {
  325. this.secondsTimer();
  326. var showEnd = convertDateToPosixTime(show.getEndTimestamp());
  327. return convertToHHMMSS(showEnd - this.estimatedSchedulePosixTime);
  328. };
  329. /* ScheduleData class END */
  330. /* Show class BEGIN */
  331. function Show(showData){
  332. this.showData = showData;
  333. }
  334. Show.prototype.getURL = function(){
  335. return this.showData.url;
  336. }
  337. Show.prototype.getName = function(){
  338. return this.showData.name;
  339. }
  340. Show.prototype.getRange = function(){
  341. return getTime(this.showData.start_timestamp) + " - " + getTime(this.showData.end_timestamp);
  342. }
  343. Show.prototype.getStartTimestamp = function(){
  344. return this.showData.start_timestamp;
  345. }
  346. Show.prototype.getEndTimestamp = function(){
  347. return this.showData.end_timestamp;
  348. }
  349. /* Show class END */
  350. /* AudioTrack class BEGINS */
  351. function AudioTrack(trackData){
  352. this.trackData = trackData;
  353. }
  354. AudioTrack.prototype.getTitle = function(){
  355. if (this.trackData === null) return "";
  356. return this.trackData.name;
  357. }
  358. /* AudioTrack class ENDS */
  359. function getTime(timestamp) {
  360. var time = timestamp.split(" ")[1].split(":");
  361. return time[0] + ":" + time[1];
  362. };
  363. /* Takes an input parameter of milliseconds and converts these into
  364. * the format HH:MM:SS */
  365. function convertToHHMMSS(timeInMS){
  366. var time = parseInt(timeInMS);
  367. var hours = parseInt(time / 3600000);
  368. time -= 3600000*hours;
  369. var minutes = parseInt(time / 60000);
  370. time -= 60000*minutes;
  371. var seconds = parseInt(time / 1000);
  372. hours = hours.toString();
  373. minutes = minutes.toString();
  374. seconds = seconds.toString();
  375. if (hours.length == 1)
  376. hours = "0" + hours;
  377. if (minutes.length == 1)
  378. minutes = "0" + minutes;
  379. if (seconds.length == 1)
  380. seconds = "0" + seconds;
  381. if (hours == "00")
  382. return minutes + ":" + seconds;
  383. else
  384. return hours + ":" + minutes + ":" + seconds;
  385. }
  386. /* Takes in a string of format similar to 2011-02-07 02:59:57,
  387. * and converts this to epoch/posix time. */
  388. function convertDateToPosixTime(s){
  389. var datetime = s.split(" ");
  390. var date = datetime[0].split("-");
  391. var time = datetime[1].split(":");
  392. var year = date[0];
  393. var month = date[1];
  394. var day = date[2];
  395. var hour = time[0];
  396. var minute = time[1];
  397. var sec = 0;
  398. var msec = 0;
  399. if (time[2].indexOf(".") != -1){
  400. var temp = time[2].split(".");
  401. sec = temp[0];
  402. msec = temp[1];
  403. } else
  404. sec = time[2];
  405. return Date.UTC(year, month-1, day, hour, minute, sec, msec);
  406. }
  407. /* Checks the incomming data's widget version tag.
  408. * The current widget version is 1.
  409. * -If the value returned is equal to 1 do nothing.
  410. * -If the value doesn't exist or it is great then 1 throw error warning the user they should upgrade their airtime install.
  411. * -If the value is less then 1 warn the user that they should upgrade the javascript to a newer version.
  412. */
  413. function checkWidgetVersion(data){
  414. var airtimeServerWidgetVersion = data['AIRTIME_API_VERSION'];
  415. if (undefined === airtimeServerWidgetVersion || airtimeServerWidgetVersion > AIRTIME_API_VERSION )
  416. throw "The version of widgets you are using is out of date with the Airtime installation, please update your widgets javascript file. (Airtime widget API version is "+airtimeServerWidgetVersion+", this widget's API version is "+AIRTIME_API_VERSION+")";
  417. else if (airtimeServerWidgetVersion < AIRTIME_API_VERSION )
  418. throw "The Airtime server has a different version than this widget supports. Please get the correct widget version for your Airtime installation. (Airtime widget API version is "+airtimeServerWidgetVersion+", this widget's API version is "+AIRTIME_API_VERSION+")";
  419. }