-
-
Save davismj/45c11cdeaa4815ab14f09f298e7c1970 to your computer and use it in GitHub Desktop.
Handwritten Drag and Drop
This file contains hidden or 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> | |
| <style> | |
| .list { | |
| user-select: none; | |
| } | |
| .list + .list { | |
| margin-top: 1em; | |
| } | |
| .list-item { | |
| border: 1px solid; | |
| } | |
| .list-item + .list-item { | |
| border-top: 0; | |
| } | |
| .aaDragging { | |
| border-style: dotted!important; | |
| } | |
| .aaRowHover { | |
| border-style: dashed; | |
| } | |
| </style> | |
| <require from="./sortableCustomAttribute"></require> | |
| <div class="list" sortable.two-way="list"> | |
| <div repeat.for="item of list" class="list-item">${item}</div> | |
| </div> | |
| <div class="list" sortable.two-way="list"> | |
| <div repeat.for="item of list" class="list-item">${item}</div> | |
| </div> | |
| <div class="list" sortable.two-way="otherList"> | |
| <div repeat.for="item of otherList" class="list-item">${item}</div> | |
| </div> | |
| </template> |
This file contains hidden or 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 { | |
| list = ['first', 'second', 'third']; | |
| otherList = ['first', 'second', 'third']; | |
| } |
This file contains hidden or 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://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script> | |
| <script src="https://jdanyow.github.io/rjs-bundle/config.js"></script> | |
| <script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script> | |
| <script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script> | |
| <script> | |
| require(['aurelia-bootstrapper']); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or 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, bindingMode } from 'aurelia-framework'; | |
| @inject(Element) | |
| export class SortableCustomAttribute { | |
| constructor(element) { | |
| debugger; | |
| this.element = element; | |
| element.addEventListener('mousedown', (event) => this.onMouseDown(event)); | |
| } | |
| onMouseDown({ target: listItem }) { | |
| listItem.classList.add('aaDragging'); | |
| const list = this.element; | |
| const items = list.querySelectorAll('.list-item'); | |
| const onMouseEnter = (event) => this.onMouseEnter(event); | |
| const onMouseLeave = (event) => this.onMouseLeave(event); | |
| const onMouseUp = (event) => { | |
| this.onMouseUp(event); | |
| for (let item of items) { | |
| item.removeEventListener('mouseenter', onMouseEnter); | |
| item.removeEventListener('mouseleave', onMouseLeave); | |
| } | |
| document.removeEventListener('mouseup', onMouseUp); | |
| }; | |
| for (let item of items) { | |
| item.addEventListener('mouseenter', onMouseEnter); | |
| item.addEventListener('mouseleave', onMouseLeave); | |
| } | |
| document.addEventListener('mouseup', onMouseUp); | |
| } | |
| onMouseUp({ target: listItem }) { | |
| const list = this.element; | |
| let dragIndex; | |
| let dropIndex; | |
| Array.from(list.querySelectorAll('.list-item')).forEach((item, index) => { | |
| if (item.classList.contains('aaDragging')) { | |
| dragIndex = index; | |
| item.classList.remove('aaDragging'); | |
| } | |
| if (item.classList.contains('aaRowHover')) { | |
| dropIndex = index; | |
| item.classList.remove('aaRowHover'); | |
| } | |
| }); | |
| this.swap(dragIndex, dropIndex); | |
| } | |
| onMouseEnter({ currentTarget: listItem }) { | |
| console.debug('mousenter') | |
| listItem.classList.add('aaRowHover'); | |
| } | |
| onMouseLeave({ currentTarget: listItem }) { | |
| console.debug('mouseleave'); | |
| listItem.classList.remove('aaRowHover'); | |
| } | |
| swap(fromIndex, toIndex) { | |
| if (fromIndex === toIndex || fromIndex === undefined || toIndex === undefined) { | |
| return; | |
| } | |
| let fromItem = this.value[fromIndex]; | |
| let toItem = this.value[toIndex]; | |
| this.value.splice(fromIndex, 1, toItem); | |
| this.value.splice(toIndex, 1, fromItem); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment