From ee4d7aef74e3f1c3c56f7e7dae1a289e73326ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20=C4=8Cern=C3=BD?= Date: Sat, 30 Sep 2023 18:13:38 +0200 Subject: [PATCH] clean --- .../column-filter.blade.php | 0 .../date-range.blade.php | 0 .../select.blade.php | 0 .../_components => components}/text.blade.php | 0 src/BaseGrid.php | 114 -------------- src/Column.php | 149 ------------------ src/Components/Select.php | 8 - src/Livewire/BaseGrid.php | 6 +- src/Livewire/Column.php | 8 +- src/Livewire/Theme.php | 2 +- src/Livewire/UiKitTheme.php | 23 --- src/Theme.php | 126 --------------- src/UiKitTheme.php | 23 --- 13 files changed, 8 insertions(+), 451 deletions(-) rename resources/views/{livewire/_components => components}/column-filter.blade.php (100%) rename resources/views/{livewire/_components => components}/date-range.blade.php (100%) rename resources/views/{livewire/_components => components}/select.blade.php (100%) rename resources/views/{livewire/_components => components}/text.blade.php (100%) delete mode 100644 src/BaseGrid.php delete mode 100644 src/Column.php delete mode 100644 src/Components/Select.php delete mode 100644 src/Theme.php delete mode 100644 src/UiKitTheme.php diff --git a/resources/views/livewire/_components/column-filter.blade.php b/resources/views/components/column-filter.blade.php similarity index 100% rename from resources/views/livewire/_components/column-filter.blade.php rename to resources/views/components/column-filter.blade.php diff --git a/resources/views/livewire/_components/date-range.blade.php b/resources/views/components/date-range.blade.php similarity index 100% rename from resources/views/livewire/_components/date-range.blade.php rename to resources/views/components/date-range.blade.php diff --git a/resources/views/livewire/_components/select.blade.php b/resources/views/components/select.blade.php similarity index 100% rename from resources/views/livewire/_components/select.blade.php rename to resources/views/components/select.blade.php diff --git a/resources/views/livewire/_components/text.blade.php b/resources/views/components/text.blade.php similarity index 100% rename from resources/views/livewire/_components/text.blade.php rename to resources/views/components/text.blade.php diff --git a/src/BaseGrid.php b/src/BaseGrid.php deleted file mode 100644 index 9d4546a..0000000 --- a/src/BaseGrid.php +++ /dev/null @@ -1,114 +0,0 @@ -reset(); - } - - public function sort($column): void - { - if ($this->sortColumn === $column) { - $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'; - } else { - $this->sortColumn = $column; - $this->sortDirection = 'asc'; - } - } - - /** - * @throws Exception - */ - public function render(): View - { - /** - * @var Column[] $columns - * @var Builder $query - */ - $columns = $this->getColumns(); - $query = $this->getDataSource(); - - foreach ($columns as $column) { - $filter = $column->getFilter(); - - if ($filter && !in_array($filter->getFiltrationType(), FiltrationType::cases())) { - throw new Exception( - 'Filtration type "' - . $filter->getFiltrationType()->name - . ' for column "' - . $column->getModelField() - . '" does not exist.' - ); - } - } - - foreach ($columns as $column) { - $activeFilters = Arr::dot($this->filter); - - if (array_key_exists($column->getModelField(), $activeFilters)) { - $searchedTerm = $activeFilters[$column->getModelField()]; - - if ($searchedTerm === null || $searchedTerm === '' || $searchedTerm === 'null') { - unset($this->filter[$column->getModelField()]); - - continue; - } - - $column->getFilter()->callBuilder( - $query, - $column->getModelField(), - $searchedTerm - ); - } - } - - if (str_contains($this->sortColumn, '.')) { - $query->orderByLeftPowerJoins( - $this->sortColumn, - $this->sortDirection - ); - } else { - $query->orderBy( - $this->sortColumn, - $this->sortDirection - ); - } - - return view('grid', [ - 'records' => $query->paginate($this->perPage), - 'columns' => $columns, - 'theme' => $this->theme, - ]); - } - -} diff --git a/src/Column.php b/src/Column.php deleted file mode 100644 index 815a851..0000000 --- a/src/Column.php +++ /dev/null @@ -1,149 +0,0 @@ -setModelField($modelField); - $this->setLabel($label ?: $modelField); - $this->setSortable(); - - $this->setRenderer(function ($value) { - return $this->defaultRender($value); - }); - } - - public static function make(string $modelField, string $label): self - { - return new static($modelField, $label); - } - - public function defaultRender(Model $record) - { - $value = data_get($record, $this->getModelField()); - - if ($value instanceof UnitEnum) { - $value = $value->name; - } - - if ($value instanceof Carbon) { - return $value->format($this->getDateFormat()); - } - - $filter = $this->getFilter(); - - if ($filter && $filter->getFilterType() === FilterType::SELECT) { - return $this->getValueLabelFromSelect($filter, $value); - } - - return $value; - } - - public function callRenderer($value) - { - return ($this->renderer)($value); - } - - public function setRenderer(Closure $renderer): Column - { - $this->renderer = $renderer; - - return $this; - } - - public function getModelField(): string - { - return $this->modelField; - } - - public function setModelField(string $modelField): void - { - $this->modelField = $modelField; - } - - public function getFilter(): ?BaseFilter - { - return $this->filter; - } - - public function setFilter(BaseFilter $filter): Column - { - $this->filter = $filter; - - return $this; - } - - public function getLabel(): string - { - return $this->label; - } - - public function setLabel(string $label): void - { - $this->label = $label; - } - - public function isSortable(): bool - { - return $this->sortable; - } - - public function setSortable($isSortable = true): self - { - $this->sortable = $isSortable; - - return $this; - } - - public function getDateFormat(): string - { - return $this->dateFormat; - } - - public function setDateFormat(string $dateFormat): Column - { - $this->dateFormat = $dateFormat; - - return $this; - } - - /************************************************ PRIVATE ************************************************/ - - /** - * This section here is because when we have for example bool value (0/1), - * we want to show label instead of value - */ - private function getValueLabelFromSelect($filter, mixed $value): ?string - { - $selectOption = collect($filter->getOptions()) - ->firstWhere(function (SelectFilterOption $option) use ($value) { - return $option->getValue() == $value; - }); - - return __($selectOption?->getLabel()); - } - -} diff --git a/src/Components/Select.php b/src/Components/Select.php deleted file mode 100644 index eb5d3f3..0000000 --- a/src/Components/Select.php +++ /dev/null @@ -1,8 +0,0 @@ - $query->paginate($this->perPage), 'columns' => $columns, 'theme' => $this->theme, diff --git a/src/Livewire/Column.php b/src/Livewire/Column.php index 06938c0..815a851 100644 --- a/src/Livewire/Column.php +++ b/src/Livewire/Column.php @@ -1,13 +1,13 @@ setTable( - 'table uk-table uk-table-divider uk-table uk-table-responsive - uk-table uk-table-hover form__table' - ) - ->setThead('') - ->setTr('row-group-actions') - ->setTh('ublaboo-datagrid-th-form-inline') - ->setTbody('grid-sortable') - ->setTd('') - ->setPagination('uk-pagination') - ->setFilterText('uk-input uk-form-small'); - } - -} diff --git a/src/Theme.php b/src/Theme.php deleted file mode 100644 index 2ff0efd..0000000 --- a/src/Theme.php +++ /dev/null @@ -1,126 +0,0 @@ -table; - } - - public function setTable(?string $table): Theme - { - $this->table = $table; - - return $this; - } - - public function getResetLink(): ?string - { - return $this->resetLink; - } - - public function setResetLink(?string $resetLink): Theme - { - $this->resetLink = $resetLink; - - return $this; - } - - public function getThead(): ?string - { - return $this->thead; - } - - public function setThead(?string $thead): Theme - { - $this->thead = $thead; - - return $this; - } - - public function getTr(): ?string - { - return $this->tr; - } - - public function setTr(?string $tr): Theme - { - $this->tr = $tr; - - return $this; - } - - public function getTh(): ?string - { - return $this->th; - } - - public function setTh(?string $th): Theme - { - $this->th = $th; - - return $this; - } - - public function getTbody(): ?string - { - return $this->tbody; - } - - public function setTbody(?string $tbody): Theme - { - $this->tbody = $tbody; - - return $this; - } - - public function getTd(): ?string - { - return $this->td; - } - - public function setTd(?string $td): Theme - { - $this->td = $td; - - return $this; - } - - public function getPagination(): ?string - { - return $this->pagination; - } - - public function setPagination(?string $pagination): Theme - { - $this->pagination = $pagination; - - return $this; - } - - public function getFilterText(): ?string - { - return $this->filterText; - } - - public function setFilterText(?string $filterText): Theme - { - $this->filterText = $filterText; - - return $this; - } - -} diff --git a/src/UiKitTheme.php b/src/UiKitTheme.php deleted file mode 100644 index f443431..0000000 --- a/src/UiKitTheme.php +++ /dev/null @@ -1,23 +0,0 @@ -setTable( - 'table uk-table uk-table-divider uk-table uk-table-responsive - uk-table uk-table-hover form__table' - ) - ->setThead('') - ->setTr('row-group-actions') - ->setTh('ublaboo-datagrid-th-form-inline') - ->setTbody('grid-sortable') - ->setTd('') - ->setPagination('uk-pagination') - ->setFilterText('uk-input uk-form-small'); - } - -}