fix column visibility if unconfigured / no cookie existing (#423)

This commit is contained in:
Kevin Papst
2018-11-18 02:13:44 +01:00
committed by GitHub
parent a073064900
commit 8cb52e22b1
6 changed files with 39 additions and 11 deletions

View File

@@ -22,7 +22,7 @@ if (typeof jQuery === 'undefined') {
$.datatable = {};
$(function() {
"use strict";
"use strict";
$.datatable = {
saveVisibility: function (modalSelector) {
@@ -30,7 +30,7 @@ $(function() {
function() {
var settings = {};
var cookieName = $(this).attr('name');
$(this).find('input:checkbox:not(:checked)').each(
$(this).find('input:checkbox').each(
function () {
settings[$(this).attr('name')] = $(this).is(':checked');
}

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,5 @@
{
"build/app.js": "/build/app.js?3650fbe8241d54cb493c",
"build/app.js": "/build/app.js?3dadf2331143d4cac84f",
"build/app.css": "/build/app.css?6501689dff217d14b179e3049295343e",
"build/images/blue@2x.png": "/build/images/blue@2x.png?2694acfd",
"build/images/blue.png": "/build/images/blue.png?96f8a905",

View File

@@ -120,9 +120,31 @@ class Extensions extends \Twig_Extension
return [
new \Twig_SimpleFunction('locales', [$this, 'getLocales']),
new \Twig_SimpleFunction('is_visible_column', [$this, 'isColumnVisible']),
new \Twig_SimpleFunction('is_datatable_configured', [$this, 'isDatatableConfigured']),
];
}
/**
* @param string $dataTable
* @param string $size
* @return bool
*/
public function isDatatableConfigured(string $dataTable, string $size)
{
$cookie = $this->getVisibilityCookieName($dataTable, $size);
return $this->requestStack->getCurrentRequest()->cookies->has($cookie);
}
/**
* @param string $dataTable
* @param string $size
* @return string
*/
public function getVisibilityCookieName(string $dataTable, string $size)
{
return $dataTable . '_visibility' . $size;
}
/**
* This is only for datatables, do not use it outside this context.
*
@@ -134,16 +156,16 @@ class Extensions extends \Twig_Extension
public function isColumnVisible(string $dataTable, string $column, string $size)
{
// name handling is spread between here and datatables.html.twig (data_table_column_modal)
$dataTable = $dataTable . '_visibility' . $size;
$cookie = $this->getVisibilityCookieName($dataTable, $size);
if (!isset($this->cookies[$dataTable])) {
if (!isset($this->cookies[$cookie])) {
$visibility = false;
if ($this->requestStack->getCurrentRequest()->cookies->has($dataTable)) {
$visibility = json_decode($this->requestStack->getCurrentRequest()->cookies->get($dataTable), true);
if ($this->requestStack->getCurrentRequest()->cookies->has($cookie)) {
$visibility = json_decode($this->requestStack->getCurrentRequest()->cookies->get($cookie), true);
}
$this->cookies[$dataTable] = $visibility;
$this->cookies[$cookie] = $visibility;
}
$values = $this->cookies[$dataTable];
$values = $this->cookies[$cookie];
if (empty($values) || !is_array($values)) {
return true;

View File

@@ -46,6 +46,12 @@
{% else %}
{% if not is_visible_column(name, column, '') %}
{% set classes = classes ~ ' hidden' %}
{% elseif not is_datatable_configured(name, '') %}
{% for tmp in classes|split(' ') %}
{% if 'hidden' == tmp %}
{% set classes = classes|replace({(tmp): ''}) %}
{% endif %}
{% endfor %}
{% else %}
{% for tmp in classes|split(' ') %}
{% if 'hidden' in tmp %}

View File

@@ -56,7 +56,7 @@ class ExtensionsTest extends TestCase
public function testGetFunctions()
{
$functions = ['locales', 'is_visible_column'];
$functions = ['locales', 'is_visible_column', 'is_datatable_configured'];
$sut = $this->getSut($this->localeDe);
$twigFunctions = $sut->getFunctions();
$this->assertCount(count($functions), $twigFunctions);