2013-04-16 10:30:04 -07:00
( function ( $ , document , window ) {
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
function reloadList ( page , updateInterval ) {
2013-02-20 18:33:05 -07:00
if ( updateInterval ) {
2013-04-16 10:30:04 -07:00
stopInterval ( ) ;
2013-02-20 18:33:05 -07:00
}
2013-11-29 09:58:24 -07:00
ApiClient . getScheduledTasks ( { isHidden : false } ) . done ( function ( tasks ) {
2013-04-16 10:30:04 -07:00
2013-04-16 19:56:16 -07:00
if ( updateInterval ) {
populateList ( page , tasks ) ;
} else {
updateTasks ( page , tasks ) ;
}
2013-02-20 18:33:05 -07:00
Dashboard . hideLoadingMsg ( ) ;
if ( updateInterval ) {
2013-04-16 10:30:04 -07:00
startInterval ( ) ;
2013-02-20 18:33:05 -07:00
}
} ) ;
2013-04-16 10:30:04 -07:00
}
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
function populateList ( page , tasks ) {
2013-02-20 18:33:05 -07:00
tasks = tasks . sort ( function ( a , b ) {
a = a . Category + " " + a . Name ;
b = b . Category + " " + b . Name ;
if ( a == b ) {
return 0 ;
}
if ( a < b ) {
return - 1 ;
}
return 1 ;
} ) ;
var html = "" ;
2013-04-09 20:06:54 -07:00
html += '<ul data-role="listview" data-inset="true" data-auto-enhanced="false" data-split-icon="Play">' ;
2013-02-20 18:33:05 -07:00
var currentCategory ;
for ( var i = 0 , length = tasks . length ; i < length ; i ++ ) {
var task = tasks [ i ] ;
if ( task . Category != currentCategory ) {
currentCategory = task . Category ;
html += "<li data-role='list-divider'>" + currentCategory + "</li>" ;
}
2013-04-16 20:05:09 -07:00
html += "<li title='" + task . Description + "'>" ;
2013-02-20 18:33:05 -07:00
2013-03-28 17:10:15 -07:00
html += "<a href='scheduledtask.html?id=" + task . Id + "'>" ;
2013-02-20 18:33:05 -07:00
html += "<h3>" + task . Name + "</h3>" ;
2013-04-16 10:30:04 -07:00
html += "<p id='" + task . Id + "'>" + getTaskProgressHtml ( task ) + "</p>" ;
2013-02-20 18:33:05 -07:00
if ( task . State == "Idle" ) {
2013-04-16 10:30:04 -07:00
html += "<a id='btnTask" + task . Id + "' class='btnStartTask' href='#' data-taskid='" + task . Id + "' data-icon='play'>Start</a>" ;
}
else if ( task . State == "Running" ) {
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
html += "<a id='btnTask" + task . Id + "' class='btnStopTask' href='#' data-taskid='" + task . Id + "' data-icon='stop'>Stop</a>" ;
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
} else {
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
html += "<a id='btnTask" + task . Id + "' class='btnStartTask' href='#' data-taskid='" + task . Id + "' data-icon='play' style='display:none;'>Start</a>" ;
}
html += "</a>" ;
html += "</li>" ;
}
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
html += "</ul>" ;
$ ( '#divScheduledTasks' , page ) . html ( html ) . trigger ( 'create' ) ;
}
function getTaskProgressHtml ( task ) {
var html = '' ;
if ( task . State == "Idle" ) {
if ( task . LastExecutionResult ) {
html += "Last ran " + humane _date ( task . LastExecutionResult . EndTimeUtc ) + ', taking ' + humane _elapsed ( task . LastExecutionResult . StartTimeUtc , task . LastExecutionResult . EndTimeUtc ) ;
if ( task . LastExecutionResult . Status == "Failed" ) {
html += " <span style='color:#FF0000;'>(failed)</span>" ;
}
else if ( task . LastExecutionResult . Status == "Cancelled" ) {
html += " <span style='color:#0026FF;'>(cancelled)</span>" ;
}
else if ( task . LastExecutionResult . Status == "Aborted" ) {
html += " <span style='color:#FF0000;'>(Aborted by server shutdown)</span>" ;
}
2013-02-20 18:33:05 -07:00
}
2013-04-16 10:30:04 -07:00
}
else if ( task . State == "Running" ) {
var progress = ( task . CurrentProgressPercentage || 0 ) . toFixed ( 1 ) ;
html += '<progress max="100" value="' + progress + '" title="' + progress + '%">' ;
html += '' + progress + '%' ;
html += '</progress>' ;
html += "<span style='color:#009F00;margin-left:5px;'>" + progress + "%</span>" ;
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
} else {
html += "<span style='color:#FF0000;'>Stopping</span>" ;
}
return html ;
}
function onWebSocketMessage ( e , msg ) {
if ( msg . MessageType == "ScheduledTasksInfo" ) {
var tasks = msg . Data ;
var page = $ . mobile . activePage ;
2013-04-16 19:56:16 -07:00
updateTasks ( page , tasks ) ;
}
}
2013-04-16 10:30:04 -07:00
2013-04-16 19:56:16 -07:00
function updateTasks ( page , tasks ) {
for ( var i = 0 , length = tasks . length ; i < length ; i ++ ) {
2013-04-16 10:30:04 -07:00
2013-04-16 19:56:16 -07:00
var task = tasks [ i ] ;
2013-04-16 10:30:04 -07:00
2013-04-16 19:56:16 -07:00
$ ( '#' + task . Id , page ) . html ( getTaskProgressHtml ( task ) ) ;
var btnTask = $ ( '#btnTask' + task . Id , page ) ;
updateTaskButton ( btnTask , task . State ) ;
2013-04-16 11:20:07 -07:00
}
}
2013-04-16 19:56:16 -07:00
2013-04-16 11:20:07 -07:00
function updateTaskButton ( btnTask , state ) {
2013-04-16 19:56:16 -07:00
var elem ;
2013-04-16 11:20:07 -07:00
if ( state == "Idle" ) {
2013-02-20 18:33:05 -07:00
2013-04-16 19:56:16 -07:00
elem = btnTask . addClass ( 'btnStartTask' ) . removeClass ( 'btnStopTask' ) . show ( ) . data ( "icon" , "play" ) . attr ( "title" , "Start" ) . buttonMarkup ( "refresh" ) ;
$ ( '.ui-icon-stop' , elem ) . removeClass ( 'ui-icon-stop' ) . addClass ( 'ui-icon-play' ) ;
2013-04-16 11:20:07 -07:00
}
else if ( state == "Running" ) {
2013-02-20 18:33:05 -07:00
2013-04-16 19:56:16 -07:00
elem = btnTask . addClass ( 'btnStopTask' ) . removeClass ( 'btnStartTask' ) . show ( ) . data ( "icon" , "stop" ) . attr ( "title" , "Stop" ) . buttonMarkup ( "refresh" ) ;
$ ( '.ui-icon-play' , elem ) . removeClass ( 'ui-icon-play' ) . addClass ( 'ui-icon-stop' ) ;
2013-02-20 18:33:05 -07:00
2013-04-16 11:20:07 -07:00
} else {
2013-02-20 18:33:05 -07:00
2013-04-16 19:56:16 -07:00
elem = btnTask . addClass ( 'btnStartTask' ) . removeClass ( 'btnStopTask' ) . hide ( ) . data ( "icon" , "play" ) . attr ( "title" , "Start" ) . buttonMarkup ( "refresh" ) ;
$ ( '.ui-icon-stop' , elem ) . removeClass ( 'ui-icon-stop' ) . addClass ( 'ui-icon-play' ) ;
2013-04-16 10:30:04 -07:00
}
}
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
function onWebSocketConnectionChange ( ) {
reloadList ( $ . mobile . activePage , true ) ;
}
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
function startInterval ( ) {
if ( ApiClient . isWebSocketOpen ( ) ) {
ApiClient . sendWebSocketMessage ( "ScheduledTasksInfoStart" , "1500,1500" ) ;
2013-02-20 18:33:05 -07:00
}
2013-04-16 10:30:04 -07:00
}
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
function stopInterval ( ) {
if ( ApiClient . isWebSocketOpen ( ) ) {
ApiClient . sendWebSocketMessage ( "ScheduledTasksInfoStop" ) ;
}
}
2013-04-09 20:06:54 -07:00
2013-04-16 10:30:04 -07:00
$ ( document ) . on ( 'pageshow' , "#scheduledTasksPage" , function ( ) {
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
var page = this ;
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
Dashboard . showLoadingMsg ( ) ;
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
reloadList ( page , true ) ;
$ ( ApiClient ) . on ( "websocketmessage" , onWebSocketMessage ) . on ( "websocketopen" , onWebSocketConnectionChange ) . on ( "websocketerror" , onWebSocketConnectionChange ) . on ( "websocketclose" , onWebSocketConnectionChange ) ;
$ ( '#divScheduledTasks' , page ) . on ( 'click' , '.btnStartTask' , function ( ) {
2013-04-16 11:20:07 -07:00
var button = this ;
var id = button . getAttribute ( 'data-taskid' ) ;
2013-04-16 10:30:04 -07:00
ApiClient . startScheduledTask ( id ) . done ( function ( ) {
2013-02-20 18:33:05 -07:00
2013-04-16 11:20:07 -07:00
updateTaskButton ( $ ( button ) , "Running" ) ;
2013-04-16 10:30:04 -07:00
reloadList ( page ) ;
} ) ;
2013-02-20 18:33:05 -07:00
2013-04-16 10:30:04 -07:00
} ) . on ( 'click' , '.btnStopTask' , function ( ) {
2013-02-20 18:33:05 -07:00
2013-04-16 11:20:07 -07:00
var button = this ;
var id = button . getAttribute ( 'data-taskid' ) ;
2013-04-16 10:30:04 -07:00
ApiClient . stopScheduledTask ( id ) . done ( function ( ) {
2013-02-20 18:33:05 -07:00
2013-04-16 11:20:07 -07:00
updateTaskButton ( $ ( button ) , "" ) ;
2013-04-16 10:30:04 -07:00
reloadList ( page ) ;
} ) ;
2013-02-20 18:33:05 -07:00
} ) ;
2013-04-16 10:30:04 -07:00
} ) . on ( 'pagehide' , "#scheduledTasksPage" , function ( ) {
var page = this ;
$ ( ApiClient ) . off ( "websocketmessage" , onWebSocketMessage ) . off ( "websocketopen" , onWebSocketConnectionChange ) . off ( "websocketerror" , onWebSocketConnectionChange ) . off ( "websocketclose" , onWebSocketConnectionChange ) ;
stopInterval ( ) ;
$ ( '#divScheduledTasks' , page ) . off ( 'click' , '.btnStartTask' ) . off ( 'click' , '.btnStopTask' ) ;
} ) ;
} ) ( jQuery , document , window ) ;