Skip to content

Commit 65504a0

Browse files
Refactoring
1 parent 5ca85ed commit 65504a0

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

src/core/componentBuilderHelper.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { capitalize } from "../util/string";
22
import { camelize } from "../util/string";
3-
import { events } from "./sortableEvents";
3+
import { events, isReadOnlyEvent } from "./sortableEvents";
4+
5+
function isHtmlAttribute(value) {
6+
return ["id", "class"].includes(value) || value.startsWith("data-");
7+
}
48

59
function getComponentAttributes($attrs, componentData) {
610
const attributes = Object.entries($attrs)
7-
.filter(
8-
([key, _]) => ["id", "class"].includes(key) || key.startsWith("data-")
9-
)
11+
.filter(([key, _]) => isHtmlAttribute(key))
1012
.reduce((res, [key, value]) => {
1113
res[key] = value;
1214
return res;
@@ -22,27 +24,22 @@ function getComponentAttributes($attrs, componentData) {
2224
return { ...attributes, ...attrs, ...props };
2325
}
2426

25-
function getDraggableOption({
26-
$attrs,
27-
callBackBuilder: { manageAndEmit, emit, manage }
28-
}) {
27+
function getSortableOption({ $attrs, callBackBuilder }) {
2928
const options = {
3029
draggable: ">*"
3130
};
32-
Object.entries($attrs).forEach(([key, value]) => {
33-
options[camelize(key)] = value;
34-
});
35-
const builders = {
36-
emit,
37-
manageAndEmit,
38-
manage
39-
};
40-
Object.entries(builders).forEach(([eventType, eventBuilder]) => {
31+
Object.entries($attrs)
32+
.map(([key, value]) => [camelize(key), value])
33+
.filter(([key, _]) => !isHtmlAttribute(key) && !isReadOnlyEvent(key))
34+
.forEach(([key, value]) => {
35+
options[key] = value;
36+
});
37+
Object.entries(callBackBuilder).forEach(([eventType, eventBuilder]) => {
4138
events[eventType].forEach(event => {
4239
options[`on${event}`] = eventBuilder(event);
4340
});
4441
});
4542
return options;
4643
}
4744

48-
export { getComponentAttributes, getDraggableOption };
45+
export { getComponentAttributes, getSortableOption };

src/core/sortableEvents.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const manageAndEmit = ["Start", "Add", "Remove", "Update", "End"];
22
const emit = ["Choose", "Unchoose", "Sort", "Filter", "Clone"];
33
const manage = ["Move"];
4-
const readonlyProperties = [...manage, ...manageAndEmit, ...emit].map(
4+
const eventHandlerNames = [...manage, ...manageAndEmit, ...emit].map(
55
evt => `on${evt}`
66
);
77

@@ -12,7 +12,7 @@ const events = {
1212
};
1313

1414
function isReadOnlyEvent(eventName) {
15-
return readonlyProperties.indexOf(eventName) !== -1;
15+
return eventHandlerNames.indexOf(eventName) !== -1;
1616
}
1717

1818
export { events, isReadOnlyEvent };

src/vuedraggable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { camelize } from "./util/string";
55
import { isHtmlTag, isTransition as isTransitionName } from "./util/tags";
66
import {
77
getComponentAttributes,
8-
getDraggableOption
8+
getSortableOption
99
} from "./core/componentBuilderHelper";
1010
import { isReadOnlyEvent } from "./core/sortableEvents";
1111

@@ -179,7 +179,7 @@ const draggableComponent = defineComponent({
179179
`Transition-group inside component is not supported. Please alter tag value or remove transition-group. Current tag value: ${tag}`
180180
);
181181
}
182-
const sortableOptions = getDraggableOption({
182+
const sortableOptions = getSortableOption({
183183
$attrs,
184184
callBackBuilder: {
185185
manageAndEmit: event => manageAndEmit.call(this, event),

0 commit comments

Comments
 (0)