Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions dev/handles.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Nested Sort | Drag Handles Demo</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha512-SfTiTlX6kk+qitfevl/7LibUOeJWlt9rbyDn92a1DqWOw9vWG2MFoays0sgObmWazO5BQPiFucnnEAjpAB+/Sw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link href="css/main.css" rel="stylesheet">
<style>
.nested-sort li {
padding: 0;
cursor: default;
}

.nested-sort li>ul {
margin: 10px 10px 10px 20px;
}

.nested-sort .item {
display: flex;
column-gap: 2rem;
padding: 10px;
}

.nested-sort .ns-handle {
cursor: move;
}
</style>
</head>
<body>

<div class="container">
<h1>Drag Handles Demo</h1>

<div class="sample-wrap">
<ul id="draggable">
<li data-id="1">
<div class="item">
<div class="icon ns-handle"><i class="fa fa-arrows"></i></div>
<div class="title">Topic 1</div>
</div>
</li>
<li data-id="2">
<div class="item">
<div class="icon ns-handle"><i class="fa fa-arrows"></i></div>
<div class="title">Topic 2</div>
</div>
</li>
<li data-id="3">
<div class="item">
<div class="icon ns-handle"><i class="fa fa-arrows"></i></div>
<div class="title">Topic 3</div>
</div>
</li>
<li data-id="4">
<div class="item">
<div class="icon ns-handle"><i class="fa fa-arrows"></i></div>
<div class="title">Topic 4</div>
</div>
</li>
<li data-id="5">
<div class="item">
<div class="icon ns-handle"><i class="fa fa-arrows"></i></div>
<div class="title">Topic 5</div>
</div>
</li>
<li data-id="6">
<div class="item">
<div class="icon ns-handle"><i class="fa fa-arrows"></i></div>
<div class="title">Topic 6</div>
</div>
</li>
<li data-id="7">
<div class="item">
<div class="icon ns-handle"><i class="fa fa-arrows"></i></div>
<div class="title">Topic 7</div>
</div>
</li>
<li data-id="8">
<div class="item">
<div class="icon ns-handle"><i class="fa fa-arrows"></i></div>
<div class="title">Topic 8</div>
</div>
</li>
<li data-id="9">
<div class="item">
<div class="icon ns-handle"><i class="fa fa-arrows"></i></div>
<div class="title">Topic 9</div>
</div>
</li>
<li data-id="10">
<div class="item">
<div class="icon ns-handle"><i class="fa fa-arrows"></i></div>
<div class="title">Topic 10</div>
</div>
</li>
</ul>
<pre class="result-wrap"></pre>
</div>
</div>

<!-- Scripts -->
<script src="../dist/nested-sort.umd.js"></script>
<script>
new NestedSort({
actions: {
onDrop: function (data) {
const resultWrap = document.querySelector('.result-wrap')
resultWrap.innerHTML = JSON.stringify(data, null, ' ')
console.log(data)
}
},
el: '#draggable',
droppingEdge: 5,
nestingLevels: -1,
classNames: {
handle: 'ns-handle',
},
})
</script>
</body>
</html>
1 change: 1 addition & 0 deletions dev/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h1>Samples</h1>
<li><a href="data-persistence-in-local-storage" target="_blank">Persisting Data in Local Storage</a></li>
<li><a href="enable-disable" target="_blank">Enable / Disable</a></li>
<li><a href="nesting-levels" target="_blank">Nesting Levels</a></li>
<li><a href="handles" target="_blank">Drag Handles</a></li>
<li><a href="custom-render" target="_blank">Custom Render</a></li>
</ol>

Expand Down
21 changes: 18 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class NestedSort {
listInterface: ListInterface
listItemClassNames: Array<string>
mainListClassName: string
mouseTarget: HTMLElement
nestingLevels: number
placeholderList: HTMLElement
placeholderInUse: HTMLElement
Expand All @@ -46,6 +47,7 @@ class NestedSort {

constructor({
actions = {},
classNames,
data,
droppingEdge = 15,
el,
Expand Down Expand Up @@ -75,16 +77,21 @@ class NestedSort {
droppingEdge,
}
this.classNames = {
dragged: 'ns-dragged',
placeholder: 'ns-placeholder',
targeted: 'ns-targeted',
...{
dragged: 'ns-dragged',
handle: null,
placeholder: 'ns-placeholder',
targeted: 'ns-targeted',
},
...classNames,
}
this.listEventListeners = {
dragover: this.onDragOver.bind(this),
dragstart: this.onDragStart.bind(this),
dragenter: this.onDragEnter.bind(this),
dragend: this.onDragEnd.bind(this),
drop: this.onDrop.bind(this),
mousedown: this.onMouseDown.bind(this),
}
const intNestingLevels = parseInt(nestingLevels)
this.nestingLevels = isNaN(intNestingLevels) ? -1 : intNestingLevels // values less than 0 mean infinite levels of nesting
Expand Down Expand Up @@ -209,6 +216,10 @@ class NestedSort {
}

onDragStart(e: DragEvent): void {
if (this.classNames.handle && ! this.mouseTarget.closest(`.${this.classNames.handle}`)) {
e.preventDefault() // Don't allow dragging if handles are specified
return
}
this.draggedNode = e.target as HTMLElement
this.draggedNode.classList.add(this.classNames.dragged)
e.dataTransfer?.setData('text', '') // Hack for mobile devices
Expand Down Expand Up @@ -247,6 +258,10 @@ class NestedSort {
}
}

onMouseDown(e: MouseEvent): void {
this.mouseTarget = e.target as HTMLElement
}

updateCoordination(e: DragEvent): void {
this.calcMouseCoords(e)
this.calcMouseToTargetedElDist()
Expand Down
3 changes: 3 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type PlaceholderMaintenanceActions = Array<'add' | 'cleanup'>

export interface ClassNames {
dragged: string
handle: string | null
placeholder: string
targeted: string
}
Expand Down Expand Up @@ -62,12 +63,14 @@ export interface EventListeners {
dragenter: (e: object) => void
dragend: (e: object) => void
drop: (e: object) => void
mousedown: (e: object) => void
}

export type ClassNamesList = Array<string> | string

export interface Options {
actions: Actions
classNames: ClassNames
data: Array<DataItem>
droppingEdge: number
el: HTMLElement | string
Expand Down