Skip to content

Instantly share code, notes, and snippets.

@ahmetemrekilinc
Last active November 13, 2017 06:55
Show Gist options
  • Save ahmetemrekilinc/e4e928934018dcd2ed3049c36e8414b3 to your computer and use it in GitHub Desktop.
Save ahmetemrekilinc/e4e928934018dcd2ed3049c36e8414b3 to your computer and use it in GitHub Desktop.
ember-drag-sort-parent
import Ember from 'ember';
import computed from 'ember-computed'
import {A} from 'ember-array/utils'
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
items1 : computed(() =>
A([
{id: 1, name : 'Parent1'},
{id: 2, name : 'Parent2'},
{id: 8, name : 'Parent3'},
{id: 3, name : 'Child1ofParent1', parentId: 1},
{id: 4, name : 'Child1ofParent2', parentId: 2},
{id: 5, name : 'Child2ofParent2', parentId: 2},
{id: 9, name : 'Child1ofParent3', parentId: 8}
])
),
items2 : computed(() =>
A([
{id: 6, name : 'OtherList1'},
{id: 7, name : 'OtherList2'}
])
),
actions : {
dragEnd ({sourceList, sourceIndex, targetList, targetIndex}) {
if (sourceList === targetList && sourceIndex === targetIndex){
return;
}
const item = sourceList.objectAt(sourceIndex);
console.log(sourceList);
let nodeToBeDragged = sourceList.get(sourceIndex);
let parentId = nodeToBeDragged.parentId;
sourceList.removeAt(sourceIndex);
targetList.insertAt(targetIndex, item);
// if parent exists
if(parentId){
let parentIndex = undefined;
sourceList.forEach(function callback(currentValue, currentIndex) {
if(currentValue.id === parentId){
parentIndex = currentIndex;
}
});
// move parent
if(parentIndex > -1){
let parentNode = sourceList.get(parentIndex);
sourceList.removeAt(parentIndex);
targetList.insertAt(targetIndex, parentNode);
}
}
}
}
});
.github-fork-ribbon {
position: fixed !important;
}
html, body {
font-family: sans-serif;
}
h4 {
margin-bottom: 10px;
}
p:first-child,
h1:first-child,
h2:first-child,
h3:first-child,
h4:first-child,
h5:first-child,
h6:first-child
{
margin-top: 0;
}
p:last-child,
h1:last-child,
h2:last-child,
h3:last-child,
h4:last-child,
h5:last-child,
h6:last-child
{
margin-bottom: 0;
}
.list-groups {
display: flex;
flex-wrap: wrap;
margin-top: 30px;
}
.list-group-wrapper {
flex-grow: 1;
min-width: 600px;
margin-bottom: 30px;
}
.list-group {
display: flex;
}
.list {
width: 200px;
margin-right: 20px;
}
.list-group .dragSortList {
background-color: #eee;
}
.dragSortItem {
overflow: auto;
}
.the-item {
border: 1px solid black;
margin: 4px;
padding: 0.2em 0.5em;
background-color: #FFD0E9;
}
.nestedItem {
margin: 4px;
padding: 1em;
border: 1px solid black;
display: flex;
}
.nestedItem-title {
flex-shrink: 0;
flex-grow: 0;
width: 100px;
}
.nestedItem .dragSortList {
flex-shrink: 0;
flex-grow: 1;
}
.nestedItem .dragSortList.-isExpanded {
padding-top: 55px;
}
.nestedItem .dragSortList.-isDragging {
outline: 2px dashed black;
background-color: deepskyblue;
}
.nestedItem .dragSortList.-isExpanded.-isDraggingOver:before {
top: 15px;
}
.nestedItem {
background-color : #FFD0E9;
}
.nestedItem .nestedItem {
background-color: #FFAAD7;
}
.nestedItem .nestedItem .nestedItem {
background-color: #FF8CC9;
}
.nestedItem .nestedItem .nestedItem .nestedItem {
background-color: #FF74BD;
}
.nestedItem .nestedItem .nestedItem .nestedItem .nestedItem {
background-color: #FF60B4;
}
.nestedItem .nestedItem .nestedItem .nestedItem .nestedItem .nestedItem {
background-color: #FF50AD;
}
.dragSortList:not(.-draggingEnabled) {
opacity: 0.66;
}
/* Table styles */
table {
width: 100%;
}
table.dragSortList.-isExpanded {
padding: 15px;
background-color: #f6f6f6;
}
table.dragSortList.-isExpanded.-isDraggingOver {
background-color: #eee;
}
table.dragSortList.-isExpanded.-isDraggingOver:before {
content: none;
}
tr.dragSortItem.-placeholderAbove:before,
tr.dragSortItem.-placeholderBelow:before {
content: none;
}
tr.dragSortItem.-placeholderAbove td {
padding-top: 25px;
}
tr.dragSortItem.-placeholderBelow td {
padding-bottom: 25px;
}
table .the-item {
margin: 0;
}
<div class="list-group-wrapper">
<h2>Simple usage</h2>
<p>
Sort lists, drag between lists. Won't let you drag items from/to other groups.
</p>
<section class="list-group">
<article class="list">
<h4>List 1</h4>
{{#drag-sort-list
items = items1
dragEndAction = (action 'dragEnd')
as |item|
}}
<div class="the-item">
{{item.name}}
</div>
{{/drag-sort-list}}
</article>
<article class="list">
<h4>List 2 with drag handles</h4>
{{#drag-sort-list
items = items2
handle = '.handle'
dragEndAction = (action 'dragEnd')
as |item|
}}
<div class="the-item">
<span class="handle" draggable="true">☰</span>
{{item.name}}
</div>
{{/drag-sort-list}}
</article>
</section>
</div>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1",
"ember-drag-sort": "1.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment