Files
nuhodds_fi4/app/Livewire/BoardDispatcher.php
T
2026-09-05 07:36:26 +02:00

92 lines
2.8 KiB
PHP

<?php
namespace App\Livewire;
use Livewire\Component;
class BoardDispatcher extends Component
{
public array $lists = [];
public function mount()
{
$this->lists['source-attack'] = [
['id' => '1', 'name' => 'Source Item A', 'origin' => 'source-attack'],
['id' => '2', 'name' => 'Source Item B', 'origin' => 'source-attack'],
];
$this->lists['source-midfield'] = [
['id' => '3', 'name' => 'Source Item C', 'origin' => 'source-midfield'],
];
$this->lists['source-defence'] = [
['id' => '4', 'name' => 'Source Item D', 'origin' => 'source-defence'],
['id' => '5', 'name' => 'Source Item E', 'origin' => 'source-defence'],
];
$this->lists['source-goalkeeper'] = [
['id' => '6', 'name' => 'Source Item F', 'origin' => 'source-goalkeeper'],
];
for ($i = 1; $i <= 21; $i++) {
$this->lists["dest-{$i}"] = [];
}
}
public function moveItem($itemId, $fromList, $toList, $newIndex)
{
$itemId = (string) $itemId;
$newIndex = (int) $newIndex;
// Abbrechen falls kein Ziel vorhanden ist oder die Position unverändert blieb
if (! $toList || $fromList === $toList) {
return;
}
$foundItem = null;
// 1. Element aus der alten Liste entfernen
if (isset($this->lists[$fromList])) {
foreach ($this->lists[$fromList] as $key => $item) {
if ((string) $item['id'] === $itemId) {
$foundItem = $item;
unset($this->lists[$fromList][$key]);
$this->lists[$fromList] = array_values($this->lists[$fromList]); // Array-Keys korrigieren
break;
}
}
}
// 2. Element in die neue Zielliste am exakten Index einfügen
if ($foundItem && isset($this->lists[$toList])) {
array_splice($this->lists[$toList], $newIndex, 0, [$foundItem]);
}
}
public function returnToOrigin($itemId, $currentListId)
{
$itemId = (string) $itemId;
if (! isset($this->lists[$currentListId])) {
return;
}
$foundItem = null;
foreach ($this->lists[$currentListId] as $key => $item) {
if ((string) $item['id'] === $itemId) {
$foundItem = $item;
unset($this->lists[$currentListId][$key]);
$this->lists[$currentListId] = array_values($this->lists[$currentListId]);
break;
}
}
if ($foundItem && isset($this->lists[$foundItem['origin']])) {
array_unshift($this->lists[$foundItem['origin']], $foundItem);
}
}
public function render()
{
return view('livewire.board-dispatcher');
}
}