|
1 | 1 | import { VNode } from '@cycle/dom';
|
2 | 2 | import { select } from 'snabbdom-selector';
|
3 |
| -import { EventHandler, MouseOffset, ItemDimensions, Intersection } from '../definitions'; |
| 3 | +import { |
| 4 | + EventHandler, |
| 5 | + MouseOffset, |
| 6 | + ItemDimensions, |
| 7 | + Intersection |
| 8 | +} from '../definitions'; |
4 | 9 |
|
5 |
| -import { updateGhostStyle, findParent, getIntersection, getArea, addAttributes, replaceNode } from '../helpers'; |
| 10 | +import { |
| 11 | + updateGhostStyle, |
| 12 | + findParent, |
| 13 | + getIntersection, |
| 14 | + getArea, |
| 15 | + addAttributes, |
| 16 | + replaceNode |
| 17 | +} from '../helpers'; |
6 | 18 |
|
7 | 19 | /**
|
8 | 20 | * Used to adjust the position of the ghost and swap the items if needed
|
9 | 21 | * @type {EventHandler}
|
10 | 22 | */
|
11 |
| -export const mousemoveHandler : EventHandler = (node, event, options) => { |
12 |
| - const parent : VNode = select(options.parentSelector, node)[0]; |
13 |
| - const ghost : VNode = parent.children[parent.children.length - 1] as VNode; |
| 23 | +export const mousemoveHandler: EventHandler = (node, event, options) => { |
| 24 | + const parent: VNode = select(options.parentSelector, node)[0]; |
| 25 | + const ghost: VNode = parent.children[parent.children.length - 1] as VNode; |
14 | 26 |
|
15 |
| - const mouseOffset : MouseOffset = JSON.parse(ghost.data.attrs['data-mouseoffset']); |
16 |
| - const itemIndex : number = parseInt(ghost.data.attrs['data-itemindex']); |
17 |
| - const item : VNode = parent.children[itemIndex] as VNode; |
18 |
| - const itemIntersection : number = getArea(getIntersection(item.elm as Element, ghost.elm as Element)); |
19 |
| - const itemArea : number = getArea(getIntersection(item.elm as Element, item.elm as Element)); |
| 27 | + const mouseOffset: MouseOffset = JSON.parse( |
| 28 | + ghost.data.attrs['data-mouseoffset'] |
| 29 | + ); |
| 30 | + const itemIndex: number = parseInt(ghost.data.attrs['data-itemindex']); |
| 31 | + const item: VNode = parent.children[itemIndex] as VNode; |
| 32 | + const itemIntersection: number = getArea( |
| 33 | + getIntersection(item.elm as Element, ghost.elm as Element) |
| 34 | + ); |
| 35 | + const itemArea: number = getArea( |
| 36 | + getIntersection(item.elm as Element, item.elm as Element) |
| 37 | + ); |
20 | 38 |
|
21 |
| - const intersectionAreas : [number, number][] = parent.children |
| 39 | + const intersectionAreas: [number, number][] = parent.children |
22 | 40 | .slice(0, -1)
|
23 | 41 | .map<Element>(c => (c as VNode).elm as Element)
|
24 | 42 | .map<Intersection>(e => getIntersection(e, ghost.elm as Element))
|
25 | 43 | .map<[number, number]>((e, i) => [getArea(e), i]);
|
26 | 44 |
|
27 |
| - const maxIntersection : [number, number] = intersectionAreas |
28 |
| - .reduce((acc, curr) => curr[0] > acc[0] ? curr : acc); |
| 45 | + const maxIntersection: [number, number] = intersectionAreas.reduce( |
| 46 | + (acc, curr) => (curr[0] > acc[0] ? curr : acc) |
| 47 | + ); |
29 | 48 |
|
30 |
| - const maxElement : Element = (parent.children[maxIntersection[1]] as VNode).elm as Element; |
31 |
| - const maxArea : number = getArea(getIntersection(maxElement, maxElement)); |
| 49 | + const maxElement: Element = (parent.children[maxIntersection[1]] as VNode) |
| 50 | + .elm as Element; |
| 51 | + const maxArea: number = getArea(getIntersection(maxElement, maxElement)); |
32 | 52 |
|
33 |
| - const newIndex : number = maxIntersection[1] === itemIndex ? itemIndex : |
34 |
| - (-itemIntersection > maxArea - itemArea ? maxIntersection[1] : itemIndex); |
| 53 | + const newIndex: number = |
| 54 | + maxIntersection[1] === itemIndex |
| 55 | + ? itemIndex |
| 56 | + : -itemIntersection > maxArea - itemArea |
| 57 | + ? maxIntersection[1] |
| 58 | + : itemIndex; |
35 | 59 |
|
36 |
| - const ghostAttrs : { [attr : string]: string } = { |
37 |
| - 'style': updateGhostStyle(event, mouseOffset, ghost.elm as Element), |
| 60 | + const ghostAttrs: { [attr: string]: string } = { |
| 61 | + style: updateGhostStyle(event, mouseOffset, ghost.elm as Element), |
38 | 62 | 'data-itemindex': newIndex.toString()
|
39 | 63 | };
|
40 | 64 |
|
41 |
| - const filteredChildren : VNode[] = (parent.children as VNode[]) |
| 65 | + const filteredChildren: VNode[] = (parent.children as VNode[]) |
42 | 66 | .filter((e, i) => i !== itemIndex)
|
43 | 67 | .slice(0, -1);
|
44 | 68 |
|
45 |
| - const newChildren : VNode[] = [ |
| 69 | + const newChildren: VNode[] = [ |
46 | 70 | ...filteredChildren.slice(0, newIndex),
|
47 | 71 | parent.children[itemIndex] as VNode,
|
48 | 72 | ...filteredChildren.slice(newIndex, filteredChildren.length)
|
49 | 73 | ];
|
50 | 74 |
|
51 |
| - return replaceNode(node, options.parentSelector, Object.assign({}, parent, { |
52 |
| - children: [...newChildren, addAttributes(ghost, ghostAttrs)] |
53 |
| - })); |
| 75 | + return replaceNode( |
| 76 | + node, |
| 77 | + options.parentSelector, |
| 78 | + Object.assign({}, parent, { |
| 79 | + children: [...newChildren, addAttributes(ghost, ghostAttrs)] |
| 80 | + }) |
| 81 | + ); |
54 | 82 | };
|
0 commit comments