Skip to content

Commit 8dcb34e

Browse files
authored
Merge branch '2.next-cake4' into 1.next-cake4
2 parents 8dafc03 + 1eb0bee commit 8dcb34e

File tree

6 files changed

+90
-41
lines changed

6 files changed

+90
-41
lines changed

src/Datatable/Datatable.php

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
namespace CakeDC\Datatables\Datatable;
66

7+
use CakeDC\Datatables\Datatables;
8+
use CakeDC\Datatables\Exception\MissConfiguredException;
9+
use CakeDC\Datatables\View\LinkFormatter\LinkInterface;
710
use Cake\Core\InstanceConfigTrait;
811
use Cake\Utility\Inflector;
912
use Cake\Utility\Text;
10-
use CakeDC\Datatables\Datatables;
11-
use CakeDC\Datatables\Exception\MissConfiguredException;
1213
use Exception;
1314
use InvalidArgumentException;
1415

@@ -181,20 +182,33 @@ class Datatable
181182
$('#:tagId').find('#from'+colIdx)
182183
.datepicker()
183184
.on('change', function () {
184-
if($('#to'+colIdx).val() !== '') {
185+
if($('#to'+colIdx).val() !== '' && validateDate($('#to'+colIdx).val())) {
185186
api.column(colIdx).search($('#:tagId').find('#from'+colIdx).val() + '|' + $('#:tagId').find('#to' + colIdx).val()).draw();
186187
} else {
187-
api.column(colIdx).search($('#:tagId').find('#from'+colIdx).val() + '|').draw();
188+
$('#to'+colIdx).val('');
189+
if($('#from'+colIdx).val() !== '' && validateDate($('#from'+colIdx).val())) {
190+
api.column(colIdx).search($('#:tagId').find('#from'+colIdx).val() + '|').draw();
191+
} else {
192+
$('#from'+colIdx).val('');
193+
api.column(colIdx).search('').draw();
194+
}
188195
}
189196
});
190197
$('#:tagId').find('#to'+colIdx)
191198
.datepicker()
192199
.on('change', function () {
193-
if($('#from'+colIdx).val() !== '') {
200+
if($('#from'+colIdx).val() !== '' && validateDate($('#from'+colIdx).val())) {
194201
api.column(colIdx).search($('#:tagId').find('#from'+colIdx).val() + '|' + $('#:tagId').find('#to' + colIdx).val()).draw();
195202
} else {
196-
api.column(colIdx).search( '|' + $('#:tagId').find('#to' + colIdx).val()).draw();
203+
$('#from'+colIdx).val('');
204+
if ($('#to'+colIdx).val() !== '' && validateDate($('#to'+colIdx).val())) {
205+
api.column(colIdx).search('|' + $('#:tagId').find('#to' + colIdx).val()).draw();
206+
} else {
207+
$('#to'+colIdx).val('');
208+
api.column(colIdx).search('').draw();
209+
}
197210
}
211+
198212
});
199213
break;
200214
case 'input':
@@ -488,6 +502,11 @@ public function getDatatableScript(): string
488502
);
489503
}
490504

