-
-
Save plwalters/34df500629a169e33df24c7a38228269 to your computer and use it in GitHub Desktop.
Aurelia - Sortable
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
<template> | |
<require from="./sortable-custom-attribute"></require> | |
<ul sortable="array.bind: left; options.bind: { group: 'test' }"> | |
<li repeat.for="item of left">${item}</li> | |
</ul> | |
<ul sortable="array.bind: right; options.bind: { group: 'test' }"> | |
<li repeat.for="item of right">${item}</li> | |
</ul> | |
</template> |
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
export class App { | |
left = ['Tesla', 'SpaceX', 'PayPal']; | |
right = ['GigaFactory']; | |
} |
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
<!doctype html> | |
<html> | |
<head> | |
<title>Aurelia</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body aurelia-app> | |
<h1>Loading...</h1> | |
<script src="https://ashleygrant.github.io/rjs-bundle/node_modules/requirejs/require.js"></script> | |
<script src="https://ashleygrant.github.io/rjs-bundle/config.js"></script> | |
<script> | |
require.config({ | |
paths: { | |
"sortable": "https://cdnjs.cloudflare.com/ajax/libs/Sortable/1.4.2/Sortable.min" | |
} | |
}); | |
</script> | |
<script src="https://ashleygrant.github.io/rjs-bundle/bundles/aurelia.js"></script> | |
<script src="https://ashleygrant.github.io/rjs-bundle/bundles/babel.js"></script> | |
<script> | |
require(['aurelia-bootstrapper']); | |
</script> | |
</body> | |
</html> |
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
import {inject, bindable, Lazy} from 'aurelia-framework'; | |
import Sortable from 'sortable'; | |
@inject(Element) | |
export class SortableCustomAttribute { | |
@bindable array; | |
@bindable options; | |
constructor(element) { | |
this.element = element; | |
} | |
onEnd({ item, from, to, newIndex, oldIndex }) { | |
// determine the "to" array | |
let toArray = item.parentElement.au.sortable.viewModel.array; | |
// restore the element's position | |
item.parentElement.removeChild(item); | |
let referenceElement = from.children[oldIndex]; | |
if (referenceElement) { | |
from.insertBefore(item, referenceElement); | |
} else { | |
from.appendChild(item); | |
} | |
// update the "from" array | |
let data = this.array.splice(oldIndex, 1)[0]; | |
// update the "to" array | |
toArray.splice(newIndex, 0, data); | |
} | |
attached() { | |
let options = this.options || {}; | |
options.onEnd = this.onEnd.bind(this); | |
this.api = Sortable.create(this.element, options); | |
} | |
detached() { | |
this.api.destroy(); | |
this.api = null; | |
this.repeat = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment