Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ee43adf
moving the test files into a dedicated folder
Aug 28, 2020
9db88b8
installing jsdom
Aug 30, 2020
08e7a2e
replacing the window and document with their jsdom equivalents for al…
Aug 30, 2020
ad15515
saving the jsdom instance in a global variable.
Aug 30, 2020
fd200eb
adding the first dragstart event test.
Aug 30, 2020
5aa8173
removing the test folder from the test coverage report.
Aug 30, 2020
c9b90fc
adding a test for the dragstart event.
Aug 30, 2020
166f79a
adding a test for the dragstart event.
Aug 30, 2020
e5adc22
removing some semicolons.
Aug 30, 2020
137ab8f
Adding some tests for the dragenter event.
Sep 6, 2020
e69bec6
Adding a test for the dragover event.
Sep 6, 2020
3ace1d5
Adding a test for the dragend event. Also refactoring the code a bit …
Sep 6, 2020
c2c67ea
Adding some more tests for the dragend event.
Sep 9, 2020
82ec734
Adding tests for the drop event.
Sep 9, 2020
881dd22
Adding a couple of tests for server rendered lists
Sep 13, 2020
826086f
Removing some semicolons
Sep 13, 2020
7ff1b71
Refactoring the canBeDropped method and writing some tests for it
Sep 13, 2020
5f8e0a8
Refactoring the maybeDrop method and writing some tests for it
Sep 13, 2020
f86ec72
Fixing a test description
Sep 13, 2020
d2a0d0a
Adding some tests for dropTheItem method.
Oct 3, 2020
2733fa2
Removing some redundant code.
Oct 3, 2020
106341c
Removing some redundant code.
Oct 3, 2020
effc9d9
Removing some redundant code.
Oct 3, 2020
cdbf0ba
Adding some tests for analysePlaceHolderSituation method.
Oct 4, 2020
c61ce8f
Adding some tests for managePlaceholderLists method.
Oct 4, 2020
c15d59a
Refactoring the addPlaceholderList method and writing a test for it.
Oct 4, 2020
35ad2ea
Adding some tests for cleanupPlaceholderLists method.
Oct 4, 2020
89e4874
Refactoring the onDragEnter method and writing a new test for it.
Oct 4, 2020
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
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
}
}]
],
"plugins": [
["@babel/plugin-transform-runtime", {
"regenerator": true
}]
],
"env": {
"test": {
"presets": [["@babel/preset-env"]]
Expand Down
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
setupFiles: [
'./test/__mocks__/dom.js',
],
coveragePathIgnorePatterns: [
'/node_modules/',
'/test/',
]
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
},
"devDependencies": {
"@babel/core": "7.9.0",
"@babel/plugin-transform-runtime": "^7.11.5",
"@babel/preset-env": "7.9.5",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "24.9.0",
"concurrently": "4.1.2",
"husky": "^4.2.5",
"jest": "24.9.0",
"jsdom": "^16.4.0",
"rollup": "1.32.1",
"rollup-plugin-babel": "4.4.0",
"rollup-plugin-commonjs": "10.1.0",
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default [
commonjs(),
babel({
exclude: 'node_modules/**',
runtimeHelpers: true,
}),
],
},
Expand Down
92 changes: 44 additions & 48 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ class NestedSort {
}
}

removeClassFromEl(el, className) {
if (el && el.classList.contains(className)) {
el.classList.remove(className)
}
}

