forked from cyclejs-community/cyclejs-sortable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.ts
311 lines (281 loc) · 9.09 KB
/
helpers.ts
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { VNode, VNodeData } from '@cycle/dom';
import { select, classNameFromVNode } from 'snabbdom-selector';
import {
SortableOptions,
MouseOffset,
StartPositionOffset,
Intersection
} from './definitions';
/**
* Takes the maybe undefined options and fills them with defaults
* @param {SortableOptions} options the options, can be undefined
* @param {VNode} the root VNode of the DOMSource
* @return {SortableOptions} the non-null options filled with default
*/
export function applyDefaults(
options: SortableOptions,
root: VNode
): SortableOptions {
const firstClass: (n: VNode) => string = node =>
'.' + classNameFromVNode(node.children[0] as VNode).split(' ')[0];
const itemSelector: string = options.handle
? ''
: options.parentSelector
? firstClass(select(options.parentSelector, root)[0] as VNode)
: firstClass(root);
return {
parentSelector:
options.parentSelector ||
'.' +
classNameFromVNode(root)
.split(' ')
.join('.'),
handle: options.handle || itemSelector,
ghostClass: options.ghostClass || '',
selectionDelay: options.selectionDelay || 0
};
}
/**
* Adds keys to the VNodes to make them able to swap
* @param {VNode} node the root VNode
* @param {key} can be used to specify a inital key
* @return {VNode} a new VNode with the keys
*/
export function addKeys(
node: VNode,
key: string = Math.random().toString()
): VNode {
if (!node) return;
if (!node.children) {
return { ...node, key: node.key ? node.key : key };
}
const children: VNode[] = (node.children as VNode[]).map((c, i) =>
addKeys(c, key + '-' + i)
);
return { ...node, key: node.key ? node.key : key, children };
}
/**
* Returns a parent VNode that has a children matching the selector.
* Currently only works with simple class selector
* @param {VNode} root the parent VNode
* @param {string} selector a valid CSS selector
* @return {VNode} The searched parent node or undefined if none was found
*/
export function getParentNode(root: VNode, selector: string): VNode {
if (root.children === undefined) {
return undefined;
}
const childMatch: boolean = (root.children as VNode[]).reduce(
(acc, curr) =>
!acc && select(selector, curr)[0] === curr ? true : false,
false
);
if (childMatch) {
return root;
}
return root.children
.map<VNode>(
e =>
typeof e !== 'object' ? undefined : getParentNode(e, selector)
)
.reduce((acc, curr) => (!acc && curr ? curr : acc), undefined);
}
/**
* Replaces the node matching the selector with the replacement
* @param {VNode} root the root VNode
* @param {string} selector a valid CSS selector for the replacement
* @param {VNode} replacement the new VNode
* @return {VNode} a new root VNode with the replacement applied
*/
export function replaceNode(
root: VNode,
selector: string,
replacement: VNode
): VNode {
if (select(selector, root)[0] === root) {
return replacement;
}
if (!root.children) {
return root;
}
const children: VNode[] = (root.children as VNode[]).map(e =>
replaceNode(e, selector, replacement)
);
return { ...root, children };
}
/**
* Returns the index of the element in the parent's children array
* @param {Element} e the child element
* @return {number} the index
*/
export function getIndex(node: any): number {
return Array.prototype.indexOf.call(node.parentNode.children, node);
}
/**
* Gets the correct style attribute value for the given parameters
* @param {StartPositionOffset} event the mouse event that was triggered, enriched with distance information
* @param {MouseOffset} mouseOffset the offset of the item
* @param {ClientRect} itemRect the bounding client rect of the item
* @return {string} the style value
*/
export function getGhostStyle(mouseOffset: MouseOffset, item: Element): string {
const itemRect: ClientRect = item.getBoundingClientRect();
return (
'z-index: 5; margin: 0; pointer-events: none; position: absolute; ' +
'width: ' +
itemRect.width +
'px; ' +
'height: ' +
itemRect.height +
'px; ' +
'top: ' +
(mouseOffset.itemTop - mouseOffset.parentTop) +
'px; ' +
'left: ' +
(mouseOffset.itemLeft - mouseOffset.parentLeft) +
'px;'
);
}
/**
* Returns the updated style for this ghost element
*/
export function updateGhostStyle(
event: StartPositionOffset,
mouseOffset: MouseOffset,
ghost: Element
): string {
const prevStyle: string = ghost.getAttribute('style');
return (
prevStyle.substring(0, prevStyle.indexOf(' top:')) +
' top: ' +
(mouseOffset.itemTop - mouseOffset.parentTop + event.distY) +
'px; left: ' +
(mouseOffset.itemLeft - mouseOffset.parentLeft + event.distX) +
'px;'
);
}
/**
* Prevents the page from randomly selecting text when dragging
* @return {string} the content of the style attribute
*/
export function getBodyStyle(): string {
return (
'-webkit-user-select: none; -moz-user-select: none;' +
' -ms-user-select: none; user-select: none; overflow: hidden;'
);
}
/**
* Finds the parent element that matches the given selector
* @param {Element} node the current element
* @param {string} selector a valid CSS selector
* @return {any} the searched parent or undefined if not found
*/
export function findParent(node: Element, selector: string): Element {
if ((node.matches || (node as any).matchesSelector).call(node, 'html')) {
return undefined;
}
if ((node.matches || (node as any).matchesSelector).call(node, selector)) {
return node;
}
return findParent(node.parentNode as Element, selector);
}
/**
* Adds the given attribute safely to the VNode
* @param {VNode} node the VNode to add the attributes on
* @param {any} addition the new attributes
* @return {VNode} the newly created VNode
*/
export function addAttributes(
e: VNode,
newAttr: { [attr: string]: any },
ghostClass?: string
): VNode {
const addition: any = {
attrs: Object.assign({}, e.data ? e.data.attrs : undefined, newAttr)
};
return addToData(e, addition);
}
/**
* Adds the given class safely to the VNode
* @param {VNode} node the VNode to add the attributes on
* @param {any} addition the css ghost class
* @return {VNode} the newly created VNode
*/
export function addGhostClass(e: VNode, ghostClass?: string): VNode {
const className =
ghostClass && ghostClass.length > 1
? ghostClass[0] === '.' ? ghostClass.substring(1) : ghostClass
: undefined;
const classVal = className ? { [className]: true } : undefined;
const addition: any = {
class: classVal
};
return addToData(e, addition);
}
/**
* Removes the given attribute from the VNode
* @param {VNode} node the VNode
* @param {string} attributeName the name of the attribute
* @return {VNode} a new VNode without the attribute
*/
export function removeAttribute(node: VNode, attributeName: string): VNode {
if (!node.data || !node.data.attrs) {
return node;
}
return Object.assign({}, node, {
data: Object.assign({}, node.data, {
attrs: Object.keys(node.data.attrs)
.filter(k => k !== attributeName)
.map(k => ({ [k]: node.data.attrs[k] }))
.reduce((acc, curr) => Object.assign(acc, curr), {})
})
});
}
/**
* Adds the given additions safely to the VNode
* @param {VNode} node the VNode to add the additions on
* @param {any} addition the new additions
* @return {VNode} the newly created VNode
*/
export function addToData(
node: VNode,
additions: { [key: string]: any }
): VNode {
const hasData: boolean = node.data !== undefined;
const key: string = Object.keys(additions)[0];
const a: any = Object.assign(
{},
hasData ? { [key]: node.data[key] } : {},
additions
);
const data: VNodeData = Object.assign({}, node.data, a);
return Object.assign({}, node, {
data: data
});
}
/**
* Calculates the intersection of two elements
* @param {Element} e1 the first element
* @param {Element} e2 the first element
* @return {Intersection} the intersection or undefined if there is no intersecton
*/
export function getIntersection(e1: Element, e2: Element): Intersection {
const c1: ClientRect = e1.getBoundingClientRect();
const c2: ClientRect = e2.getBoundingClientRect();
const intersection: Intersection = {
xmin: Math.max(c1.left, c2.left),
ymin: Math.max(c1.top, c2.top),
xmax: Math.min(c1.right, c2.right),
ymax: Math.min(c1.bottom, c2.bottom)
};
return intersection;
}
/**
* Calculates the area of the given rectangle
*/
export function getArea(intersection: Intersection): number {
return (
(intersection.xmax - intersection.xmin) *
(intersection.ymax - intersection.ymin)
);
}