22 * Created by bmiller on 3/19/15.
33 */
44
5- function RunestoneBase ( ) {
6-
7- }
8-
9- RunestoneBase . prototype . logBookEvent = function ( info ) {
10- console . log ( "logging event " + this . divid ) ;
11- } ;
12-
13- RunestoneBase . prototype . logRunEvent = function ( info ) {
14- console . log ( "running " + this . divid ) ;
15- } ;
165
176var edList = { } ;
187
@@ -230,6 +219,13 @@ ActiveCode.prototype.createOutput = function () {
230219
231220} ;
232221
222+ ActiveCode . prototype . disableSaveLoad = function ( ) {
223+ $ ( this . saveButton ) . addClass ( 'disabled' ) ;
224+ $ ( this . saveButton ) . attr ( 'title' , 'Login to save your code' ) ;
225+ $ ( this . loadButton ) . addClass ( 'disabled' ) ;
226+ $ ( this . loadButton ) . attr ( 'title' , 'Login to load your code' ) ;
227+ } ;
228+
233229ActiveCode . prototype . addCaption = function ( ) {
234230 //someElement.parentNode.insertBefore(newElement, someElement.nextSibling);
235231 var capDiv = document . createElement ( 'p' ) ;
@@ -242,10 +238,82 @@ ActiveCode.prototype.addCaption = function() {
242238
243239ActiveCode . prototype . saveEditor = function ( ) {
244240
241+ var saveSuccess = function ( data , status , whatever ) {
242+ if ( data . redirect ) {
243+ alert ( "Did not save! It appears you are not logged in properly" )
244+ } else if ( data == "" ) {
245+ alert ( "Error: Program not saved" ) ;
246+ }
247+ else {
248+ var acid = eval ( data ) [ 0 ] ;
249+ if ( acid . indexOf ( "ERROR:" ) == 0 ) {
250+ alert ( acid ) ;
251+ } else {
252+ // use a tooltip to provide some success feedback
253+ var save_btn = $ ( "#" + acid + "_saveb" ) ;
254+ save_btn . attr ( 'title' , 'Saved your code.' ) ;
255+ opts = {
256+ 'trigger' : 'manual' ,
257+ 'placement' : 'bottom' ,
258+ 'delay' : { show : 100 , hide : 500 }
259+ } ;
260+ save_btn . tooltip ( opts ) ;
261+ save_btn . tooltip ( 'show' ) ;
262+ setTimeout ( function ( ) {
263+ save_btn . tooltip ( 'destroy' )
264+ } , 4000 ) ;
265+
266+ $ ( '#' + acid + ' .CodeMirror' ) . css ( 'border-top' , '2px solid #aaa' ) ;
267+ $ ( '#' + acid + ' .CodeMirror' ) . css ( 'border-bottom' , '2px solid #aaa' ) ;
268+ }
269+ }
270+ } ;
271+
272+ var data = { acid : this . divid , code : this . editor . getValue ( ) } ;
273+ data . lang = this . language ;
274+ $ ( document ) . ajaxError ( function ( e , jqhxr , settings , exception ) {
275+ alert ( "Request Failed for" + settings . url )
276+ } ) ;
277+ jQuery . post ( eBookConfig . ajaxURL + 'saveprog' , data , saveSuccess ) ;
278+ if ( this . editor . acEditEvent ) {
279+ logBookEvent ( { 'event' : 'activecode' , 'act' : 'edit' , 'div_id' : this . divid } ) ; // Log the run event
280+ this . editor . acEditEvent = false ;
281+ }
282+ logBookEvent ( { 'event' : 'activecode' , 'act' : 'save' , 'div_id' : this . divid } ) ; // Log the run event
283+
245284} ;
246285
247286ActiveCode . prototype . loadEditor = function ( ) {
248287
288+ var loadEditor = ( function ( data , status , whatever ) {
289+ // function called when contents of database are returned successfully
290+ var res = eval ( data ) [ 0 ] ;
291+
292+ if ( res . source ) {
293+ this . editor . setValue ( res . source ) ;
294+ this . loadButton . tooltip ( { 'placement' : 'bottom' ,
295+ 'title' : "Loaded your saved code." ,
296+ 'trigger' : 'manual'
297+ } ) ;
298+ } else {
299+ this . loadButton . tooltip ( { 'placement' : 'bottom' ,
300+ 'title' : "No saved code." ,
301+ 'trigger' : 'manual'
302+ } ) ;
303+ }
304+ this . loadButton . tooltip ( 'show' ) ;
305+ setTimeout ( function ( ) {
306+ this . loadButton . tooltip ( 'destroy' )
307+ } , 4000 ) ;
308+ } ) . bind ( this ) ;
309+
310+ var data = { acid : this . divid } ;
311+ if ( sid !== undefined ) {
312+ data [ 'sid' ] = sid ;
313+ }
314+ logBookEvent ( { 'event' : 'activecode' , 'act' : 'load' , 'div_id' : this . divid } ) ; // Log the run event
315+ jQuery . get ( eBookConfig . ajaxURL + 'getprog' , data , loadEditor ) ;
316+
249317} ;
250318
251319ActiveCode . prototype . createGradeSummary = function ( ) {
@@ -1280,5 +1348,13 @@ $(document).ready(function() {
12801348 edList [ this . id ] = new ActiveCode ( { 'orig' : this } ) ;
12811349 }
12821350 } ) ;
1283- } ) ;
1351+ if ( loggedout ) {
1352+ for ( k in edList ) {
1353+ edList [ k ] . disableSaveLoad ( ) ;
1354+ }
1355+ }
1356+
1357+ } ) ;
12841358
1359+ var loggedout ;
1360+ $ ( document ) . bind ( "runestone:logout" , function ( ) { loggedout = true ; } )
0 commit comments