150 lines
3.9 KiB
PHP
150 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
|
|
class ProjectNameCoder extends Component
|
|
{
|
|
public $components = [];
|
|
public $nextId = 1;
|
|
|
|
protected $listeners = [
|
|
'nameUpdated' => 'headerLabelUpdate',
|
|
'componentUpdated' => 'handleComponentUpdate',
|
|
'removeComponent' => 'removeComponent'
|
|
];
|
|
|
|
public function mount()
|
|
{
|
|
// Inicializar con un componente vacío
|
|
$this->addComponent();
|
|
}
|
|
|
|
public function addComponent()
|
|
{
|
|
$id = $this->nextId++;
|
|
$this->components[] = [
|
|
'id' => $id,
|
|
'data' => [],
|
|
'order' => count($this->components),
|
|
'headerLabel' => ''
|
|
];
|
|
}
|
|
|
|
public function removeComponent($componentId)
|
|
{
|
|
$this->components = array_filter($this->components, function($component) use ($componentId) {
|
|
return $component['id'] != $componentId;
|
|
});
|
|
|
|
// Reindexar el orden
|
|
$this->reorderComponents();
|
|
}
|
|
|
|
public function headerLabelUpdate($componentId, $data)
|
|
{
|
|
foreach ($this->components as &$component) {
|
|
if ($component['id'] == $componentId) {
|
|
$component['headerLabel'] = $data['name'];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function handleComponentUpdate($componentId, $data)
|
|
{
|
|
foreach ($this->components as &$component) {
|
|
if ($component['id'] == $componentId) {
|
|
$component['data'] = $data;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function updateComponentOrder($orderedIds)
|
|
{
|
|
foreach ($orderedIds as $index => $id) {
|
|
foreach ($this->components as &$component) {
|
|
if ($component['id'] == $id) {
|
|
$component['order'] = $index;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ordenar el array por el campo 'order'
|
|
usort($this->components, function($a, $b) {
|
|
return $a['order'] - $b['order'];
|
|
});
|
|
}
|
|
|
|
private function reorderComponents()
|
|
{
|
|
foreach ($this->components as $index => &$component) {
|
|
$component['order'] = $index;
|
|
}
|
|
}
|
|
|
|
public function moveComponentUp($componentId)
|
|
{
|
|
$index = $this->findComponentIndex($componentId);
|
|
|
|
if ($index > 0) {
|
|
// Intercambiar con el componente anterior
|
|
$temp = $this->components[$index];
|
|
$this->components[$index] = $this->components[$index - 1];
|
|
$this->components[$index - 1] = $temp;
|
|
|
|
// Actualizar órdenes
|
|
$this->reorderComponents();
|
|
}
|
|
}
|
|
|
|
public function moveComponentDown($componentId)
|
|
{
|
|
$index = $this->findComponentIndex($componentId);
|
|
|
|
if ($index < count($this->components) - 1) {
|
|
// Intercambiar con el componente siguiente
|
|
$temp = $this->components[$index];
|
|
$this->components[$index] = $this->components[$index + 1];
|
|
$this->components[$index + 1] = $temp;
|
|
|
|
// Actualizar órdenes
|
|
$this->reorderComponents();
|
|
}
|
|
}
|
|
|
|
private function findComponentIndex($componentId)
|
|
{
|
|
foreach ($this->components as $index => $component) {
|
|
if ($component['id'] == $componentId) {
|
|
return $index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public function getComponentsCountProperty()
|
|
{
|
|
return count($this->components);
|
|
}
|
|
|
|
public function getTotalDocumentTypesProperty()
|
|
{
|
|
$total = 0;
|
|
foreach ($this->components as $component) {
|
|
if (isset($component['data']['documentTypes'])) {
|
|
$total += count($component['data']['documentTypes']);
|
|
}
|
|
}
|
|
return $total;
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.project-name-coder');
|
|
}
|
|
}
|