Skip to content

Commit 2964c88

Browse files
committed
fix error when the datakey get number value
1 parent c8a6f97 commit 2964c88

File tree

6 files changed

+113
-86
lines changed

6 files changed

+113
-86
lines changed

dist/virtual-drag-list.js

Lines changed: 91 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* vue-virtual-draglist v3.3.6
2+
* vue-virtual-draglist v3.3.7
33
* open source under the MIT license
44
* https://github.com/mfuu/vue3-virtual-drag-list#readme
55
*/
@@ -857,6 +857,54 @@
857857
})(sortableDnd_min);
858858
var Dnd = sortableDnd_min.exports;
859859

860+
function throttle(fn, wait) {
861+
var timer;
862+
var result = function result() {
863+
var _this = this;
864+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
865+
args[_key] = arguments[_key];
866+
}
867+
if (timer) return;
868+
if (wait <= 0) {
869+
fn.apply(this, args);
870+
} else {
871+
timer = setTimeout(function () {
872+
timer = null;
873+
fn.apply(_this, args);
874+
}, wait);
875+
}
876+
};
877+
result['cancel'] = function () {
878+
if (timer) {
879+
clearTimeout(timer);
880+
timer = null;
881+
}
882+
};
883+
return result;
884+
}
885+
function debounce(fn, wait) {
886+
var throttled = throttle(fn, wait);
887+
var result = function result() {
888+
throttled['cancel']();
889+
throttled.apply(this, arguments);
890+
};
891+
result['cancel'] = function () {
892+
throttled['cancel']();
893+
};
894+
return result;
895+
}
896+
function isSameValue(a, b) {
897+
return a == b;
898+
}
899+
function getDataKey(item, dataKey) {
900+
return (!Array.isArray(dataKey) ? dataKey.replace(/\[/g, '.').replace(/\]/g, '.').split('.') : dataKey).reduce(function (o, k) {
901+
return (o || {})[k];
902+
}, item);
903+
}
904+
function elementIsDocumentOrWindow(element) {
905+
return element instanceof Document && element.nodeType === 9 || element instanceof Window;
906+
}
907+
860908
var SortableAttrs = ['delay', 'group', 'handle', 'lockAxis', 'disabled', 'sortable', 'draggable', 'animation', 'autoScroll', 'ghostClass', 'ghostStyle', 'chosenClass', 'scrollSpeed', 'fallbackOnBody', 'scrollThreshold', 'delayOnTouchOnly', 'placeholderClass'];
861909
var Sortable = /*#__PURE__*/function () {
862910
function Sortable(el, options) {
@@ -923,9 +971,10 @@
923971
}, {
924972
key: "onDrag",
925973
value: function onDrag(event) {
926-
var key = event.node.getAttribute('data-key');
927-
var index = this.getIndex(key);
974+
var dataKey = event.node.getAttribute('data-key');
975+
var index = this.getIndex(dataKey);
928976
var item = this.options.list[index];
977+
var key = this.options.uniqueKeys[index];
929978
// store the dragged item
930979
this.sortable.option('store', {
931980
item: item,
@@ -1014,7 +1063,16 @@
10141063
}, {
10151064
key: "getIndex",
10161065
value: function getIndex(key) {
1017-
return this.options.uniqueKeys.indexOf(key);
1066+
if (key === null || key === undefined) {
1067+
return -1;
1068+
}
1069+
var uniqueKeys = this.options.uniqueKeys;
1070+
for (var i = 0, len = uniqueKeys.length; i < len; i++) {
1071+
if (isSameValue(uniqueKeys[i], key)) {
1072+
return i;
1073+
}
1074+
}
1075+
return -1;
10181076
}
10191077
}, {
10201078
key: "dispatchEvent",
@@ -1025,51 +1083,6 @@
10251083
}]);
10261084
}();
10271085

1028-
function throttle(fn, wait) {
1029-
var timer;
1030-
var result = function result() {
1031-
var _this = this;
1032-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1033-
args[_key] = arguments[_key];
1034-
}
1035-
if (timer) return;
1036-
if (wait <= 0) {
1037-
fn.apply(this, args);
1038-
} else {
1039-
timer = setTimeout(function () {
1040-
timer = null;
1041-
fn.apply(_this, args);
1042-
}, wait);
1043-
}
1044-
};
1045-
result['cancel'] = function () {
1046-
if (timer) {
1047-
clearTimeout(timer);
1048-
timer = null;
1049-
}
1050-
};
1051-
return result;
1052-
}
1053-
function debounce(fn, wait) {
1054-
var throttled = throttle(fn, wait);
1055-
var result = function result() {
1056-
throttled['cancel']();
1057-
throttled.apply(this, arguments);
1058-
};
1059-
result['cancel'] = function () {
1060-
throttled['cancel']();
1061-
};
1062-
return result;
1063-
}
1064-
function getDataKey(item, dataKey) {
1065-
return (!Array.isArray(dataKey) ? dataKey.replace(/\[/g, '.').replace(/\]/g, '.').split('.') : dataKey).reduce(function (o, k) {
1066-
return (o || {})[k];
1067-
}, item);
1068-
}
1069-
function elementIsDocumentOrWindow(element) {
1070-
return element instanceof Document && element.nodeType === 9 || element instanceof Window;
1071-
}
1072-
10731086
var VirtualAttrs = ['size', 'keeps', 'scroller', 'direction', 'debounceTime', 'throttleTime'];
10741087
var Virtual = /*#__PURE__*/function () {
10751088
function Virtual(options) {
@@ -1097,7 +1110,8 @@
10971110
start: 0,
10981111
end: 0,
10991112
front: 0,
1100-
behind: 0
1113+
behind: 0,
1114+
total: 0
11011115
};
11021116
this.offset = 0;
11031117
this.direction = 'STATIONARY';
@@ -1161,7 +1175,7 @@
11611175
if (index >= this.options.uniqueKeys.length - 1) {
11621176
this.scrollToBottom();
11631177
} else {
1164-
var indexOffset = this.getOffsetByIndex(index);
1178+
var indexOffset = this.getOffsetByRange(0, index);
11651179
var startOffset = this.getScrollStartOffset();
11661180
this.scrollToOffset(indexOffset + startOffset);
11671181
}
@@ -1289,7 +1303,8 @@
12891303
value: function updateScrollElement() {
12901304
var scroller = this.options.scroller;
12911305
if (elementIsDocumentOrWindow(scroller)) {
1292-
this.scrollEl = document.scrollingElement || document.documentElement || document.body;
1306+
var scrollEl = document.scrollingElement || document.documentElement || document.body;
1307+
this.scrollEl = scrollEl;
12931308
} else {
12941309
this.scrollEl = scroller;
12951310
}
@@ -1376,7 +1391,7 @@
13761391
var middleOffset = 0;
13771392
while (low <= high) {
13781393
middle = low + Math.floor((high - low) / 2);
1379-
middleOffset = this.getOffsetByIndex(middle);
1394+
middleOffset = this.getOffsetByRange(0, middle);
13801395
if (middleOffset === offset) {
13811396
return middle;
13821397
} else if (middleOffset < offset) {
@@ -1408,15 +1423,23 @@
14081423
this.range.end = this.getEndByStart(start);
14091424
this.range.front = this.getFrontOffset();
14101425
this.range.behind = this.getBehindOffset();
1426+
this.range.total = this.getTotalOffset();
14111427
this.options.onUpdate(Object.assign({}, this.range));
14121428
}
1429+
}, {
1430+
key: "getTotalOffset",
1431+
value: function getTotalOffset() {
1432+
var offset = this.range.front + this.range.behind;
1433+
offset += this.getOffsetByRange(this.range.start, this.range.end + 1);
1434+
return offset;
1435+
}
14131436
}, {
14141437
key: "getFrontOffset",
14151438
value: function getFrontOffset() {
14161439
if (this.isFixed()) {
14171440
return this.fixedSize * this.range.start;
14181441
} else {
1419-
return this.getOffsetByIndex(this.range.start);
1442+
return this.getOffsetByRange(0, this.range.start);
14201443
}
14211444
}
14221445
}, {
@@ -1430,13 +1453,13 @@
14301453
return (last - end) * this.getItemSize();
14311454
}
14321455
}, {
1433-
key: "getOffsetByIndex",
1434-
value: function getOffsetByIndex(index) {
1435-
if (!index) {
1456+
key: "getOffsetByRange",
1457+
value: function getOffsetByRange(start, end) {
1458+
if (start >= end) {
14361459
return 0;
14371460
}
14381461
var offset = 0;
1439-
for (var i = 0; i < index; i++) {
1462+
for (var i = start; i < end; i++) {
14401463
var size = this.sizes.get(this.options.uniqueKeys[i]);
14411464
offset = offset + (typeof size === 'number' ? size : this.getItemSize());
14421465
}
@@ -1471,9 +1494,9 @@
14711494
}
14721495
var offset = 0;
14731496
if (scroller && wrapper) {
1474-
var sizeKey = this.isHorizontal() ? 'left' : 'top';
1497+
var offsetKey = this.isHorizontal() ? 'left' : 'top';
14751498
var rect = elementIsDocumentOrWindow(scroller) ? Dnd.utils.getRect(wrapper) : Dnd.utils.getRect(wrapper, true, scroller);
1476-
offset = this.offset + rect[sizeKey];
1499+
offset = this.offset + rect[offsetKey];
14771500
}
14781501
return offset;
14791502
}
@@ -1716,7 +1739,8 @@
17161739
start: 0,
17171740
end: props.keeps - 1,
17181741
front: 0,
1719-
behind: 0
1742+
behind: 0,
1743+
total: 0
17201744
});
17211745
var horizontal = vue.computed(function () {
17221746
return props.direction !== 'vertical';
@@ -1840,8 +1864,8 @@
18401864
};
18411865
// ========================================== use virtual ==========================================
18421866
var virtual;
1843-
var choosen = vue.ref();
18441867
var dragging = vue.ref(false);
1868+
var chosenKey = vue.ref('');
18451869
var virtualAttributes = vue.computed(function () {
18461870
return VirtualAttrs.reduce(function (res, key) {
18471871
res[key] = props[key];
@@ -1891,7 +1915,7 @@
18911915
};
18921916
var onItemResized = function onItemResized(size, key) {
18931917
// ignore changes for dragging element
1894-
if (key === choosen.value) {
1918+
if (isSameValue(key, chosenKey.value)) {
18951919
return;
18961920
}
18971921
var sizes = virtual.sizes.size;
@@ -1918,10 +1942,10 @@
19181942
}
19191943
});
19201944
var onChoose = function onChoose(event) {
1921-
choosen.value = event.node.getAttribute('data-key');
1945+
chosenKey.value = event.node.getAttribute('data-key');
19221946
};
19231947
var onUnchoose = function onUnchoose() {
1924-
choosen.value = '';
1948+
chosenKey.value = '';
19251949
};
19261950
var onDrag = function onDrag(event) {
19271951
dragging.value = true;
@@ -1978,10 +2002,11 @@
19782002
var record = listRef.value[index];
19792003
if (record) {
19802004
var dataKey = getDataKey(record, props.dataKey);
2005+
var isChosen = isSameValue(dataKey, chosenKey.value);
19812006
renders.push(slots.item ? vue.h(Item, {
19822007
key: dataKey,
19832008
"class": props.itemClass,
1984-
style: dragging.value && dataKey == choosen.value && {
2009+
style: dragging.value && isChosen && {
19852010
display: 'none'
19862011
},
19872012
dataKey: dataKey,

dist/virtual-drag-list.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-virtual-draglist",
3-
"version": "3.3.6",
3+
"version": "3.3.7",
44
"description": "A virtual scrolling list component that can be sorted by dragging, support vue3",
55
"main": "dist/virtual-drag-list.min.js",
66
"types": "types/index.d.ts",

src/core

0 commit comments

Comments
 (0)