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

Add support more sortable options by custom attribute #11

Merged
merged 4 commits into from
Jun 22, 2022
Merged
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
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ require('@nextapps-be/livewire-sortablejs');

When you only have one list of draggable items (e.g. to-do list), you have to add the following attributes to your html:
- `wire:sortable="methodName"`: This attribute should be added to the html element that encapsulates all draggable items. The value of this attribute is the Livewire method that will be executed when an item has been dragged.
- `wire:sortable.options`: This optional attribute can be added to the html element that has the `wire:sortable` attribute. With the different [options](https://github.com/SortableJS/Sortable#options) of Sortable.js, you can use this attribute to customize how the items are dragged and sorted.
- `wire:sortable.item="itemIdentifier"`: This atttribute should be added to each individual draggable item. The value of this attribute will be used to inform you about the updated order.
- `wire:sortable.handle`: This is an optional attribute. If you provide this attribute, then you will only be able to drag an item by dragging this html element. If you do not provide it, then the complete item will draggable.

```blade
<ul wire:sortable="updateTaskOrder">
<ul wire:sortable="updateTaskOrder" wire:sortable.options="{ animation: 100 }">
@foreach ($tasks as $task)
<li wire:sortable.item="{{ $task->id }}" wire:key="task-{{ $task->id }}">
<h4>{{ $task->title }}</h4>
Expand All @@ -74,16 +75,17 @@ When the order is updated, you will receive the following array structure in you
When you have multiple lists, each with items that can be moved between those different lists, you have to add the following attributes to your html:
- `wire:sortable-group="methodName"`: This attribute should be added to the html element that encapsulates all lists. The value of this attribute is the Livewire method that will be executed when an item has been dragged.
- `wire:sortable-group.item-group="groupIdentifier"`: This atttribute should be added to the root element of a list with draggable items. The value of this attribute will be used to inform you about the updated order.
- `wire:sortable-group.options`: This optional attribute can be added to every html element that has the `wire:sortable-group.item-group` attribute. With the different [options](https://github.com/SortableJS/Sortable#options) of Sortable.js, you can use this attribute to customize how the items are dragged and sorted.
- `wire:sortable-group.item="itemIdentifier"`: This atttribute should be added to each individual draggable item in each list. The value of this attribute will be used to inform you about the updated order.
- `wire:sortable-group.handle`: This is an optional attribute. If you provide this attribute, then you will only be able to drag an item by dragging this html element. If you do not provide it, then the complete item will draggable.

```blade
<div wire:sortable-group="updateTaskOrder" style="display: flex">
<div wire:sortable-group="updateTaskOrder">
@foreach ($groups as $group)
<div wire:key="group-{{ $group->id }}">
<h4>{{ $group->label }}</h4>

<ul wire:sortable-group.item-group="{{ $group->id }}">
<ul wire:sortable-group.item-group="{{ $group->id }}" wire:sortable-group.options="{ animation: 100 }">
@foreach ($group->tasks()->orderBy('order')->get() as $task)
<li wire:sortable-group.item="{{ $task->id }}" wire:key="task-{{ $task->id }}">
<span>{{ $task->title }}</span>
Expand Down Expand Up @@ -117,22 +119,24 @@ When an item is dragged, you will receive the following array structure in the L

When you have multiple lists, each with items that can be moved between those different lists and the lists themselves also need to be draggable, you have to add the following attributes to your html:
- `wire:sortable="methodName"`: This attribute should be added to the html element that encapsulates all draggable groups. The value of this attribute is the Livewire method that will be executed when a group has been dragged.
- `wire:sortable.options`: This optional attribute can be added to the html element that has the `wire:sortable` attribute. With the different [options](https://github.com/SortableJS/Sortable#options) of Sortable.js, you can use this attribute to customize how the groups are dragged and sorted.
- `wire:sortable.item="groupIdentifier"`: This atttribute should be added to each individual draggable group. The value of this attribute will be used to inform you about the updated group order.
- `wire:sortable.handle`: This is an optional attribute. If you provide this attribute, then you will only be able to drag a group by dragging this html element. If you do not provide it, then the complete group will draggable.

- `wire:sortable-group="methodName"`: This attribute should be added to the html element that encapsulates all lists. The value of this attribute is the Livewire method that will be executed when an item has been dragged.
- `wire:sortable-group.item-group="groupIdentifier"`: This atttribute should be added to the root element of a list with draggable items. The value of this attribute will be used to inform you about the updated order.
- `wire:sortable-group.options`: This optional attribute can be added to every html element that has the `wire:sortable-group.item-group` attribute. With the different [options](https://github.com/SortableJS/Sortable#options) of Sortable.js, you can use this attribute to customize how the items are dragged and sorted.
- `wire:sortable-group.item="itemIdentifier"`: This atttribute should be added to each individual draggable item in each list. The value of this attribute will be used to inform you about the updated order.
- `wire:sortable-group.handle`: This is an optional attribute. If you provide this attribute, then you will only be able to drag an item by dragging this html element. If you do not provide it, then the complete item will draggable.

```blade
<div wire:sortable="updateGroupOrder" wire:sortable-group="updateTaskOrder" style="display: flex">
<div wire:sortable="updateGroupOrder" wire:sortable-group="updateTaskOrder" wire:sortable.options="{ animation: 50 }"
@foreach ($groups as $group)
<div wire:sortable.item="{{ $group->id }}" wire:key="group-{{ $group->id }}">
<h4>{{ $group->label }}</h4>
<button wire:sortable.handle>drag group</button>

<ul wire:sortable-group.item-group="{{ $group->id }}">
<ul wire:sortable-group.item-group="{{ $group->id }}" wire:sortable-group.options="{ animation: 100 }">
@foreach ($group->tasks()->orderBy('order')->get() as $task)
<li wire:sortable-group.item="{{ $task->id }}" wire:key="task-{{ $task->id }}">
<span>{{ $task->title }}</span>
Expand Down
2 changes: 1 addition & 1 deletion dist/livewire-sortable.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/livewire-sortable.js.map

Large diffs are not rendered by default.

58 changes: 43 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ window.Livewire.directive('sortable', (el, directive, component) => {
return;
}

let options = {};

if (el.hasAttribute('wire:sortable.options')) {
options = (new Function(`return ${el.getAttribute('wire:sortable.options')};`))();
}

el.livewire_sortable = window.Sortable.create(el, {
...options,
draggable: '[wire\\:sortable\\.item]',
handle: el.querySelector('[wire\\:sortable\\.handle]') ? '[wire\\:sortable\\.handle]' : null,
sort: true,
Expand Down Expand Up @@ -43,7 +50,14 @@ window.Livewire.directive('sortable-group', (el, directive, component) => {
return;
}

let options = {};

if (el.hasAttribute('wire:sortable-group.options')) {
options = (new Function(`return ${el.getAttribute('wire:sortable-group.options')};`))();
}

el.livewire_sortable = window.Sortable.create(el, {
...options,
draggable: '[wire\\:sortable-group\\.item]',
handle: el.querySelector('[wire\\:sortable-group\\.handle]') ? '[wire\\:sortable-group\\.handle]' : null,
sort: true,
Expand Down