Skip to content

Commit 7997c98

Browse files
Fix for issue #558 and issue #555
1 parent 19caac1 commit 7997c98

File tree

6 files changed

+127
-17
lines changed

6 files changed

+127
-17
lines changed

example/components/simple.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
:list="list"
3131
:disabled="!enabled"
3232
class="list-group"
33-
ghostClass="ghost"
33+
ghost-class="ghost"
3434
@start="dragging = true"
3535
@end="dragging = false"
3636
>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<template>
2+
<div class="row">
3+
<div class="col-4">
4+
<h3>First draggable with header</h3>
5+
6+
<draggable :list="list" class="list-group" draggable=".item" group="a">
7+
<div
8+
class="list-group-item item"
9+
v-for="element in list"
10+
:key="element.name"
11+
>
12+
{{ element.name }}
13+
</div>
14+
15+
<div
16+
slot="footer"
17+
class="btn-group list-group-item"
18+
role="group"
19+
aria-label="Basic example"
20+
>
21+
<button class="btn btn-secondary" @click="add">Add</button>
22+
<button class="btn btn-secondary" @click="replace">Replace</button>
23+
</div>
24+
</draggable>
25+
</div>
26+
27+
<div class="col-4">
28+
<h3>Second draggable with header</h3>
29+
30+
<draggable :list="list2" class="list-group" draggable=".item" group="a">
31+
<div
32+
class="list-group-item item"
33+
v-for="element in list2"
34+
:key="element.name"
35+
>
36+
{{ element.name }}
37+
</div>
38+
39+
<div
40+
slot="footer"
41+
class="btn-group list-group-item"
42+
role="group"
43+
aria-label="Basic example"
44+
>
45+
<button class="btn btn-secondary" @click="add">Add</button>
46+
<button class="btn btn-secondary" @click="replace">Replace</button>
47+
</div>
48+
</draggable>
49+
</div>
50+
51+
<rawDisplayer class="col-2" :value="list" title="List" />
52+
53+
<rawDisplayer class="col-2" :value="list2" title="List2" />
54+
</div>
55+
</template>
56+
57+
<script>
58+
import draggable from "@/vuedraggable";
59+
let id = 1;
60+
export default {
61+
name: "headerslots",
62+
display: "Two list header slot",
63+
order: 13,
64+
components: {
65+
draggable
66+
},
67+
data() {
68+
return {
69+
list: [
70+
{ name: "John 1", id: 0 },
71+
{ name: "Joao 2", id: 1 },
72+
{ name: "Jean 3", id: 2 }
73+
],
74+
list2: [{ name: "Jonny 4", id: 3 }, { name: "Guisepe 5", id: 4 }]
75+
};
76+
},
77+
methods: {
78+
add: function() {
79+
this.list.push({ name: "Juan " + id, id: id++ });
80+
},
81+
replace: function() {
82+
this.list = [{ name: "Edgard", id: id++ }];
83+
}
84+
}
85+
};
86+
</script>
87+
<style scoped></style>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@
9797
],
9898
"testURL": "http://localhost/",
9999
"collectCoverageFrom": [
100-
"<rootDir>/src/vuedraggable.js"
100+
"<rootDir>/src/vuedraggable.js",
101+
"<rootDir>/src/util/helper.js"
101102
]
102103
},
103104
"files": [

src/util/helper.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function getConsole() {
2+
if (typeof window !== "undefined") {
3+
return window.console;
4+
}
5+
return global.console;
6+
}
7+
const console = getConsole();
8+
9+
function cached(fn) {
10+
const cache = Object.create(null);
11+
return function cachedFn(str) {
12+
const hit = cache[str];
13+
return hit || (cache[str] = fn(str));
14+
};
15+
}
16+
17+
const regex = /-(\w)/g;
18+
const camelize = cached(str =>
19+
str.replace(regex, (_, c) => (c ? c.toUpperCase() : ""))
20+
);
21+
22+
export { console, camelize };

src/vuedraggable.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
const Sortable = require("sortablejs");
2-
3-
function getConsole() {
4-
if (typeof window !== "undefined") {
5-
return window.console;
6-
}
7-
return global.console;
8-
}
9-
10-
const console = getConsole();
1+
import Sortable from "sortablejs";
2+
import { console, camelize } from "./util/helper";
113

124
function buildAttribute(object, propName, value) {
135
if (value == undefined) {
@@ -207,7 +199,12 @@ const draggableComponent = {
207199
optionsAdded["on" + elt] = emit.bind(this, elt);
208200
});
209201

210-
const options = Object.assign({}, this.options, this.$attrs, optionsAdded, {
202+
const attributes = Object.keys(this.$attrs).reduce((res, key) => {
203+
res[camelize(key)] = this.$attrs[key];
204+
return res;
205+
}, {});
206+
207+
const options = Object.assign({}, this.options, attributes, optionsAdded, {
211208
onMove: (evt, originalEvent) => {
212209
return this.onDragMove(evt, originalEvent);
213210
}
@@ -272,8 +269,9 @@ const draggableComponent = {
272269

273270
updateOptions(newOptionValue) {
274271
for (var property in newOptionValue) {
275-
if (readonlyProperties.indexOf(property) == -1) {
276-
this._sortable.option(property, newOptionValue[property]);
272+
const value = camelize(property);
273+
if (readonlyProperties.indexOf(value) == -1) {
274+
this._sortable.option(value, newOptionValue[property]);
277275
}
278276
}
279277
},

tests/unit/vuedraggable.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ describe("draggable.vue when initialized with list", () => {
3333
list: items
3434
},
3535
attrs: {
36-
sortableOption: "value"
36+
sortableOption: "value",
37+
"to-be-camelized": true
3738
},
3839
slots: {
3940
default: items.map(item => `<div>${item}</div>`),
@@ -187,7 +188,8 @@ describe("draggable.vue when initialized with list", () => {
187188
expect(parameters[0]).toBe(element);
188189
expect(parameters[1]).toMatchObject({
189190
draggable: ">*",
190-
sortableOption: "value"
191+
sortableOption: "value",
192+
toBeCamelized: true
191193
});
192194
})
193195

0 commit comments

Comments
 (0)