|
1 | 1 | /*! |
2 | | - * vue-virtual-scroll-list v2.1.1 |
| 2 | + * vue-virtual-scroll-list v2.1.2 |
3 | 3 | * open source under the MIT license |
4 | 4 | * https://github.com/tangbc/vue-virtual-scroll-list#readme |
5 | 5 | */ |
|
427 | 427 | type: String, |
428 | 428 | "default": '' |
429 | 429 | }, |
| 430 | + itemClassAdd: { |
| 431 | + type: Function |
| 432 | + }, |
430 | 433 | headerTag: { |
431 | 434 | type: String, |
432 | 435 | "default": 'div' |
|
449 | 452 | } |
450 | 453 | }; |
451 | 454 | var ItemProps = { |
| 455 | + index: { |
| 456 | + type: Number |
| 457 | + }, |
452 | 458 | event: { |
453 | 459 | type: String |
454 | 460 | }, |
|
492 | 498 | */ |
493 | 499 | var Wrapper = { |
494 | 500 | created: function created() { |
495 | | - this.hasInitial = false; |
496 | 501 | this.shapeKey = this.horizontal ? 'offsetWidth' : 'offsetHeight'; |
497 | 502 | }, |
498 | 503 | mounted: function mounted() { |
499 | 504 | var _this = this; |
500 | 505 |
|
501 | | - // dispatch once at initial |
502 | | - this.dispatchSizeChange(); |
503 | | - |
504 | 506 | if (typeof ResizeObserver !== 'undefined') { |
505 | 507 | this.resizeObserver = new ResizeObserver(function () { |
506 | | - // dispatch when size changed |
507 | | - if (_this.hasInitial) { |
508 | | - _this.dispatchSizeChange(); |
509 | | - } else { |
510 | | - _this.hasInitial = true; |
511 | | - } |
| 508 | + _this.dispatchSizeChange(); |
512 | 509 | }); |
513 | 510 | this.resizeObserver.observe(this.$el); |
514 | 511 | } |
515 | 512 | }, |
| 513 | + // since componet will be reused, so disptach when updated |
| 514 | + updated: function updated() { |
| 515 | + this.dispatchSizeChange(); |
| 516 | + }, |
516 | 517 | beforeDestroy: function beforeDestroy() { |
517 | 518 | if (this.resizeObserver) { |
518 | 519 | this.resizeObserver.disconnect(); |
|
537 | 538 | var tag = this.tag, |
538 | 539 | component = this.component, |
539 | 540 | _this$extraProps = this.extraProps, |
540 | | - extraProps = _this$extraProps === void 0 ? {} : _this$extraProps; |
| 541 | + extraProps = _this$extraProps === void 0 ? {} : _this$extraProps, |
| 542 | + index = this.index; |
541 | 543 | extraProps.source = this.source; |
| 544 | + extraProps.index = index; |
542 | 545 | return h(tag, { |
543 | 546 | role: 'item' |
544 | 547 | }, [h(component, { |
|
581 | 584 | }; |
582 | 585 | }, |
583 | 586 | watch: { |
584 | | - dataSources: function dataSources(newValue, oldValue) { |
585 | | - if (newValue.length !== oldValue.length) { |
586 | | - this.virtual.updateParam('uniqueIds', this.getUniqueIdFromDataSources()); |
587 | | - this.virtual.handleDataSourcesChange(); |
588 | | - } |
| 587 | + 'dataSources.length': function dataSourcesLength() { |
| 588 | + this.virtual.updateParam('uniqueIds', this.getUniqueIdFromDataSources()); |
| 589 | + this.virtual.handleDataSourcesChange(); |
589 | 590 | }, |
590 | 591 | start: function start(newValue) { |
591 | 592 | this.scrollToIndex(newValue); |
|
621 | 622 | this.virtual.destroy(); |
622 | 623 | }, |
623 | 624 | methods: { |
| 625 | + // get item size by id |
| 626 | + getSize: function getSize(id) { |
| 627 | + return this.virtual.sizes.get(id); |
| 628 | + }, |
| 629 | + // get the total number of stored (rendered) items |
| 630 | + getSizes: function getSizes() { |
| 631 | + return this.virtual.sizes.size; |
| 632 | + }, |
624 | 633 | // set current scroll position to a expectant offset |
625 | 634 | scrollToOffset: function scrollToOffset(offset) { |
626 | 635 | var root = this.$refs.root; |
|
703 | 712 | // event called when each item mounted or size changed |
704 | 713 | onItemResized: function onItemResized(id, size) { |
705 | 714 | this.virtual.saveSize(id, size); |
| 715 | + this.$emit('resized', id, size); |
706 | 716 | }, |
707 | 717 | // event called when slot mounted or size changed |
708 | 718 | onSlotResized: function onSlotResized(type, size, hasInit) { |
|
734 | 744 | }, |
735 | 745 | // emit event in special position |
736 | 746 | emitEvent: function emitEvent(offset, clientSize, scrollSize, evt) { |
737 | | - var range = this.virtual.getRange(); |
738 | | - |
739 | 747 | if (this.virtual.isFront() && !!this.dataSources.length && offset - this.topThreshold <= 0) { |
740 | | - this.$emit('totop', evt, range); |
| 748 | + this.$emit('totop'); |
741 | 749 | } else if (this.virtual.isBehind() && offset + clientSize + this.bottomThreshold >= scrollSize) { |
742 | | - this.$emit('tobottom', evt, range); |
| 750 | + this.$emit('tobottom'); |
743 | 751 | } else { |
744 | | - this.$emit('scroll', evt, range); |
| 752 | + this.$emit('scroll', evt, this.virtual.getRange()); |
745 | 753 | } |
746 | 754 | }, |
747 | 755 | // get the real render slots based on range data |
|
766 | 774 | if (dataSource) { |
767 | 775 | if (dataSource[dataKey]) { |
768 | 776 | slots.push(h(Item, { |
769 | | - "class": itemClass, |
770 | 777 | props: { |
| 778 | + index: index, |
771 | 779 | tag: itemTag, |
772 | 780 | event: EVENT_TYPE.ITEM, |
773 | 781 | horizontal: isHorizontal, |
774 | 782 | uniqueKey: dataSource[dataKey], |
775 | 783 | source: dataSource, |
776 | 784 | extraProps: extraProps, |
777 | 785 | component: dataComponent |
778 | | - } |
| 786 | + }, |
| 787 | + "class": "".concat(itemClass, " ").concat(this.itemClassAdd ? this.itemClassAdd(index) : '') |
779 | 788 | })); |
780 | 789 | } else { |
781 | 790 | console.warn("Cannot get the data-key '".concat(dataKey, "' from data-sources.")); |
|
0 commit comments