Skip to content

Commit

Permalink
Merge pull request #1282 from lee-to/table-builder-lazy
Browse files Browse the repository at this point in the history
feat: TableBuilder lazy
  • Loading branch information
lee-to authored Oct 2, 2024
2 parents 93183c9 + 9832ccf commit f87f1b3
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 98 deletions.
5 changes: 2 additions & 3 deletions src/Laravel/config/moonshine.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
return [
'title' => env('MOONSHINE_TITLE', 'MoonShine'),
'domain' => env('MOONSHINE_DOMAIN'),

'prefix' => 'admin',
'page_prefix' => 'page',
'prefix' => env('MOONSHINE_ROUTE_PREFIX', 'admin'),
'page_prefix' =>env('MOONSHINE_PAGE_PREFIX', 'page'),

'middleware' => [
EncryptCookies::class,
Expand Down
14 changes: 7 additions & 7 deletions src/UI/dist/assets/app.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/UI/resources/js/Components/TableBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {crudFormQuery} from '../Support/Forms.js'
import sortableFunction from './Sortable.js'
import {listComponentRequest} from '../Request/Sets.js'
import {urlWithQuery} from '../Request/Core.js'
import {dispatchEvents} from '../Support/DispatchEvents.js'

export default (
creatable = false,
Expand All @@ -13,6 +14,7 @@ export default (
actionsOpen: false,
lastRow: null,
table: null,
container: null,
block: null,
async: async,
asyncUrl: asyncUrl,
Expand All @@ -23,6 +25,8 @@ export default (
init() {
this.block = this.$root
this.table = this.$root.querySelector('table')
this.container = this.$root.closest('.js-table-builder-container')

const removeAfterClone = this.table?.dataset?.removeAfterClone
const tbody = this.table?.querySelector('tbody')
const tfoot = this.table?.querySelector('tfoot')
Expand Down Expand Up @@ -56,6 +60,14 @@ export default (
}

this.initColumnSelection()

if(this.container?.dataset?.lazy) {
const event = this.container?.dataset?.lazy
this.container.removeAttribute('data-lazy')

this.$nextTick(() => dispatchEvents(event, 'success', this))

}
},
add(force = false) {
if (!this.creatable && !force) {
Expand Down
5 changes: 4 additions & 1 deletion src/UI/resources/js/Request/Sets.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ export function listComponentRequest(component, pushState = false) {
dispatchEvents(t.$root.dataset.events, 'success', t)
}

t.$root.outerHTML = data
let tempElement = document.createElement('div');
tempElement.innerHTML = data;

t.$root.outerHTML = tempElement.firstElementChild.innerHTML
t.loading = false
})
.withErrorCallback(stopLoading)
Expand Down
180 changes: 93 additions & 87 deletions src/UI/resources/views/components/table/builder.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,112 +16,118 @@
'reorderable' => false,
'searchable' => false,
'sticky' => false,
'lazy' => false,
'columnSelection' => false,
'searchValue' => '',
'translates' => [],
])
<div x-data="tableBuilder(
<div
class="js-table-builder-container"
@if($async && $lazy) data-lazy="{{ "table_updated:$name" }}" @endif
>
<div x-data="tableBuilder(
{{ (int) $creatable }},
{{ (int) $reorderable }},
{{ (int) $reindex }},
{{ (int) $async }},
'{{ $asyncUrl }}'
)"
data-pushstate="{{ $attributes->get('data-pushstate', false)}}"
@defineEvent('table_row_added', $name, 'add(true)')
@defineEvent('table_reindex', $name, 'resolveReindex')
@defineEventWhen($async, 'table_updated', $name, 'asyncRequest')
{{ $attributes->only(['data-events'])}}
>
@if($async && $searchable)
<div class="flex items-center gap-2">
<form action="{{ $asyncUrl }}"
@submit.prevent="asyncFormRequest"
>
<x-moonshine::form.input
name="search"
type="search"
value="{{ $searchValue }}"
placeholder="{{ $translates['search'] }}"
/>
</form>
</div>
@endif
data-pushstate="{{ $attributes->get('data-pushstate', false)}}"
@defineEvent('table_row_added', $name, 'add(true)')
@defineEvent('table_reindex', $name, 'resolveReindex')
@defineEventWhen($async, 'table_updated', $name, 'asyncRequest')
{{ $attributes->only(['data-events'])}}
>
@if($async && $searchable)
<div class="flex items-center gap-2">
<form action="{{ $asyncUrl }}"
@submit.prevent="asyncFormRequest"
>
<x-moonshine::form.input
name="search"
type="search"
value="{{ $searchValue }}"
placeholder="{{ $translates['search'] }}"
/>
</form>
</div>
@endif

@if($columnSelection)
<x-moonshine::layout.flex justify-align="end">
<x-moonshine::dropdown>
<div class="p-2">
@foreach($columns as $column => $label)
<div class="form-group form-group-inline">
<x-moonshine::form.switcher
:id="'column_selection_' . $column"
data-column-selection-checker="true"
data-column="{{ $column }}"
@change="columnSelection()"
/>
@if($columnSelection)
<x-moonshine::layout.flex justify-align="end">
<x-moonshine::dropdown>
<div class="p-2">
@foreach($columns as $column => $label)
<div class="form-group form-group-inline">
<x-moonshine::form.switcher
:id="'column_selection_' . $column"
data-column-selection-checker="true"
data-column="{{ $column }}"
@change="columnSelection()"
/>

<x-moonshine::form.label :for="'column_selection_' . $column">
{{ $label }}
</x-moonshine::form.label>
<x-moonshine::form.label :for="'column_selection_' . $column">
{{ $label }}
</x-moonshine::form.label>
</div>
@endforeach
</div>
@endforeach
</div>

<x-slot:toggler>
<x-moonshine::icon icon="table-cells" />
</x-slot:toggler>
</x-moonshine::dropdown>
</x-moonshine::layout.flex>
@endif
<x-slot:toggler>
<x-moonshine::icon icon="table-cells" />
</x-slot:toggler>
</x-moonshine::dropdown>
</x-moonshine::layout.flex>
@endif

<x-moonshine::loader x-show="loading" />
<div x-show="!loading">
<x-moonshine::table
:simple="$simple"
:notfound="$notfound"
:attributes="$attributes"
:headAttributes="$headAttributes"
:bodyAttributes="$bodyAttributes"
:footAttributes="$footAttributes"
:creatable="$creatable"
:sticky="$sticky"
:translates="$translates"
data-name="{{ $name }}"
>
@if($headRows->isNotEmpty())
<x-slot:thead>
@foreach($headRows as $row)
{!! $row !!}
@endforeach
</x-slot:thead>
@endif
<x-moonshine::loader x-show="loading" />
<div x-show="!loading">
<x-moonshine::table
:simple="$simple"
:notfound="$notfound"
:attributes="$attributes"
:headAttributes="$headAttributes"
:bodyAttributes="$bodyAttributes"
:footAttributes="$footAttributes"
:creatable="$creatable"
:sticky="$sticky"
:translates="$translates"
data-name="{{ $name }}"
>
@if($headRows->isNotEmpty())
<x-slot:thead>
@foreach($headRows as $row)
{!! $row !!}
@endforeach
</x-slot:thead>
@endif

@if($rows->isNotEmpty())
<x-slot:tbody>
@foreach($rows as $row)
{!! $row !!}
@endforeach
</x-slot:tbody>
@endif
@if($rows->isNotEmpty())
<x-slot:tbody>
@foreach($rows as $row)
{!! $row !!}
@endforeach
</x-slot:tbody>
@endif

@if($footRows->isNotEmpty())
<x-slot:tfoot>
@foreach($footRows as $row)
{!! $row !!}
@endforeach
</x-slot:tfoot>
@endif
</x-moonshine::table>
@if($footRows->isNotEmpty())
<x-slot:tfoot>
@foreach($footRows as $row)
{!! $row !!}
@endforeach
</x-slot:tfoot>
@endif
</x-moonshine::table>

@if($creatable)
<x-moonshine::layout.divider />
@if($creatable)
<x-moonshine::layout.divider />

{!! $createButton !!}
@endif
{!! $createButton !!}
@endif

@if($hasPaginator)
{!! $paginator !!}
@endif
@if($hasPaginator)
{!! $paginator !!}
@endif
</div>
</div>
</div>
11 changes: 11 additions & 0 deletions src/UI/src/Traits/HasAsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ public function isAsync(): bool
return ! is_null($this->asyncUrl);
}

/**
* @param Closure(static $ctx): static $callback
*/
public function whenAsync(Closure $callback): static
{
return $this->when(
fn() => $this->getCore()->getRequest()->get('_component_name') === $this->getName(),
fn() => $callback($this)
);
}

protected function prepareAsyncUrl(Closure|string|null $url = null): Closure|string|null
{
return $url;
Expand Down
15 changes: 15 additions & 0 deletions src/UI/src/Traits/Table/TableStates.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ trait TableStates

protected bool $isSticky = false;

protected bool $isLazy = false;

protected bool $isColumnSelection = false;

protected bool $searchable = false;
Expand Down Expand Up @@ -201,6 +203,18 @@ public function isSticky(): bool
return $this->isSticky;
}

public function lazy(): static
{
$this->isLazy = true;

return $this;
}

public function isLazy(): bool
{
return $this->isLazy;
}

public function columnSelection(): static
{
$this->isColumnSelection = true;
Expand Down Expand Up @@ -267,6 +281,7 @@ public function statesToArray(): array
'reorderable' => $this->isReorderable(),
'simple' => $this->isSimple(),
'sticky' => $this->isSticky(),
'lazy' => $this->isLazy(),
'columnSelection' => $this->isColumnSelection(),
'searchable' => $this->isSearchable(),
'searchValue' => $this->getCore()->getRequest()->get('search', ''),
Expand Down

0 comments on commit f87f1b3

Please sign in to comment.