Commit 849d8d42 authored by ThorbenQuast's avatar ThorbenQuast
Browse files

editor: store last command in cookies (one cookie per workspace)

parent c8742a62
......@@ -17,6 +17,10 @@ var CommandLine = Emitter.extend({
this.commands = [];
this.defaultCommand = null;
this.lastCommand = null;
this.cookieName = null;
this.commandCookie = null;
},
setup: function(node) {
......@@ -33,7 +37,54 @@ var CommandLine = Emitter.extend({
trigger: "focus"
});
this.histNode = rightTopNode.find(".dropdown-menu");
this.setDefault();
this.cookieName = "commands-ws" + String(this.view.getWorkspaceId());
this.getCookie(function() {
self.setLastCommandFromCookie();
self.setDefault();
self.renderHistory();
});
},
getCookie: function(callback) {
this.commandCookie = $.cookie("commands");
if (!this.commandCookie) //if no cookie has been set yet --> make a new one
this.commandCookie = {};
else
this.commandCookie = JSON.parse(this.commandCookie);
if ($.isFunction(callback))
callback();
},
setLastCommandFromCookie: function() {
var key = this.getCookieKey();
if (!key)
return;
var last = this.commandCookie[key];
if (!last)
return;
this.pushLast(last);
},
pushLast: function(last) {
if (!last)
return;
this.lastCommand = {
cmd: last
};
},
saveToCookie: function(cmd) {
var key = this.getCookieKey();
this.commandCookie[key] = cmd;
$.cookie("commands", JSON.stringify(this.commandCookie));
},
//key == file name
getCookieKey: function() {
var key = this.view.editor.path;
return key ? key.split("/").pop() : null;
},
setDefault: function() {
......@@ -44,7 +95,7 @@ var CommandLine = Emitter.extend({
for (var i in this.commandlineTemplates) {
if (~this.commandlineTemplates[i].indexOf(fileExtension)) {
if (this.commands.length === 0)
this.commandline.val(i);
this.setVal(i);
this.pushDefault(i);
return true;
}
......@@ -59,7 +110,6 @@ var CommandLine = Emitter.extend({
this.defaultCommand = {
cmd: cmd
};
this.renderHistory();
},
pushEntry: function(cmd) {
......@@ -74,8 +124,6 @@ var CommandLine = Emitter.extend({
name: cmd,
cmd: cmd
});
this.renderHistory();
},
checkExistence: function(cmd) {
......@@ -89,14 +137,17 @@ var CommandLine = Emitter.extend({
renderHistory: function() {
var self = this;
this.histNode.html("");
$.each(this.commands, function(i, entry) {
self.histNode.append(self.createNode(entry.name, entry.cmd));
});
this.histNode.append($("<li/>").addClass("divider"));
//append the default and last commands
if (this.defaultCommand !== null)
this.histNode.append(self.createNode("default", this.defaultCommand.cmd));
if (this.lastCommand !== null)
this.histNode.append(self.createNode("last", this.lastCommand.cmd));
},
createNode: function(name, cmd) {
......@@ -120,6 +171,26 @@ var CommandLine = Emitter.extend({
getVal: function() {
return this.commandline.val();
},
setVal: function(cmd) {
this.commandline.val(cmd);
},
handleExecution: function() {
var cmd = this.getVal();
if (!cmd)
return false;
this.saveToCookie(cmd); //save the last command in a cookie
this.pushLast(cmd); //ID for matching is the set path of the file
this.pushEntry(cmd);
this.renderHistory();
return true;
},
handleSave: function() {
this.setDefault();
this.renderHistory();
}
});
\ No newline at end of file
......@@ -346,7 +346,7 @@ var CodeEditor = Emitter.extend({
self.lastContent = self.getContent();
self.checkModifications();
self.setMode();
self.view.commandLine.setDefault();
self.view.commandLine.handleSave();
self.view.preview.setPath(self.path, true);
if ($.isFunction(callback))
callback();
......
......@@ -111,13 +111,13 @@ var CodeEditorOutput = Emitter.extend({
return this;
}
var cmd = self.view.commandLine.getVal();
if (!cmd) {
//check the command and process the history
if (!this.view.commandLine.handleExecution()) {
this.view.alert("Please indicate a valid command for script execution!");
return this;
}
this.view.commandLine.pushEntry(cmd);
var cmd = this.view.commandLine.getVal();
this.view.editor.save(function() {
// create the command
//store the command
......
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