Skip to content

Commit 4613f5d

Browse files
Simplify render function
1 parent 95b4ab4 commit 4613f5d

File tree

2 files changed

+67
-57
lines changed

2 files changed

+67
-57
lines changed

src/util/helper.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,18 @@ const camelize = cached(str =>
1919
str.replace(regex, (_, c) => (c ? c.toUpperCase() : ""))
2020
);
2121

22-
export { console, camelize };
22+
function removeNode(node) {
23+
if (node.parentElement !== null) {
24+
node.parentElement.removeChild(node);
25+
}
26+
}
27+
28+
function insertNodeAt(fatherNode, node, position) {
29+
const refNode =
30+
position === 0
31+
? fatherNode.children[0]
32+
: fatherNode.children[position - 1].nextSibling;
33+
fatherNode.insertBefore(node, refNode);
34+
}
35+
36+
export { insertNodeAt, camelize, console, removeNode };

src/vuedraggable.js

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Sortable from "sortablejs";
2-
import { console, camelize } from "./util/helper";
2+
import { insertNodeAt, camelize, console, removeNode } from "./util/helper";
33

44
function buildAttribute(object, propName, value) {
55
if (value == undefined) {
@@ -10,20 +10,6 @@ function buildAttribute(object, propName, value) {
1010
return object;
1111
}
1212

13-
function removeNode(node) {
14-
if (node.parentElement !== null) {
15-
node.parentElement.removeChild(node);
16-
}
17-
}
18-
19-
function insertNodeAt(fatherNode, node, position) {
20-
const refNode =
21-
position === 0
22-
? fatherNode.children[0]
23-
: fatherNode.children[position - 1].nextSibling;
24-
fatherNode.insertBefore(node, refNode);
25-
}
26-
2713
function computeVmIndex(vnodes, element) {
2814
return vnodes.map(elt => elt.elm).indexOf(element);
2915
}
@@ -54,6 +40,54 @@ function delegateAndEmit(evtName) {
5440
};
5541
}
5642

43+
function isTransition(slots) {
44+
if (!slots || slots.length !== 1) {
45+
return false;
46+
}
47+
const [{ componentOptions }] = slots;
48+
if (!componentOptions) {
49+
return false;
50+
}
51+
return ["transition-group", "TransitionGroup"].includes(componentOptions.tag);
52+
}
53+
54+
function computeChildrenAndOffsets(children, { header, footer }) {
55+
let headerOffset = 0;
56+
let footerOffset = 0;
57+
if (header) {
58+
headerOffset = header.length;
59+
children = children ? [...header, ...children] : [...header];
60+
}
61+
if (footer) {
62+
footerOffset = footer.length;
63+
children = children ? [...children, ...footer] : [...footer];
64+
}
65+
return { children, headerOffset, footerOffset };
66+
}
67+
68+
function getComponentAttributes($attrs, componentData) {
69+
let attributes = null;
70+
const update = (name, value) => {
71+
attributes = buildAttribute(attributes, name, value);
72+
};
73+
const attrs = Object.keys($attrs)
74+
.filter(key => key === "id" || key.startsWith("data-"))
75+
.reduce((res, key) => {
76+
res[key] = $attrs[key];
77+
return res;
78+
}, {});
79+
update("attrs", attrs);
80+
81+
if (!componentData) {
82+
return attributes;
83+
}
84+
const { on, props, attrs: componentDataAttrs } = componentData;
85+
update("on", on);
86+
update("props", props);
87+
Object.assign(attributes.attrs, componentDataAttrs);
88+
return attributes;
89+
}
90+
5791
const eventsListened = ["Start", "Add", "Remove", "Update", "End"];
5892
const eventsToEmit = ["Choose", "Sort", "Filter", "Clone"];
5993
const readonlyProperties = ["Move", ...eventsListened, ...eventsToEmit].map(
@@ -119,49 +153,11 @@ const draggableComponent = {
119153

120154
render(h) {
121155
const slots = this.$slots.default;
122-
if (slots && slots.length === 1) {
123-
const child = slots[0];
124-
if (
125-
child.componentOptions &&
126-
["transition-group", "TransitionGroup"].includes(
127-
child.componentOptions.tag
128-
)
129-
) {
130-
this.transitionMode = true;
131-
}
132-
}
133-
let headerOffset = 0;
134-
let footerOffset = 0;
135-
let children = slots;
136-
const { header, footer } = this.$slots;
137-
if (header) {
138-
headerOffset = header.length;
139-
children = children ? [...header, ...children] : [...header];
140-
}
141-
if (footer) {
142-
footerOffset = footer.length;
143-
children = children ? [...children, ...footer] : [...footer];
144-
}
156+
this.transitionMode = isTransition(slots);
157+
const { children, headerOffset, footerOffset } = computeChildrenAndOffsets(slots, this.$slots);
145158
this.headerOffset = headerOffset;
146159
this.footerOffset = footerOffset;
147-
var attributes = null;
148-
const update = (name, value) => {
149-
attributes = buildAttribute(attributes, name, value);
150-
};
151-
const attrs = Object.keys(this.$attrs)
152-
.filter(key => key === "id" || key.startsWith("data-"))
153-
.reduce((res, key) => {
154-
res[key] = this.$attrs[key];
155-
return res;
156-
}, {});
157-
update("attrs", attrs);
158-
159-
if (this.componentData) {
160-
const { on, props, attrs } = this.componentData;
161-
update("on", on);
162-
update("props", props);
163-
Object.assign(attributes.attrs, attrs);
164-
}
160+
const attributes = getComponentAttributes(this.$attrs, this.componentData);
165161
return h(this.getTag(), attributes, children);
166162
},
167163

0 commit comments

Comments
 (0)