|
34 | 34 | },
|
35 | 35 |
|
36 | 36 | oncreate() {
|
37 |
| - const { items, _props, itemHeight } = this.get(); |
38 |
| - const { viewport, container } = this.refs; |
39 |
| - const viewportHeight = viewport.offsetHeight; |
| 37 | + const { items, _props } = this.get(); |
| 38 | + const { container } = this.refs; |
40 | 39 |
|
41 | 40 | const keys = Object.keys(this.options.data).filter(key => key !== 'items' && key !== 'component' && key !== 'height' && key !== 'itemHeight');
|
42 | 41 | if (keys.length) {
|
|
47 | 46 | this.set({ _props });
|
48 | 47 | }
|
49 | 48 |
|
| 49 | + this.heightMap = []; |
50 | 50 | this.rows = container.getElementsByClassName('row');
|
51 | 51 |
|
52 |
| - if (itemHeight) { |
53 |
| - this.heightMap = items.map(item => itemHeight); |
54 |
| - this.set({ |
55 |
| - end: Math.min(items.length, Math.ceil(viewportHeight / itemHeight)), |
56 |
| - bottom: items.length * itemHeight |
57 |
| - }); |
58 |
| - } else { |
59 |
| - this.heightMap = []; |
60 |
| - |
61 |
| - let height = 0; |
62 |
| - let i = 0; |
63 |
| - |
64 |
| - while (height < viewportHeight && i < items.length) { |
65 |
| - this.set({ end: i + 1 }); |
66 |
| - |
67 |
| - const rowHeight = this.heightMap[i] = this.rows[i].offsetHeight; |
68 |
| - height += rowHeight; |
69 |
| - |
70 |
| - i += 1; |
71 |
| - } |
72 |
| - |
73 |
| - const end = i; |
74 |
| - const avg = Math.round(height / i); |
75 |
| - |
76 |
| - for (; i < items.length; i += 1) this.heightMap[i] = avg; |
77 |
| - |
78 |
| - this.set({ |
79 |
| - bottom: (items.length - end) * avg |
80 |
| - }); |
| 52 | + if (items.length > 0) { |
| 53 | + this.initialise(); |
81 | 54 | }
|
82 | 55 |
|
83 | 56 | this.on('state', ({ changed, previous, current }) => {
|
84 | 57 | if (changed.items || changed.height || changed.itemHeight) {
|
85 |
| - if (current.itemHeight && (changed.itemHeight || current.items.length !== this.heightMap.length)) { |
| 58 | + if (current.itemHeight && (changed.itemHeight || current.items.length > this.heightMap.length)) { |
86 | 59 | this.heightMap = current.items.map(() => current.itemHeight);
|
87 | 60 | }
|
88 | 61 |
|
| 62 | + else if (current.items.length > this.heightMap.length) { |
| 63 | + if (this.heightMap.length === 0) { |
| 64 | + this.initialise(); |
| 65 | + } else { |
| 66 | + let height = 0; |
| 67 | + let i = 0; |
| 68 | + for (; i < this.heightMap.length; i += 1) height += this.heightMap[i]; |
| 69 | + const avg = height / this.heightMap.length; |
| 70 | + for (; i < current.items.length; i += 1) this.heightMap[i] = avg; |
| 71 | + } |
| 72 | + } |
| 73 | + |
89 | 74 | this.refresh();
|
90 | 75 | }
|
91 | 76 |
|
|
100 | 85 | },
|
101 | 86 |
|
102 | 87 | methods: {
|
| 88 | + initialise() { |
| 89 | + const { items, itemHeight } = this.get(); |
| 90 | + const { viewport } = this.refs; |
| 91 | + const viewportHeight = viewport.offsetHeight; |
| 92 | + |
| 93 | + if (itemHeight) { |
| 94 | + this.heightMap = items.map(item => itemHeight); |
| 95 | + this.set({ |
| 96 | + end: Math.min(items.length, Math.ceil(viewportHeight / itemHeight)), |
| 97 | + bottom: items.length * itemHeight |
| 98 | + }); |
| 99 | + } else { |
| 100 | + let height = 0; |
| 101 | + let i = 0; |
| 102 | + |
| 103 | + while (height < viewportHeight && i < items.length) { |
| 104 | + this.set({ end: i + 1 }); |
| 105 | + |
| 106 | + const rowHeight = this.heightMap[i] = this.rows[i].offsetHeight; |
| 107 | + height += rowHeight; |
| 108 | + |
| 109 | + i += 1; |
| 110 | + } |
| 111 | + |
| 112 | + const end = i; |
| 113 | + const avg = Math.round(height / i); |
| 114 | + |
| 115 | + for (; i < items.length; i += 1) this.heightMap[i] = avg; |
| 116 | + |
| 117 | + this.set({ |
| 118 | + bottom: (items.length - end) * avg |
| 119 | + }); |
| 120 | + } |
| 121 | + }, |
| 122 | + |
103 | 123 | refresh() {
|
104 | 124 | const { items, start, end, itemHeight } = this.get();
|
105 | 125 | const { offsetHeight, scrollTop } = this.refs.viewport;
|
|
0 commit comments