Commit 7162d3d9 authored by Benjamin Fischer's avatar Benjamin Fischer
Browse files

Reworked all code using the return values of .GET, .POST and .getTemplate as promises.

The server responses and their JS-side handling can still be missmatching.
parent 5665783e
......@@ -99,7 +99,11 @@ define(["jquery", "emitter"], function($, Emitter) {
this.view.POST("/ajax/fs/get_file", {
path: self.giveAutoSavePath(),
utf8: true
}).done(function(res) {
}, function(err, res) {
if (err) {
self.view.setLoading(false);
return;
}
if (res.success) { //an autosave file exists
self.view.confirm("An autosave file exists. Do you want to load its content?",
function(b) {
......@@ -110,10 +114,12 @@ define(["jquery", "emitter"], function($, Emitter) {
path: path,
utf8: true,
watch_id: "code"
}).done(function(res) {
}, function(err, res) {
if (err) {
self.view.setLoading(false);
return;
}
setContent(res);
}).fail(function() {
self.view.setLoading(false);
});
});
} else {
......@@ -121,14 +127,14 @@ define(["jquery", "emitter"], function($, Emitter) {
path: path,
utf8: true,
watch_id: "code"
}).done(function(res) {
}, function(err, res) {
if (err) {
self.view.setLoading(false);
return;
}
setContent(res);
}).fail(function() {
self.view.setLoading(false);
});
}
}).fail(function() {
self.view.setLoading(false);
});
return this;
},
......@@ -151,7 +157,8 @@ define(["jquery", "emitter"], function($, Emitter) {
path: self.giveAutoSavePath(),
content: self.editor.getContent(),
utf8: true
}).done(function(res) {
}, function(err, res) {
if (err) return;
self.savingProcessing = false;
self.mtime = res.mtime;
self.setupAutoSave();
......@@ -213,7 +220,7 @@ define(["jquery", "emitter"], function($, Emitter) {
callback: function(path) {
self.view.POST("/ajax/fs/exists", {
path: path
}).done(function(selectedType) {
}, function(err, selectedType) {
//check if saving is possible
var fileExtension = path.split(".").pop().toLowerCase();
var unsupportedExtension = !~self.view._extension.fileExtensions.indexOf(fileExtension);
......@@ -292,8 +299,9 @@ define(["jquery", "emitter"], function($, Emitter) {
content: this.editor.getContent(),
utf8: true,
watch_id: "code"
}).done(function(res) {
if (!res.success) {
}, function(err, res) {
if (err) {
} else if (!res.success) {
self.view.alert("Saving not possible. Please check file permissions.");
} else {
self.mtime = res.mtime;
......@@ -310,7 +318,6 @@ define(["jquery", "emitter"], function($, Emitter) {
if ($.isFunction(callback))
callback();
}
}).always(function() {
self.view.setLoading(false);
self.savingProcessing = false;
});
......
......@@ -53,7 +53,7 @@ define(["jquery", "emitter"], function($, Emitter) {
});
// check if there is a running job
this.view.POST("runningjob").done(function(res) {
this.view.POST("runningjob", function(err, res) {
self.running = !!res.runningjob;
self.updateExeButtons();
});
......@@ -140,45 +140,45 @@ define(["jquery", "emitter"], function($, Emitter) {
self.view.commandLine.handleExecute();
this.view.editor.actions.save(function() {
if (self.running) {
self.view.alert("There is already a running job!");
return this;
}
self.running = true;
self.updateExeButtons();
//parse the command
var parts = self.view.editor.path.split("/");
var fileName = parts.pop();
var filePath = parts.join("/");
cmd = cmd.replace(/\%file/g, fileName);
//additional, user defined replacements:
cmd = self.view.commandLine.performReplacements(cmd);
self.view.POST("execute", {
cmd: cmd,
base: filePath
}).done(function(res) { //double check for running jobs
if (!res.success) {
if (self.running) {
self.view.alert("There is already a running job!");
return this;
}
});
var handleFail = function() {
self.abort();
self.append(
"\n--------------------\n" +
"Initialization has failed, please try again.\n" +
"====================\n\n\n");
self.running = false;
self.running = true;
self.updateExeButtons();
};
//extra timeout (10s) if the "start" is not sent back/ websockets fail
self.failedExecuteTimeOut = setTimeout(handleFail, 10000);
});
//parse the command
var parts = self.view.editor.path.split("/");
var fileName = parts.pop();
var filePath = parts.join("/");
cmd = cmd.replace(/\%file/g, fileName);
//additional, user defined replacements:
cmd = self.view.commandLine.performReplacements(cmd);
self.view.POST("execute", {
cmd: cmd,
base: filePath
}, function(err, res) { //double check for running jobs
if (!res.success) {
self.view.alert("There is already a running job!");
}
});
var handleFail = function() {
self.abort();
self.append(
"\n--------------------\n" +
"Initialization has failed, please try again.\n" +
"====================\n\n\n");
self.running = false;
self.updateExeButtons();
};
//extra timeout (10s) if the "start" is not sent back/ websockets fail
self.failedExecuteTimeOut = setTimeout(handleFail, 10000);
});
return this;
},
......@@ -290,10 +290,12 @@ define(["jquery", "emitter"], function($, Emitter) {
path: path,
content: self.getText(),
utf8: true,
}).done(function() {
}, function(err) {
if (err) {
self.view.setLoading(false);
return;
}
self.view.alert("Output saved in file " + path);
}).always(function() {
self.view.setLoading(false);
});
}
},
......
......@@ -151,7 +151,12 @@ define(["jquery", "emitter"], function($, Emitter) {
filefilter: "\\.(" + this.allowedExtensions.join("|") + ")$",
reverse: true,
watch_id: "preview"
}).done(function(res) {
}, function(err, res) {
if (err) {
self.previewUpdating = false;
return;
}
var files = $.grep(res.filelist, function(file) {
return file.type == "f" && ~self.allowedExtensions.indexOf(file.name.split(".").pop().toLowerCase());
});
......@@ -189,8 +194,6 @@ define(["jquery", "emitter"], function($, Emitter) {
self.addPreview(data);
});
self.previewUpdating = false;
}).fail(function() {
self.previewUpdating = false;
});
......
......@@ -85,7 +85,12 @@ define(["vispa/extensions", "vispa/views/center", "require", "jquery"], function
var self = this;
this.setLoading(true);
var promise = this.GET("data", function(response) {
this.GET("data", function(err, response) {
if (err) {
self.setLoading(false);
return;
}
node.html(response);
// example showing how to open a dialog with a button that opens a file selector
......@@ -110,7 +115,6 @@ define(["vispa/extensions", "vispa/views/center", "require", "jquery"], function
self.setLoading(false);
});
promise.fail(this.setLoading.bind(this, false));
}
});
......
......@@ -146,16 +146,8 @@ define(["jquery", "jclass"], function($, JClass) {
undefined) {
return;
}
var dfd = self.FileBase.instance.GET("/ajax/fs/rename", {
"path": self.FileBase.instance.getState("path"),
"name": name,
"new_name": newName
});
dfd.done(function() {
// rename triggers 2 events, let"s try to wait for both
self.FileBase.updateView(400);
});
self._rename(self.FileBase.instance.getState("path"), name, newName);
}, {
defaultValue: name
});
......@@ -169,15 +161,15 @@ define(["jquery", "jclass"], function($, JClass) {
newName = name;
}
if (newName == name) {
self.FileBase.updateView(400);
return true;
}
var dfd = self.FileBase.instance.GET("/ajax/fs/rename", {
"path": path,
"name": name,
"new_name": newName
});
dfd.done(function() {
}, function() {
if (err) return;
// rename triggers 2 events, let's try to wait for both
self.FileBase.updateView(400);
});
return true;
......@@ -225,8 +217,7 @@ define(["jquery", "jclass"], function($, JClass) {
"path": this.FileBase.instance.getState("path"),
"paths": JSON.stringify(paths),
"cut": cut
});
dfd.done(function() {
}, function() {
if (cut) {
self.removebool = false;
// delete cookie
......@@ -271,8 +262,7 @@ define(["jquery", "jclass"], function($, JClass) {
var self = this;
var dfd = self.FileBase.instance.POST("/ajax/fs/remove", {
"path": JSON.stringify(self.entries)
});
dfd.done(function() {
}, function(err) {
//self.FileBase.updateView();
self.entries = [];
self.FileBase.selections.entries = {};
......@@ -313,8 +303,6 @@ define(["jquery", "jclass"], function($, JClass) {
path: self.FileBase.instance.getState("path"),
paths: JSON.stringify(self.entries),
name: newName
}).done(function() {
//self.FileBase.updateView();
});
});
......
......@@ -131,8 +131,12 @@ define([
this.instance.GET("/ajax/fs/filecount", {
path: self.instance.getState('path'),
watch_id: "0"
}).done(function(res) {
if (res.count < 0) {
}, function(err, data) {
if (err) {
this.instance.close();
return;
}
if (data.count < 0) {
self.instance.setLoading(true);
self.instance.getTemplate("html/alert.html",
function(err, tmpl) {
......@@ -144,34 +148,24 @@ define([
self.instance.setLoading(false);
return;
}
self.instance.setLoading(true, res.count > 50 ? 0 : null);
// Check if last request is less than one second away
var promise = self.instance.GET("/ajax/fs/filelist", {
path: self.instance.getState('path'),
watch_id: "0"
});
self.instance.setLoading(true, data.count > 50 ? 0 : null);
if (self.view.fileContentContainer !== null) {
self.selections.unselectAll();
self.view.fileContentContainer.empty();
}
promise.done(function(content) {
if (Object.keys(content.filelist).length == 1 && content.filelist[0].warning !==
undefined) {
self.instance.alert(content.filelist[0].warning);
vispa.messenger.error(content.filelist[0].warning);
content.filelist = [];
self.instance.GET("/ajax/fs/filelist", {
path: self.instance.getState('path'),
watch_id: "0"
}, function(err, data) {
if (err) {
self.instance.setLoading(false);
} else {
self.refresh(data);
}
self.refresh(content);
self.workflow.updateState++;
}).fail(function() {
self.instance.setLoading(false);
self.workflow.updateState++;
});
}).fail(function() {
self.instance.close();
});
},
......
......@@ -101,8 +101,8 @@ define(["jquery", "jclass"], function($, JClass) {
request: JSON.stringify({
Bookmarks: ["names", "paths", "symbols"]
})
}).done(function(data) {
if (!data.success) {
}, function(err, data) {
if (err) {
self.names = [];
self.paths = [];
self.symbols = [];
......@@ -383,7 +383,7 @@ define(["jquery", "jclass"], function($, JClass) {
var self = this;
$(".input-bookmarkname", self.container).blur();
self.instance.getTemplate("html/bookmarkSymbolDialog.html").done(function(tmpl) {
self.instance.getTemplate("html/bookmarkSymbolDialog.html", function(err, tmpl) {
var body = $(tmpl).children().first();
var footer = $(tmpl).children().last();
var dialog = null;
......
......@@ -18,11 +18,8 @@ define(["jquery", "jclass", "./bookmark"], function($, JClass, Bookmark) {
this.node = node;
// get template
this.render();
var dfd = this.instance.getTemplate("html/pathbar.html", function(err, tmpl) {
this.instance.getTemplate("html/pathbar.html", function(err, tmpl) {
self.filePathBar = $(tmpl);
});
dfd.done(function() {
// make options wheel work
self.optionWheel();
});
},
......@@ -32,12 +29,12 @@ define(["jquery", "jclass", "./bookmark"], function($, JClass, Bookmark) {
var clicktype = vispa.device.hasTouch ? "tap" : "click";
// get extended path
var path = self.FileBase.instance.getState("path");
var dfd = self.FileBase.instance.GET("/ajax/fs/expand", {
self.FileBase.instance.GET("/ajax/fs/expand", {
"path": JSON.stringify(path)
});
dfd.done(function() {
}, function(err, data) {
if (err) return;
// build links to pathParts
var pathParts = self.FileBase.helper.splitPath(dfd.responseText);
var pathParts = self.FileBase.helper.splitPath(data);
var pathPartsLinks = [];
var item = "";
$.each(pathParts, function(idx, part) {
......@@ -73,7 +70,7 @@ define(["jquery", "jclass", "./bookmark"], function($, JClass, Bookmark) {
$("a", pathList).on(clicktype, function(event) {
event.preventDefault();
var newPath = $(this).attr("href");
if (newPath != dfd.responseText) {
if (newPath != data) {
self.FileBase.instance.setState("path", newPath);
}
self.FileBase.updateView();
......
......@@ -41,11 +41,12 @@ define(["jquery", "jclass"], function($, JClass) {
var path = self.FileBase.instance.getState("path") + "/" + this.name;
self.instance.GET("/ajax/fs/expand", {
"path": JSON.stringify("$HOME")
}).done(function(res) {
path = path.replace("$HOME", res);
}, function(err, data) {
if (err) return;
path = path.replace("$HOME", data);
var i;
for (i = 0; i < paths.length; i++) {
paths[i] = paths[i].replace("$HOME", res);
paths[i] = paths[i].replace("$HOME", data);
}
if ($.inArray(path, paths) !== -1) {
$(nodeObj.element).css("opacity", "0.4");
......
......@@ -8,7 +8,7 @@ define(["jquery", "../base/views/table"], function($, TableView) {
render: function() {
var self = this;
this.instance.getTemplate("html/fileselectorTableview.html", function(error, template, dfd) {
this.instance.getTemplate("html/fileselectorTableview.html", function(error, template) {
// self.fileContentContainer.append(template);
self.template = template;
if (self.FileBase.workflow.data === null) {
......
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