2011-04-06 23:13:00 +08:00
/ * *
* Copyright ( C ) 2011 Instructure , Inc .
*
* This file is part of Canvas .
*
* Canvas is free software : you can redistribute it and / or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation , version 3 of the License .
*
* Canvas is distributed in the hope that it will be useful , but WITHOUT ANY
* WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE . See the GNU Affero General Public License for more
* details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* /
2012-03-06 01:37:09 +08:00
define ( [
2011-11-11 00:31:45 +08:00
'i18n!content_exports' ,
'jquery' /* $ */ ,
'jquery.ajaxJSON' /* ajaxJSON */ ,
'jquery.instructure_forms' /* formSubmit */ ,
'jqueryui/progressbar' /* /\.progressbar/ */
] , function ( I18n , $ ) {
2011-04-06 23:13:00 +08:00
$ ( document ) . ready ( function ( event ) {
var state = 'nothing' ;
var current _id = null ;
2011-11-11 00:31:45 +08:00
2011-04-06 23:13:00 +08:00
function startPoll ( ) {
2011-06-17 02:30:59 +08:00
$ ( "#exporter_form" ) . html ( I18n . t ( 'messages.processing' , "Processing" ) + "<div style='font-size: 0.8em;'>" + I18n . t ( 'messages.this_may_take_a_bit' , "this may take a bit..." ) + "</div>" )
2011-04-06 23:13:00 +08:00
. attr ( 'disabled' , true ) ;
$ ( ".instruction" ) . hide ( ) ;
$ ( ".progress_bar_holder" ) . slideDown ( ) ;
$ ( ".export_progress" ) . progressbar ( ) ;
state = "nothing" ;
var fakeTickCount = 0 ;
var tick = function ( ) {
if ( state == "nothing" ) {
fakeTickCount ++ ;
var progress = ( $ ( ".export_progress" ) . progressbar ( 'option' , 'value' ) || 0 ) + 0.25 ;
if ( fakeTickCount < 10 ) {
$ ( ".export_progress" ) . progressbar ( 'option' , 'value' , progress ) ;
}
setTimeout ( tick , 2000 ) ;
} else {
state = "nothing" ;
fakeTickCount = 0 ;
setTimeout ( tick , 10000 ) ;
}
} ;
var checkup = function ( ) {
var lastProgress = null ;
var waitTime = 1500 ;
$ . ajaxJSON ( location . href + "/" + current _id , 'GET' , { } , function ( data ) {
state = "updating" ;
var content _export = data . content _export ;
var progress = 0 ;
if ( content _export ) {
progress = Math . max ( $ ( ".export_progress" ) . progressbar ( 'option' , 'value' ) || 0 , content _export . progress ) ;
$ ( ".export_progress" ) . progressbar ( 'option' , 'value' , progress ) ;
}
if ( content _export . workflow _state == 'exported' ) {
$ ( "#exporter_form" ) . hide ( ) ;
$ ( ".export_progress" ) . progressbar ( 'option' , 'value' , 100 ) ;
$ ( ".progress_message" ) . html ( "The course has been exported." ) ;
2011-10-25 02:08:05 +08:00
$ ( "#exports" ) . append ( '<p>' + I18n . t ( 'labels.new_course_export' , "New Course Export:" ) + ' <a href="' + content _export . download _url + '">' + I18n . t ( 'links.download_plain' , "Click here to download" ) + '</a> </p>' )
2011-04-06 23:13:00 +08:00
} else if ( content _export . workflow _state == 'failed' ) {
code = "content_export_" + content _export . id ;
$ ( ".progress_bar_holder" ) . hide ( ) ;
$ ( "#exporter_form" ) . hide ( ) ;
2011-06-17 02:30:59 +08:00
var message = I18n . t ( 'errors.error' , "There was an error exporting your course. Please notify your system administrator and give them the following export identifier: \"%{code}\"" , { code : code } ) ;
2011-04-06 23:13:00 +08:00
$ ( ".export_messages .error_message" ) . html ( message ) ;
$ ( ".export_messages" ) . show ( ) ;
} else {
if ( progress == lastProgress ) {
waitTime = Math . max ( waitTime + 500 , 30000 ) ;
} else {
waitTime = 1500 ;
}
lastProgress = progress ;
setTimeout ( checkup , 1500 ) ;
}
} , function ( ) {
setTimeout ( checkup , 3000 ) ;
} ) ;
} ;
setTimeout ( checkup , 2000 ) ;
setTimeout ( tick , 1000 )
}
$ ( "#exporter_form" ) . formSubmit ( {
success : function ( data ) {
if ( data && data . content _export ) {
current _id = data . content _export . id
startPoll ( ) ;
} else {
//show error message
$ ( ".export_messages .error_message" ) . text ( data . error _message ) ;
$ ( ".export_messages" ) . show ( ) ;
}
} ,
error : function ( data ) {
2011-06-17 02:30:59 +08:00
$ ( this ) . find ( ".submit_button" ) . attr ( 'disabled' , false ) . text ( I18n . t ( 'buttons.process' , "Process Data" ) ) ;
2011-04-06 23:13:00 +08:00
}
} ) ;
function check _if _exporting ( ) {
//state = "checking";
if ( $ ( '#current_export_id' ) . size ( ) ) {
//state = "nothing";
current _id = $ ( '#current_export_id' ) . text ( )
startPoll ( ) ;
}
}
check _if _exporting ( ) ;
} ) ;
2011-06-17 02:30:59 +08:00
} ) ;