Skip to content

Commit 2897020

Browse files
committed
Apply prettier formatting
1 parent 1210356 commit 2897020

File tree

8 files changed

+377
-210
lines changed

8 files changed

+377
-210
lines changed

src/definitions.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { Stream } from 'xstream';
22
import { VNode } from '@cycle/dom';
33

4-
export type Transform<T, U> = (s : Stream<T>) => Stream<U>
5-
export type EventHandler = (node : VNode, event : MouseEvent, options : SortableOptions) => VNode;
4+
export type Transform<T, U> = (s: Stream<T>) => Stream<U>;
5+
export type EventHandler = (
6+
node: VNode,
7+
event: MouseEvent,
8+
options: SortableOptions
9+
) => VNode;
610

711
export interface SortableOptions {
812
/**
@@ -12,57 +16,53 @@ export interface SortableOptions {
1216
* @default the root VNode of the given DOMSource
1317
* @type {string}
1418
*/
15-
parentSelector? : string;
19+
parentSelector?: string;
1620

1721
/**
1822
* Optional, has to be a valid CSS selector.
1923
* Used to define a drag handle on the sortable items
2024
* @default string the whole item
2125
* @type {string}
2226
*/
23-
handle? : string;
27+
handle?: string;
2428

2529
/**
2630
* Optional, has to be a CSS class name
2731
* Can be used to style the ghost item
2832
* @default the first CSS class of the first item
2933
* @type {string}
3034
*/
31-
ghostClass? : string;
35+
ghostClass?: string;
3236
}
3337

