Commit 4cf0878a authored by Tino Goratsch's avatar Tino Goratsch

- promoted the ajax-form handling to be a library and enabled it by default

- added the possibility to disable some libraries for AJAX requests
parent ea5a6b25
......@@ -8,6 +8,7 @@
namespace ACP3\Core\Assets;
use ACP3\Core\Assets\Event\AddLibraryEvent;
use ACP3\Core\Http\RequestInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Libraries
......@@ -22,12 +23,20 @@ class Libraries
],
'jquery' => [
'enabled' => true,
'enabled_for_ajax' => false,
'js' => 'jquery.min.js',
],
'js-cookie' => [
'enabled' => true,
'enabled' => false,
'enabled_for_ajax' => false,
'js' => 'js.cookie.js',
],
'ajax-form' => [
'enabled' => true,
'enabled_for_ajax' => false,
'dependencies' => ['jquery'],
'js' => 'ajax-form.js',
],
'fancybox' => [
'enabled' => false,
'dependencies' => ['jquery'],
......@@ -66,15 +75,22 @@ class Libraries
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var \ACP3\Core\Http\RequestInterface
*/
private $request;
/**
* Libraries constructor.
*
* @param EventDispatcherInterface $eventDispatcher
* @param \ACP3\Core\Http\RequestInterface $request
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(EventDispatcherInterface $eventDispatcher)
public function __construct(
RequestInterface $request,
EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
$this->request = $request;
}
public function dispatchAddLibraryEvent()
......@@ -140,11 +156,27 @@ class Libraries
{
$enabledLibraries = [];
foreach ($this->libraries as $library => $values) {
if ($values['enabled'] === true) {
$enabledLibraries[] = $library;
if ($this->includeInXmlHttpRequest($values)) {
continue;
}
if ($values['enabled'] === false) {
continue;
}
$enabledLibraries[] = $library;
}
return $enabledLibraries;
}
/**
* @param array $values
* @return bool
*/
private function includeInXmlHttpRequest(array $values): bool
{
return $this->request->isXmlHttpRequest()
&& isset($values['enabled_for_ajax'])
&& $values['enabled_for_ajax'] === false;
}
}
......@@ -48,6 +48,7 @@ services:
core.assets.libraries:
class: ACP3\Core\Assets\Libraries
arguments:
- '@core.http.request'
- '@core.event_dispatcher'
core.assets.page_css_classes:
......
......@@ -18,7 +18,4 @@
{event name="captcha.event.display_captcha"}
{include file="asset:System/Partials/form_group.submit.tpl" form_token=$form_token}
</form>
{javascripts}
{include_js module="system" file="ajax-form"}
{/javascripts}
{/if}
......@@ -104,9 +104,6 @@
</table>
</div>
{if isset($pages_list)}
{javascripts}
{include_js module="system" file="ajax-form"}
{/javascripts}
{if $can_delete === true}
{include file="asset:System/Partials/mark.tpl"}
{/if}
......
......@@ -12,7 +12,4 @@
{/block}
{block ADMIN_GRID_CONTENT}
{include file="asset:System/Partials/datagrid.tpl" dataTable=$grid}
{javascripts}
{include_js module="system" file="ajax-form"}
{/javascripts}
{/block}
......@@ -55,7 +55,6 @@
{include file="asset:System/Partials/form_group.submit_split.tpl" form_token=$form_token back_url={uri args="acp/polls"}}
</form>
{javascripts}
{include_js module="system" file="ajax-form"}
{include_js module="system" file="hash-change"}
{/javascripts}
{/if}
......
......@@ -11,6 +11,5 @@
</div>
{javascripts}
{js_libraries enable='font-awesome'}
{include_js module="system" file="ajax-form"}
{/javascripts}
{/if}
......@@ -183,6 +183,7 @@
} else {
self.scrollIntoView();
self.replaceContent(hash, responseData);
self.rebindHandlers(hash);
if (typeof hash !== 'undefined') {
window.location.hash = hash;
......@@ -263,8 +264,12 @@
} else {
$(this.settings.targetElement).html(responseData);
}
},
rebindHandlers: function(hash) {
var $bindingTarget = (hash && $(hash).length) ? $(hash) : $(this.settings.targetElement);
$bindingTarget.find('[data-ajax-form="true"]').formSubmit();
// Rebind the submit/click handlers
this.findSubmitButton();
},
hideLoadingLayer: function ($submitButton) {
......
......@@ -32,7 +32,4 @@
{/foreach}
</tbody>
</table>
{javascripts}
{include_js module="system" file="ajax-form"}
{/javascripts}
{/block}
......@@ -113,7 +113,6 @@
</div>
</div>
{javascripts}
{include_js module="system" file="ajax-form"}
{include_js module="system" file="hash-change"}
{/javascripts}
{/block}
......@@ -23,7 +23,4 @@
</div>
</div>
{/foreach}
{javascripts}
{include_js module="system" file="ajax-form"}
{/javascripts}
{/block}
{javascripts}
{include_js module="system" file="ajax-form"}
{include_js module="system" file="mark" depends="bootbox"}
{/javascripts}
......@@ -16,8 +16,5 @@
data-ajax-form-loading-text="{lang t="system|loading_please_wait"}">
{block CONTENT_AJAX_FORM}{/block}
</form>
{javascripts}
{include_js module="system" file="ajax-form"}
{/javascripts}
{/if}
{/block}
Markdown is supported
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