From 390bd97db30d5630c2f271a5ecc2c24c13170e1a Mon Sep 17 00:00:00 2001 From: mhuser Date: Fri, 28 Oct 2022 16:55:05 +0200 Subject: [PATCH 01/38] Draft of Grid->addBulkModalAction() Hard-coded version in demo to pack things together. Should be moved to Grid->addBulkModalAction() --- src/Grid.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Grid.php b/src/Grid.php index 99c79e1a5b..7aef067a49 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -678,4 +678,33 @@ public function jsRow() { return $this->table->jsRow(); } + + /** + * Adds a new menu item which will open a modal dialog and dynamically + * load contents through $callback. Will pass a virtual page. + * + * @param string|array $defaults modal title or modal defaults array + * @param View $owner + * @param array $args + * + * @return View + */ + public function addModalItem($defaults, \Closure $callback, $owner = null, $args = []) + { + if ($owner === null) { + $owner = $this->getOwner()->getOwner(); + } + + if (is_string($defaults)) { + $defaults = ['title' => $defaults]; + } + + $modal = Modal::addTo($owner, $defaults); + + $modal->set(function (View $t) use ($callback) { + $callback($t, $t->stickyGet($this->name)); + }); + + return $this->addButton($button, $modal->jsShow(array_merge([$this->name => $this->getOwner()->jsRow()->data('id')], $args))); + } } From 135f04ddaddb99d11e980bf53e1e4c3f3a564a47 Mon Sep 17 00:00:00 2001 From: mhuser Date: Fri, 28 Oct 2022 17:09:51 +0200 Subject: [PATCH 02/38] Revert "Draft of Grid->addBulkModalAction()" This reverts commit 390bd97db30d5630c2f271a5ecc2c24c13170e1a. --- src/Grid.php | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index 7aef067a49..99c79e1a5b 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -678,33 +678,4 @@ public function jsRow() { return $this->table->jsRow(); } - - /** - * Adds a new menu item which will open a modal dialog and dynamically - * load contents through $callback. Will pass a virtual page. - * - * @param string|array $defaults modal title or modal defaults array - * @param View $owner - * @param array $args - * - * @return View - */ - public function addModalItem($defaults, \Closure $callback, $owner = null, $args = []) - { - if ($owner === null) { - $owner = $this->getOwner()->getOwner(); - } - - if (is_string($defaults)) { - $defaults = ['title' => $defaults]; - } - - $modal = Modal::addTo($owner, $defaults); - - $modal->set(function (View $t) use ($callback) { - $callback($t, $t->stickyGet($this->name)); - }); - - return $this->addButton($button, $modal->jsShow(array_merge([$this->name => $this->getOwner()->jsRow()->data('id')], $args))); - } } From 0ea68c9a880a28a74a5c20efd51951ea5cc068a9 Mon Sep 17 00:00:00 2001 From: mhuser Date: Fri, 28 Oct 2022 17:11:14 +0200 Subject: [PATCH 03/38] Add demo to process multiple selection in grid --- demos/collection/grid.php | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 912ccb0d09..58e342047d 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -11,10 +11,14 @@ use Atk4\Ui\JsExpression; use Atk4\Ui\JsReload; use Atk4\Ui\JsToast; +use Atk4\Ui\JsModal; use Atk4\Ui\Message; use Atk4\Ui\Table; use Atk4\Ui\UserAction\BasicExecutor; use Atk4\Ui\View; +use Atk4\Ui\VirtualPage; +use Atk4\Ui\Modal; +use Atk4\Ui\Form; /** @var \Atk4\Ui\App $app */ require_once __DIR__ . '/../init-app.php'; @@ -65,10 +69,49 @@ // $grid->addExecutorButton($deleteExecutor, new Button(['icon' => 'times circle outline'])); $sel = $grid->addSelection(); +// Executing a modal on a bulk selection +$callback = function ($m, $ids) use ($grid) { + if(!$ids){ + $msg = Message::addTo($m, [ + 'No records were selected.', + 'type' => 'error', + 'icon' => 'times', + ]); + } else { + $msg = Message::addTo($m, [ + 'The selected records will be permanently deleted.', + 'type' => 'warning', + 'icon' => 'warning', + ]); + $msg->text->addParagraph('Ids that will be deleted:'); + foreach($ids as $id){ + $msg->text->addParagraph($id); + } + + $f = Form::addTo($m); + $f->buttonSave->set('Delete'); + $f->buttonSave->icon = 'trash'; + $f->onSubmit(function ($f) use ($grid, $ids) { + // iterate trough the selected id and delete them. + foreach($ids as $id){ + $grid->model->delete($id); + } + return [[$grid->jsReload(), $f->success()]]; + }); + } +}; +$modal = Modal::addTo($grid, ['Delete selected']); +$modal->set(function (View $t) use ($callback, $grid) { + $callback($t, $t->stickyGet($grid->name)?explode(',', $t->stickyGet($grid->name)):False); +}); + +$grid->menu->addItem(['Delete selected', 'icon' => 'trash', 'class.orange active' => true]) + ->on('click', $modal->jsShow(array_merge([$grid->name => $grid->selection->jsChecked()])), []); $grid->menu->addItem('show selection')->on('click', new JsExpression( 'alert(\'Selected: \' + [])', [$sel->jsChecked()] )); + // Setting ipp with an array will add an ItemPerPageSelector to paginator. $grid->setIpp([10, 25, 50, 100]); From 8844ce2b994b04139fd3d353dbc3029de7dea374 Mon Sep 17 00:00:00 2001 From: mhuser Date: Fri, 28 Oct 2022 17:20:11 +0200 Subject: [PATCH 04/38] ordered imports --- demos/collection/grid.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 58e342047d..efc8a96ec1 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -6,19 +6,18 @@ use Atk4\Data\Model; use Atk4\Ui\Button; +use Atk4\Ui\Form; use Atk4\Ui\Grid; use Atk4\Ui\Jquery; use Atk4\Ui\JsExpression; use Atk4\Ui\JsReload; use Atk4\Ui\JsToast; -use Atk4\Ui\JsModal; use Atk4\Ui\Message; +use Atk4\Ui\Modal; use Atk4\Ui\Table; use Atk4\Ui\UserAction\BasicExecutor; use Atk4\Ui\View; -use Atk4\Ui\VirtualPage; -use Atk4\Ui\Modal; -use Atk4\Ui\Form; + /** @var \Atk4\Ui\App $app */ require_once __DIR__ . '/../init-app.php'; From 9ce212be7883387caa6c1eefd6116e3b622abdc5 Mon Sep 17 00:00:00 2001 From: mhuser Date: Fri, 28 Oct 2022 17:27:17 +0200 Subject: [PATCH 05/38] refine codestyle already --- demos/collection/grid.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index efc8a96ec1..50ac750ec2 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -18,7 +18,6 @@ use Atk4\Ui\UserAction\BasicExecutor; use Atk4\Ui\View; - /** @var \Atk4\Ui\App $app */ require_once __DIR__ . '/../init-app.php'; @@ -70,7 +69,7 @@ $sel = $grid->addSelection(); // Executing a modal on a bulk selection $callback = function ($m, $ids) use ($grid) { - if(!$ids){ + if (!$ids){ $msg = Message::addTo($m, [ 'No records were selected.', 'type' => 'error', @@ -83,25 +82,25 @@ 'icon' => 'warning', ]); $msg->text->addParagraph('Ids that will be deleted:'); - foreach($ids as $id){ + foreach ($ids as $id){ $msg->text->addParagraph($id); } - $f = Form::addTo($m); $f->buttonSave->set('Delete'); $f->buttonSave->icon = 'trash'; $f->onSubmit(function ($f) use ($grid, $ids) { // iterate trough the selected id and delete them. - foreach($ids as $id){ + foreach ($ids as $id){ $grid->model->delete($id); } + return [[$grid->jsReload(), $f->success()]]; }); } }; $modal = Modal::addTo($grid, ['Delete selected']); $modal->set(function (View $t) use ($callback, $grid) { - $callback($t, $t->stickyGet($grid->name)?explode(',', $t->stickyGet($grid->name)):False); + $callback($t, $t->stickyGet($grid->name) ? explode(',', $t->stickyGet($grid->name)) : false); }); $grid->menu->addItem(['Delete selected', 'icon' => 'trash', 'class.orange active' => true]) @@ -111,6 +110,5 @@ [$sel->jsChecked()] )); - // Setting ipp with an array will add an ItemPerPageSelector to paginator. $grid->setIpp([10, 25, 50, 100]); From 81b50ebb1c22351412eae5c637fb98620e5c06c9 Mon Sep 17 00:00:00 2001 From: mhuser Date: Fri, 28 Oct 2022 17:30:02 +0200 Subject: [PATCH 06/38] refine codestyle even more --- demos/collection/grid.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 50ac750ec2..12fc59dcf4 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -69,7 +69,7 @@ $sel = $grid->addSelection(); // Executing a modal on a bulk selection $callback = function ($m, $ids) use ($grid) { - if (!$ids){ + if (!$ids) { $msg = Message::addTo($m, [ 'No records were selected.', 'type' => 'error', @@ -82,7 +82,7 @@ 'icon' => 'warning', ]); $msg->text->addParagraph('Ids that will be deleted:'); - foreach ($ids as $id){ + foreach ($ids as $id) { $msg->text->addParagraph($id); } $f = Form::addTo($m); @@ -90,10 +90,9 @@ $f->buttonSave->icon = 'trash'; $f->onSubmit(function ($f) use ($grid, $ids) { // iterate trough the selected id and delete them. - foreach ($ids as $id){ + foreach ($ids as $id) { $grid->model->delete($id); } - return [[$grid->jsReload(), $f->success()]]; }); } From 48233f7d9a0694459b0551983eb810f9b45f383b Mon Sep 17 00:00:00 2001 From: mhuser Date: Fri, 28 Oct 2022 17:33:22 +0200 Subject: [PATCH 07/38] refine codestyle even even more --- demos/collection/grid.php | 1 + 1 file changed, 1 insertion(+) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 12fc59dcf4..1b0d15aa25 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -93,6 +93,7 @@ foreach ($ids as $id) { $grid->model->delete($id); } + return [[$grid->jsReload(), $f->success()]]; }); } From 095b8541b462de8fa7e7143080d0c293497bedab Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 1 Nov 2022 09:37:59 +0100 Subject: [PATCH 08/38] Add addModalBulkAction() to Grid --- demos/collection/grid.php | 9 ++------- src/Grid.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 1b0d15aa25..ce84974f25 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -13,7 +13,6 @@ use Atk4\Ui\JsReload; use Atk4\Ui\JsToast; use Atk4\Ui\Message; -use Atk4\Ui\Modal; use Atk4\Ui\Table; use Atk4\Ui\UserAction\BasicExecutor; use Atk4\Ui\View; @@ -98,13 +97,9 @@ }); } }; -$modal = Modal::addTo($grid, ['Delete selected']); -$modal->set(function (View $t) use ($callback, $grid) { - $callback($t, $t->stickyGet($grid->name) ? explode(',', $t->stickyGet($grid->name)) : false); -}); -$grid->menu->addItem(['Delete selected', 'icon' => 'trash', 'class.orange active' => true]) - ->on('click', $modal->jsShow(array_merge([$grid->name => $grid->selection->jsChecked()])), []); +$grid->addModalBulkAction(['Delete selected', 'icon' => 'trash', 'class.orange active' => true], $callback); + $grid->menu->addItem('show selection')->on('click', new JsExpression( 'alert(\'Selected: \' + [])', [$sel->jsChecked()] diff --git a/src/Grid.php b/src/Grid.php index 99c79e1a5b..4d91f55309 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -7,10 +7,12 @@ use Atk4\Core\Factory; use Atk4\Core\HookTrait; use Atk4\Data\Field; +use Atk4\Ui\Modal; use Atk4\Data\Model; use Atk4\Ui\UserAction\ConfirmationExecutor; use Atk4\Ui\UserAction\ExecutorFactory; use Atk4\Ui\UserAction\ExecutorInterface; +use Atk4\Ui\View; class Grid extends View { @@ -536,6 +538,38 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) { return $this->getActionButtons()->addModal($button, $title, $callback, $this, $args); } + + /** + * Similar to addModalAction but apply to a multiple recors selection and display in menu. + * When menu item is clicked, modal is displayed with the $title and $callback is executed through VirtualPage. + * + * @param string|array|View $item + * @param string $title + * @param \Closure $callback function (View $page) {... + * @param array $args extra url argument for callback + * + * @return View + */ + public function addModalBulkAction($item, \Closure $callback, $args = []) + { + if (!$this->menu) { + throw new Exception('Unable to add Modal Bulk Action without Menu'); + } + + $owner = $this->getOwner(); + + if (is_string($item)) { + $item = ['title' => $item]; + } + + $modal = Modal::addTo($owner, [$item[0]]); + + $modal->set(function (View $t) use ($callback) { + $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : false); + }); + + return $this->menu->addItem($item)->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args)), []); + } /** * Get sortBy value from url parameter. From 18169bc551876eb40499386606a48f969f45d3b9 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 1 Nov 2022 09:46:50 +0100 Subject: [PATCH 09/38] remove $title from comment --- src/Grid.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index 4d91f55309..6800c8dc87 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -543,10 +543,9 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) * Similar to addModalAction but apply to a multiple recors selection and display in menu. * When menu item is clicked, modal is displayed with the $title and $callback is executed through VirtualPage. * - * @param string|array|View $item - * @param string $title - * @param \Closure $callback function (View $page) {... - * @param array $args extra url argument for callback + * @param string|array|MenuItem $item + * @param \Closure $callback function (View $page) {... + * @param array $args extra url argument for callback * * @return View */ From 5c59aac40caccedc333ff1623160a2d9d0c812c3 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 1 Nov 2022 10:04:41 +0100 Subject: [PATCH 10/38] syntax --- src/Grid.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index 6800c8dc87..ccc54b17a6 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -7,8 +7,9 @@ use Atk4\Core\Factory; use Atk4\Core\HookTrait; use Atk4\Data\Field; -use Atk4\Ui\Modal; use Atk4\Data\Model; +use Atk4\Ui\JsQuery; +use Atk4\Ui\Modal; use Atk4\Ui\UserAction\ConfirmationExecutor; use Atk4\Ui\UserAction\ExecutorFactory; use Atk4\Ui\UserAction\ExecutorInterface; @@ -547,26 +548,26 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) * @param \Closure $callback function (View $page) {... * @param array $args extra url argument for callback * - * @return View + * @return Jquery */ public function addModalBulkAction($item, \Closure $callback, $args = []) { if (!$this->menu) { throw new Exception('Unable to add Modal Bulk Action without Menu'); } - + $owner = $this->getOwner(); - + if (is_string($item)) { $item = ['title' => $item]; } - + $modal = Modal::addTo($owner, [$item[0]]); - + $modal->set(function (View $t) use ($callback) { $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : false); }); - + return $this->menu->addItem($item)->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args)), []); } From 035fdc2afb4f3f4cf19d8e2a0d91a39956da38e2 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 1 Nov 2022 10:17:18 +0100 Subject: [PATCH 11/38] more syntax --- src/Grid.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index ccc54b17a6..a9e49a813c 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -539,14 +539,14 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) { return $this->getActionButtons()->addModal($button, $title, $callback, $this, $args); } - + /** * Similar to addModalAction but apply to a multiple recors selection and display in menu. * When menu item is clicked, modal is displayed with the $title and $callback is executed through VirtualPage. * - * @param string|array|MenuItem $item - * @param \Closure $callback function (View $page) {... - * @param array $args extra url argument for callback + * @param string|array|MenuItem $item + * @param \Closure $callback function (View $page) {... + * @param array $args extra url argument for callback * * @return Jquery */ From ad3ec17634307f78422c19fe1a2585fb6b9e5d37 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 1 Nov 2022 10:35:34 +0100 Subject: [PATCH 12/38] remove extra import --- src/Grid.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index a9e49a813c..29698c2a83 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -8,12 +8,9 @@ use Atk4\Core\HookTrait; use Atk4\Data\Field; use Atk4\Data\Model; -use Atk4\Ui\JsQuery; -use Atk4\Ui\Modal; use Atk4\Ui\UserAction\ConfirmationExecutor; use Atk4\Ui\UserAction\ExecutorFactory; use Atk4\Ui\UserAction\ExecutorInterface; -use Atk4\Ui\View; class Grid extends View { From 605035fd22a28bb1d4c2d4fde42532258cc506ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 29 May 2023 10:08:42 +0200 Subject: [PATCH 13/38] fix merge --- demos/collection/grid.php | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 844e7f507d..035f12ea07 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -9,6 +9,7 @@ use Atk4\Ui\Form; use Atk4\Ui\Grid; use Atk4\Ui\Js\Jquery; +use Atk4\Ui\Js\JsBlock; use Atk4\Ui\Js\JsExpression; use Atk4\Ui\Js\JsReload; use Atk4\Ui\Js\JsToast; @@ -76,7 +77,7 @@ // Executing a modal on a bulk selection $callback = function ($m, $ids) use ($grid) { if (!$ids) { - $msg = Message::addTo($m, [ + Message::addTo($m, [ 'No records were selected.', 'type' => 'error', 'icon' => 'times', @@ -87,20 +88,25 @@ 'type' => 'warning', 'icon' => 'warning', ]); - $msg->text->addParagraph('Ids that will be deleted:'); + $msg->text->addParagraph('IDs to be deleted:'); foreach ($ids as $id) { $msg->text->addParagraph($id); } - $f = Form::addTo($m); - $f->buttonSave->set('Delete'); - $f->buttonSave->icon = 'trash'; - $f->onSubmit(function ($f) use ($grid, $ids) { - // iterate trough the selected id and delete them. - foreach ($ids as $id) { - $grid->model->delete($id); - } - - return [[$grid->jsReload(), $f->success()]]; + $form = Form::addTo($m); + $form->buttonSave->set('Delete'); + $form->buttonSave->icon = 'trash'; + $form->onSubmit(function (Form $form) use ($grid, $ids) { + // iterate trough the selected IDs and delete them + $grid->model->atomic(function () use ($grid, $ids) { + foreach ($ids as $id) { + $grid->model->delete($id); + } + }); + + return new JsBlock([ + $grid->jsReload(), + $form->jsSuccess(), + ]); }); } }; From 59fbea9215539a8b3ed6f4520284a4c79e56dbea Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 13 Jun 2023 00:08:06 +0200 Subject: [PATCH 14/38] implement reviews --- demos/collection/grid.php | 8 ++++---- src/Grid.php | 17 ++++++----------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 035f12ea07..86a7089578 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -75,15 +75,15 @@ $sel = $grid->addSelection(); // Executing a modal on a bulk selection -$callback = function ($m, $ids) use ($grid) { +$callback = function (View $modal, ?Array $ids) use ($grid) { if (!$ids) { - Message::addTo($m, [ + Message::addTo($modal, [ 'No records were selected.', 'type' => 'error', 'icon' => 'times', ]); } else { - $msg = Message::addTo($m, [ + $msg = Message::addTo($modal, [ 'The selected records will be permanently deleted.', 'type' => 'warning', 'icon' => 'warning', @@ -92,7 +92,7 @@ foreach ($ids as $id) { $msg->text->addParagraph($id); } - $form = Form::addTo($m); + $form = Form::addTo($modal); $form->buttonSave->set('Delete'); $form->buttonSave->icon = 'trash'; $form->onSubmit(function (Form $form) use ($grid, $ids) { diff --git a/src/Grid.php b/src/Grid.php index 914f50a7b3..bac1578115 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -515,30 +515,25 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) /** * Similar to addModalAction but apply to a multiple recors selection and display in menu. - * When menu item is clicked, modal is displayed with the $title and $callback is executed through VirtualPage. + * When menu item is clicked, modal is displayed with the $title and $callback is executed through + * VirtualPage. * * @param string|array|MenuItem $item - * @param \Closure $callback function (View $page) {... - * @param array $args extra url argument for callback + * @param \Closure(View, array|null): void $callback + * @param array $args extra url argument for callback * * @return Jquery */ public function addModalBulkAction($item, \Closure $callback, $args = []) { - if (!$this->menu) { - throw new Exception('Unable to add Modal Bulk Action without Menu'); - } - - $owner = $this->getOwner(); - if (is_string($item)) { $item = ['title' => $item]; } - $modal = Modal::addTo($owner, [$item[0]]); + $modal = Modal::addTo($this->getOwner()); $modal->set(function (View $t) use ($callback) { - $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : false); + $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : null); }); return $this->menu->addItem($item)->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args)), []); From ba4924dc7c3bbeaf958fc3c845ab5a80065d984c Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 13 Jun 2023 00:12:22 +0200 Subject: [PATCH 15/38] fixe coding style --- demos/collection/grid.php | 2 +- src/Grid.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 86a7089578..90c6e2d6b3 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -75,7 +75,7 @@ $sel = $grid->addSelection(); // Executing a modal on a bulk selection -$callback = function (View $modal, ?Array $ids) use ($grid) { +$callback = function (View $modal, ?array $ids) use ($grid) { if (!$ids) { Message::addTo($modal, [ 'No records were selected.', diff --git a/src/Grid.php b/src/Grid.php index bac1578115..6f3d902da8 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -518,7 +518,7 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) * When menu item is clicked, modal is displayed with the $title and $callback is executed through * VirtualPage. * - * @param string|array|MenuItem $item + * @param string|array|MenuItem $item * @param \Closure(View, array|null): void $callback * @param array $args extra url argument for callback * From a11ae25d1ede4e8cee3184fe24837b5b22dd2dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 12 Aug 2023 01:49:55 +0200 Subject: [PATCH 16/38] fix typo --- src/Grid.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Grid.php b/src/Grid.php index 6f3d902da8..442f42895d 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -514,7 +514,7 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) } /** - * Similar to addModalAction but apply to a multiple recors selection and display in menu. + * Similar to addModalAction but apply to a multiple records selection and display in menu. * When menu item is clicked, modal is displayed with the $title and $callback is executed through * VirtualPage. * From 087c5935b5c2c93976e1e14278990f26a5f57c00 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 15:37:08 +0200 Subject: [PATCH 17/38] add behat test and fix reviews --- demos/collection/grid.php | 59 +++++++++++++++++---------------------- src/Grid.php | 12 ++++---- tests-behat/grid.feature | 11 ++++++++ 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 472687977e..0e923f0c03 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -76,42 +76,35 @@ $sel = $grid->addSelection(); // Executing a modal on a bulk selection $callback = function (View $modal, ?array $ids) use ($grid) { - if (!$ids) { - Message::addTo($modal, [ - 'No records were selected.', - 'type' => 'error', - 'icon' => 'times', - ]); - } else { - $msg = Message::addTo($modal, [ - 'The selected records will be permanently deleted.', - 'type' => 'warning', - 'icon' => 'warning', - ]); - $msg->text->addParagraph('IDs to be deleted:'); - foreach ($ids as $id) { - $msg->text->addParagraph($id); - } - $form = Form::addTo($modal); - $form->buttonSave->set('Delete'); - $form->buttonSave->icon = 'trash'; - $form->onSubmit(function (Form $form) use ($grid, $ids) { - // iterate trough the selected IDs and delete them - $grid->model->atomic(function () use ($grid, $ids) { - foreach ($ids as $id) { - $grid->model->delete($id); - } - }); - - return new JsBlock([ - $grid->jsReload(), - $form->jsSuccess(), - ]); - }); + $toDelete = ""; + foreach ($ids as $id) { + $toDelete .=$id . ", "; } + $toDelete .= "#"; + $msg = Message::addTo($modal, [ + 'The selected records will be permanently deleted: ' . $toDelete, + 'type' => 'warning', + 'icon' => 'warning', + ]); + $form = Form::addTo($modal); + $form->buttonSave->set('Delete'); + $form->buttonSave->icon = 'trash'; + $form->onSubmit(function (Form $form) use ($grid, $ids) { + // iterate trough the selected IDs and delete them + $grid->model->atomic(function () use ($grid, $ids) { + foreach ($ids as $id) { + $grid->model->delete($id); + } + }); + + return new JsBlock([ + $grid->jsReload(), + $form->jsSuccess(), + ]); + }); }; -$grid->addModalBulkAction(['Delete selected', 'icon' => 'trash', 'class.orange active' => true], $callback); +$grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], $callback); $grid->menu->addItem('show selection') ->on('click', new JsExpression( diff --git a/src/Grid.php b/src/Grid.php index 6aa7f73573..2b092b221c 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -518,11 +518,11 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) * When menu item is clicked, modal is displayed with the $title and $callback is executed through * VirtualPage. * - * @param string|array|MenuItem $item - * @param \Closure(View, array|null): void $callback - * @param array $args extra url argument for callback + * @param string|array|MenuItem $item + * @param \Closure(View, List): void $callback + * @param array $args extra URL argument for callback * - * @return Jquery + * @return View */ public function addModalBulkAction($item, \Closure $callback, $args = []) { @@ -533,10 +533,10 @@ public function addModalBulkAction($item, \Closure $callback, $args = []) $modal = Modal::addTo($this->getOwner()); $modal->set(function (View $t) use ($callback) { - $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : null); + $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : []); }); - return $this->menu->addItem($item)->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args)), []); + return $this->menu->addItem($item)->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args))); } /** diff --git a/tests-behat/grid.feature b/tests-behat/grid.feature index fd73c8f667..ef14ff7596 100644 --- a/tests-behat/grid.feature +++ b/tests-behat/grid.feature @@ -76,3 +76,14 @@ Feature: Grid Then I should see "Andorra" Then I should see "China" Then I should see "Zambia" + + Scenario: Bulk Modal Action + Given I am on "collection/grid.php" + Then I press button "Delete Selected" + Then I should see "The selected records will be permanently deleted: #" + Then I press button "Delete" + When I click using selector "//tr[5]//div.ui.child.checkbox" + When I click using selector "//tr[8]//div.ui.child.checkbox" + Then I press button "Delete Selected" + Then I should see "The selected records will be permanently deleted: 5, 8, #" + Then I press button "Delete" \ No newline at end of file From 7f942f80b0e811aeaf7c112cefe08cf11e071ea7 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 15:39:17 +0200 Subject: [PATCH 18/38] fix typo --- src/Grid.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index 2b092b221c..f272a897c2 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -519,8 +519,8 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) * VirtualPage. * * @param string|array|MenuItem $item - * @param \Closure(View, List): void $callback - * @param array $args extra URL argument for callback + * @param \Closure(View, list): void $callback + * @param array $args extra URL argument for callback * * @return View */ From f9bcc2ee64693b9374096ac2911d5dc47c72221d Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 15:41:32 +0200 Subject: [PATCH 19/38] more behat --- tests-behat/grid.feature | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests-behat/grid.feature b/tests-behat/grid.feature index ef14ff7596..16e3c8bdd6 100644 --- a/tests-behat/grid.feature +++ b/tests-behat/grid.feature @@ -80,10 +80,11 @@ Feature: Grid Scenario: Bulk Modal Action Given I am on "collection/grid.php" Then I press button "Delete Selected" - Then I should see "The selected records will be permanently deleted: #" + Then Modal is open with text "The selected records will be permanently deleted: #" Then I press button "Delete" + Given I am on "collection/grid.php" When I click using selector "//tr[5]//div.ui.child.checkbox" When I click using selector "//tr[8]//div.ui.child.checkbox" Then I press button "Delete Selected" - Then I should see "The selected records will be permanently deleted: 5, 8, #" + Then Modal is open with text "The selected records will be permanently deleted: 5, 8, #" Then I press button "Delete" \ No newline at end of file From 880a28d1c41a7529866c5c3fa018784800a2e45a Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 16:00:28 +0200 Subject: [PATCH 20/38] fix coding style --- demos/collection/grid.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 0e923f0c03..8b85484bc0 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -76,11 +76,11 @@ $sel = $grid->addSelection(); // Executing a modal on a bulk selection $callback = function (View $modal, ?array $ids) use ($grid) { - $toDelete = ""; + $toDelete = ''; foreach ($ids as $id) { - $toDelete .=$id . ", "; + $toDelete .= $id . ', '; } - $toDelete .= "#"; + $toDelete .= '#'; $msg = Message::addTo($modal, [ 'The selected records will be permanently deleted: ' . $toDelete, 'type' => 'warning', From 41ec7e7ca769a1efee4900112219f95f12326290 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 16:04:18 +0200 Subject: [PATCH 21/38] fix behat and typecasting --- demos/collection/grid.php | 2 +- src/Grid.php | 2 +- tests-behat/grid.feature | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 8b85484bc0..5be35e5838 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -75,7 +75,7 @@ $sel = $grid->addSelection(); // Executing a modal on a bulk selection -$callback = function (View $modal, ?array $ids) use ($grid) { +$callback = function (View $modal, array $ids) use ($grid) { $toDelete = ''; foreach ($ids as $id) { $toDelete .= $id . ', '; diff --git a/src/Grid.php b/src/Grid.php index f272a897c2..28a0496432 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -522,7 +522,7 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) * @param \Closure(View, list): void $callback * @param array $args extra URL argument for callback * - * @return View + * @return JsQuery */ public function addModalBulkAction($item, \Closure $callback, $args = []) { diff --git a/tests-behat/grid.feature b/tests-behat/grid.feature index 16e3c8bdd6..80dd7e92e0 100644 --- a/tests-behat/grid.feature +++ b/tests-behat/grid.feature @@ -79,12 +79,12 @@ Feature: Grid Scenario: Bulk Modal Action Given I am on "collection/grid.php" - Then I press button "Delete Selected" + Then I press button "Delete selected" Then Modal is open with text "The selected records will be permanently deleted: #" Then I press button "Delete" Given I am on "collection/grid.php" When I click using selector "//tr[5]//div.ui.child.checkbox" When I click using selector "//tr[8]//div.ui.child.checkbox" - Then I press button "Delete Selected" + Then I press button "Delete selected" Then Modal is open with text "The selected records will be permanently deleted: 5, 8, #" Then I press button "Delete" \ No newline at end of file From f6dab9d8dd37caf6f95de3aad688a7c1f3aa6706 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 16:32:46 +0200 Subject: [PATCH 22/38] fix typecasting and behat --- demos/collection/grid.php | 1 + src/Grid.php | 2 +- tests-behat/grid.feature | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 5be35e5838..fd3a40bfb8 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -106,6 +106,7 @@ $grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], $callback); +// TODO: replace show selection by addBulkAction $grid->menu->addItem('show selection') ->on('click', new JsExpression( 'alert(\'Selected: \' + [])', diff --git a/src/Grid.php b/src/Grid.php index 28a0496432..51dad0698e 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -522,7 +522,7 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) * @param \Closure(View, list): void $callback * @param array $args extra URL argument for callback * - * @return JsQuery + * @return Js\Jquery */ public function addModalBulkAction($item, \Closure $callback, $args = []) { diff --git a/tests-behat/grid.feature b/tests-behat/grid.feature index 80dd7e92e0..6884d90673 100644 --- a/tests-behat/grid.feature +++ b/tests-behat/grid.feature @@ -83,8 +83,8 @@ Feature: Grid Then Modal is open with text "The selected records will be permanently deleted: #" Then I press button "Delete" Given I am on "collection/grid.php" - When I click using selector "//tr[5]//div.ui.child.checkbox" - When I click using selector "//tr[8]//div.ui.child.checkbox" + When I click using selector "//tr[5]//div.ui.checkbox" + When I click using selector "//tr[8]//div.ui.checkbox" Then I press button "Delete selected" Then Modal is open with text "The selected records will be permanently deleted: 5, 8, #" Then I press button "Delete" \ No newline at end of file From a0ca20b3b5b2ff8acb01492f96162b58ff03aee0 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 18:35:10 +0200 Subject: [PATCH 23/38] fix return View --- demos/collection/grid.php | 2 +- src/Grid.php | 7 ++++--- tests-behat/grid.feature | 4 +++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index fd3a40bfb8..db98fb2413 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -106,7 +106,7 @@ $grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], $callback); -// TODO: replace show selection by addBulkAction +// TODO: replace show selection by addBulkAction and toast $grid->menu->addItem('show selection') ->on('click', new JsExpression( 'alert(\'Selected: \' + [])', diff --git a/src/Grid.php b/src/Grid.php index 51dad0698e..40ef2bdba3 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -522,7 +522,7 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) * @param \Closure(View, list): void $callback * @param array $args extra URL argument for callback * - * @return Js\Jquery + * @return View */ public function addModalBulkAction($item, \Closure $callback, $args = []) { @@ -535,8 +535,9 @@ public function addModalBulkAction($item, \Closure $callback, $args = []) $modal->set(function (View $t) use ($callback) { $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : []); }); - - return $this->menu->addItem($item)->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args))); + $this->menu->addItem($item)->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args))); + + return $modal; } /** diff --git a/tests-behat/grid.feature b/tests-behat/grid.feature index 6884d90673..4e83fbb65c 100644 --- a/tests-behat/grid.feature +++ b/tests-behat/grid.feature @@ -82,9 +82,11 @@ Feature: Grid Then I press button "Delete selected" Then Modal is open with text "The selected records will be permanently deleted: #" Then I press button "Delete" + Then I should see "Success" Given I am on "collection/grid.php" When I click using selector "//tr[5]//div.ui.checkbox" When I click using selector "//tr[8]//div.ui.checkbox" Then I press button "Delete selected" Then Modal is open with text "The selected records will be permanently deleted: 5, 8, #" - Then I press button "Delete" \ No newline at end of file + Then I press button "Delete" + Then I should see "Success" \ No newline at end of file From 18b256ce49867c77d682137cc16e932815fc9def Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 18:37:47 +0200 Subject: [PATCH 24/38] fix coding style --- src/Grid.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Grid.php b/src/Grid.php index 40ef2bdba3..14553208a9 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -536,7 +536,7 @@ public function addModalBulkAction($item, \Closure $callback, $args = []) $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : []); }); $this->menu->addItem($item)->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args))); - + return $modal; } From e1282e782f61689aea0cdf6d8b8a09abbb657074 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 20:06:13 +0200 Subject: [PATCH 25/38] add addBulkAction --- demos/collection/grid.php | 9 +++------ src/Grid.php | 26 ++++++++++++++++++++++++-- tests-behat/grid.feature | 12 +++++++++++- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index db98fb2413..9ebe3faeaa 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -106,12 +106,9 @@ $grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], $callback); -// TODO: replace show selection by addBulkAction and toast -$grid->menu->addItem('show selection') - ->on('click', new JsExpression( - 'alert(\'Selected: \' + [])', - [$sel->jsChecked()] - )); +$grid->addBulkAction(['show selection', 'icon' => 'binoculars'], function (Jquery $f, string $ids) { + return new JsToast('Selected: ' . $ids . '#'); + }); // Setting ipp with an array will add an ItemPerPageSelector to paginator. $grid->setIpp([10, 100, 1000]); diff --git a/src/Grid.php b/src/Grid.php index 14553208a9..85592ee4bf 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -535,9 +535,31 @@ public function addModalBulkAction($item, \Closure $callback, $args = []) $modal->set(function (View $t) use ($callback) { $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : []); }); - $this->menu->addItem($item)->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args))); + $mi = $this->menu->addItem($item); + $mi->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args))); - return $modal; + return $mi; + } + + /** + * Similar to addAction but apply to a multiple records selection and display in menu. + * When menu item is clicked, $callback is executed + * + * @param string|array|MenuItem $item + * @param \Closure(Js\Jquery, string): void $callback + * @param array $args extra URL argument for callback + * + */ + public function addBulkAction($item, \closure $callback, $args = []) + { + if (is_string($item)) { + $item = ['title' => $item]; + } + + $mi = $this->menu->addItem($item); + $mi->on('click', $callback, [$this->selection->jsChecked()]); + + return $mi; } /** diff --git a/tests-behat/grid.feature b/tests-behat/grid.feature index 4e83fbb65c..e24d5dbc55 100644 --- a/tests-behat/grid.feature +++ b/tests-behat/grid.feature @@ -89,4 +89,14 @@ Feature: Grid Then I press button "Delete selected" Then Modal is open with text "The selected records will be permanently deleted: 5, 8, #" Then I press button "Delete" - Then I should see "Success" \ No newline at end of file + Then I should see "Success" + + Scenario: Bulk Action + Given I am on "collection/grid.php" + Then I press button "Show selected" + Then Toast display should contain text "Selected: #" + Given I am on "collection/grid.php" + When I click using selector "//tr[5]//div.ui.checkbox" + When I click using selector "//tr[8]//div.ui.checkbox" + Then I press button "Show selected" + Then Toast display should contain text "Selected: 5,8#" \ No newline at end of file From 6cdd8bccff5ecc81c3e96db0e7974b034a6669e2 Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 20:11:43 +0200 Subject: [PATCH 26/38] fix code style --- demos/collection/grid.php | 4 ++-- src/Grid.php | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 9ebe3faeaa..94d071c996 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -107,8 +107,8 @@ $grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], $callback); $grid->addBulkAction(['show selection', 'icon' => 'binoculars'], function (Jquery $f, string $ids) { - return new JsToast('Selected: ' . $ids . '#'); - }); + return new JsToast('Selected: ' . $ids . '#'); +}); // Setting ipp with an array will add an ItemPerPageSelector to paginator. $grid->setIpp([10, 100, 1000]); diff --git a/src/Grid.php b/src/Grid.php index 85592ee4bf..255cec838e 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -543,14 +543,15 @@ public function addModalBulkAction($item, \Closure $callback, $args = []) /** * Similar to addAction but apply to a multiple records selection and display in menu. - * When menu item is clicked, $callback is executed + * When menu item is clicked, $callback is executed. * - * @param string|array|MenuItem $item - * @param \Closure(Js\Jquery, string): void $callback - * @param array $args extra URL argument for callback + * @param string|array|MenuItem $item + * @param \Closure(Js\Jquery, string) $callback + * @param array $args extra URL argument for callback * + * @return View */ - public function addBulkAction($item, \closure $callback, $args = []) + public function addBulkAction($item, \Closure $callback, $args = []) { if (is_string($item)) { $item = ['title' => $item]; From a1f4dd86300b6169357796bb1d2e1d0f8e6fd1cb Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 20:16:36 +0200 Subject: [PATCH 27/38] fix closure return type --- src/Grid.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index 255cec838e..ed56b7ef72 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -545,9 +545,9 @@ public function addModalBulkAction($item, \Closure $callback, $args = []) * Similar to addAction but apply to a multiple records selection and display in menu. * When menu item is clicked, $callback is executed. * - * @param string|array|MenuItem $item - * @param \Closure(Js\Jquery, string) $callback - * @param array $args extra URL argument for callback + * @param string|array|MenuItem $item + * @param \Closure(Js\Jquery, string): JsExpressionable $callback + * @param array $args extra URL argument for callback * * @return View */ From 24b99e441f70679209516060b391c4c57a4687ab Mon Sep 17 00:00:00 2001 From: mhuser Date: Tue, 15 Aug 2023 20:25:39 +0200 Subject: [PATCH 28/38] fix behat --- demos/collection/grid.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 94d071c996..4705ae4b7e 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -106,7 +106,7 @@ $grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], $callback); -$grid->addBulkAction(['show selection', 'icon' => 'binoculars'], function (Jquery $f, string $ids) { +$grid->addBulkAction(['Show selected', 'icon' => 'binoculars'], function (Jquery $f, string $ids) { return new JsToast('Selected: ' . $ids . '#'); }); From 87a98d96fbcc2433cb1f1d276e0c57aa83fa10c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Sep 2023 08:48:45 +0200 Subject: [PATCH 29/38] fix "static function" CS --- demos/collection/grid.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index b0e4c5d554..4701f9319c 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -75,7 +75,7 @@ $sel = $grid->addSelection(); // Executing a modal on a bulk selection -$callback = function (View $modal, array $ids) use ($grid) { +$callback = static function (View $modal, array $ids) use ($grid) { $toDelete = ''; foreach ($ids as $id) { $toDelete .= $id . ', '; @@ -89,9 +89,9 @@ $form = Form::addTo($modal); $form->buttonSave->set('Delete'); $form->buttonSave->icon = 'trash'; - $form->onSubmit(function (Form $form) use ($grid, $ids) { + $form->onSubmit(static function (Form $form) use ($grid, $ids) { // iterate trough the selected IDs and delete them - $grid->model->atomic(function () use ($grid, $ids) { + $grid->model->atomic(static function () use ($grid, $ids) { foreach ($ids as $id) { $grid->model->delete($id); } @@ -106,7 +106,7 @@ $grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], $callback); -$grid->addBulkAction(['Show selected', 'icon' => 'binoculars'], function (Jquery $f, string $ids) { +$grid->addBulkAction(['Show selected', 'icon' => 'binoculars'], static function (Jquery $f, string $ids) { return new JsToast('Selected: ' . $ids . '#'); }); From 7f2855276f679ae89792908d80df5e0dc8c06ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Sep 2023 10:34:17 +0200 Subject: [PATCH 30/38] fix no up-to-date phpdocs --- src/Grid.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index bd465e0618..02995a5401 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -52,7 +52,7 @@ class Grid extends View public $actionButtons; /** - * Calling addAction will add a new column inside $table with dropdown menu, + * Calling addActionMenuItem will add a new column inside $table with dropdown menu, * and will be re-used for next addActionMenuItem(). * * @var Table\Column|null @@ -386,7 +386,7 @@ private function getActionButtons(): Table\Column\ActionButtons } /** - * Similar to addAction. Will add Button that when click will display + * Similar to addActionButton. Will add Button that when click will display * a Dropdown menu. * * @param View|string $view @@ -498,8 +498,8 @@ public function addPopup($columnName, $popup = null, $icon = 'caret square down' } /** - * Similar to addAction but when button is clicked, modal is displayed - * with the $title and $callback is executed through VirtualPage. + * Similar to addActionButton but when button is clicked, modal is displayed + * with the $title and $callback is executed. * * @param string|array|View $button * @param string $title @@ -515,8 +515,7 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) /** * Similar to addModalAction but apply to a multiple records selection and display in menu. - * When menu item is clicked, modal is displayed with the $title and $callback is executed through - * VirtualPage. + * When menu item is clicked, modal is displayed with the $title and $callback is executed. * * @param string|array|MenuItem $item * @param \Closure(View, list): void $callback @@ -542,7 +541,7 @@ public function addModalBulkAction($item, \Closure $callback, $args = []) } /** - * Similar to addAction but apply to a multiple records selection and display in menu. + * Similar to addActionButton but apply to a multiple records selection and display in menu. * When menu item is clicked, $callback is executed. * * @param string|array|MenuItem $item From 8b7a45bc101d3263262d0ab85b298337886205b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Sep 2023 09:19:53 +0200 Subject: [PATCH 31/38] simplify --- demos/collection/grid.php | 25 +++++++++---------------- src/Grid.php | 14 +++++++------- src/Table/Column/ActionButtons.php | 2 +- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index 4701f9319c..a746f4d319 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -73,16 +73,16 @@ // TODO button is added not only to the table rows, but also below the table! // $grid->addExecutorButton($deleteExecutor, new Button(['icon' => 'times circle outline'])); -$sel = $grid->addSelection(); +$grid->addSelection(); + +$grid->addBulkAction(['Show selected', 'icon' => 'binoculars'], static function (Jquery $f, string $ids) { + return new JsToast('Selected: ' . $ids . '#'); +}); + // Executing a modal on a bulk selection -$callback = static function (View $modal, array $ids) use ($grid) { - $toDelete = ''; - foreach ($ids as $id) { - $toDelete .= $id . ', '; - } - $toDelete .= '#'; - $msg = Message::addTo($modal, [ - 'The selected records will be permanently deleted: ' . $toDelete, +$grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], static function (View $modal, array $ids) use ($grid) { + Message::addTo($modal, [ + 'The selected records will be permanently deleted: ' . implode(', ', $ids) . '#', 'type' => 'warning', 'icon' => 'warning', ]); @@ -90,7 +90,6 @@ $form->buttonSave->set('Delete'); $form->buttonSave->icon = 'trash'; $form->onSubmit(static function (Form $form) use ($grid, $ids) { - // iterate trough the selected IDs and delete them $grid->model->atomic(static function () use ($grid, $ids) { foreach ($ids as $id) { $grid->model->delete($id); @@ -102,12 +101,6 @@ $form->jsSuccess(), ]); }); -}; - -$grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], $callback); - -$grid->addBulkAction(['Show selected', 'icon' => 'binoculars'], static function (Jquery $f, string $ids) { - return new JsToast('Selected: ' . $ids . '#'); }); // Setting ipp with an array will add an ItemPerPageSelector to paginator. diff --git a/src/Grid.php b/src/Grid.php index 02995a5401..7dcb264f5a 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -530,14 +530,14 @@ public function addModalBulkAction($item, \Closure $callback, $args = []) } $modal = Modal::addTo($this->getOwner()); - $modal->set(function (View $t) use ($callback) { $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : []); }); - $mi = $this->menu->addItem($item); - $mi->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args))); - return $mi; + $menuItem = $this->menu->addItem($item); + $menuItem->on('click', $modal->jsShow(array_merge([$this->name => $this->selection->jsChecked()], $args))); + + return $menuItem; } /** @@ -556,10 +556,10 @@ public function addBulkAction($item, \Closure $callback, $args = []) $item = ['title' => $item]; } - $mi = $this->menu->addItem($item); - $mi->on('click', $callback, [$this->selection->jsChecked()]); + $menuItem = $this->menu->addItem($item); + $menuItem->on('click', $callback, [$this->selection->jsChecked()]); - return $mi; + return $menuItem; } /** diff --git a/src/Table/Column/ActionButtons.php b/src/Table/Column/ActionButtons.php index 9ccf256cae..ed1e7fdc8c 100644 --- a/src/Table/Column/ActionButtons.php +++ b/src/Table/Column/ActionButtons.php @@ -85,7 +85,7 @@ public function addButton($button, $action = null, string $confirmMsg = '', $isD */ public function addModal($button, $defaults, \Closure $callback, $owner = null, $args = []) { - if ($owner === null) { + if ($owner === null) { // TODO explicit owner should not be needed $owner = $this->getOwner()->getOwner(); } From 58706c8b8b346fce38768b635c3e9eab51dae376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Sep 2023 10:54:53 +0200 Subject: [PATCH 32/38] fix addModalBulkAction consistency --- demos/collection/grid.php | 2 +- src/Grid.php | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index a746f4d319..d27d024407 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -80,7 +80,7 @@ }); // Executing a modal on a bulk selection -$grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], static function (View $modal, array $ids) use ($grid) { +$grid->addModalBulkAction(['Delete selected', 'icon' => 'trash'], '', static function (View $modal, array $ids) use ($grid) { Message::addTo($modal, [ 'The selected records will be permanently deleted: ' . implode(', ', $ids) . '#', 'type' => 'warning', diff --git a/src/Grid.php b/src/Grid.php index 7dcb264f5a..1644854e3c 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -518,18 +518,17 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) * When menu item is clicked, modal is displayed with the $title and $callback is executed. * * @param string|array|MenuItem $item + * @param string $title * @param \Closure(View, list): void $callback * @param array $args extra URL argument for callback * * @return View */ - public function addModalBulkAction($item, \Closure $callback, $args = []) + public function addModalBulkAction($item, $title, \Closure $callback, $args = []) { - if (is_string($item)) { - $item = ['title' => $item]; - } + $modalDefaults = is_string($title) ? ['title' => $title] : []; - $modal = Modal::addTo($this->getOwner()); + $modal = Modal::addTo($this->getOwner(), $modalDefaults); $modal->set(function (View $t) use ($callback) { $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : []); }); From 5bcc05ecdebe0c7833a1dad484baaa6747a6482f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Sep 2023 11:15:56 +0200 Subject: [PATCH 33/38] improve methods order --- src/Grid.php | 44 ++++++++++++++++++++-------------------- tests-behat/grid.feature | 20 +++++++++--------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index 1644854e3c..c8ccc46591 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -513,6 +513,28 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) return $this->getActionButtons()->addModal($button, $title, $callback, $this, $args); } + /** + * Similar to addActionButton but apply to a multiple records selection and display in menu. + * When menu item is clicked, $callback is executed. + * + * @param string|array|MenuItem $item + * @param \Closure(Js\Jquery, string): JsExpressionable $callback + * @param array $args extra URL argument for callback + * + * @return View + */ + public function addBulkAction($item, \Closure $callback, $args = []) + { + if (is_string($item)) { + $item = ['title' => $item]; + } + + $menuItem = $this->menu->addItem($item); + $menuItem->on('click', $callback, [$this->selection->jsChecked()]); + + return $menuItem; + } + /** * Similar to addModalAction but apply to a multiple records selection and display in menu. * When menu item is clicked, modal is displayed with the $title and $callback is executed. @@ -539,28 +561,6 @@ public function addModalBulkAction($item, $title, \Closure $callback, $args = [] return $menuItem; } - /** - * Similar to addActionButton but apply to a multiple records selection and display in menu. - * When menu item is clicked, $callback is executed. - * - * @param string|array|MenuItem $item - * @param \Closure(Js\Jquery, string): JsExpressionable $callback - * @param array $args extra URL argument for callback - * - * @return View - */ - public function addBulkAction($item, \Closure $callback, $args = []) - { - if (is_string($item)) { - $item = ['title' => $item]; - } - - $menuItem = $this->menu->addItem($item); - $menuItem->on('click', $callback, [$this->selection->jsChecked()]); - - return $menuItem; - } - /** * Get sortBy value from URL parameter. */ diff --git a/tests-behat/grid.feature b/tests-behat/grid.feature index e24d5dbc55..5137a5cfc3 100644 --- a/tests-behat/grid.feature +++ b/tests-behat/grid.feature @@ -77,6 +77,16 @@ Feature: Grid Then I should see "China" Then I should see "Zambia" + Scenario: Bulk Action + Given I am on "collection/grid.php" + Then I press button "Show selected" + Then Toast display should contain text "Selected: #" + Given I am on "collection/grid.php" + When I click using selector "//tr[5]//div.ui.checkbox" + When I click using selector "//tr[8]//div.ui.checkbox" + Then I press button "Show selected" + Then Toast display should contain text "Selected: 5,8#" + Scenario: Bulk Modal Action Given I am on "collection/grid.php" Then I press button "Delete selected" @@ -90,13 +100,3 @@ Feature: Grid Then Modal is open with text "The selected records will be permanently deleted: 5, 8, #" Then I press button "Delete" Then I should see "Success" - - Scenario: Bulk Action - Given I am on "collection/grid.php" - Then I press button "Show selected" - Then Toast display should contain text "Selected: #" - Given I am on "collection/grid.php" - When I click using selector "//tr[5]//div.ui.checkbox" - When I click using selector "//tr[8]//div.ui.checkbox" - Then I press button "Show selected" - Then Toast display should contain text "Selected: 5,8#" \ No newline at end of file From 0c9dbb45ec6cef14a32a4744b078419183c89b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Sep 2023 11:12:34 +0200 Subject: [PATCH 34/38] addBulkAction must pass IDs as array --- demos/collection/grid.php | 4 ++-- src/Grid.php | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/demos/collection/grid.php b/demos/collection/grid.php index d27d024407..874d96df3a 100644 --- a/demos/collection/grid.php +++ b/demos/collection/grid.php @@ -75,8 +75,8 @@ $grid->addSelection(); -$grid->addBulkAction(['Show selected', 'icon' => 'binoculars'], static function (Jquery $f, string $ids) { - return new JsToast('Selected: ' . $ids . '#'); +$grid->addBulkAction(['Show selected', 'icon' => 'binoculars'], static function (Jquery $j, array $ids) { + return new JsToast('Selected: ' . implode(', ', $ids) . '#'); }); // Executing a modal on a bulk selection diff --git a/src/Grid.php b/src/Grid.php index c8ccc46591..45dc42c2b4 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -513,13 +513,21 @@ public function addModalAction($button, $title, \Closure $callback, $args = []) return $this->getActionButtons()->addModal($button, $title, $callback, $this, $args); } + /** + * @return list + */ + private function explodeSelectionValue(string $value): array + { + return $value === '' ? [] : explode(',', $value); + } + /** * Similar to addActionButton but apply to a multiple records selection and display in menu. * When menu item is clicked, $callback is executed. * - * @param string|array|MenuItem $item - * @param \Closure(Js\Jquery, string): JsExpressionable $callback - * @param array $args extra URL argument for callback + * @param string|array|MenuItem $item + * @param \Closure(Js\Jquery, list): JsExpressionable $callback + * @param array $args extra URL argument for callback * * @return View */ @@ -530,7 +538,9 @@ public function addBulkAction($item, \Closure $callback, $args = []) } $menuItem = $this->menu->addItem($item); - $menuItem->on('click', $callback, [$this->selection->jsChecked()]); + $menuItem->on('click', function (Js\Jquery $j, string $value) use ($callback) { + return $callback($j, $this->explodeSelectionValue($value)); + }, [$this->selection->jsChecked()]); return $menuItem; } @@ -552,7 +562,7 @@ public function addModalBulkAction($item, $title, \Closure $callback, $args = [] $modal = Modal::addTo($this->getOwner(), $modalDefaults); $modal->set(function (View $t) use ($callback) { - $callback($t, $t->stickyGet($this->name) ? explode(',', $t->stickyGet($this->name)) : []); + $callback($t, $this->explodeSelectionValue($t->stickyGet($this->name) ?? '')); }); $menuItem = $this->menu->addItem($item); From c1914d281827c2b2850be99961a947ed13e8a88c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Sep 2023 11:18:15 +0200 Subject: [PATCH 35/38] cleanup behat tests --- src/Grid.php | 2 +- tests-behat/grid.feature | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index 45dc42c2b4..bacd88725e 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -558,7 +558,7 @@ public function addBulkAction($item, \Closure $callback, $args = []) */ public function addModalBulkAction($item, $title, \Closure $callback, $args = []) { - $modalDefaults = is_string($title) ? ['title' => $title] : []; + $modalDefaults = is_string($title) ? ['title' => $title] : []; // @phpstan-ignore-line $modal = Modal::addTo($this->getOwner(), $modalDefaults); $modal->set(function (View $t) use ($callback) { diff --git a/tests-behat/grid.feature b/tests-behat/grid.feature index 5137a5cfc3..1cb4bd4e30 100644 --- a/tests-behat/grid.feature +++ b/tests-behat/grid.feature @@ -81,7 +81,6 @@ Feature: Grid Given I am on "collection/grid.php" Then I press button "Show selected" Then Toast display should contain text "Selected: #" - Given I am on "collection/grid.php" When I click using selector "//tr[5]//div.ui.checkbox" When I click using selector "//tr[8]//div.ui.checkbox" Then I press button "Show selected" @@ -93,7 +92,7 @@ Feature: Grid Then Modal is open with text "The selected records will be permanently deleted: #" Then I press button "Delete" Then I should see "Success" - Given I am on "collection/grid.php" + Then I click close modal When I click using selector "//tr[5]//div.ui.checkbox" When I click using selector "//tr[8]//div.ui.checkbox" Then I press button "Delete selected" From 760fe5b5106397632b03bde1d7c32bd98da2f56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Sep 2023 11:28:32 +0200 Subject: [PATCH 36/38] fix behat expectations --- tests-behat/grid.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests-behat/grid.feature b/tests-behat/grid.feature index 1cb4bd4e30..0f1b20c496 100644 --- a/tests-behat/grid.feature +++ b/tests-behat/grid.feature @@ -84,7 +84,7 @@ Feature: Grid When I click using selector "//tr[5]//div.ui.checkbox" When I click using selector "//tr[8]//div.ui.checkbox" Then I press button "Show selected" - Then Toast display should contain text "Selected: 5,8#" + Then Toast display should contain text "Selected: 5, 8#" Scenario: Bulk Modal Action Given I am on "collection/grid.php" @@ -96,6 +96,6 @@ Feature: Grid When I click using selector "//tr[5]//div.ui.checkbox" When I click using selector "//tr[8]//div.ui.checkbox" Then I press button "Delete selected" - Then Modal is open with text "The selected records will be permanently deleted: 5, 8, #" + Then Modal is open with text "The selected records will be permanently deleted: 5, 8#" Then I press button "Delete" Then I should see "Success" From 2fbd6ae8cadb13fdd8b389488a0bc4da5de7cd29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Sep 2023 11:47:48 +0200 Subject: [PATCH 37/38] unify casing --- tests-behat/grid.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests-behat/grid.feature b/tests-behat/grid.feature index 0f1b20c496..1e3a1dd508 100644 --- a/tests-behat/grid.feature +++ b/tests-behat/grid.feature @@ -77,7 +77,7 @@ Feature: Grid Then I should see "China" Then I should see "Zambia" - Scenario: Bulk Action + Scenario: Bulk action Given I am on "collection/grid.php" Then I press button "Show selected" Then Toast display should contain text "Selected: #" @@ -86,7 +86,7 @@ Feature: Grid Then I press button "Show selected" Then Toast display should contain text "Selected: 5, 8#" - Scenario: Bulk Modal Action + Scenario: Bulk modal action Given I am on "collection/grid.php" Then I press button "Delete selected" Then Modal is open with text "The selected records will be permanently deleted: #" From c38cd3a98f3bf260f3d719a9478e7c0d18843a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Sep 2023 11:50:19 +0200 Subject: [PATCH 38/38] not needed as well --- src/Grid.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Grid.php b/src/Grid.php index bacd88725e..a4704c6399 100644 --- a/src/Grid.php +++ b/src/Grid.php @@ -533,10 +533,6 @@ private function explodeSelectionValue(string $value): array */ public function addBulkAction($item, \Closure $callback, $args = []) { - if (is_string($item)) { - $item = ['title' => $item]; - } - $menuItem = $this->menu->addItem($item); $menuItem->on('click', function (Js\Jquery $j, string $value) use ($callback) { return $callback($j, $this->explodeSelectionValue($value));