@@ -198,6 +198,11 @@ function(
198
198
}
199
199
} ;
200
200
201
+ /**
202
+ * Rename the file.
203
+ * @param {string } new_name
204
+ * @return {Promise } promise that resolves when the file is renamed.
205
+ */
201
206
Editor . prototype . rename = function ( new_name ) {
202
207
/** rename the file */
203
208
var that = this ;
@@ -214,6 +219,13 @@ function(
214
219
) ;
215
220
} ;
216
221
222
+
223
+ /**
224
+ * Save this file on the server.
225
+ *
226
+ * @param {boolean } check_last_modified - checks if file has been modified on disk
227
+ * @return {Promise } - promise that resolves when the notebook is saved.
228
+ */
217
229
Editor . prototype . save = function ( check_last_modified ) {
218
230
/** save the file */
219
231
if ( ! this . save_enabled ) {
@@ -249,22 +261,28 @@ function(
249
261
} ) ;
250
262
}
251
263
252
- // check data after
264
+ /*
265
+ * Gets the current working file, and checks if the file has been modified on disk. If so, it
266
+ * creates & opens a modal that issues the user a warning and prompts them to overwrite the file.
267
+ *
268
+ * If it can't get the working file, it builds a new file and saves.
269
+ */
253
270
if ( check_last_modified ) {
254
271
return this . contents . get ( that . file_path , { content : false } ) . then (
255
- function ( data ) {
272
+ function check_if_modified ( data ) {
256
273
var last_modified = new Date ( data . last_modified ) ;
257
274
// We want to check last_modified (disk) > that.last_modified (our last save)
258
275
// In some cases the filesystem reports an inconsistent time,
259
276
// so we allow 0.5 seconds difference before complaining.
260
- console . log ( that . last_modified ) ;
261
277
if ( ( last_modified . getTime ( ) - that . last_modified . getTime ( ) ) > 500 ) { // 500 ms
262
278
console . warn ( "Last saving was done on `" + that . last_modified + "`(" + that . _last_modified + "), " +
263
279
"while the current file seem to have been saved on `" + data . last_modified + "`" ) ;
264
280
if ( that . _changed_on_disk_dialog !== null ) {
265
- console . log ( "Showing the modal" ) ;
266
- // update save callback on the confirmation button
281
+ // since the modal's event bindings are removed when destroyed,
282
+ // we reinstate save & reload callbacks on the confirmation & reload buttons
267
283
that . _changed_on_disk_dialog . find ( '.save-confirm-btn' ) . click ( _save ) ;
284
+ that . _changed_on_disk_dialog . find ( '.btn-warning' ) . click ( function ( ) { window . location . reload ( ) } ) ;
285
+
268
286
// redisplay existing dialog
269
287
that . _changed_on_disk_dialog . modal ( 'show' ) ;
270
288
} else {
@@ -278,7 +296,7 @@ function(
278
296
buttons : {
279
297
Reload : {
280
298
class : 'btn-warning' ,
281
- click : function ( ) {
299
+ click : function ( ) {
282
300
window . location . reload ( ) ;
283
301
}
284
302
} ,
@@ -298,7 +316,6 @@ function(
298
316
} , function ( error ) {
299
317
console . log ( error ) ;
300
318
// maybe it has been deleted or renamed? Go ahead and save.
301
- // (need to test this)
302
319
return _save ( ) ;
303
320
} )
304
321
} else {
0 commit comments