-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.js
More file actions
120 lines (113 loc) · 4.14 KB
/
Utils.js
File metadata and controls
120 lines (113 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
dragDirection,
cellAnimationTypes,
} from './constants'
function isPointInPath ({ touchCoordinate, cellCoordinate, cellWidth, cellHeight, }) {
let { x, y, } = cellCoordinate
let { x: coordinateX, y: coordinateY } = touchCoordinate
////console.log(`isPointInPath x = ${x}, y = ${y}, coordinateX = ${coordinateX}, coordinateY = ${coordinateY}, cellWidth = ${cellWidth}, cellHeight= ${cellHeight},`)
if (!(coordinateX < x || coordinateX > x + cellWidth)
&& !(coordinateY < y || coordinateY > y + cellHeight)) {
return true
}
return false
}
function getCellsAnimationOptions ({ currentCellIndex, hoverCellIndex, columnCount, cells }) {
let cellsAnimation = []
let { up, right, down, left, } = dragDirection
let { rightTranslation, leftTranslation, leftBottomTranslation, rightTopTranslation, } = cellAnimationTypes
let currentRowNumber = getRowNumber({
cellIndex: currentCellIndex,
columnCount,
})
let hoverRowNumber = getRowNumber({
cellIndex: hoverCellIndex,
columnCount,
})
//console.log(`getCellsAnimationOptions currentCellIndex = ${currentCellIndex}, hoverCellIndex = ${hoverCellIndex}, currentRowNumber = ${currentRowNumber},hoverRowNumber = ${hoverRowNumber} columnCount = ${columnCount},`)
let currentDirection = getDragDirection({
currentRowNumber,
hoverRowNumber,
currentCellIndex,
hoverCellIndex,
})
//console.log(`getCellsAnimationOptions currentDirection = ${currentDirection}`)
let len = Math.abs(currentCellIndex - hoverCellIndex)
for (let i = 0; i < len; i++) {
let cellIndex, animationType
switch (currentDirection) {
case up: //currentCellIndex > hoverCellIndex
cellIndex = hoverCellIndex + i
if (isLastCellOfRow({ cellIndex, columnCount, })) {
animationType = leftBottomTranslation
}
else {
animationType = rightTranslation
}
break
case right: //currentCellIndex > hoverCellIndex
//cellIndex = hoverCellIndex
cellIndex = hoverCellIndex - i //for supporting remove cell logic
animationType = leftTranslation
break
case down: //currentCellIndex < hoverCellIndex
cellIndex = currentCellIndex + 1 + i
if (isFirstCellOfRow({ cellIndex, columnCount, })) {
animationType = rightTopTranslation
}
else {
animationType = leftTranslation
}
break
case left: //currentCellIndex < hoverCellIndex
cellIndex = hoverCellIndex
animationType = rightTranslation
break
}
//console.log(`cellIndex = ${cellIndex} length = ${cells.length}`)
//console.log(`cells[ cellIndex ] = `)
//console.log(cells[ cellIndex ])
let cellComponent = cells[ cellIndex ].component
cellsAnimation.push({
cellIndex,
cellComponent,
animationType,
})
}
return cellsAnimation
}
function getDragDirection ({ currentRowNumber, hoverRowNumber, currentCellIndex, hoverCellIndex, }) {
let { up, right, down, left, none } = dragDirection
if (currentRowNumber < hoverRowNumber) {
return down
}
else if (currentRowNumber > hoverRowNumber) {
return up
}
else {
if (currentCellIndex > hoverCellIndex) {
return left
}
else if (currentCellIndex < hoverCellIndex) {
return right
}
else {
return none
}
}
}
function getRowNumber ({ cellIndex, columnCount, }) {
return Math.floor(cellIndex / columnCount)
}
function isFirstCellOfRow ({ cellIndex, columnCount, }) {
return cellIndex % columnCount == 0
}
function isLastCellOfRow ({ cellIndex, columnCount, }) {
return (cellIndex + 1) % columnCount == 0
}
export default {
isPointInPath,
getDragDirection,
getCellsAnimationOptions,
getRowNumber,
}