505+
public function setCallback($callback): void
506+
{
507+
$this->setConfig('drawCallback', $callback);
508+
}
509+
491510
public function getCommonScript(): string
492511
{
493512
return 'console.log("from getCommonScript")';
@@ -518,7 +537,7 @@ public function setGetDataUrl($defaultUrl = null)
518537
};
519538
GET_DATA;
520539
} else {
521-
if ($csrfToken !== null){
540+
if ($csrfToken !== null) {
522541
$headers = "headers: { 'X-CSRF-Token': '{$csrfToken}' },";
523542
} else {
524543
$headers = "";
@@ -582,9 +601,9 @@ protected function processColumnRenderCallbacks()
582601
if ($key['width'] ?? null) {
583602
$output .= "\nwidth: '{$key['width']}',";
584603
}
585-
if ($key['className'] ?? null) {
586-
$output .= "\nclassName: '{$key['className']}',";
587-
}
604+
if ($key['className'] ?? null) {
605+
$output .= "\nclassName: '{$key['className']}',";
606+
}
588607
}
589608
$output .= '}';
590609

@@ -606,7 +625,7 @@ protected function processActionLinkList(array $sourceLinks): array
606625
{
607626
$links = [];
608627
foreach ($sourceLinks as $link) {
609-
$links[] = $this->processActionLink($link);
628+
$links[] = $this->processActionLink($link)->render();
610629
}
611630

612631
return $links;
@@ -616,34 +635,29 @@ protected function processActionLinkList(array $sourceLinks): array
616635
* Format link with specified options from links array.
617636
*
618637
* @param array $link
619-
* @return string
638+
* @return LinkInterface
620639
*/
621-
protected function processActionLink(array $link): string
640+
protected function processActionLink(array $link): LinkInterface
622641
{
623642
switch ($link['type'] ?? null) {
624643
case Datatables::LINK_TYPE_DELETE:
625644
case Datatables::LINK_TYPE_PUT:
626645
case Datatables::LINK_TYPE_POST:
627-
$output = new \CakeDC\Datatables\View\Formatter\Link\PostLink($this->Helper, $link);
646+
$output = new \CakeDC\Datatables\View\LinkFormatter\PostLink($this->Helper, $link);
628647
break;
629648
case Datatables::LINK_TYPE_CUSTOM:
630-
if (!class_exists($link['formatter'] ?? null)) {
631-
throw new \OutOfBoundsException("Please specify a custom formatter");
649+
if (!class_exists($link['linkFormatter'] ?? null)) {
650+
throw new \OutOfBoundsException("Please specify a custom linkFormatter");
632651
}
633-
$output = new $link['formatter']($this->Helper, $link);
634-
635-
if (!method_exists($output, 'link')) {
636-
throw new \OutOfBoundsException("Method link is not found in class");
637-
}
638-
652+
$output = new $link['linkFormatter']($this->Helper, $link);
639653
break;
640654
case Datatables::LINK_TYPE_GET:
641655
default:
642-
$output = new \CakeDC\Datatables\View\Formatter\Link\Link($this->Helper, $link);
656+
$output = new \CakeDC\Datatables\View\LinkFormatter\Link($this->Helper, $link);
643657
break;
644658
}
645659

646-
return $output->link();
660+
return $output;
647661
}
648662

649663
/**

src/View/Helper/DatatableHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,9 @@ public function getTableHeaders(
164164
->getTableHeaders();
165165
}
166166

167+
public function setCallback(string $functionCallback)
168+
{
169+
$this->dtInstance->setCallback($functionCallback);
170+
}
171+
167172
}

src/View/Formatter/Link/Link.php renamed to src/View/LinkFormatter/Link.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
declare(strict_types=1);
44

5-
namespace CakeDC\Datatables\View\Formatter\Link;
5+
namespace CakeDC\Datatables\View\LinkFormatter;
66

77
use Cake\Utility\Text;
88
use CakeDC\Datatables\Datatables;
99

10-
class Link extends AbstractLink
10+
class Link implements LinkInterface
1111
{
12+
use LinkTrait;
13+
1214
protected $_defaultConfig = [
1315
'template' => '<a href=":href" target=":target">:content</a>',
1416
'url' => null,
1517
'value' => null,
1618
'label' => null,
1719
'disable' => null,
20+
'disableValue' => '',
1821
'type' => Datatables::LINK_TYPE_GET,
1922
'confirm' => false,
2023
'target' => '_self',
@@ -23,7 +26,7 @@ class Link extends AbstractLink
2326
/**
2427
* @return string
2528
*/
26-
public function link(): string
29+
public function render(): string
2730
{
2831
$urlExtraValue = '';
2932

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CakeDC\Datatables\View\LinkFormatter;
6+
7+
use Cake\View\Helper;
8+
9+
interface LinkInterface
10+
{
11+
public function __construct(Helper $helper, array $config = []);
12+
public function initialize(array $config = []): void;
13+
public function render(): string;
14+
}

src/View/Formatter/Link/AbstractLink.php renamed to src/View/LinkFormatter/LinkTrait.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
declare(strict_types=1);
44

5-
namespace CakeDC\Datatables\View\Formatter\Link;
5+
namespace CakeDC\Datatables\View\LinkFormatter;
66

77
use Cake\Core\InstanceConfigTrait;
8+
use Cake\Utility\Text;
89
use Cake\View\Helper;
910
use Exception;
1011

11-
class AbstractLink
12+
trait LinkTrait
1213
{
1314
use InstanceConfigTrait;
1415

@@ -36,24 +37,33 @@ public function initialize(array $config = []): void
3637
/**
3738
* @return string
3839
*/
39-
public function link(): string
40+
public function render(): string
4041
{
4142
return '';
4243
}
4344

45+
protected $conditionalLinkScript = <<<CONDITIONAL_LINK_SCRIPT
46+
function (value) {
47+
const disable = :disable
48+
if (disable(value, obj)) {
49+
return value ?? "";
50+
}
51+
52+
return ':htmlLink';
53+
}(:valueObj)
54+
CONDITIONAL_LINK_SCRIPT;
55+
56+
4457
protected function conditionalLink(string $htmlLink)
4558
{
4659
if (empty($this->getConfig('disable'))) {
4760
return '\'' . $htmlLink . '\'';
4861
}
4962

50-
return 'function(value) {
51-
let disable = ' . $this->getConfig('disable') . '
52-
if (disable(value, obj)) {
53-
return value;
54-
}
55-
56-
return \'' . $htmlLink . '\';
57-
}(' . $this->getConfig('value') . ')';
63+
return Text::insert($this->conditionalLinkScript, [
64+
'disable' => $this->getConfig('disable'),
65+
'htmlLink' => $htmlLink,
66+
'valueObj' => $this->getConfig('value'),
67+
]);
5868
}
5969
}

src/View/Formatter/Link/PostLink.php renamed to src/View/LinkFormatter/PostLink.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
declare(strict_types=1);
44

5-
namespace CakeDC\Datatables\View\Formatter\Link;
5+
namespace CakeDC\Datatables\View\LinkFormatter;
66

77
use Cake\Utility\Text;
88
use CakeDC\Datatables\Datatables;
99

10-
class PostLink extends AbstractLink
10+
class PostLink implements LinkInterface
1111
{
12+
use LinkTrait;
13+
1214
protected $_defaultConfig = [
1315
'template' => '<a href=":href" target=":target" onclick=":onclick">:content</a>',
1416
'url' => null,
1517
'value' => null,
1618
'label' => null,
1719
'disable' => null,
20+
'disableValue' => '',
1821
'type' => Datatables::LINK_TYPE_POST,
1922
'target' => '_self',
2023
'confirm' => false,
@@ -24,7 +27,7 @@ class PostLink extends AbstractLink
2427
/**
2528
* @return string
2629
*/
27-
public function link(): string
30+
public function render(): string
2831
{
2932
$urlExtraValue = '';
3033

0 commit comments

Comments
 (0)