-
Notifications
You must be signed in to change notification settings - Fork 546
/
Copy pathrenderHelper.js
73 lines (67 loc) · 1.93 KB
/
renderHelper.js
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
import { resolveComponent, TransitionGroup } from "vue";
import { isHtmlTag, isTransition } from "../util/tags";
import { ComponentStructure } from "./componentStructure";
function getSlot(slots, key) {
const slotValue = slots[key];
return slotValue ? slotValue() : [];
}
function getRealNode(rawNode) {
let node = rawNode;
let failed = false;
while (!failed && node && !node.el) {
const children = node.children;
if (children && typeof children !== "string" && children.length === 1) {
node = children[0];
} else {
failed = true;
}
}
return node;
}
function computeNodes({ $slots, realList, getKey }) {
const normalizedList = realList || [];
const [header, footer] = ["header", "footer"].map(name =>
getSlot($slots, name)
);
const { item } = $slots;
if (!item) {
throw new Error("draggable element must have an item slot");
}
const defaultNodes = normalizedList.flatMap((element, index) =>
item({ element, index }).map(rawNode => {
const node = getRealNode(rawNode);
if (node) {
node.key = getKey(element);
node.props = { ...(node.props || {}), "data-draggable": true };
}
return node;
})
);
if (defaultNodes.length !== normalizedList.length) {
throw new Error("Item slot must have only one child");
}
return {
header,
footer,
default: defaultNodes
};
}
function getRootInformation(tag) {
const transition = isTransition(tag);
const externalComponent = !isHtmlTag(tag) && !transition;
return {
transition,
externalComponent,
tag: externalComponent
? resolveComponent(tag)
: transition
? TransitionGroup
: tag
};
}
function computeComponentStructure({ $slots, tag, realList, getKey }) {
const nodes = computeNodes({ $slots, realList, getKey });
const root = getRootInformation(tag);
return new ComponentStructure({ nodes, root, realList });
}
export { computeComponentStructure };