From 5bec8cb55e10c000be1711b21fc2ef7290ce77a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Sala=20Morral?= Date: Thu, 19 Oct 2023 16:10:06 +0200 Subject: [PATCH] PR fixes Fixed resource actions swap --- src/Controllers/ThrustActionsController.php | 19 ++++++---- src/Resource.php | 8 +++++ .../views/components/js/actions.blade.php | 35 +++++++++++-------- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/Controllers/ThrustActionsController.php b/src/Controllers/ThrustActionsController.php index d272c498..3b042238 100644 --- a/src/Controllers/ThrustActionsController.php +++ b/src/Controllers/ThrustActionsController.php @@ -25,7 +25,7 @@ public function toggle($resourceName, $id, $field) public function create($resourceName) { - $action = $this->findActionForResource($resourceName, request('action')); + $action = $this->findActionForResource($resourceName, request('action')); if (! $action) { abort(404); @@ -33,6 +33,9 @@ public function create($resourceName) $action->setSelectedTargets(collect(explode(',', request('ids')))); + if(request('search')) { + $resourceName = Thrust::make($resourceName)::$searchResource ?? $resourceName; + } return view('thrust::actions.create', [ 'action' => $action, 'resourceName' => $resourceName, @@ -63,11 +66,9 @@ public function perform($resourceName) public function index($resourceName) { $resource = Thrust::make($resourceName); - if(request('search') && $resource::$searchResource) { - $resource = Thrust::make($resource::$searchResource); - } + return view('thrust::components.actionsIndex', [ - 'actions' => collect($resource->actions()), + 'actions' => collect($resource->searchActions(request('search'))), 'resourceName' => $resource->name(), ]); } @@ -75,10 +76,14 @@ public function index($resourceName) private function findActionForResource($resourceName, $actionClass) { $resource = Thrust::make($resourceName); - $action = collect($resource->actions())->first(function ($action) use ($actionClass) { + $action = collect($resource->searchActions(request('search')))->first(function ($action) use ($actionClass) { return $action instanceof $actionClass; }); - $action->resource = $resource; + + $action->resource = request('search') && $resource::$searchResource + ? Thrust::make($resource::$searchResource) + : $resource; + return $action; } } diff --git a/src/Resource.php b/src/Resource.php index a5c03809..62c1d6a1 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -8,6 +8,7 @@ use BadChoice\Thrust\Contracts\FormatsNewObject; use BadChoice\Thrust\Contracts\Prunable; use BadChoice\Thrust\Exceptions\CanNotDeleteException; +use BadChoice\Thrust\Facades\Thrust; use BadChoice\Thrust\Fields\Edit; use BadChoice\Thrust\Fields\FieldContainer; use BadChoice\Thrust\Fields\Relationship; @@ -282,6 +283,13 @@ public function actions() : []; } + public function searchActions(?bool $whileSearch = false) + { + return $whileSearch && static::$searchResource + ? Thrust::make(static::$searchResource)->actions() + : $this->actions(); + } + public function filters() { return null; diff --git a/src/resources/views/components/js/actions.blade.php b/src/resources/views/components/js/actions.blade.php index f7389a82..f6be26d8 100644 --- a/src/resources/views/components/js/actions.blade.php +++ b/src/resources/views/components/js/actions.blade.php @@ -6,7 +6,7 @@ return alert("{!! __("thrust::messages.noRowsSelected") !!}") } - this.setAttribute('href', this.getAttribute('href') + "&ids=" + selected) + this.setAttribute('href', this.getAttribute('href') + "&ids=" + selected + "&search=" + searching) showPopup(this.getAttribute('href')) } @@ -35,7 +35,8 @@ function doAction(actionClass, selected){ $.post("{{ route('thrust.actions.perform', [$resourceName]) }}", { "_token": "{{ csrf_token() }}", "action" : actionClass, - "ids" : selected + "ids" : selected, + "search": searching, }).done(function(data){ document.getElementById('actions-loading').style.display = 'none' if (data["responseAsPopup"]){ @@ -58,7 +59,6 @@ function getSelectedRowsIds(){ } function toggleSelectAll(checkbox){ - console.log([...document.querySelectorAll('input[name^=selected]')]); [...document.querySelectorAll('input[name^=selected]')] .forEach(elem => checkbox.checked ? elem.checked = true @@ -68,21 +68,26 @@ function toggleSelectAll(checkbox){ registerActionPopupListeners() - window.addEventListener('thrust.searchStarted', () => { - fetch("{{ route('thrust.actions.index', ['resourceName' => $resourceName, 'search' => true]) }}").then(response => { - response.text().then(html => { - document.getElementById('thrust-resource-actions').innerHTML = html - registerActionPopupListeners() + let searching = false + @if($resource::$searchResource) + window.addEventListener('thrust.searchStarted', () => { + searching = true + fetch("{{ route('thrust.actions.index', ['resourceName' => $resourceName, 'search' => true]) }}").then(response => { + response.text().then(html => { + document.getElementById('thrust-resource-actions').innerHTML = html + registerActionPopupListeners() + }) }) }) - }) - window.addEventListener('thrust.searchEnded', () => { - fetch("{{ route('thrust.actions.index', ['resourceName' => $resourceName]) }}").then(response => { - response.text().then(html => { - document.getElementById('thrust-resource-actions').innerHTML = html - registerActionPopupListeners() + window.addEventListener('thrust.searchEnded', () => { + searching = false + fetch("{{ route('thrust.actions.index', ['resourceName' => $resourceName]) }}").then(response => { + response.text().then(html => { + document.getElementById('thrust-resource-actions').innerHTML = html + registerActionPopupListeners() + }) }) }) - }) + @endif