2012-07-12 01:31:24 +02:00
var AIRTIME _API _VERSION = "1.1" ;
2012-02-09 21:04:52 +01:00
2011-06-28 22:47:48 +02:00
( function ( $ ) {
$ . fn . airtimeShowSchedule = function ( options ) {
var defaults = {
updatePeriod : 20 , //seconds
sourceDomain : "http://localhost/" , //where to get show status from
2012-07-12 01:31:24 +02:00
text : { onAirToday : "On air today" } ,
showLimit : 5
2011-06-28 22:47:48 +02:00
} ;
2012-02-09 21:04:52 +01:00
options = $ . extend ( true , defaults , options ) ;
2011-06-28 22:47:48 +02:00
options . sourceDomain = addEndingBackslash ( options . sourceDomain ) ;
return this . each ( function ( ) {
var obj = $ ( this ) ;
var sd ;
getServerData ( ) ;
function updateWidget ( ) {
2011-11-23 18:00:54 +01:00
var currentShow = sd . getCurrentShow ( ) ;
var nextShows = sd . getNextShows ( ) ;
var shows = currentShow . length == 0 ? nextShows : currentShow . concat ( nextShows ) ;
2011-06-28 22:47:48 +02:00
tableString = "" ;
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
tableString += "<h3>" + options . text . onAirToday + "</h3>" ;
2012-02-08 20:57:48 +01:00
tableString += "<table width='100%' border='0' cellspacing='0' cellpadding='0' class='widget widget now-playing-list small'>" +
2011-06-28 22:47:48 +02:00
"<tbody>" ;
for ( var i = 0 ; i < shows . length ; i ++ ) {
tableString +=
"<tr>" +
"<td class='time'>" + shows [ i ] . getRange ( ) + "</td>" ;
var url = shows [ i ] . getURL ( ) ;
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
if ( url . length > 0 ) {
tableString += "<td><a href='" + shows [ i ] . getURL ( ) + "'>" + shows [ i ] . getName ( ) + "</a></td></tr>" ;
} else {
tableString += "<td>" + shows [ i ] . getName ( ) + "</td></tr>" ;
}
2011-06-28 22:47:48 +02:00
}
tableString += "</tbody></table>" ;
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
obj . empty ( ) ;
2011-06-28 22:47:48 +02:00
obj . append ( tableString ) ;
}
function processData ( data ) {
2012-02-08 20:57:48 +01:00
checkWidgetVersion ( data ) ;
2011-06-28 22:47:48 +02:00
sd = new ScheduleData ( data ) ;
updateWidget ( ) ;
}
function airtimeScheduleJsonpError ( jqXHR , textStatus , errorThrown ) {
}
function getServerData ( ) {
2011-11-24 19:54:58 +01:00
$ . ajax ( { url : options . sourceDomain + "api/live-info/" ,
2012-07-12 01:31:24 +02:00
data : { type : "endofday" , limit : options . showLimit } ,
2011-11-24 19:54:58 +01:00
dataType : "jsonp" ,
success : function ( data ) {
2011-06-28 22:47:48 +02:00
processData ( data ) ;
2011-11-24 19:54:58 +01:00
} ,
error : airtimeScheduleJsonpError } ) ;
2011-06-28 22:47:48 +02:00
setTimeout ( getServerData , options . updatePeriod * 1000 ) ;
}
} ) ;
} ;
} ) ( jQuery ) ;
( function ( $ ) {
$ . fn . airtimeLiveInfo = function ( options ) {
var defaults = {
updatePeriod : 5 , //seconds
sourceDomain : "http://localhost/" , //where to get show status from
text : { onAirNow : "On Air Now" , offline : "Offline" , current : "Current" , next : "Next" }
} ;
2011-06-29 00:53:02 +02:00
options = $ . extend ( true , defaults , options ) ;
2011-06-28 22:47:48 +02:00
options . sourceDomain = addEndingBackslash ( options . sourceDomain ) ;
return this . each ( function ( ) {
var obj = $ ( this ) ;
var sd = null ;
getServerData ( ) ;
//refresh the UI to update the elapsed/remaining time
setInterval ( updateWidget , 1000 ) ;
function updateWidget ( ) {
if ( sd == null ) {
return ;
}
var currentShow = sd . getCurrentShow ( ) ;
var nextShows = sd . getNextShows ( ) ;
var showStatus = options . text . offline ;
var currentShowName = "" ;
var timeElapsed = "" ;
var timeRemaining = "" ;
var nextShowName = "" ;
var nextShowRange = "" ;
if ( currentShow . length > 0 ) {
showStatus = options . text . onAirNow ;
currentShowName = currentShow [ 0 ] . getName ( ) ;
timeElapsed = sd . getShowTimeElapsed ( currentShow [ 0 ] ) ;
timeRemaining = sd . getShowTimeRemaining ( currentShow [ 0 ] ) ;
}
if ( nextShows . length > 0 ) {
nextShowName = nextShows [ 0 ] . getName ( ) ;
nextShowRange = nextShows [ 0 ] . getRange ( ) ;
}
obj . empty ( ) ;
obj . append ( "<h4>" + showStatus + " >></h4>" ) ;
2012-02-08 20:57:48 +01:00
obj . append ( "<ul class='widget now-playing-bar'>" +
2011-06-28 22:47:48 +02:00
"<li class='current'>" + options . text . current + ": " + currentShowName +
"<span id='time-elapsed' class='time-elapsed'>" + timeElapsed + "</span>" +
"<span id='time-remaining' class='time-remaining'>" + timeRemaining + "</span>" +
"</li>" +
"<li class='next'>" + options . text . next + ": " + nextShowName + "<span>" + nextShowRange + "</span></li>" +
"</ul>" ) ;
}
function processData ( data ) {
2012-02-08 20:57:48 +01:00
checkWidgetVersion ( data ) ;
2011-06-28 22:47:48 +02:00
sd = new ScheduleData ( data ) ;
}
function airtimeScheduleJsonpError ( jqXHR , textStatus , errorThrown ) {
}
function getServerData ( ) {
2011-11-24 19:54:58 +01:00
$ . ajax ( { url : options . sourceDomain + "api/live-info/" ,
data : { type : "interval" , limit : "5" } ,
dataType : "jsonp" ,
success : function ( data ) {
2011-06-28 22:47:48 +02:00
processData ( data ) ;
2011-11-24 19:54:58 +01:00
} ,
error : airtimeScheduleJsonpError } ) ;
2011-06-28 22:47:48 +02:00
setTimeout ( getServerData , options . updatePeriod * 1000 ) ;
}
} ) ;
} ;
} ) ( jQuery ) ;
( function ( $ ) {
$ . fn . airtimeWeekSchedule = function ( options ) {
var defaults = {
sourceDomain : "http://localhost/" , //where to get show status from
updatePeriod : 600 ,
dowText : { monday : "Monday" , tuesday : "Tuesday" , wednesday : "Wednesday" , thursday : "Thursday" , friday : "Friday" , saturday : "Saturday" , sunday : "Sunday" } ,
miscText : { time : "Time" , programName : "Program Name" , details : "Details" , readMore : "Read More" }
} ;
2012-02-09 21:04:52 +01:00
options = $ . extend ( true , defaults , options ) ;
2011-06-28 22:47:48 +02:00
options . sourceDomain = addEndingBackslash ( options . sourceDomain ) ;
return this . each ( function ( ) {
var obj = $ ( this ) ;
obj . empty ( ) ;
obj . attr ( "class" , "ui-tabs" ) ;
var dow = [ "monday" , "tuesday" , "wednesday" , "thursday" , "friday" , "saturday" , "sunday" ] ;
var date = new Date ( ) ;
//subtract 1 because javascript date function returns
//sunday as 0 based, but we want Monday to be 0-based.
var todayInt = ( date . getDay ( ) - 1 ) ;
if ( todayInt < 0 )
todayInt += 7 ;
var html = '<ul>' ;
for ( var i = 0 ; i < dow . length ; i ++ ) {
html += '<li' + ( i == todayInt ? ' class="ui-tabs-selected ui-state-active"' : '' ) + '><a href="#' + dow [ i ] + '">' + options . dowText [ dow [ i ] ] + '</a></li>' ;
}
html += '</ul>' ;
for ( var i = 0 ; i < dow . length ; i ++ ) {
html += '<div id="' + dow [ i ] + '" class="ui-tabs-hide"></div>'
}
obj . append ( html ) ;
getServerData ( ) ;
function updateWidget ( data ) {
for ( var i = 0 ; i < dow . length ; i ++ ) {
var html =
2012-02-08 20:57:48 +01:00
'<table class="widget widget now-playing-list">' +
2011-06-28 22:47:48 +02:00
'<colgroup>' +
'<col width="150" />' +
'<col width="350" />' +
'<col width="240" />' +
'</colgroup>' +
'<thead>' +
'<tr>' +
'<td>' + options . miscText . time + '</td>' +
'<td>' + options . miscText . programName + '</td>' +
'<td>' + options . miscText . details + '</td>' +
'</tr>' +
'</thead>' +
'<tfoot>' +
'<tr>' +
'<td></td>' +
'</tr>' +
'</tfoot>' +
'<tbody>' ;
var daySchedule = data [ dow [ i ] ] ;
for ( var j = 0 ; j < daySchedule . length ; j ++ ) {
var url = daySchedule [ j ] . url ;
html +=
'<tr>' +
2011-11-22 00:03:56 +01:00
'<td>' + getTime ( daySchedule [ j ] . start _timestamp ) + " - " + getTime ( daySchedule [ j ] . end _timestamp ) + '</td>' +
2011-06-28 22:47:48 +02:00
'<td>' +
2011-11-22 00:03:56 +01:00
'<h4>' + daySchedule [ j ] . name + '</h4>' +
2011-06-28 22:47:48 +02:00
'</td>' +
'<td>' +
'<ul>' +
'<li>' + ( url . length > 0 ? '<a href="' + url + '">' + options . miscText . readMore + '</a>' : '' ) + '</li>' +
'</ul>' +
'</td>' +
'</tr>' ;
}
html +=
'</tbody>' +
'</table>' ;
$ ( "#" + dow [ i ] ) . empty ( ) ;
$ ( "#" + dow [ i ] ) . append ( html ) ;
}
}
function processData ( data ) {
2012-02-08 20:57:48 +01:00
checkWidgetVersion ( data ) ;
2011-06-28 22:47:48 +02:00
updateWidget ( data ) ;
}
function airtimeScheduleJsonpError ( jqXHR , textStatus , errorThrown ) {
}
function getServerData ( ) {
$ . ajax ( { url : options . sourceDomain + "api/week-info/" , dataType : "jsonp" , success : function ( data ) {
processData ( data ) ;
} , error : airtimeScheduleJsonpError } ) ;
setTimeout ( getServerData , options . updatePeriod * 1000 ) ;
}
} ) ;
} ;
} ) ( jQuery ) ;
function addEndingBackslash ( str ) {
if ( str . charAt ( str . length - 1 ) != '/' )
return str + '/' ;
else return str ;
}
/* ScheduleData class BEGIN */
function ScheduleData ( data ) {
this . data = data ;
this . estimatedSchedulePosixTime ;
this . currentShow = new Array ( ) ;
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
if ( data . currentShow != undefined ) {
for ( var i = 0 ; i < data . currentShow . length ; i ++ ) {
this . currentShow [ i ] = new Show ( data . currentShow [ i ] ) ;
}
2011-06-28 22:47:48 +02:00
}
this . nextShows = new Array ( ) ;
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
if ( data . nextShow != undefined ) {
for ( var i = 0 ; i < data . nextShow . length ; i ++ ) {
this . nextShows [ i ] = new Show ( data . nextShow [ i ] ) ;
}
2011-06-28 22:47:48 +02:00
}
this . schedulePosixTime = convertDateToPosixTime ( data . schedulerTime ) ;
2012-05-15 20:20:53 +02:00
//this.schedulePosixTime += parseInt(data.timezoneOffset, 10)*1000;
2011-06-28 22:47:48 +02:00
var date = new Date ( ) ;
this . localRemoteTimeOffset = date . getTime ( ) - this . schedulePosixTime ;
}
ScheduleData . prototype . secondsTimer = function ( ) {
var date = new Date ( ) ;
this . estimatedSchedulePosixTime = date . getTime ( ) - this . localRemoteTimeOffset ;
}
ScheduleData . prototype . getCurrentShow = function ( ) {
return this . currentShow ;
}
ScheduleData . prototype . getNextShows = function ( ) {
return this . nextShows ;
}
ScheduleData . prototype . getShowTimeElapsed = function ( show ) {
this . secondsTimer ( ) ;
var showStart = convertDateToPosixTime ( show . getStartTimestamp ( ) ) ;
return convertToHHMMSS ( this . estimatedSchedulePosixTime - showStart ) ;
} ;
ScheduleData . prototype . getShowTimeRemaining = function ( show ) {
this . secondsTimer ( ) ;
var showEnd = convertDateToPosixTime ( show . getEndTimestamp ( ) ) ;
return convertToHHMMSS ( showEnd - this . estimatedSchedulePosixTime ) ;
} ;
/* ScheduleData class END */
/* Show class BEGIN */
function Show ( showData ) {
this . showData = showData ;
}
Show . prototype . getURL = function ( ) {
return this . showData . url ;
}
Show . prototype . getName = function ( ) {
return this . showData . name ;
}
Show . prototype . getRange = function ( ) {
return getTime ( this . showData . start _timestamp ) + " - " + getTime ( this . showData . end _timestamp ) ;
}
Show . prototype . getStartTimestamp = function ( ) {
return this . showData . start _timestamp ;
}
Show . prototype . getEndTimestamp = function ( ) {
return this . showData . end _timestamp ;
}
/* Show class END */
function getTime ( timestamp ) {
var time = timestamp . split ( " " ) [ 1 ] . split ( ":" ) ;
return time [ 0 ] + ":" + time [ 1 ] ;
} ;
/ * T a k e s a n i n p u t p a r a m e t e r o f m i l l i s e c o n d s a n d c o n v e r t s t h e s e i n t o
* the format HH : MM : SS * /
function convertToHHMMSS ( timeInMS ) {
var time = parseInt ( timeInMS ) ;
var hours = parseInt ( time / 3600000 ) ;
time -= 3600000 * hours ;
var minutes = parseInt ( time / 60000 ) ;
time -= 60000 * minutes ;
var seconds = parseInt ( time / 1000 ) ;
hours = hours . toString ( ) ;
minutes = minutes . toString ( ) ;
seconds = seconds . toString ( ) ;
if ( hours . length == 1 )
hours = "0" + hours ;
if ( minutes . length == 1 )
minutes = "0" + minutes ;
if ( seconds . length == 1 )
seconds = "0" + seconds ;
if ( hours == "00" )
return minutes + ":" + seconds ;
else
return hours + ":" + minutes + ":" + seconds ;
}
/ * T a k e s i n a s t r i n g o f f o r m a t s i m i l a r t o 2 0 1 1 - 0 2 - 0 7 0 2 : 5 9 : 5 7 ,
* and converts this to epoch / posix time . * /
function convertDateToPosixTime ( s ) {
var datetime = s . split ( " " ) ;
var date = datetime [ 0 ] . split ( "-" ) ;
var time = datetime [ 1 ] . split ( ":" ) ;
var year = date [ 0 ] ;
var month = date [ 1 ] ;
var day = date [ 2 ] ;
var hour = time [ 0 ] ;
var minute = time [ 1 ] ;
var sec = 0 ;
var msec = 0 ;
if ( time [ 2 ] . indexOf ( "." ) != - 1 ) {
var temp = time [ 2 ] . split ( "." ) ;
sec = temp [ 0 ] ;
msec = temp [ 1 ] ;
} else
sec = time [ 2 ] ;
return Date . UTC ( year , month - 1 , day , hour , minute , sec , msec ) ;
}
2012-02-08 20:57:48 +01:00
/ * C h e c k s t h e i n c o m m i n g d a t a ' s w i d g e t v e r s i o n t a g .
* The current widget version is 1.
* - If the value returned is equal to 1 do nothing .
* - If the value doesn ' t exist or it is great then 1 throw error warning the user they should upgrade their airtime install .
* - If the value is less then 1 warn the user that they should upgrade the javascript to a newer version .
* /
function checkWidgetVersion ( data ) {
2012-02-09 21:04:52 +01:00
var airtimeServerWidgetVersion = data [ 'AIRTIME_API_VERSION' ] ;
2012-02-08 21:34:14 +01:00
2012-02-09 21:04:52 +01:00
if ( undefined === airtimeServerWidgetVersion || airtimeServerWidgetVersion > AIRTIME _API _VERSION )
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 + ")" ;
else if ( airtimeServerWidgetVersion < AIRTIME _API _VERSION )
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 + ")" ;
2012-02-08 20:57:48 +01:00
}