Further improve mobile search

This commit is contained in:
Björn Fromme
2021-10-26 11:26:09 +02:00
parent 2b2dbae409
commit 4923944175
10 changed files with 214 additions and 46 deletions
@@ -0,0 +1,83 @@
<?php
namespace EP\EpTheme\ViewHelpers;
/***************************************************************
*
* Copyright notice
*
* (c) 2018 Björn Fromme <[email protected]>, dreipunktnull
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
class StimulusActionViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
protected $escapeOutput = false;
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('controller', 'string', 'Controller name', true);
$this->registerArgument('action', 'string', 'Action name', true);
$this->registerArgument('event', 'string', 'Event name', false);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
)
{
$controllerName = static::normalizeControllerName($arguments['controller']);
$action = $arguments['action'];
$event = $arguments['event'];
if (!$event) {
return sprintf('data-action="%s#%s"', $controllerName, $action);
}
return sprintf('data-action="%s->%s#%s"', $event, $controllerName, $action);
}
/**
* Method taken from symfony/webpack-encore-bundle
*
* @param string $str
* @return string
*/
private static function normalizeControllerName(string $str): string
{
return preg_replace('/^@/', '', str_replace('_', '-', str_replace('/', '--', $str)));
}
}
@@ -0,0 +1,108 @@
<?php
namespace EP\EpTheme\ViewHelpers;
/***************************************************************
*
* Copyright notice
*
* (c) 2018 Björn Fromme <[email protected]>, dreipunktnull
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
class StimulusControllerViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
protected $escapeOutput = false;
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('controller', 'string', 'Controller name', true);
$this->registerArgument('values', 'array', 'Controller values', false, []);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
)
{
$controllerName = static::normalizeControllerName($arguments['controller']);
$attributes = [
sprintf('data-controller="%s"', $controllerName),
];
foreach ($arguments['values'] as $key => $value) {
if (!is_scalar($value)) {
$value = htmlspecialchars(json_encode($value), ENT_QUOTES);
}
if (\is_bool($value)) {
$value = $value ? 'true' : 'false';
}
$key = static::normalizeKeyName($key);
$attributes[] = sprintf('data-%s-%s-value="%s"', $controllerName, $key, $value);
}
return implode(' ', $attributes);
}
/**
* Method taken from symfony/webpack-encore-bundle
*
* @param string $str
* @return string
*/
private static function normalizeControllerName(string $str): string
{
return preg_replace('/^@/', '', str_replace('_', '-', str_replace('/', '--', $str)));
}
/**
* Method taken from symfony/webpack-encore-bundle
*
* @param string $str
* @return string
*/
private static function normalizeKeyName(string $str): string
{
// Adapted from ByteString::camel
$str = ucfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $str))));
// Adapted from ByteString::snake
return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1-\2', $str));
}
}