Commit 88238713 authored by ThorbenQuast's avatar ThorbenQuast
Browse files

editor: fix#2189 & show saveAs prompt properly in OnBeforeClose

parent d249f31d
define(["jquery", "emitter"], function($, Emitter) {
var CodeEditorActions = Emitter._extend({
init: function init(editor, view) {
init._super.call(this);
var self = this;
this.editor = editor;
this.view = view;
this.mtime = null;
this.savingProcessing = false;
this.lastContent = "";
this.modifiedProcessing = false;
this.autosaveTimeOut = null;
},
watchSetup: function() {
var self = this;
this.view.onSocket("watch", function(data) {
if (data.watch_id != "code") {
return;
......@@ -58,20 +58,22 @@ define(["jquery", "emitter"], function($, Emitter) {
}
});
},
loadContent: function(path) {
var self = this;
path = path || this.editor.path;
if (!path) {
this.checkModifications();
return this;
}
this.view.setLoading(true);
var initContent = function(res) {
var setContent = function(res, reset) {
reset = reset === undefined ? false : true;
if (!res.success) {
self.view.alert("<html>An internal server error has occured: <br />" +
"" + res.error + " <br />" +
......@@ -83,10 +85,11 @@ define(["jquery", "emitter"], function($, Emitter) {
self.view.setState("path", path);
self.mtime = res.mtime;
self.editor.setContent(res.content);
self.lastContent = self.editor.getContent();
if (!reset)
self.lastContent = self.editor.getContent();
self.checkModifications();
self.editor.setMode();
self.editor.goToTop();
self.checkModifications();
self.removeAutoSaveFile();
self.setupAutoSave();
self.view.emit("stateChange", "line");
......@@ -100,16 +103,15 @@ define(["jquery", "emitter"], function($, Emitter) {
if (res.success) { //an autosave file exists
self.view.confirm("An autosave file exists. Do you want to load its content?",
function(b) {
if (b) {
initContent(res);
self.view.setModified(true);
} else
if (b)
setContent(res, true);
else
self.view.POST("/ajax/fs/get_file", {
path: path,
utf8: true,
watch_id: "code"
}).done(function(res) {
initContent(res);
setContent(res);
}).fail(function() {
self.view.setLoading(false);
});
......@@ -120,7 +122,7 @@ define(["jquery", "emitter"], function($, Emitter) {
utf8: true,
watch_id: "code"
}).done(function(res) {
initContent(res);
setContent(res);
}).fail(function() {
self.view.setLoading(false);
});
......@@ -130,10 +132,10 @@ define(["jquery", "emitter"], function($, Emitter) {
});
return this;
},
setupAutoSave: function() {
var self = this;
clearTimeout(this.autosaveTimeOut);
this.autosaveTimeOut = null;
......@@ -141,7 +143,7 @@ define(["jquery", "emitter"], function($, Emitter) {
t = t * 1000.0;
if (t == 0)
return this;
this.autosaveTimeOut = setTimeout(function() {
if (self.editor.path && self.view.getState("writable") && self.view.isModified()) {
self.savingProcessing = true;
......@@ -158,25 +160,25 @@ define(["jquery", "emitter"], function($, Emitter) {
self.setupAutoSave();
}, t);
},
checkModifications: function() {
clearTimeout(this.editor.keyupMinTimeout);
clearTimeout(this.editor.keyupMaxTimeout);
this.editor.keyupMinTimeout = null;
this.editor.keyupMaxTimeout = null;
var isModified = this.editor.getContent() != this.lastContent;
if (isModified != this.view.isModified()) {
this.view.setModified(isModified);
}
this.toggleSaveButton(isModified, this.editor.path, this.view.getState("writable"));
return this;
},
toggleSaveButton: function(isModified, hasPath, writable) {
this.view.toggleMenuEntry("save", isModified && hasPath && writable);
var btn = this.view._getFastMenuEntryNode("save");
if (btn) {
var msg = "";
......@@ -193,10 +195,10 @@ define(["jquery", "emitter"], function($, Emitter) {
}
return this;
},
saveAs: function(callback) {
var self = this;
// get the default path for the file selector
var path = null;
if (this.editor.path) {
......@@ -204,65 +206,73 @@ define(["jquery", "emitter"], function($, Emitter) {
parts.pop();
path = parts.join("/");
}
//file selection with selector
var args = {
"path": path,
callback: function(path) {
self.view.POST("/ajax/fs/exists", {
path: path
}).done(function(selectedType) {
//check if saving is possible
var fileExtension = path.split(".").pop().toLowerCase();
var unsupportedExtension = !~self.view._extension.fileExtensions.indexOf(fileExtension);
var existingFile = selectedType == "f";
var msg = "";
if (existingFile && unsupportedExtension)
msg = "<html>File extension '" + fileExtension +
"' of existing file is not supported! <br />" +
" Save and overwrite anyway?</html>";
else if (unsupportedExtension)
msg = "<html>File extension '" + fileExtension +
"' is not supported! <br /> Save anyway?</html>";
else if (existingFile)
msg = "<html> File already exists. <br />" +
"Do you want to overwrite it ? </html>";
self.view.confirm(msg, function(confirmed) {
if (confirmed)
self.view.POST("/ajax/fs/exists", {
path: path
}).done(function(selectedType) {
//check if saving is possible
var fileExtension = path.split(".").pop().toLowerCase();
var unsupportedExtension = !~self.view._extension.fileExtensions.indexOf(fileExtension);
var existingFile = selectedType == "f";
//case: no problems
if (!(existingFile || unsupportedExtension)){
self.save(callback, "new", path);
else
return;
}
//case: problems-->addtional confirm
var msg = "";
if (existingFile && unsupportedExtension)
msg = "<html>File extension '" + fileExtension +
"' of existing file is not supported! <br />" +
" Save and overwrite anyway?</html>";
else if (unsupportedExtension)
msg = "<html>File extension '" + fileExtension +
"' is not supported! <br /> Save anyway?</html>";
else if (existingFile)
msg = "<html> File already exists. <br />" +
"Do you want to overwrite it ? </html>";
self.view.confirm(msg, function(confirmed) {
if (confirmed)
self.save(callback, "new", path);
else
return;
});
});
});
},
sort: "name",
reverse: false
},
sort: "name",
reverse: false
};
this.view.spawnInstance("file", "FileSelector", args);
return this;
},
save: function(callback, option, path) {
var self = this;
var isNew = option == "new";
this.setupAutoSave();
this.removeAutoSaveFile();
if (this.editor.path && !this.view.isModified() && !isNew) {
if ($.isFunction(callback))
callback();
return this;
}
if (!this.view.getState("writable") && !isNew) {
console.log("No writing access!");
if ($.isFunction(callback))
callback();
return this;
}
if (!this.editor.path && !path) {
//a new file must be given a default command
var extendedCB = function() {
......@@ -271,12 +281,12 @@ define(["jquery", "emitter"], function($, Emitter) {
self.editor.view.commandLine.setDefault();
};
this.saveAs(extendedCB);
return this;
return this;
}
this.view.setLoading(true);
this.savingProcessing = true;
this.view.POST("/ajax/fs/save_file", {
path: path !== undefined ? path : this.editor.path,
content: this.editor.getContent(),
......@@ -292,11 +302,11 @@ define(["jquery", "emitter"], function($, Emitter) {
self.view.setState("path", res.path);
self.lastContent = self.editor.getContent();
self.checkModifications();
if (isNew) {
self.editor.manualSyntax = false;
self.view.preview.setPath(self.editor.path, true);
}
self.editor.setMode();
if (isNew) {
self.editor.manualSyntax = false;
self.view.preview.setPath(self.editor.path, true);
}
self.editor.setMode();
if ($.isFunction(callback))
callback();
}
......@@ -304,10 +314,10 @@ define(["jquery", "emitter"], function($, Emitter) {
self.view.setLoading(false);
self.savingProcessing = false;
});
return this;
},
giveAutoSavePath: function() {
if (!this.editor.path)
return "";
......@@ -315,17 +325,17 @@ define(["jquery", "emitter"], function($, Emitter) {
var parts = path.split("/");
var file = parts.pop();
path = parts.join("/") + "/." + file + "~";
return path;
},
removeAutoSaveFile: function() {
this.view.GET("/ajax/fs/remove", {
"path": JSON.stringify(this.giveAutoSavePath())
//syntax of path required for the fs remove function
});
}
});
return CodeEditorActions;
......
......@@ -215,7 +215,7 @@ define([
self.close(); //close the editor after saving
};
if (b) {
if (self.editor.writable)
if (self.getState("writable"))
self.editor.actions.save(afterSave);
else
self.editor.actions.saveAs(afterSave);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment