-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3915c8b
commit 767caa4
Showing
7 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Livewire Sortable |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Sortable from '@shopify/draggable/lib/sortable' | ||
|
||
if (typeof window.livewire === 'undefined') { | ||
throw 'Livewire Sortable Plugin: window.livewire is undefined. Make sure @livewireScripts is placed above this script include' | ||
} | ||
|
||
window.livewire.directive('sortable-group', (el, directive, component) => { | ||
// Only fire this handler on the "root" directive. | ||
if (directive.modifiers.length > 0) return | ||
|
||
let options = { draggable: '[wire\\:sortable-group\\.item]' } | ||
|
||
if (el.querySelector('[wire\\:sortable-group\\.handle]')) { | ||
options.handle ='[wire\\:sortable-group\\.handle]' | ||
} | ||
|
||
const sortable = new Sortable(el.querySelectorAll('[wire\\:sortable-group\\.item-group]'), options); | ||
|
||
sortable.on('sortable:stop', () => { | ||
setTimeout(() => { | ||
let groups = [] | ||
|
||
document.querySelectorAll('[wire\\:sortable-group\\.item-group]').forEach((el, index) => { | ||
let items = [] | ||
el.querySelectorAll('[wire\\:sortable-group\\.item]').forEach((el, index) => { | ||
items.push({ order: index + 1, value: el.getAttribute('wire:sortable-group.item')}) | ||
}) | ||
|
||
groups.push({ | ||
order: index + 1, | ||
value: el.getAttribute('wire:sortable-group.item-group'), | ||
items: items, | ||
}) | ||
}) | ||
|
||
component.call(directive.method, groups) | ||
}, 1) | ||
}) | ||
}) | ||
|
||
window.livewire.directive('sortable', (el, directive, component) => { | ||
// Only fire this handler on the "root" directive. | ||
if (directive.modifiers.length > 0) return | ||
|
||
let options = { draggable: '[wire\\:sortable\\.item]' } | ||
|
||
if (el.querySelector('[wire\\:sortable\\.handle]')) { | ||
options.handle ='[wire\\:sortable\\.handle]' | ||
} | ||
|
||
const sortable = new Sortable(el, options); | ||
|
||
sortable.on('sortable:stop', () => { | ||
setTimeout(() => { | ||
let items = [] | ||
|
||
document.querySelectorAll('[wire\\:sortable\\.item]').forEach((el, index) => { | ||
items.push({ order: index + 1, value: el.getAttribute('wire:sortable.item')}) | ||
}) | ||
|
||
component.call(directive.method, items) | ||
}, 1) | ||
}) | ||
}) |