From 376e19979679b5f96969a4fc25e4a43b23f686ff Mon Sep 17 00:00:00 2001 From: Nils Haagen Date: Mon, 30 Sep 2024 16:03:59 +0200 Subject: [PATCH] [FIX] UI: remove Table\Data::withNumberOfRows(), consider initial ordering and range. (#8106) * Fix https://mantis.ilias.de/view.php?id=41328 * Fix https://mantis.ilias.de/view.php?id=42095 * Remove Table\Data::withNumberOfRows() * Replace IRSS usage of removed method --- .../Collections/View/RequestToDataTable.php | 4 ++-- .../Container/View/RequestToDataTable.php | 12 ++++++------ components/ILIAS/UI/src/Component/Table/Data.php | 5 ----- .../src/Implementation/Component/Table/Data.php | 8 +++++--- .../Component/Table/TableViewControlOrdering.php | 16 +++++++++++----- .../Table/TableViewControlPagination.php | 14 +------------- .../ILIAS/UI/src/examples/Table/Data/base.php | 10 ++++++++-- .../ILIAS/UI/tests/Component/Table/DataTest.php | 8 -------- .../Component/Table/DataViewControlsTest.php | 4 ++-- 9 files changed, 35 insertions(+), 46 deletions(-) diff --git a/components/ILIAS/ResourceStorage/classes/Collections/View/RequestToDataTable.php b/components/ILIAS/ResourceStorage/classes/Collections/View/RequestToDataTable.php index ae3f2f8ea3ae..aae0215f99a7 100755 --- a/components/ILIAS/ResourceStorage/classes/Collections/View/RequestToDataTable.php +++ b/components/ILIAS/ResourceStorage/classes/Collections/View/RequestToDataTable.php @@ -100,8 +100,8 @@ protected function buildTable(): \ILIAS\UI\Component\Table\Data $this->http->request() )->withActions( $this->action_builder->getActions() - )->withNumberOfRows( - $this->request->getItemsPerPage() + )->withRange( + new Range(0, $this->request->getItemsPerPage()) ); } diff --git a/components/ILIAS/ResourceStorage/classes/Container/View/RequestToDataTable.php b/components/ILIAS/ResourceStorage/classes/Container/View/RequestToDataTable.php index 96b5a0197c6d..7bc63de05817 100755 --- a/components/ILIAS/ResourceStorage/classes/Container/View/RequestToDataTable.php +++ b/components/ILIAS/ResourceStorage/classes/Container/View/RequestToDataTable.php @@ -45,7 +45,7 @@ class RequestToDataTable implements RequestToComponents, DataRetrieval public const F_TYPE = 'type'; public const F_MODIFICATION_DATE = 'create_date'; public const FIELD_TITLE = 'title'; - const HOME = 'HOME'; + public const HOME = 'HOME'; private \ILIAS\Data\Factory $data_factory; private \ILIAS\ResourceStorage\Services $irss; private \ILIAS\UI\Renderer $ui_renderer; @@ -127,9 +127,9 @@ protected function getBreadcrumbs(): \Generator foreach ($directories as $i => $directory) { $path_inside_zip = rtrim( - implode('/', array_slice($directories, 0, $i + 1)), - '/' - ) . '/'; + implode('/', array_slice($directories, 0, $i + 1)), + '/' + ) . '/'; $links[] = $this->ui_factory->link()->standard( $directory, $get_action($path_inside_zip) @@ -184,8 +184,8 @@ protected function buildTable(): \ILIAS\UI\Component\Table\Data $this->http->request() )->withActions( $this->action_builder->getActions() - )->withNumberOfRows( - $this->request->getItemsPerPage() + )->withRange( + new Range(0, $this->request->getItemsPerPage()) ); } diff --git a/components/ILIAS/UI/src/Component/Table/Data.php b/components/ILIAS/UI/src/Component/Table/Data.php index 0145ce801fd1..46c5221b6336 100755 --- a/components/ILIAS/UI/src/Component/Table/Data.php +++ b/components/ILIAS/UI/src/Component/Table/Data.php @@ -43,11 +43,6 @@ public function withActions(array $actions): static; */ public function withRequest(ServerRequestInterface $request): static; - /** - * Number of Rows is the amount of rows shown per page - */ - public function withNumberOfRows(int $number_of_rows): self; - /** * Not all columns are neccessarily visible; "selected optional" is the * positive list of shown columns (the non-optional columns are always shown diff --git a/components/ILIAS/UI/src/Implementation/Component/Table/Data.php b/components/ILIAS/UI/src/Implementation/Component/Table/Data.php index 6efe3575ccc8..bcf27af0b114 100755 --- a/components/ILIAS/UI/src/Implementation/Component/Table/Data.php +++ b/components/ILIAS/UI/src/Implementation/Component/Table/Data.php @@ -29,11 +29,10 @@ use ILIAS\UI\Implementation\Component\JavaScriptBindable; use ILIAS\UI\Component\JavaScriptBindable as JSBindable; use ILIAS\Data\Factory as DataFactory; - - use ILIAS\UI\Component\Input\ViewControl; use ILIAS\UI\Component\Input\Container\ViewControl as ViewControlContainer; use ILIAS\Data\Range; +use ILIAS\Data\Order; class Data extends AbstractTable implements T\Data { @@ -130,9 +129,12 @@ public function applyViewControls( $data = $view_controls->getData(); $range = $data[self::VIEWCONTROL_KEY_PAGINATION]; $range = ($range instanceof Range) ? $range->croppedTo($total_count ?? PHP_INT_MAX) : null; + $order = $data[self::VIEWCONTROL_KEY_ORDERING]; + $order = ($order instanceof Order) ? $order : null; + $table = $table ->withRange($range) - ->withOrder($data[self::VIEWCONTROL_KEY_ORDERING] ?? null) + ->withOrder($order) ->withSelectedOptionalColumns($data[self::VIEWCONTROL_KEY_FIELDSELECTION] ?? null); } diff --git a/components/ILIAS/UI/src/Implementation/Component/Table/TableViewControlOrdering.php b/components/ILIAS/UI/src/Implementation/Component/Table/TableViewControlOrdering.php index 6fc1a34fffa3..cd2f92058164 100644 --- a/components/ILIAS/UI/src/Implementation/Component/Table/TableViewControlOrdering.php +++ b/components/ILIAS/UI/src/Implementation/Component/Table/TableViewControlOrdering.php @@ -21,7 +21,7 @@ namespace ILIAS\UI\Implementation\Component\Table; use ILIAS\UI\Component\Input\ViewControl\Sortation; -use ILIAS\UI\Component\Input\ViewControl\NullControl; +use ILIAS\UI\Implementation\Component\Input\ViewControl; use ILIAS\UI\Component\Table\Column\Column; use ILIAS\Data\Order; @@ -31,7 +31,7 @@ trait TableViewControlOrdering protected function initViewControlOrdering(): void { - $this->order = $this->data_factory->order($this->initialOrder(), Order::ASC); + $this->order = $this->getOrder(); } private function initialOrder(): string @@ -47,7 +47,7 @@ private function initialOrder(): string return array_key_first($sortable_visible_cols); } - protected function getViewControlOrdering(int|null $total_count): Sortation|NullControl + protected function getViewControlOrdering(int|null $total_count): Sortation|ViewControl\Group { $sortable_visible_cols = array_filter( @@ -58,7 +58,10 @@ protected function getViewControlOrdering(int|null $total_count): Sortation|Null if ($sortable_visible_cols === [] || ($total_count !== null && $total_count < 2) ) { - return $this->view_control_factory->nullControl(); + return $this->view_control_factory->group([ + $this->view_control_factory->nullControl(), + $this->view_control_factory->nullControl() + ]); } $sort_options = []; @@ -71,7 +74,10 @@ protected function getViewControlOrdering(int|null $total_count): Sortation|Null $sort_options[$labels[0]] = $order_asc; $sort_options[$labels[1]] = $order_desc; } - return $this->view_control_factory->sortation($sort_options); + + $value = $this->getOrder()->join('', fn($ret, $key, $value) => [$key, $value]); + return $this->view_control_factory->sortation($sort_options) + ->withValue($value); } public function withOrder(?Order $order): self diff --git a/components/ILIAS/UI/src/Implementation/Component/Table/TableViewControlPagination.php b/components/ILIAS/UI/src/Implementation/Component/Table/TableViewControlPagination.php index 8cd3c2b0a9f2..5de6d8af4d58 100644 --- a/components/ILIAS/UI/src/Implementation/Component/Table/TableViewControlPagination.php +++ b/components/ILIAS/UI/src/Implementation/Component/Table/TableViewControlPagination.php @@ -31,7 +31,7 @@ trait TableViewControlPagination protected function initViewControlpagination(): void { - $this->range = $this->data_factory->range(0, $this->number_of_rows); + $this->range = $this->getRange(); } protected function getViewControlPagination(?int $total_count = null): ViewControl\Pagination|ViewControl\Group @@ -53,18 +53,6 @@ protected function getViewControlPagination(?int $total_count = null): ViewContr ]); } - public function withNumberOfRows(int $number_of_rows): self - { - $clone = clone $this; - $clone->number_of_rows = $number_of_rows; - return $clone; - } - - public function getNumberOfRows(): int - { - return $this->number_of_rows; - } - public function withRange(?Range $range): self { $clone = clone $this; diff --git a/components/ILIAS/UI/src/examples/Table/Data/base.php b/components/ILIAS/UI/src/examples/Table/Data/base.php index ae6b4a527080..3ef40a528c8a 100755 --- a/components/ILIAS/UI/src/examples/Table/Data/base.php +++ b/components/ILIAS/UI/src/examples/Table/Data/base.php @@ -157,10 +157,10 @@ public function getRows( $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_x.svg', '', 'small'), ]; $icon = $icons[2]; - if($record['achieve'] > 80) { + if ($record['achieve'] > 80) { $icon = $icons[0]; } - if($record['achieve'] < 30) { + if ($record['achieve'] < 30) { $icon = $icons[1]; } $record['achieve'] = $icon; @@ -227,6 +227,12 @@ protected function getRecords(Range $range = null, Order $order = null): array ->data('a data table', $columns, $data_retrieval) ->withId('example_base') ->withActions($actions) + + //these are initial settings that apply if the according view control + //has not been operated, yet + ->withRange(new Range(0, 2)) + ->withOrder(new Order('achieve', Order::DESC)) + ->withRequest($request); /** diff --git a/components/ILIAS/UI/tests/Component/Table/DataTest.php b/components/ILIAS/UI/tests/Component/Table/DataTest.php index d5b20c883467..50a015f7e305 100755 --- a/components/ILIAS/UI/tests/Component/Table/DataTest.php +++ b/components/ILIAS/UI/tests/Component/Table/DataTest.php @@ -73,7 +73,6 @@ public function testDataTableBasicConstruction(): void $data = $this->getDataRetrieval(); $cols = ['f0' => $this->getTableFactory()->column()->text("col1")]; $table = $this->getTableFactory()->data('title', $cols, $data); - $this->assertEquals(800, $table->getNumberOfRows()); $this->assertInstanceOf(Order::class, $table->getOrder()); $this->assertInstanceOf(Range::class, $table->getRange()); $this->assertInstanceOf(I\Signal::class, $table->getAsyncActionSignal()); @@ -154,13 +153,6 @@ public function testDataTableWithRequest(): void $this->assertEquals($request, $table->withRequest($request)->getRequest()); } - public function testDataTableWithNumberOfRows(): void - { - $table = $this->getTable(); - $nor = 12; - $this->assertEquals($nor, $table->withNumberOfRows($nor)->getNumberOfRows()); - } - public function testDataTableWithOrder(): void { $table = $this->getTable(); diff --git a/components/ILIAS/UI/tests/Component/Table/DataViewControlsTest.php b/components/ILIAS/UI/tests/Component/Table/DataViewControlsTest.php index e5f42a3d4fe5..c60859dca470 100755 --- a/components/ILIAS/UI/tests/Component/Table/DataViewControlsTest.php +++ b/components/ILIAS/UI/tests/Component/Table/DataViewControlsTest.php @@ -145,7 +145,7 @@ public function testDataTableHasNoOrderingViewControl(): void array_keys($inputs) ); $this->assertInstanceOf( - I\Input\ViewControl\NullControl::class, + I\Input\ViewControl\Group::class, $inputs[C\Table\Data::VIEWCONTROL_KEY_ORDERING] ); } @@ -173,7 +173,7 @@ public function testDataTableHasNoOrderingViewControlDueToRecordCount(): void array_keys($inputs) ); $this->assertInstanceOf( - I\Input\ViewControl\NullControl::class, + I\Input\ViewControl\Group::class, $inputs[C\Table\Data::VIEWCONTROL_KEY_ORDERING] ); }