track dirty state in editor for onbeforeunload

Min RK 12 years ago
parent 0efd335553
commit 776c9a0316

@ -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");
});

@ -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?";
}
};
});

Loading…
Cancel
Save