diff --git a/IPython/html/static/edit/js/editor.js b/IPython/html/static/edit/js/editor.js
index 9026ab5..24a52de 100644
--- a/IPython/html/static/edit/js/editor.js
+++ b/IPython/html/static/edit/js/editor.js
@@ -31,6 +31,7 @@ function($,
this.file_path = options.file_path;
this.config = options.config;
this.codemirror = new CodeMirror($(this.selector)[0]);
+ this.generation = -1;
// It appears we have to set commands on the CodeMirror class, not the
// instance. I'd like to be wrong, but since there should only be one CM
@@ -79,6 +80,7 @@ function($,
cm.setOption('mode', mode);
});
that.save_enabled = true;
+ that.generation = cm.changeGeneration();
},
function(error) {
cm.setValue("Error! " + error.message +
@@ -101,6 +103,8 @@ function($,
content: this.codemirror.getValue(),
};
var that = this;
+ // record change generation for isClean
+ this.generation = this.codemirror.changeGeneration();
return this.contents.save(this.file_path, model).then(function() {
that.events.trigger("save_succeeded.TextEditor");
});
diff --git a/IPython/html/static/edit/js/main.js b/IPython/html/static/edit/js/main.js
index e172d5f..2bcaf6a 100644
--- a/IPython/html/static/edit/js/main.js
+++ b/IPython/html/static/edit/js/main.js
@@ -19,7 +19,7 @@ require([
events,
contents,
configmod,
- editor,
+ editmod,
menubar,
notificationarea
){
@@ -28,10 +28,10 @@ require([
var base_url = utils.get_body_data('baseUrl');
var file_path = utils.get_body_data('filePath');
contents = new contents.Contents({base_url: base_url});
- var config = new configmod.ConfigSection('edit', {base_url: base_url})
+ var config = new configmod.ConfigSection('edit', {base_url: base_url});
config.load();
- var editor = new editor.Editor('#texteditor-container', {
+ var editor = new editmod.Editor('#texteditor-container', {
base_url: base_url,
events: events,
contents: contents,
@@ -63,4 +63,11 @@ require([
});
editor.load();
page.show();
+
+ window.onbeforeunload = function () {
+ if (!editor.codemirror.isClean(editor.generation)) {
+ return "Unsaved changes will be lost. Close anyway?";
+ }
+ };
+
});