-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathcomponentStructure.js
69 lines (60 loc) · 1.84 KB
/
componentStructure.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
const getHtmlElementFromNode = ({ el }) => el;
const addContext = (domElement, context) =>
(domElement.__draggable_context = context);
const getContext = domElement => domElement.__draggable_context;
class ComponentStructure {
constructor({
nodes: { header, default: defaultNodes, footer },
root,
realList
}) {
this.defaultNodes = defaultNodes;
this.children = [...header, ...defaultNodes, ...footer];
this.externalComponent = root.externalComponent;
this.rootTransition = root.transition;
this.tag = root.tag;
this.realList = realList;
}
get _isRootComponent() {
return this.externalComponent || this.rootTransition;
}
render(h, attributes) {
const { tag, children, _isRootComponent } = this;
const option = !_isRootComponent ? children : { default: () => children };
return h(tag, attributes, option);
}
updated() {
const { defaultNodes, realList } = this;
defaultNodes.forEach((node, index) => {
addContext(getHtmlElementFromNode(node), {
element: realList[index],
index
});
});
}
getUnderlyingVm(domElement) {
return getContext(domElement);
}
getVmIndexFromDomIndex(domIndex, element) {
const { defaultNodes } = this;
const { length } = defaultNodes;
const domChildren = element.children;
const domElement = domChildren.item(domIndex);
if (domElement === null) {
return length;
}
const context = getContext(domElement);
if (context) {
return context.index;
}
if (length === 0) {
return 0;
}
const firstDomListElement = getHtmlElementFromNode(defaultNodes[0]);
const indexFirstDomListElement = [...domChildren].findIndex(
element => element === firstDomListElement
);
return domIndex < indexFirstDomListElement ? 0 : length;
}
}
export { ComponentStructure };