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 = {}; $.datatable = {};
$(function() { $(function() {
"use strict"; "use strict";
$.datatable = { $.datatable = {
saveVisibility: function (modalSelector) { saveVisibility: function (modalSelector) {
@@ -30,7 +30,7 @@ $(function() {
function() { function() {
var settings = {}; var settings = {};
var cookieName = $(this).attr('name'); var cookieName = $(this).attr('name');
$(this).find('input:checkbox:not(:checked)').each( $(this).find('input:checkbox').each(
function () { function () {
settings[$(this).attr('name')] = $(this).is(':checked'); 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/app.css": "/build/app.css?6501689dff217d14b179e3049295343e",
"build/images/blue@2x.png": "/build/images/blue@2x.png?2694acfd", "build/images/blue@2x.png": "/build/images/blue@2x.png?2694acfd",
"build/images/blue.png": "/build/images/blue.png?96f8a905", "build/images/blue.png": "/build/images/blue.png?96f8a905",

View File

@@ -120,9 +120,31 @@ class Extensions extends \Twig_Extension
return [ return [
new \Twig_SimpleFunction('locales', [$this, 'getLocales']), new \Twig_SimpleFunction('locales', [$this, 'getLocales']),
new \Twig_SimpleFunction('is_visible_column', [$this, 'isColumnVisible']), 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. * 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) public function isColumnVisible(string $dataTable, string $column, string $size)
{ {
// name handling is spread between here and datatables.html.twig (data_table_column_modal) // 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; $visibility = false;
if ($this->requestStack->getCurrentRequest()->cookies->has($dataTable)) { if ($this->requestStack->getCurrentRequest()->cookies->has($cookie)) {
$visibility = json_decode($this->requestStack->getCurrentRequest()->cookies->get($dataTable), true); $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)) { if (empty($values) || !is_array($values)) {
return true; return true;

View File

@@ -46,6 +46,12 @@
{% else %} {% else %}
{% if not is_visible_column(name, column, '') %} {% if not is_visible_column(name, column, '') %}
{% set classes = classes ~ ' hidden' %} {% 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 %} {% else %}
{% for tmp in classes|split(' ') %} {% for tmp in classes|split(' ') %}
{% if 'hidden' in tmp %} {% if 'hidden' in tmp %}

View File

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