3438
/**
3539
* Contains the offset from the cursor to the top left of the item
3640
* x and y are negative or zero, so you have to add them to the current mouse position
3741
* @type {MouseOffset}
3842
*/
39-
export interface MouseOffset
40-
{
41-
x : number;
42-
y : number;
43+
export interface MouseOffset {
44+
x: number;
45+
y: number;
4346
}
4447

4548
/**
4649
* Defines the object available on a custom updateOrder event
4750
* @type {EventDetails}
4851
*/
49-
export interface EventDetails
50-
{
51-
newOrder : number[];
52-
oldIndex : number;
53-
newIndex : number;
52+
export interface EventDetails {
53+
newOrder: number[];
54+
oldIndex: number;
55+
newIndex: number;
5456
}
5557

56-
export interface ItemDimensions
57-
{
58-
width : number;
59-
height : number;
58+
export interface ItemDimensions {
59+
width: number;
60+
height: number;
6061
}
6162

62-
export interface Intersection
63-
{
64-
xmin : number;
65-
ymin : number;
66-
xmax : number;
67-
ymax : number;
63+
export interface Intersection {
64+
xmin: number;
65+
ymin: number;
66+
xmax: number;
67+
ymax: number;
6868
}

src/eventHandlers.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ import { mousemoveHandler } from './eventHandlers/mousemove';
1212
* @param {SortableOptions} options a valid SortableOptions object, it and its properties have to be defined
1313
* @return {VNode} The new root VNode
1414
*/
15-
export function handleEvent(node : VNode, event : MouseEvent, options : SortableOptions) : VNode
16-
{
17-
const eventHandlerMapping : { [type : string]: EventHandler } = {
18-
'mousedown': mousedownHandler,
19-
'mouseup': mouseupHandler,
20-
'mouseleave': mouseupHandler,
21-
'mousemove': mousemoveHandler,
22-
'touchstart': mousedownHandler,
23-
'touchend': mouseupHandler,
24-
'touchmove': mousemoveHandler
15+
export function handleEvent(
16+
node: VNode,
17+
event: MouseEvent,
18+
options: SortableOptions
19+
): VNode {
20+
const eventHandlerMapping: { [type: string]: EventHandler } = {
21+
mousedown: mousedownHandler,
22+
mouseup: mouseupHandler,
23+
mouseleave: mouseupHandler,
24+
mousemove: mousemoveHandler,
25+
touchstart: mousedownHandler,
26+
touchend: mouseupHandler,
27+
touchmove: mousemoveHandler
2528
};
2629

2730
return eventHandlerMapping[event.type](node, event, options);

src/eventHandlers/mousedown.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,56 @@ import { VNode } from '@cycle/dom';
22
import { select } from 'snabbdom-selector';
33
import { EventHandler, MouseOffset } from '../definitions';
44

5-
import { getIndex, getGhostStyle, findParent, addAttributes, replaceNode, getBodyStyle, addKeys } from '../helpers';
5+
import {
6+
getIndex,
7+
getGhostStyle,
8+
findParent,
9+
addAttributes,
10+
replaceNode,
11+
getBodyStyle,
12+
addKeys
13+
} from '../helpers';
614

715
/**
816
* Used to create the ghost and hide the item dragged
917
* @type {EventHandler}
1018
*/
11-
export const mousedownHandler : EventHandler = (node, event, options) => {
12-
const item : Element = findParent(event.target as Element, options.parentSelector + ' > *');
13-
const itemRect : ClientRect = item.getBoundingClientRect();
14-
const mouseOffset : MouseOffset = {
19+
export const mousedownHandler: EventHandler = (node, event, options) => {
20+
const item: Element = findParent(
21+
event.target as Element,
22+
options.parentSelector + ' > *'
23+
);
24+
const itemRect: ClientRect = item.getBoundingClientRect();
25+
const mouseOffset: MouseOffset = {
1526
x: itemRect.left - event.clientX,
1627
y: itemRect.top - event.clientY
1728
};
1829

19-
const body : Element = findParent(event.target as Element, 'body');
30+
const body: Element = findParent(event.target as Element, 'body');
2031
body.setAttribute('style', getBodyStyle());
2132

22-
const parent : VNode = addKeys(select(options.parentSelector, node)[0]);
23-
const index : number = getIndex(item);
33+
const parent: VNode = addKeys(select(options.parentSelector, node)[0]);
34+
const index: number = getIndex(item);
2435

25-
const ghostAttrs : { [name : string]: string } = {
36+
const ghostAttrs: { [name: string]: string } = {
2637
'data-mouseoffset': JSON.stringify(mouseOffset),
27-
'data-itemdimensions': JSON.stringify({ width: itemRect.width, height: itemRect.height }),
38+
'data-itemdimensions': JSON.stringify({
39+
width: itemRect.width,
40+
height: itemRect.height
41+
}),
2842
'data-itemindex': index.toString(),
2943
'data-originalIndex': index.toString(),
30-
'style': getGhostStyle(event, mouseOffset, item)
44+
style: getGhostStyle(event, mouseOffset, item)
3145
};
3246

33-
const items : VNode[] = parent.children as VNode[];
47+
const items: VNode[] = parent.children as VNode[];
3448

35-
const children : VNode[] = [
49+
const children: VNode[] = [
3650
...items.slice(0, index),
37-
addAttributes(items[index], { 'style': 'opacity: 0;' }),
51+
addAttributes(items[index], { style: 'opacity: 0;' }),
3852
...items.slice(index + 1),
39-
addAttributes({ ...items[index], elm: undefined }, ghostAttrs)
40-
].map((c, i) => addAttributes(c, { 'data-index' : i }));
53+
addAttributes({ ...items[index], elm: undefined }, ghostAttrs)
54+
].map((c, i) => addAttributes(c, { 'data-index': i }));
4155

4256
return replaceNode(node, options.parentSelector, { ...parent, children });
4357
};

src/eventHandlers/mousemove.ts

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,82 @@
11
import { VNode } from '@cycle/dom';
22
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';
49

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';
618

719
/**
820
* Used to adjust the position of the ghost and swap the items if needed
921
* @type {EventHandler}
1022
*/
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;
1426

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+
);
2038

21-
const intersectionAreas : [number, number][] = parent.children
39+
const intersectionAreas: [number, number][] = parent.children
2240
.slice(0, -1)
2341
.map<Element>(c => (c as VNode).elm as Element)
2442
.map<Intersection>(e => getIntersection(e, ghost.elm as Element))
2543
.map<[number, number]>((e, i) => [getArea(e), i]);
2644

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+
);
2948

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));
3252

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;
3559

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),
3862
'data-itemindex': newIndex.toString()
3963
};
4064

41-
const filteredChildren : VNode[] = (parent.children as VNode[])
65+
const filteredChildren: VNode[] = (parent.children as VNode[])
4266
.filter((e, i) => i !== itemIndex)
4367
.slice(0, -1);
4468

45-
const newChildren : VNode[] = [
69+
const newChildren: VNode[] = [
4670
...filteredChildren.slice(0, newIndex),
4771
parent.children[itemIndex] as VNode,
4872
...filteredChildren.slice(newIndex, filteredChildren.length)
4973
];
5074

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+
);
5482
};

src/eventHandlers/mouseup.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,38 @@ import { replaceNode, removeAttribute, findParent } from '../helpers';
88
* Used to remove the ghost element
99
* @type {EventHandler}
1010
*/
11-
export const mouseupHandler : EventHandler = (node, event, options) => {
12-
const parent : VNode = select(options.parentSelector, node)[0];
13-
const items : VNode[] = parent.children as VNode[];
11+
export const mouseupHandler: EventHandler = (node, event, options) => {
12+
const parent: VNode = select(options.parentSelector, node)[0];
13+
const items: VNode[] = parent.children as VNode[];
1414

15-
const ghost : VNode = items[items.length - 1];
15+
const ghost: VNode = items[items.length - 1];
1616

17-
if (!parent || !ghost) { return node; }
17+
if (!parent || !ghost) {
18+
return node;
19+
}
1820

19-
const itemIndex : number = parseInt(ghost.data.attrs['data-itemindex']);
21+
const itemIndex: number = parseInt(ghost.data.attrs['data-itemindex']);
2022

21-
const body : Element = findParent(event.target as Element, 'body');
23+
const body: Element = findParent(event.target as Element, 'body');
2224
body.removeAttribute('style');
2325

24-
const newItems : VNode[] = [
26+
const newItems: VNode[] = [
2527
...items.slice(0, itemIndex),
2628
removeAttribute(items[itemIndex], 'style'),
2729
...items.slice(itemIndex + 1, -1)
2830
];
2931

30-
const indexes : number[] = newItems
32+
const indexes: number[] = newItems
3133
.map(c => c.data.attrs['data-index'])
3234
.map(s => parseInt(s));
3335

34-
const tuple : [number, number] = [
36+
const tuple: [number, number] = [
3537
parseInt(ghost.data.attrs['data-originalIndex']),
3638
parseInt(ghost.data.attrs['data-itemindex'])
3739
];
3840

3941
if (tuple[0] !== tuple[1]) {
40-
const customEvent : CustomEvent = new CustomEvent('updateOrder', {
42+
const customEvent: CustomEvent = new CustomEvent('updateOrder', {
4143
bubbles: true,
4244
detail: {
4345
newOrder: indexes,
@@ -49,7 +51,9 @@ export const mouseupHandler : EventHandler = (node, event, options) => {
4951
parent.elm.dispatchEvent(customEvent);
5052
}
5153

52-
const children : VNode[] = newItems.map(c => removeAttribute(c, 'data-index'));
54+
const children: VNode[] = newItems.map(c =>
55+
removeAttribute(c, 'data-index')
56+
);
5357

5458
return replaceNode(node, options.parentSelector, { ...parent, children });
5559
};

0 commit comments

Comments
 (0)