CLOUD: Make "/create" support AJAX

Now creating directories doesn't refresh the "/filesAJAX" page.
This commit is contained in:
Alexander Tkachev 2016-07-19 14:31:37 +06:00
parent da229dd84c
commit 36b381e411
4 changed files with 59 additions and 11 deletions

View File

@ -38,25 +38,25 @@ void CreateDirectoryHandler::handle(Client &client) {
// check that <path> is not an absolute root // check that <path> is not an absolute root
if (path == "" || path == "/") { if (path == "" || path == "/") {
HandlerUtils::setFilesManagerErrorMessageHandler(client, _("Can't create directory here!")); handleError(client, _("Can't create directory here!"));
return; return;
} }
// transform virtual path to actual file system one // transform virtual path to actual file system one
Common::String prefixToRemove = "", prefixToAdd = ""; Common::String prefixToRemove = "", prefixToAdd = "";
if (!transformPath(path, prefixToRemove, prefixToAdd) || path.empty()) { if (!transformPath(path, prefixToRemove, prefixToAdd) || path.empty()) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, _("Invalid path!")); handleError(client, _("Invalid path!"));
return; return;
} }
// check that <path> exists and is directory // check that <path> exists and is directory
AbstractFSNode *node = g_system->getFilesystemFactory()->makeFileNodePath(path); AbstractFSNode *node = g_system->getFilesystemFactory()->makeFileNodePath(path);
if (!node->exists()) { if (!node->exists()) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, _("Parent directory doesn't exists!")); handleError(client, _("Parent directory doesn't exists!"));
return; return;
} }
if (!node->isDirectory()) { if (!node->isDirectory()) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, _("Can't create a directory within a file!")); handleError(client, _("Can't create a directory within a file!"));
return; return;
} }
@ -65,17 +65,23 @@ void CreateDirectoryHandler::handle(Client &client) {
node = g_system->getFilesystemFactory()->makeFileNodePath(path + name); node = g_system->getFilesystemFactory()->makeFileNodePath(path + name);
if (node->exists()) { if (node->exists()) {
if (!node->isDirectory()) { if (!node->isDirectory()) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, _("There is a file with that name in the parent directory!")); handleError(client, _("There is a file with that name in the parent directory!"));
return; return;
} }
} else { } else {
// create the <directory_name> in <path> // create the <directory_name> in <path>
if (!node->create(true)) { if (!node->create(true)) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, _("Failed to create the directory!")); handleError(client, _("Failed to create the directory!"));
return; return;
} }
} }
// if json requested, respond with it
if (client.queryParameter("answer_json") == "true") {
setJsonResponseHandler(client, "success", _("Directory created successfully!"));
return;
}
// set redirect on success // set redirect on success
HandlerUtils::setMessageHandler( HandlerUtils::setMessageHandler(
client, client,
@ -91,6 +97,20 @@ void CreateDirectoryHandler::handle(Client &client) {
); );
} }
void CreateDirectoryHandler::handleError(Client &client, Common::String message) const {
if (client.queryParameter("answer_json") == "true") setJsonResponseHandler(client, "error", message);
else HandlerUtils::setFilesManagerErrorMessageHandler(client, message);
}
void CreateDirectoryHandler::setJsonResponseHandler(Client &client, Common::String type, Common::String message) const {
Common::JSONObject response;
response.setVal("type", new Common::JSONValue(type));
response.setVal("message", new Common::JSONValue(message));
Common::JSONValue json = response;
LocalWebserver::setClientGetHandler(client, json.stringify(true));
}
/// public /// public
ClientHandlerCallback CreateDirectoryHandler::getHandler() { ClientHandlerCallback CreateDirectoryHandler::getHandler() {

View File

@ -29,6 +29,8 @@ namespace Networking {
class CreateDirectoryHandler: public FilesBaseHandler { class CreateDirectoryHandler: public FilesBaseHandler {
void handle(Client &client); void handle(Client &client);
void handleError(Client &client, Common::String message) const;
void setJsonResponseHandler(Client &client, Common::String type, Common::String message) const;
public: public:
CreateDirectoryHandler(); CreateDirectoryHandler();
virtual ~CreateDirectoryHandler(); virtual ~CreateDirectoryHandler();

Binary file not shown.

View File

@ -17,7 +17,7 @@
</tr></table> </tr></table>
<div id="create_directory" class="modal"> <div id="create_directory" class="modal">
<p>{create_directory_desc}</p> <p>{create_directory_desc}</p>
<form action="create" id="create_directory_form"> <form action="create" id="create_directory_form" onsubmit="return createDirectory();">
<input type="hidden" name="path" value="{path}"/> <input type="hidden" name="path" value="{path}"/>
<input type="hidden" name="ajax" value="true"/> <input type="hidden" name="ajax" value="true"/>
<input type="text" name="directory_name" value=""/> <input type="text" name="directory_name" value=""/>
@ -71,8 +71,6 @@
function getCallback(path) { function getCallback(path) {
return function (jsonResponse) { return function (jsonResponse) {
console.log(jsonResponse);
if (jsonResponse.type == "error") { if (jsonResponse.type == "error") {
showError(); showError();
return; return;
@ -83,6 +81,34 @@
}; };
} }
function createDirectory() {
if (isLoading) return;
showLoading();
var data = {"answer_json": "true"};
var elements = document.getElementById("create_directory_form").elements;
for (var el in elements)
data[elements[el].name] = elements[el].value;
ajax.getAndParseJson("./create", data, getCreateDirectoryCallback(data["path"]));
show("create_directory");
return false; // invalidate form, so it won't submit
}
function getCreateDirectoryCallback(path) {
return function (jsonResponse) {
console.log(jsonResponse);
if (jsonResponse.type == "error") {
showError();
return;
}
hideLoading();
showDirectory(path);
};
}
var isLoading = false; var isLoading = false;
function showLoading() { function showLoading() {
@ -122,7 +148,7 @@
function makeBreadcrumb(name, path) { function makeBreadcrumb(name, path) {
var a = createElementWithContents("a", name); var a = createElementWithContents("a", name);
a.onclick = function () { showDirectory(path); }; a.onclick = function () { showDirectory(path); };
a.href = "javascript:onclick();"; a.href = "javascript:void(0);";
return a; return a;
} }
@ -194,7 +220,7 @@
var a = createElementWithContents("a", item.name); var a = createElementWithContents("a", item.name);
if (item.isDirectory) { if (item.isDirectory) {
a.onclick = function () { showDirectory(item.path); }; a.onclick = function () { showDirectory(item.path); };
a.href = "javascript:onclick();"; a.href = "javascript:void(0);";
} else } else
a.href = "./download?path=" + encodeURIComponent(item.path); a.href = "./download?path=" + encodeURIComponent(item.path);
td.appendChild(a); td.appendChild(a);