-
-
Save amk221/106bbc0ab364f8d7323baf29d80d48ef to your computer and use it in GitHub Desktop.
ember-dragula
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 Controller from '@ember/controller'; | |
import { tracked } from '@glimmer/tracking'; | |
import { action } from '@ember/object'; | |
const { stringify } = JSON; | |
export default class ApplicationController extends Controller { | |
maxId = 6; | |
@tracked | |
listOne = [ | |
{ id: 1, name: 'Item 1' }, | |
{ id: 2, name: 'Item 2' }, | |
{ id: 3, name: 'Item 3' } | |
]; | |
@tracked | |
listTwo = [ | |
{ id: 4, name: 'Item 4' }, | |
{ id: 5, name: 'Item 5' }, | |
{ id: 6, name: 'Item 6' } | |
]; | |
get listOneString() { | |
return stringify(this.listOne, null, 2); | |
} | |
get listTwoString() { | |
return stringify(this.listTwo, null, 2); | |
} | |
@action | |
register(drake) { | |
this.drake = drake; | |
} | |
@action | |
pushToFirstList() { | |
const id = ++this.maxId; | |
const item = { | |
id, | |
name: `Item ${id}` | |
}; | |
this.listOne.push(item); | |
this.listOne = this.listOne; | |
} | |
@action | |
handleDrop(itemEl, listEl) { | |
const listEls = this.drake.containers; | |
const itemEls = [...listEl.children]; | |
const srcItemIndex = parseInt(itemEl.dataset.index, 10); | |
const srcListIndex = parseInt(itemEl.dataset.listIndex, 10); | |
const destListIndex = listEls.indexOf(listEl); | |
const destItemIndex = itemEls.indexOf(itemEl); | |
const srcList = srcListIndex === 0 | |
? this.listOne | |
: this.listTwo; | |
const destList = destListIndex === 0 | |
? this.listOne | |
: this.listTwo; | |
const item = srcList[srcItemIndex]; | |
console.log( | |
'Moved', | |
item.name, | |
'from position', | |
srcItemIndex, | |
'in list', | |
srcListIndex, | |
'to position', | |
destItemIndex, | |
'in list', | |
destListIndex, | |
); | |
// Keep lists in sync with changes made to the DOM. | |
srcList.splice(srcItemIndex, 1); | |
destList.splice(destItemIndex, 0, item); | |
// Remove the DOM element if moving to a new list | |
// because Ember will re-render us a new node | |
if (destListIndex !== srcListIndex) { | |
this.drake.remove(itemEl); | |
} | |
this.listOne = this.listOne; | |
this.listTwo = this.listTwo; | |
} | |
} |
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
body { | |
margin: 10px; | |
} | |
ul { | |
margin-bottom: 25px; | |
list-style: disc; | |
margin-left: 20px; | |
} | |
.list { | |
margin-right: 20px; | |
} | |
.item { | |
margin: 5px auto; | |
white-space: nowrap | |
} | |
.ember-dragula { | |
display: flex; | |
} | |
pre { | |
font-size: 12px; | |
} |
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 Application from '../app'; | |
import config from '../config/environment'; | |
import { setApplication } from '@ember/test-helpers'; | |
import { assign } from '@ember/polyfills'; | |
import { start } from 'ember-qunit'; | |
let attributes = { | |
rootElement: '#test-root', | |
autoboot: false | |
}; | |
attributes = assign(attributes, config.APP); | |
let application = Application.create(attributes); | |
setApplication(application); | |
start(); |
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
{ | |
"version": "0.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0", | |
"@zestia/ember-dragula": "10.0.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment