Skip to content

Commit 9c09d85

Browse files
Merge pull request #503 from SortableJS/header-slot
Header slot
2 parents f1ad3b2 + 42f0f28 commit 9c09d85

13 files changed

+2625
-1479
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,15 @@ Draggable component should directly wrap the draggable elements, or a `transitio
113113
<button slot="footer" @click="addPeople">Add</button>
114114
</draggable>
115115
```
116-
116+
### With header slot:
117+
``` html
118+
<draggable v-model="myArray" :options="{draggable:'.item'}">
119+
<div v-for="element in myArray" :key="element.id" class="item">
120+
{{element.name}}
121+
</div>
122+
<button slot="header" @click="addPeople">Add</button>
123+
</draggable>
124+
```
117125
118126
### With Vuex:
119127
@@ -290,9 +298,26 @@ HTML:
290298
- `element`: the moved element
291299
292300
### Slots
301+
302+
#### Header
303+
Use the `header` slot to add none-draggable element inside the vuedraggable component.
304+
Important: it should be used in conjunction with draggable option to tag draggable element.
305+
Note that header slot will always be added before the default slot regardless its position in the template.
306+
Ex:
307+
308+
``` html
309+
<draggable v-model="myArray" :options="{draggable:'.item'}">
310+
<div v-for="element in myArray" :key="element.id" class="item">
311+
{{element.name}}
312+
</div>
313+
<button slot="header" @click="addPeople">Add</button>
314+
</draggable>
315+
```
316+
317+
#### Footer
293318
Use the `footer` slot to add none-draggable element inside the vuedraggable component.
294319
Important: it should be used in conjunction with draggable option to tag draggable element.
295-
Note that footer slot will always be added after the default slot.
320+
Note that footer slot will always be added after the default slot regardless its position in the template.
296321
Ex:
297322
298323
``` html

dist/vuedraggable.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
4+
35
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
46

57
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
@@ -137,12 +139,20 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
137139
this.transitionMode = true;
138140
}
139141
}
142+
var headerOffset = 0;
140143
var children = slots;
141-
var footer = this.$slots.footer;
144+
var _$slots = this.$slots,
145+
header = _$slots.header,
146+
footer = _$slots.footer;
142147

148+
if (header) {
149+
headerOffset = header.length;
150+
children = children ? [].concat(_toConsumableArray(header), _toConsumableArray(children)) : [].concat(_toConsumableArray(header));
151+
}
143152
if (footer) {
144-
children = slots ? [].concat(_toConsumableArray(slots), _toConsumableArray(footer)) : [].concat(_toConsumableArray(footer));
153+
children = children ? [].concat(_toConsumableArray(children), _toConsumableArray(footer)) : [].concat(_toConsumableArray(footer));
145154
}
155+
this.headerOffset = headerOffset;
146156
var attributes = null;
147157
var update = function update(name, value) {
148158
attributes = buildAttribute(attributes, name, value);
@@ -182,7 +192,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
182192
this.computeIndexes();
183193
},
184194
beforeDestroy: function beforeDestroy() {
185-
this._sortable.destroy();
195+
if (this._sortable !== undefined) this._sortable.destroy();
186196
},
187197

188198

@@ -327,6 +337,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
327337
draggingElement = evt.item;
328338
},
329339
onDragAdd: function onDragAdd(evt) {
340+
this.updateEvenemt(evt);
330341
var element = evt.item._underlying_vm_;
331342
if (element === undefined) {
332343
return;
@@ -339,6 +350,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
339350
this.emitChanges({ added: added });
340351
},
341352
onDragRemove: function onDragRemove(evt) {
353+
this.updateEvenemt(evt);
342354
insertNodeAt(this.rootContainer, evt.item, evt.oldIndex);
343355
if (this.isCloning) {
344356
removeNode(evt.clone);
@@ -351,6 +363,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
351363
this.emitChanges({ removed: removed });
352364
},
353365
onDragUpdate: function onDragUpdate(evt) {
366+
this.updateEvenemt(evt);
354367
removeNode(evt.item);
355368
insertNodeAt(evt.from, evt.item, evt.oldIndex);
356369
var oldIndex = this.context.index;
@@ -359,6 +372,13 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
359372
var moved = { element: this.context.element, oldIndex: oldIndex, newIndex: newIndex };
360373
this.emitChanges({ moved: moved });
361374
},
375+
updateEvenemt: function updateEvenemt(evt) {
376+
this.updateProperty(evt, 'newIndex');
377+
this.updateProperty(evt, 'oldIndex');
378+
},
379+
updateProperty: function updateProperty(evt, propertyName) {
380+
evt.hasOwnProperty(propertyName) && (evt[propertyName] += this.headerOffset);
381+
},
362382
computeFutureIndex: function computeFutureIndex(relatedContext, evt) {
363383
if (!relatedContext.element) {
364384
return 0;

dist/vuedraggable.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)