Commit 56aae606 authored by ThorbenQuast's avatar ThorbenQuast
Browse files

editor: read-only mode, fix #1969

parent 4d9f7723
...@@ -20,6 +20,7 @@ var CodeEditor = Emitter.extend({ ...@@ -20,6 +20,7 @@ var CodeEditor = Emitter.extend({
this.node = null; this.node = null;
this.autosaveTimeOut = null; this.autosaveTimeOut = null;
this.writable = true; //default case = new file case
}, },
setup: function(node) { setup: function(node) {
...@@ -114,10 +115,11 @@ var CodeEditor = Emitter.extend({ ...@@ -114,10 +115,11 @@ var CodeEditor = Emitter.extend({
self.view.close(); self.view.close();
} }
self.path = path; self.path = path;
self.setContent(res.content); self.writable = res.writable;
self.mtime = res.mtime; self.mtime = res.mtime;
self.setContent(res.content);
self.lastContent = self.getContent(); self.lastContent = self.getContent();
self.setMode(path); self.setMode();
self.goToTop(); self.goToTop();
self.checkModifications(); self.checkModifications();
self.setupAutoSave(); self.setupAutoSave();
...@@ -173,7 +175,7 @@ var CodeEditor = Emitter.extend({ ...@@ -173,7 +175,7 @@ var CodeEditor = Emitter.extend({
if (isModified != this.view.isModified()) { if (isModified != this.view.isModified()) {
this.view.setModified(isModified); this.view.setModified(isModified);
} }
this.view.toggleMenuEntry("save", isModified && this.path); this.view.toggleMenuEntry("save", isModified && this.path && this.writable);
return this; return this;
}, },
...@@ -230,20 +232,12 @@ var CodeEditor = Emitter.extend({ ...@@ -230,20 +232,12 @@ var CodeEditor = Emitter.extend({
var msg = "<html>File extension '" + fileExtension + "' is not supported! <br /> Save anyway?</html>"; var msg = "<html>File extension '" + fileExtension + "' is not supported! <br /> Save anyway?</html>";
self.view.confirm(msg, function(confirmed) { self.view.confirm(msg, function(confirmed) {
if (confirmed) { if (confirmed) {
self.path = path; self.save(callback, "new", path);
self.view.preview.setPath(path, true);
self.save(null, "new");
if ($.isFunction(callback))
callback();
} }
}); });
} else { } else {
self.path = path;
self.view.preview.setPath(path, true);
self.view.output.setCommandLine(); self.view.output.setCommandLine();
self.save(null, "new"); self.save(callback, "new", path);
if ($.isFunction(callback))
callback();
} }
}, },
sort: ["name", "ext"], sort: ["name", "ext"],
...@@ -254,7 +248,7 @@ var CodeEditor = Emitter.extend({ ...@@ -254,7 +248,7 @@ var CodeEditor = Emitter.extend({
return this; return this;
}, },
save: function(callback, option) { save: function(callback, option, path) {
var self = this; var self = this;
this.setupAutoSave(); this.setupAutoSave();
...@@ -264,7 +258,14 @@ var CodeEditor = Emitter.extend({ ...@@ -264,7 +258,14 @@ var CodeEditor = Emitter.extend({
return this; return this;
} }
if (!this.path) { if (!this.writable && option != "new") {
console.log("No writing access!");
if ($.isFunction(callback))
callback();
return this;
}
if (!this.path && !path) {
this.saveAs(callback); this.saveAs(callback);
return this; return this;
} }
...@@ -273,17 +274,24 @@ var CodeEditor = Emitter.extend({ ...@@ -273,17 +274,24 @@ var CodeEditor = Emitter.extend({
this.saving_processing = true; this.saving_processing = true;
this.view.POST(vispa.url.dynamic("/ajax/fs/save_file"), { this.view.POST(vispa.url.dynamic("/ajax/fs/save_file"), {
path: this.path, path: path ? path : this.path,
content: this.getContent(), content: this.getContent(),
utf8: true, utf8: true,
watch_id: "code" watch_id: "code"
}).done(function(res) { }).done(function(res) {
self.mtime = res.mtime; if (!res.success) {
self.lastContent = self.getContent(); self.view.alert("Saving not possible. Please check file permissions.");
self.checkModifications(); } else {
self.setMode(); self.mtime = res.mtime;
if ($.isFunction(callback)) self.path = res.path;
callback(); self.writable = true;
self.lastContent = self.getContent();
self.checkModifications();
self.setMode();
self.view.preview.setPath(self.path, true);
if ($.isFunction(callback))
callback();
}
}).always(function() { }).always(function() {
self.view.setLoading(false); self.view.setLoading(false);
self.saving_processing = false; self.saving_processing = false;
...@@ -312,7 +320,12 @@ var CodeEditor = Emitter.extend({ ...@@ -312,7 +320,12 @@ var CodeEditor = Emitter.extend({
if (path == null) if (path == null)
return this; return this;
this.view.setLabel(path, true); var label = path;
if (!this.writable)
label = label + " Read-Only";
this.view.setLabel(label, true);
var fileExtension = path.split(".").pop().toLowerCase(); var fileExtension = path.split(".").pop().toLowerCase();
if (!~this.view._extension.fileExtensions.indexOf(fileExtension)) { if (!~this.view._extension.fileExtensions.indexOf(fileExtension)) {
...@@ -436,5 +449,5 @@ var CodeEditor = Emitter.extend({ ...@@ -436,5 +449,5 @@ var CodeEditor = Emitter.extend({
} }
return mode; return mode;
} }
}); });
\ No newline at end of file
...@@ -185,13 +185,17 @@ var CodeEditorView = vispa.ExtensionView.Center.extend({ ...@@ -185,13 +185,17 @@ var CodeEditorView = vispa.ExtensionView.Center.extend({
var doAsk = !this.forceClose && this.isModified(); var doAsk = !this.forceClose && this.isModified();
if (doAsk) if (doAsk)
this.confirm("Save changes before closing?", function(b) { this.confirm("Save changes before closing?", function(b) {
self.forceClose = true; var after_save = function() {
if (b) self.forceClose = true;
self.editor.save(function() { cleanUp();
cleanUp(); self.close(); //close the editor after saving
self.close(); //close the editor after saving }
}); if (b) {
else if (self.editor.writable)
self.editor.save(after_save);
else
self.editor.saveAs(after_save);
} else
self.close(); self.close();
}); });
else else
......
...@@ -385,8 +385,9 @@ class FileSystem(object): ...@@ -385,8 +385,9 @@ class FileSystem(object):
return json.dumps({ return json.dumps({
"mtime": os.path.getmtime(path), "mtime": os.path.getmtime(path),
"success": mtime > 0, "success": mtime > 0 and self.checkPermissions(path), #save is not successful, if file not writable
"watch_error": watch_error "watch_error": watch_error,
"path": path
}) })
def get_file(self, path, binary=False, def get_file(self, path, binary=False,
...@@ -404,18 +405,26 @@ class FileSystem(object): ...@@ -404,18 +405,26 @@ class FileSystem(object):
content = f.read() content = f.read()
if utf8: if utf8:
content = content.decode('utf8') content = content.decode('utf8')
#new: check for writing rights
writable = self.checkPermissions(path)
mtime = os.path.getmtime(path) mtime = os.path.getmtime(path)
except Exception as e: except Exception as e:
mtime = 0 mtime = 0
content = "" content = ""
writable = None
return json.dumps({ return json.dumps({
"content": content, "content": content,
"mtime": mtime, "mtime": mtime,
"success": mtime > 0, "success": mtime > 0,
"watch_error": watch_error "watch_error": watch_error,
"writable": writable
}) })
def checkPermissions(self, path):
return os.access(path, os.W_OK)
def save_file_content(self, filename, content, def save_file_content(self, filename, content,
path=None, force=True, append=False): path=None, force=True, append=False):
# check write permissions # check write permissions
......
Supports Markdown
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