Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Jan 13, 2020
1 parent 3915c8b commit 767caa4
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Livewire Sortable
2 changes: 2 additions & 0 deletions dist/livewire-sortable.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/livewire-sortable.js.map

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
"devDependencies": {
"@babel/core": "^7.7.7",
"@babel/preset-env": "^7.7.7",
"@shopify/draggable": "^1.0.0-beta.8",
"@testing-library/dom": "^6.11.0",
"@testing-library/jest-dom": "^4.2.4",
"babel-jest": "^24.8.0",
"jest": "^24.8.0",
"jsdom-simulant": "^1.1.2",
"rollup": "^1.27.14",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-filesize": "^6.2.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.1.3"
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import babel from 'rollup-plugin-babel';
import filesize from 'rollup-plugin-filesize';
import { terser } from "rollup-plugin-terser";
import resolve from "rollup-plugin-node-resolve"
import commonjs from 'rollup-plugin-commonjs';

export default {
input: 'src/index.js',
Expand All @@ -12,6 +13,7 @@ export default {
sourcemap: true,
},
plugins: [
commonjs(),
resolve(),
filesize(),
terser({
Expand Down
64 changes: 64 additions & 0 deletions src/index.js
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)
})
})

0 comments on commit 767caa4

Please sign in to comment.