Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a Two Columns List Formfield #5874

Open
wants to merge 5 commits into
base: 1.6
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions publishable/lang/en/generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'action' => 'Action',
'actions' => 'Actions',
'add' => 'Add',
'add_element' => 'Add Element',
'add_folder' => 'Add Folder',
'add_new' => 'Add New',
'all_done' => 'All done',
Expand All @@ -22,6 +23,10 @@
'choose_type' => 'Choose Type',
'click_here' => 'Click Here',
'close' => 'Close',
'column' => 'Column',
'column_first' => 'First Column',
'column_second' => 'Second Column',
'column_third' => 'Third Column',
'compass' => 'Compass',
'created_at' => 'Created at',
'custom' => 'Custom',
Expand Down
5 changes: 5 additions & 0 deletions publishable/lang/pt/generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'action' => 'Ação',
'actions' => 'Ações',
'add' => 'Adicionar',
'add_element' => 'Adicionar Elemento',
'add_folder' => 'Adicionar Pasta',
'add_new' => 'Adicionar',
'all_done' => 'Concluído',
Expand All @@ -17,6 +18,10 @@
'choose_type' => 'Escolha o tipo',
'click_here' => 'Clique aqui',
'close' => 'Fechar',
'column' => 'Coluna',
'column_first' => 'Primeira Coluna',
'column_second' => 'Segunda Coluna',
'column_third' => 'Terceira Coluna',
'compass' => 'Bússola',
'created_at' => 'Criado em',
'custom' => 'Personalizado',
Expand Down
5 changes: 5 additions & 0 deletions publishable/lang/pt_br/generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'action' => 'Ação',
'actions' => 'Ações',
'add' => 'Adicionar',
'add_element' => 'Adicionar Elemento',
'add_folder' => 'Adicionar Pasta',
'add_new' => 'Adicionar',
'all_done' => 'Concluído',
Expand All @@ -22,6 +23,10 @@
'choose_type' => 'Escolha o tipo',
'click_here' => 'Clique aqui',
'close' => 'Fechar',
'column' => 'Coluna',
'column_first' => 'Primeira Coluna',
'column_seconf' => 'Segunda Coluna',
'column_third' => 'Terceira Coluna',
'compass' => 'Bússola',
'created_at' => 'Criado em',
'custom' => 'Personalizado',
Expand Down
21 changes: 21 additions & 0 deletions resources/views/bread/browse.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@
@elseif($row->type == 'text_area')
@include('voyager::multilingual.input-hidden-bread-browse')
<div>{{ mb_strlen( $data->{$row->field} ) > 200 ? mb_substr($data->{$row->field}, 0, 200) . ' ...' : $data->{$row->field} }}</div>
@elseif($row->type == 'two_columns_list')
@php
$currentTwoColumnsList = isset($data->{$row->field}) ? $data->{$row->field} : '{}';
$currentTwoColumnsList = json_decode($currentTwoColumnsList, true);
@endphp
@if(isset($currentTwoColumnsList) && (count($currentTwoColumnsList)))
<ul>
@foreach($currentTwoColumnsList as $values)
<li>
@if(isset($values) && (count($values)))
<ol>
@foreach($values as $value)
<li>{{ $value }}</li>
@endforeach
</ol>
<br>
@endif
</li>
@endforeach
</ul>
@endif
@elseif($row->type == 'file' && !empty($data->{$row->field}) )
@include('voyager::multilingual.input-hidden-bread-browse')
@if(json_decode($data->{$row->field}) !== null)
Expand Down
21 changes: 21 additions & 0 deletions resources/views/bread/read.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@
{{ __('voyager::generic.download') }}
</a>
@endif
@elseif($row->type == 'two_columns_list')
@php
$currentTwoColumnsList = isset($dataTypeContent->{$row->field}) ? $dataTypeContent->{$row->field} : '{}';
$currentTwoColumnsList = json_decode($currentTwoColumnsList, true);
@endphp
@if(isset($currentTwoColumnsList) && (count($currentTwoColumnsList)))
<ul>
@foreach($currentTwoColumnsList as $values)
<li>
@if(isset($values) && (count($values)))
<ol>
@foreach($values as $value)
<li>{{ $value }}</li>
@endforeach
</ol>
<br>
@endif
</li>
@endforeach
</ul>
@endif
@else
@include('voyager::multilingual.input-hidden-bread-read')
<p>{{ $dataTypeContent->{$row->field} }}</p>
Expand Down
145 changes: 145 additions & 0 deletions resources/views/formfields/two_columns_list.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<br>
@php
$currentTwoColumnsList = isset($dataTypeContent->{$row->field}) ? $dataTypeContent->{$row->field} : '{}';
$currentTwoColumnsList = json_decode($currentTwoColumnsList, true);
@endphp
<div>
<div id="twoColumnsList-{{ $row->field }}">
<input type="hidden" id="two-columns-list-{{ $row->field }}" name="{{ $row->field }}" data-name="{{ $row->display_name }}">
<div id="twoColumnsListContainer-{{ $row->field }}">
<table class="table table-bordered two-columns-list-table">
<thead>
<tr>
<th>{{ __('voyager::generic.column_first') }}</th>
<th>{{ __('voyager::generic.column_second') }}</th>
<th>{{ __('voyager::generic.action') }}</th>
</tr>
</thead>
<tbody>
@if(isset($currentTwoColumnsList) && (count($currentTwoColumnsList)))
@foreach($currentTwoColumnsList as $value)
<tr>
<td contenteditable="true">{{ $value['0'] }}</td>
<td contenteditable="true">{{ $value['1'] }}</td>
<td>
<button type="button" class="btn btn-warning btn-sm two-columns-list-move-up-{{ $row->field }}">&uarr;</button>
<button type="button" class="btn btn-warning btn-sm two-columns-list-move-down-{{ $row->field }}">&darr;</button>
<button type="button" class="btn btn-danger btn-sm two-columns-list-delete-row-{{ $row->field }}">{{ __('voyager::generic.delete') }}</button>
</td>
</tr>
@endforeach
@else
<tr>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td>
<button type="button" class="btn btn-warning btn-sm two-columns-list-move-up-{{ $row->field }}" disabled>&uarr;</button>
<button type="button" class="btn btn-warning btn-sm two-columns-list-move-down-{{ $row->field }}" disabled>&darr;</button>
<button type="button" class="btn btn-danger btn-sm two-columns-list-delete-row-{{ $row->field }}">{{ __('voyager::generic.delete') }}</button>
</td>
</tr>
@endif
</tbody>
</table>
</div>
<button type="button" class="btn btn-success" onclick="twoColumnsListAddRow{{ $row->field }}()">{{ __('voyager::generic.add_element') }}</button>
</div>
</div>

