Skip to content

Commit d468081

Browse files
committed
added documentation, and fixed reload button
1 parent 8c90419 commit d468081

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

notebook/static/edit/js/editor.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ function(
198198
}
199199
};
200200

201+
/**
202+
* Rename the file.
203+
* @param {string} new_name
204+
* @return {Promise} promise that resolves when the file is renamed.
205+
*/
201206
Editor.prototype.rename = function (new_name) {
202207
/** rename the file */
203208
var that = this;
@@ -214,6 +219,13 @@ function(
214219
);
215220
};
216221

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+
*/
217229
Editor.prototype.save = function (check_last_modified) {
218230
/** save the file */
219231
if (!this.save_enabled) {
@@ -249,22 +261,28 @@ function(
249261
});
250262
}
251263

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+
*/
253270
if (check_last_modified) {
254271
return this.contents.get(that.file_path, {content: false}).then(
255-
function (data) {
272+
function check_if_modified(data) {
256273
var last_modified = new Date(data.last_modified);
257274
// We want to check last_modified (disk) > that.last_modified (our last save)
258275
// In some cases the filesystem reports an inconsistent time,
259276
// so we allow 0.5 seconds difference before complaining.
260-
console.log(that.last_modified);
261277
if ((last_modified.getTime() - that.last_modified.getTime()) > 500) { // 500 ms
262278
console.warn("Last saving was done on `"+that.last_modified+"`("+that._last_modified+"), "+
263279
"while the current file seem to have been saved on `"+data.last_modified+"`");
264280
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
267283
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+
268286
// redisplay existing dialog
269287
that._changed_on_disk_dialog.modal('show');
270288
} else {
@@ -278,7 +296,7 @@ function(
278296
buttons: {
279297
Reload: {
280298
class: 'btn-warning',
281-
click: function() {
299+
click: function () {
282300
window.location.reload();
283301
}
284302
},
@@ -298,7 +316,6 @@ function(
298316
}, function (error) {
299317
console.log(error);
300318
// maybe it has been deleted or renamed? Go ahead and save.
301-
// (need to test this)
302319
return _save();
303320
})
304321
} else {

0 commit comments

Comments
 (0)