Added AJAX wrappers to a few forms

This commit is contained in:
Marcin Kurczewski 2014-05-20 20:54:31 +02:00
parent bba35875a3
commit e3617434e6
12 changed files with 94 additions and 17 deletions

View file

@ -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()
{

View file

@ -106,7 +106,6 @@ $(function()
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data)
{

View file

@ -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);
}

View file

@ -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:

View file

@ -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()

View file

@ -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');
}
}

View file

@ -1,7 +1,10 @@
<div class="form-wrapper">
<form method="post" action="<?= \Chibi\Router::linkTo(
['PostController', 'listRedirectAction'],
['source' => 'mass-tag']) ?>">
<form method="post"
class="simple-action"
action="<?= \Chibi\Router::linkTo(
['PostController', 'listRedirectAction'],
['source' => 'mass-tag']) ?>">
<h1>mass tag</h1>
<?php

View file

@ -1,5 +1,8 @@
<div class="form-wrapper">
<form method="post" action="<?= \Chibi\Router::linkTo(['TagController', 'mergeAction']) ?>">
<form method="post"
class="simple-action"
action="<?= \Chibi\Router::linkTo(['TagController', 'mergeAction']) ?>">
<h1>merge tags</h1>
<?php
@ -12,6 +15,7 @@
$context = new StdClass;
$context->name = 'target-tag';
$context->label = 'Target tag';
$context->inputClass = 'autocomplete';
$this->renderExternal('input-text', $context);
$this->renderExternal('message');

View file

@ -1,5 +1,8 @@
<div class="form-wrapper">
<form method="post" action="<?= \Chibi\Router::linkTo(['TagController', 'renameAction']) ?>">
<form method="post"
class="simple-action"
action="<?= \Chibi\Router::linkTo(['TagController', 'renameAction']) ?>">
<h1>rename tags</h1>
<?php

View file

@ -3,7 +3,7 @@
['UserController', 'deleteAction'],
['identifier' => $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?">

View file

@ -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">
<?php

View file

@ -7,7 +7,7 @@ $settings = $this->context->transport->user->getSettings();
['UserController', 'settingsAction'],
['identifier' => $this->context->transport->user->getName()]) ?>"
method="post"
class="settings">
class="user-settings simple-action">
<?php
$safety = array_filter(