From 36b381e411f4e55ba9590a4102f5b84ce6b724ce Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Tue, 19 Jul 2016 14:31:37 +0600 Subject: CLOUD: Make "/create" support AJAX Now creating directories doesn't refresh the "/filesAJAX" page. --- .../sdl_net/handlers/createdirectoryhandler.cpp | 32 +++++++++++++++---- .../sdl_net/handlers/createdirectoryhandler.h | 2 ++ backends/networking/wwwroot.zip | Bin 241963 -> 242704 bytes backends/networking/wwwroot/.filesAJAX.html | 34 ++++++++++++++++++--- 4 files changed, 58 insertions(+), 10 deletions(-) (limited to 'backends') diff --git a/backends/networking/sdl_net/handlers/createdirectoryhandler.cpp b/backends/networking/sdl_net/handlers/createdirectoryhandler.cpp index fc02b643c1..2ded9785a0 100644 --- a/backends/networking/sdl_net/handlers/createdirectoryhandler.cpp +++ b/backends/networking/sdl_net/handlers/createdirectoryhandler.cpp @@ -38,25 +38,25 @@ void CreateDirectoryHandler::handle(Client &client) { // check that is not an absolute root if (path == "" || path == "/") { - HandlerUtils::setFilesManagerErrorMessageHandler(client, _("Can't create directory here!")); + handleError(client, _("Can't create directory here!")); return; } // transform virtual path to actual file system one Common::String prefixToRemove = "", prefixToAdd = ""; if (!transformPath(path, prefixToRemove, prefixToAdd) || path.empty()) { - HandlerUtils::setFilesManagerErrorMessageHandler(client, _("Invalid path!")); + handleError(client, _("Invalid path!")); return; } // check that exists and is directory AbstractFSNode *node = g_system->getFilesystemFactory()->makeFileNodePath(path); if (!node->exists()) { - HandlerUtils::setFilesManagerErrorMessageHandler(client, _("Parent directory doesn't exists!")); + handleError(client, _("Parent directory doesn't exists!")); return; } 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; } @@ -65,17 +65,23 @@ void CreateDirectoryHandler::handle(Client &client) { node = g_system->getFilesystemFactory()->makeFileNodePath(path + name); if (node->exists()) { 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; } } else { // create the in if (!node->create(true)) { - HandlerUtils::setFilesManagerErrorMessageHandler(client, _("Failed to create the directory!")); + handleError(client, _("Failed to create the directory!")); return; } } + // if json requested, respond with it + if (client.queryParameter("answer_json") == "true") { + setJsonResponseHandler(client, "success", _("Directory created successfully!")); + return; + } + // set redirect on success HandlerUtils::setMessageHandler( 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 ClientHandlerCallback CreateDirectoryHandler::getHandler() { diff --git a/backends/networking/sdl_net/handlers/createdirectoryhandler.h b/backends/networking/sdl_net/handlers/createdirectoryhandler.h index 2ffaf96f1f..bd0e8874da 100644 --- a/backends/networking/sdl_net/handlers/createdirectoryhandler.h +++ b/backends/networking/sdl_net/handlers/createdirectoryhandler.h @@ -29,6 +29,8 @@ namespace Networking { class CreateDirectoryHandler: public FilesBaseHandler { void handle(Client &client); + void handleError(Client &client, Common::String message) const; + void setJsonResponseHandler(Client &client, Common::String type, Common::String message) const; public: CreateDirectoryHandler(); virtual ~CreateDirectoryHandler(); diff --git a/backends/networking/wwwroot.zip b/backends/networking/wwwroot.zip index 6f821d9090..b767d7c5d7 100644 Binary files a/backends/networking/wwwroot.zip and b/backends/networking/wwwroot.zip differ diff --git a/backends/networking/wwwroot/.filesAJAX.html b/backends/networking/wwwroot/.filesAJAX.html index d648466d2c..f736b74b7e 100644 --- a/backends/networking/wwwroot/.filesAJAX.html +++ b/backends/networking/wwwroot/.filesAJAX.html @@ -17,7 +17,7 @@