<script>
// Function to add a new row
function twoColumnsListAddRow{{ $row->field }}() {
const table = document.querySelector('#twoColumnsListContainer-{{ $row->field }} .two-columns-list-table tbody');
const newRow = table.insertRow();
newRow.innerHTML = `
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td>
<button type="button" class="btn btn-warning btn-sm two-columns-list-move-up-{{ $row->field }}">&uarr;</button>
<button type="button" class="btn btn-warning btn-sm two-columns-list-move-down-{{ $row->field }}">&darr;</button>
<button type="button" class="btn btn-danger btn-sm two-columns-list-delete-row-{{ $row->field }}">{{ __('voyager::generic.delete') }}</button>
</td>
`;
attachCellEventListeners{{ $row->field }}(newRow.cells[0]);
attachCellEventListeners{{ $row->field }}(newRow.cells[1]); // Attach event listener to the new second column cell
updateJson{{ $row->field }}(); // Update JSON immediately after adding a row
updateMoveButtons{{ $row->field }}(); // Update move buttons state
}

// Function to update JSON data
function updateJson{{ $row->field }}() {
const json = {};
const table = document.querySelector('#twoColumnsListContainer-{{ $row->field }} .two-columns-list-table tbody');

// Iterate through rows to construct JSON object
table.querySelectorAll('tr').forEach((row, index) => {
const column1 = row.cells[0].innerText.trim();
const column2 = row.cells[1].innerText.trim();
if (column1 !== '' || column2 !== '') {
json[index] = { "0": column1, "1": column2 };
}
});

// Update hidden input value with JSON string
document.getElementById('two-columns-list-{{ $row->field }}').value = JSON.stringify(json, null, 2);
}

// Function to attach event listeners to editable cells
function attachCellEventListeners{{ $row->field }}(cell) {
cell.addEventListener('input', updateJson{{ $row->field }});
}

