simple vue-based column editor

This commit is contained in:
2018-08-04 11:48:35 +02:00
parent 755c65a42d
commit e5a6527d18
22 changed files with 422 additions and 177 deletions
+21 -22
View File
@@ -72,19 +72,22 @@ class TableController extends Controller
*/
public function create()
{
$exampleColumns =
"latin,string,Latin Name\n" .
"common,string,Common Name\n" .
"lifespan,int,Lifespan (years)";
$exampleData =
"Mercenaria mercenaria,hard clam,40\n" .
"Magallana gigas,pacific oyster,30\n" .
"Patella vulgata,common limpet,20";
return view('table.create',
compact('exampleColumns', 'exampleData')
);
$columns = Column::columnsFromJson([
['name' => 'latin', 'type' => 'string', 'title' => 'Latin Name'],
['name' => 'common', 'type' => 'string', 'title' => 'Common Name'],
['name' => 'lifespan', 'type' => 'int', 'title' => 'Lifespan (years)']
]);
return view('table.create', [
'exampleColumns' => '',
'columns' => $columns,
'exampleData' => $exampleData,
]);
}
public function settings(Request $request, User $user, string $table)
@@ -141,7 +144,7 @@ class TableController extends Controller
'description' => ['nullable', VALI_TEXT],
'license' => ['nullable', VALI_TEXT],
'origin' => ['nullable', VALI_TEXT],
'columns' => 'required|string',
'columns' => 'required|json',
'data' => 'string|nullable',
]);
@@ -156,28 +159,24 @@ class TableController extends Controller
/** @var Column[] $columns */
$columns = [];
$column_keys = []; // for checking duplicates
$colTable = array_map('str_getcsv', explode("\n", $input->columns));
$colsArray = fromJSON($input->columns);
// prevent griefing via long list of columns
if (count($colTable) > 100) return $this->backWithErrors(['columns' => "Too many columns"]);
if (count($colsArray) > 100) return $this->backWithErrors(['columns' => "Too many columns"]);
foreach ($colTable as $col) {
$col = array_map('trim', $col);
if (count($col) < 2 || strlen($col[0])==0) {
foreach ($colsArray as $col) {
if (!isset($col->name) || !isset($col->type) || empty($col->name) || empty($col->type)) {
return $this->backWithErrors(['columns' => "All columns must have at least name and type."]);
}
try {
if (in_array($col[0], $column_keys)) {
return $this->backWithErrors(['columns' => "Duplicate column: $col[0]"]);
if (in_array($col->name, $column_keys)) {
return $this->backWithErrors(['columns' => "Duplicate column: $col->name"]);
}
$column_keys[] = $col[0];
$column_keys[] = $col->name;
$columns[] = new Column([
'name' => $col[0],
'type' => $col[1],
'title' => count($col) >= 3 ? $col[2] : $col[0], // title falls back to =name if not specified,
]);
if (!isset($col->title)) $col->title = $col->name;
$columns[] = new Column($col);
} catch (\Exception $e) {
return $this->backWithErrors(['columns' => $e->getMessage()]);
}