onDragStart(e) {
this.draggedNode = e.target;
this.draggedNode.classList.add(this.classNames.dragged);
Expand All @@ -158,35 +164,25 @@ class NestedSort {
}

onDragEnter(e) {
if (!this.draggedNode) return
if (!(this.draggedNode && ['LI', 'UL'].includes(e.target.nodeName))) return

if (['LI', 'UL'].includes(e.target.nodeName)) {
e.preventDefault(); // prevent default to allow drop

if (this.targetedNode) this.targetedNode.classList.remove(this.classNames.targeted);
this.targetedNode = e.target;
e.target.classList.add(this.classNames.targeted);
}
if (this.targetedNode) this.targetedNode.classList.remove(this.classNames.targeted)
this.targetedNode = e.target
this.targetedNode.classList.add(this.classNames.targeted)
}

onDragLeave(e) {
e.preventDefault();
e.target.removeEventListener('dragover', this.onDrop);
e.target.removeEventListener('drop', this.onDrop);
e.target.removeEventListener('dragleave', this.onDragLeave);
}

onDragEnd(e) {
e.preventDefault();
e.stopPropagation()
this.draggedNode.classList.remove(this.classNames.dragged);
this.targetedNode.classList.remove(this.classNames.targeted)
this.removeClassFromEl(this.draggedNode, this.classNames.dragged)
this.removeClassFromEl(this.targetedNode, this.classNames.targeted)
this.cleanupPlaceholderLists();
this.draggedNode = null
}

onDrop(e) {
e.preventDefault();
e.stopPropagation()
this.maybeDrop();
this.cleanupPlaceholderLists();
Expand All @@ -206,29 +202,23 @@ class NestedSort {
this.calcMouseToTargetedElDist();
}

maybeDrop(e) {
if (!this.canBeDropped()) {
return;
}

let dropLocation;
if (this.targetedNode.nodeName === 'LI' && !this.cursorIsIndentedEnough()) {
dropLocation = 'before';
} else if (this.targetedNode.nodeName === 'UL') {
dropLocation = 'inside';
getDropLocation() {
if (this.canBeDropped()) {
if (this.targetedNode.nodeName === 'LI' && !this.cursorIsIndentedEnough()) return 'before'
else if (this.targetedNode.nodeName === 'UL') return 'inside'
}
}

if (dropLocation) this.dropTheItem(dropLocation, e);
maybeDrop(e) {
const location = this.getDropLocation()
if (location) this.dropTheItem(location, e)
}

dropTheItem(place, e) {
switch (place) {
case 'before':
this.targetedNode.parentNode.insertBefore(this.draggedNode, this.targetedNode);
break;
case 'after':
this.insertAfter(this.draggedNode, this.targetedNode);
break;
case 'inside':
this.targetedNode.appendChild(this.draggedNode);
break;
Expand Down Expand Up @@ -269,10 +259,6 @@ class NestedSort {
return this.cursor.Y - this.targetNode.Y < this.distances.droppingEdge;
}

insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

managePlaceholderLists(e) {

let actions = this.analysePlaceHolderSituation(e);
Expand All @@ -286,8 +272,6 @@ class NestedSort {
case 'cleanup':
this.cleanupPlaceholderLists();
break;
default:
break;
}
});
}
Expand Down Expand Up @@ -316,26 +300,38 @@ class NestedSort {
return actions;
}

addPlaceholderList() {
const list = this.getPlaceholderList();
list.style.minHeight = '0';
this.targetedNode.appendChild(list);
list.style.transition = 'min-height ease .2s';
list.style.minHeight = `${this.draggedNode.offsetHeight}px`;
animatePlaceholderList() {
this.placeholderInUse.style.minHeight = '0'
this.placeholderInUse.style.transition = 'min-height ease .2s'
this.placeholderInUse.style.minHeight = `${this.draggedNode.offsetHeight}px`
}

async addPlaceholderList() {
this.getPlaceholderList()
await this.targetedNode.appendChild(this.placeholderInUse)
this.animatePlaceholderList()
}

targetNodeIsIdentified() {
return !!this.targetedNode;
}

canBeDropped() {
let result = true;
targetNodeIsBeingDragged() {
return this.targetNodeIsIdentified()
&& this.targetedNode === this.draggedNode
}

result &= this.targetNodeIsIdentified() && this.targetedNode !== this.draggedNode;
result &= this.targetNodeIsIdentified() && !(this.targetedNode.nodeName === 'UL' && this.targetedNode.querySelectorAll('li').length);
result &= !this.areNested(this.targetedNode, this.draggedNode);
targetNodeIsListWithItems() {
return this.targetNodeIsIdentified()
&& this.targetedNode.nodeName === 'UL'
&& this.targetedNode.querySelectorAll('li').length
}

return result;
canBeDropped() {
return this.targetNodeIsIdentified()
&& !this.targetNodeIsBeingDragged()
&& !this.targetNodeIsListWithItems()
&& !this.areNested(this.targetedNode, this.draggedNode)
}

cleanupPlaceholderLists() {
Expand Down
113 changes: 0 additions & 113 deletions src/main.test.js

This file was deleted.

5 changes: 5 additions & 0 deletions test/__mocks__/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { JSDOM } from 'jsdom'
const dom = new JSDOM()
global.jsdom = dom
global.document = dom.window.document
global.window = dom.window
2 changes: 1 addition & 1 deletion src/data-engine.test.js → test/data-engine.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DataEngine from './data-engine'
import DataEngine from '../src/data-engine'

describe('DataEngine class', () => {
let list
Expand Down
Loading