WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 767caa4

Browse files
committed
wip
1 parent 3915c8b commit 767caa4

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Livewire Sortable

dist/livewire-sortable.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/livewire-sortable.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
"devDependencies": {
1919
"@babel/core": "^7.7.7",
2020
"@babel/preset-env": "^7.7.7",
21+
"@shopify/draggable": "^1.0.0-beta.8",
2122
"@testing-library/dom": "^6.11.0",
2223
"@testing-library/jest-dom": "^4.2.4",
2324
"babel-jest": "^24.8.0",
2425
"jest": "^24.8.0",
2526
"jsdom-simulant": "^1.1.2",
2627
"rollup": "^1.27.14",
2728
"rollup-plugin-babel": "^4.3.3",
29+
"rollup-plugin-commonjs": "^10.1.0",
2830
"rollup-plugin-filesize": "^6.2.1",
2931
"rollup-plugin-node-resolve": "^5.2.0",
3032
"rollup-plugin-terser": "^5.1.3"

rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import babel from 'rollup-plugin-babel';
22
import filesize from 'rollup-plugin-filesize';
33
import { terser } from "rollup-plugin-terser";
44
import resolve from "rollup-plugin-node-resolve"
5+
import commonjs from 'rollup-plugin-commonjs';
56

67
export default {
78
input: 'src/index.js',
@@ -12,6 +13,7 @@ export default {
1213
sourcemap: true,
1314
},
1415
plugins: [
16+
commonjs(),
1517
resolve(),
1618
filesize(),
1719
terser({

src/index.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Sortable from '@shopify/draggable/lib/sortable'
2+
3+
if (typeof window.livewire === 'undefined') {
4+
throw 'Livewire Sortable Plugin: window.livewire is undefined. Make sure @livewireScripts is placed above this script include'
5+
}
6+
7+
window.livewire.directive('sortable-group', (el, directive, component) => {
8+
// Only fire this handler on the "root" directive.
9+
if (directive.modifiers.length > 0) return
10+
11+
let options = { draggable: '[wire\\:sortable-group\\.item]' }
12+
13+
if (el.querySelector('[wire\\:sortable-group\\.handle]')) {
14+
options.handle ='[wire\\:sortable-group\\.handle]'
15+
}
16+
17+
const sortable = new Sortable(el.querySelectorAll('[wire\\:sortable-group\\.item-group]'), options);
18+
19+
sortable.on('sortable:stop', () => {
20+
setTimeout(() => {
21+
let groups = []
22+
23+
document.querySelectorAll('[wire\\:sortable-group\\.item-group]').forEach((el, index) => {
24+
let items = []
25+
el.querySelectorAll('[wire\\:sortable-group\\.item]').forEach((el, index) => {
26+
items.push({ order: index + 1, value: el.getAttribute('wire:sortable-group.item')})
27+
})
28+
29+
groups.push({
30+
order: index + 1,
31+
value: el.getAttribute('wire:sortable-group.item-group'),
32+
items: items,
33+
})
34+
})
35+
36+
component.call(directive.method, groups)
37+
}, 1)
38+
})
39+
})
40+
41+
window.livewire.directive('sortable', (el, directive, component) => {
42+
// Only fire this handler on the "root" directive.
43+
if (directive.modifiers.length > 0) return
44+
45+
let options = { draggable: '[wire\\:sortable\\.item]' }
46+
47+
if (el.querySelector('[wire\\:sortable\\.handle]')) {
48+
options.handle ='[wire\\:sortable\\.handle]'
49+
}
50+
51+
const sortable = new Sortable(el, options);
52+
53+
sortable.on('sortable:stop', () => {
54+
setTimeout(() => {
55+
let items = []
56+
57+
document.querySelectorAll('[wire\\:sortable\\.item]').forEach((el, index) => {
58+
items.push({ order: index + 1, value: el.getAttribute('wire:sortable.item')})
59+
})
60+
61+
component.call(directive.method, items)
62+
}, 1)
63+
})
64+
})

0 commit comments

Comments
 (0)