From e3617434e6fb2fad8e7acb022004c8935f6e4ed4 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 20 May 2014 20:54:31 +0200 Subject: [PATCH] Added AJAX wrappers to a few forms --- public_html/media/js/core.js | 48 ++++++++++++++++++++++++++ public_html/media/js/post-view.js | 1 - src/Controllers/AbstractController.php | 7 +++- src/Controllers/PostController.php | 3 +- src/Controllers/TagController.php | 10 ++++-- src/Controllers/UserController.php | 16 +++++++-- src/Views/tag/tag-mass-tag.phtml | 9 +++-- src/Views/tag/tag-merge.phtml | 6 +++- src/Views/tag/tag-rename.phtml | 5 ++- src/Views/user/user-delete.phtml | 2 +- src/Views/user/user-edit.phtml | 2 +- src/Views/user/user-settings.phtml | 2 +- 12 files changed, 94 insertions(+), 17 deletions(-) diff --git a/public_html/media/js/core.js b/public_html/media/js/core.js index d029c3e4..0e0db0c1 100644 --- a/public_html/media/js/core.js +++ b/public_html/media/js/core.js @@ -120,6 +120,54 @@ $(function() }); + //simple action forms + $('form.simple-action').bindOnce('simple-action', 'submit', function(e) + { + e.preventDefault(); + rememberLastSearchQuery(); + + var formDom = $(this); + if (formDom.hasClass('inactive')) + return; + formDom.addClass('inactive'); + formDom.find(':input').attr('readonly', true); + + var url = formDom.attr('action'); + var fd = new FormData(formDom[0]); + + var ajaxData = + { + url: url, + data: fd, + processData: false, + contentType: false, + }; + + postJSON(ajaxData) + .success(function(data) + { + if (data.message) + alert(data.message); + console.log(data); + disableExitConfirmation(); + formDom.find(':input').attr('readonly', false); + formDom.removeClass('inactive'); + if (data.redirectUrl) + window.location.href = data.redirectUrl; + else + window.location.reload(); + }) + .error(function(xhr) + { + alert(xhr.responseJSON + ? xhr.responseJSON.message + : 'Fatal error'); + formDom.find(':input').attr('readonly', false); + formDom.removeClass('inactive'); + }); + }); + + //attach data from submit buttons to forms before .submit() gets called $('.submit').each(function() { diff --git a/public_html/media/js/post-view.js b/public_html/media/js/post-view.js index f8eeaa81..8938a835 100644 --- a/public_html/media/js/post-view.js +++ b/public_html/media/js/post-view.js @@ -106,7 +106,6 @@ $(function() data: fd, processData: false, contentType: false, - type: 'POST', success: function(data) { diff --git a/src/Controllers/AbstractController.php b/src/Controllers/AbstractController.php index 5b60b809..9235ff4d 100644 --- a/src/Controllers/AbstractController.php +++ b/src/Controllers/AbstractController.php @@ -48,7 +48,12 @@ class AbstractController protected function redirect($url) { - if (!$this->isAjax()) + if ($this->isAjax()) + { + Core::getContext()->transport->redirectUrl = $url; + $this->renderAjax(); + } + else \Chibi\Util\Url::forward($url); } diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 00d9b816..6e5750db 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -288,8 +288,7 @@ class PostController extends AbstractController { $context->transport->lastSearchQuery = ''; list ($prevPostId, $nextPostId) = - PostSearchService::getPostIdsAround( - $context->transport->lastSearchQuery, $id); + PostSearchService::getPostIdsAround($context->transport->lastSearchQuery, $id); } //todo: diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index 99baa4ce..41b33f6f 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -97,7 +97,10 @@ class TagController extends AbstractController Messenger::fail($e->getMessage()); } - $this->renderViewWithSource('tag-list-wrapper', 'merge'); + if ($this->isAjax()) + $this->renderAjax(); + else + $this->renderViewWithSource('tag-list-wrapper', 'merge'); } public function renameView() @@ -124,7 +127,10 @@ class TagController extends AbstractController Messenger::fail($e->getMessage()); } - $this->renderViewWithSource('tag-list-wrapper', 'rename'); + if ($this->isAjax()) + $this->renderAjax(); + else + $this->renderViewWithSource('tag-list-wrapper', 'rename'); } public function massTagRedirectView() diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 428205d9..767b3ff4 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -57,7 +57,10 @@ class UserController extends AbstractController Messenger::fail($e->getMessage()); } - $this->renderView('user-view'); + if ($this->isAjax()) + $this->renderAjax(); + else + $this->renderView('user-view'); } public function editAction($identifier) @@ -111,7 +114,10 @@ class UserController extends AbstractController Messenger::fail($e->getMessage()); } - $this->renderView('user-view'); + if ($this->isAjax()) + $this->renderAjax(); + else + $this->renderView('user-view'); } public function deleteAction($identifier) @@ -136,7 +142,11 @@ class UserController extends AbstractController { \Chibi\Util\Headers::setCode(400); Messenger::fail($e->getMessage()); - $this->renderView('user-view'); + + if ($this->isAjax()) + $this->renderAjax(); + else + $this->renderView('user-view'); } } diff --git a/src/Views/tag/tag-mass-tag.phtml b/src/Views/tag/tag-mass-tag.phtml index 4817cfc0..4e1db0c7 100644 --- a/src/Views/tag/tag-mass-tag.phtml +++ b/src/Views/tag/tag-mass-tag.phtml @@ -1,7 +1,10 @@
-
+ +

mass tag

- + +

merge tags

name = 'target-tag'; $context->label = 'Target tag'; + $context->inputClass = 'autocomplete'; $this->renderExternal('input-text', $context); $this->renderExternal('message'); diff --git a/src/Views/tag/tag-rename.phtml b/src/Views/tag/tag-rename.phtml index 75b4e1d0..9f0549e0 100644 --- a/src/Views/tag/tag-rename.phtml +++ b/src/Views/tag/tag-rename.phtml @@ -1,5 +1,8 @@
- + +

rename tags

$this->context->transport->user->getName()]) ?>" method="post" - class="delete confirmable" + class="user-delete simple-action confirmable" autocomplete="off" data-confirm-text="Are you sure you want to delete your account?"> diff --git a/src/Views/user/user-edit.phtml b/src/Views/user/user-edit.phtml index 480ecc09..6d864d26 100644 --- a/src/Views/user/user-edit.phtml +++ b/src/Views/user/user-edit.phtml @@ -8,7 +8,7 @@ $this->assets->addScript('user-edit.js'); ['identifier' => $this->context->transport->user->getName()]) ?>" enctype="multipart/form-data" method="post" - class="edit" + class="edit-user simple-action" autocomplete="off"> context->transport->user->getSettings(); ['UserController', 'settingsAction'], ['identifier' => $this->context->transport->user->getName()]) ?>" method="post" - class="settings"> + class="user-settings simple-action">