Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/Controllers/ThrustActionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ 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);
}

$action->setSelectedTargets(collect(explode(',', request('ids'))));

if(request('search')) {
$resourceName = Thrust::make($resourceName)::$searchResource ?? $resourceName;
}
return view('thrust::actions.create', [
'action' => $action,
'resourceName' => $resourceName,
Expand Down Expand Up @@ -63,22 +66,24 @@ 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(),
]);
}

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;
}
}
8 changes: 8 additions & 0 deletions src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 20 additions & 15 deletions src/resources/views/components/js/actions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
}

Expand Down Expand Up @@ -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"]){
Expand All @@ -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
Expand All @@ -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
</script>