// Function to move a row up
function moveRowUp{{ $row->field }}(row) {
const prevRow = row.previousElementSibling;
if (prevRow) {
row.parentNode.insertBefore(row, prevRow);
updateJson{{ $row->field }}();
updateMoveButtons{{ $row->field }}();
}
}

// Function to move a row down
function moveRowDown{{ $row->field }}(row) {
const nextRow = row.nextElementSibling;
if (nextRow) {
row.parentNode.insertBefore(nextRow, row);
updateJson{{ $row->field }}();
updateMoveButtons{{ $row->field }}();
}
}

// Function to update the state of move buttons
function updateMoveButtons{{ $row->field }}() {
const rows = document.querySelectorAll('#twoColumnsListContainer-{{ $row->field }} .two-columns-list-table tbody tr');
rows.forEach((row, index) => {
const moveUpButton = row.querySelector('.two-columns-list-move-up-{{ $row->field }}');
const moveDownButton = row.querySelector('.two-columns-list-move-down-{{ $row->field }}');
moveUpButton.disabled = index === 0;
moveDownButton.disabled = index === rows.length - 1;
});
}

// Event listener for dynamically added delete buttons and move buttons
document.addEventListener('click', function (event) {
if (event.target.classList.contains('two-columns-list-delete-row-{{ $row->field }}')) {
const row = event.target.closest('tr');
row.parentNode.removeChild(row);
updateJson{{ $row->field }}();
updateMoveButtons{{ $row->field }}();
} else if (event.target.classList.contains('two-columns-list-move-up-{{ $row->field }}')) {
moveRowUp{{ $row->field }}(event.target.closest('tr'));
} else if (event.target.classList.contains('two-columns-list-move-down-{{ $row->field }}')) {
moveRowDown{{ $row->field }}(event.target.closest('tr'));
}
});

// Attach initial event listeners to existing cells and update move buttons
document.querySelectorAll('#twoColumnsListContainer-{{ $row->field }} .two-columns-list-table tbody tr').forEach(row => {
attachCellEventListeners{{ $row->field }}(row.cells[0]);
attachCellEventListeners{{ $row->field }}(row.cells[1]); // Attach event listener to the second column cells
});
updateMoveButtons{{ $row->field }}();

updateJson{{ $row->field }}();
</script>
18 changes: 18 additions & 0 deletions src/FormFields/TwoColumnsListHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace TCG\Voyager\FormFields;

class TwoColumnsListHandler extends AbstractHandler
{
protected $codename = 'two_columns_list';

public function createContent($row, $dataType, $dataTypeContent, $options)
{
return view('voyager::formfields.two_columns_list', [
'row' => $row,
'options' => $options,
'dataType' => $dataType,
'dataTypeContent' => $dataTypeContent,
]);
}
}
14 changes: 14 additions & 0 deletions src/Http/Controllers/ContentTypes/TwoColumnsList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace TCG\Voyager\Http\Controllers\ContentTypes;

class TwoColumnsList extends BaseType
{
/**
* @return string
*/
public function handle()
{
return $this->request->input($this->row->field) ?? '{}';
}
}
4 changes: 4 additions & 0 deletions src/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use TCG\Voyager\Http\Controllers\ContentTypes\SelectMultiple;
use TCG\Voyager\Http\Controllers\ContentTypes\Text;
use TCG\Voyager\Http\Controllers\ContentTypes\Timestamp;
use TCG\Voyager\Http\Controllers\ContentTypes\TwoColumnsList;
use TCG\Voyager\Traits\AlertsMessages;
use Validator;

Expand Down Expand Up @@ -284,6 +285,9 @@ public function getContentBasedOnType(Request $request, $slug, $row, $options =
/********** TIMESTAMP TYPE **********/
case 'timestamp':
return (new Timestamp($request, $slug, $row, $options))->handle();
/********** TWO COLUMNS LIST TYPE **********/
case 'two_columns_list':
return (new TwoColumnsList($request, $slug, $row, $options))->handle();
/********** COORDINATES TYPE **********/
case 'coordinates':
return (new Coordinates($request, $slug, $row, $options))->handle();
Expand Down
1 change: 1 addition & 0 deletions src/VoyagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ protected function registerFormFields()
'text_area',
'time',
'timestamp',
'two_columns_list',
'hidden',
'coordinates',
];
Expand Down