-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
ember-collection.js
248 lines (220 loc) · 7.5 KB
/
ember-collection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { A } from '@ember/array';
import Component from '@ember/component';
import { action, set, get } from '@ember/object';
import layout from './ember-collection/template';
import identity from '../utils/identity';
import needsRevalidate from '../utils/needs-revalidate';
class Cell {
constructor(key, item, index, style) {
this.key = key;
this.hidden = false;
this.item = item;
this.index = index;
this.style = style;
}
}
function noop() {}
export default Component.extend({
layout: layout,
init() {
// State pulled from attrs is prefixed with an underscore
// so that there's no chance of shadowing the attrs proxy.
this._buffer = undefined;
this._cellLayout = undefined;
this._rawItems = undefined;
this._items = undefined;
this._scrollLeft = undefined;
this._scrollTop = undefined;
this._clientWidth = undefined;
this._clientHeight = undefined;
this._contentSize = undefined;
// this.firstCell = undefined;
// this.lastCell = undefined;
// this.cellCount = undefined;
this.contentElement = undefined;
this._cells = A();
this._cellMap = Object.create(null);
// TODO: Super calls should always be at the top of the constructor.
// I had to move the super call after the properties were defined to
// work around what I believe is a bug in the attrs proxy. The problem
// seems to arise when you:
//
// 1. Call this._super() immediately.
// 2. Set a property on `this` that is both not in the
// initial attrs hash and not on the prototype.
this._super();
// initialize from passed in attrs
let buffer = this.getAttr('buffer'); // getIntAttr('buffer', 5)
this._buffer = (typeof buffer === 'number') ? buffer : 5;
this._scrollLeft = this.getAttr('scroll-left') | 0;
this._scrollTop = this.getAttr('scroll-top') | 0;
this._clientWidth = this.getAttr('estimated-width') | 0;
this._clientHeight = this.getAttr('estimated-height') | 0;
this._scrollChange = this.getAttr('scroll-change');
},
_needsRevalidate(){
if (this.isDestroyed || this.isDestroying) {return;}
if (this._isGlimmer2()) {
this.rerender();
} else {
needsRevalidate(this);
}
},
didReceiveAttrs() {
// Work around emberjs/ember.js#11992. Affects <=1.13.8 and <=2.0.0.
// This will likely be patched in 1.13.9 and 2.0.1.
this._super();
this.updateItems();
this.updateScrollPosition();
},
willDestroyElement() {
if (this._items && this._items.removeArrayObserver) {
this._items.removeArrayObserver(this, {
willChange: noop,
didChange: '_needsRevalidate'
});
}
},
updateItems(){
this._cellLayout = this.getAttr('cell-layout');
var rawItems = this.getAttr('items');
if (this._rawItems !== rawItems) {
if (this._items && this._items.removeArrayObserver) {
this._items.removeArrayObserver(this, {
willChange: noop,
didChange: '_needsRevalidate'
});
}
this._rawItems = rawItems;
var items = A(rawItems);
this.set('_items', items);
if (items && items.addArrayObserver) {
items.addArrayObserver(this, {
willChange: noop,
didChange: '_needsRevalidate'
});
}
}
},
updateScrollPosition(){
if (!this._scrollChange) { return; } // don't process bound scroll coords unless our action is being handled
let scrollLeftAttr = this.getAttr('scroll-left');
if (scrollLeftAttr !== undefined) {
scrollLeftAttr = parseInt(scrollLeftAttr, 10);
if (this._scrollLeft !== scrollLeftAttr) {
this.set('_scrollLeft', scrollLeftAttr);
}
}
let scrollTopAttr = this.getAttr('scroll-top');
if (scrollTopAttr !== undefined) {
scrollTopAttr = parseInt(scrollTopAttr, 10);
if (this._scrollTop !== scrollTopAttr) {
// console.log('updateScrollPosition', this._scrollTop, scrollTopAttr);
this.set('_scrollTop', scrollTopAttr);
}
}
},
updateContentSize() {
var cellLayout = this._cellLayout;
var contentSize = cellLayout.contentSize(this._clientWidth, this._clientHeight);
if (this._contentSize === undefined ||
contentSize.width !== this._contentSize.width ||
contentSize.height !== this._contentSize.height) {
this.set('_contentSize', contentSize);
}
},
willRender: function() {
this.updateCells();
this.updateContentSize();
},
updateCells() {
if (!this._items) { return; }
const numItems = get(this._items, 'length');
if (this._cellLayout.length !== numItems) {
this._cellLayout.length = numItems;
}
var priorMap = this._cellMap;
var cellMap = Object.create(null);
var index = this._cellLayout.indexAt(this._scrollLeft, this._scrollTop, this._clientWidth, this._clientHeight);
var count = this._cellLayout.count(this._scrollLeft, this._scrollTop, this._clientWidth, this._clientHeight);
var items = this._items;
var bufferBefore = Math.min(index, this._buffer);
index -= bufferBefore;
count += bufferBefore;
count = Math.min(count + this._buffer, get(items, 'length') - index);
var i, style, itemIndex, itemKey, cell;
var newItems = [];
for (i=0; i<count; i++) {
itemIndex = index+i;
itemKey = identity(items.objectAt(itemIndex));
if (priorMap) {
cell = priorMap[itemKey];
}
if (cell) {
style = this._cellLayout.formatItemStyle(itemIndex, this._clientWidth, this._clientHeight);
set(cell, 'style', style);
set(cell, 'hidden', false);
set(cell, 'key', itemKey);
set(cell, 'index', itemIndex);
cellMap[itemKey] = cell;
} else {
newItems.push(itemIndex);
}
}
for (i=0; i<this._cells.length; i++) {
cell = this._cells[i];
if (!cellMap[cell.key]) {
if (newItems.length) {
itemIndex = newItems.pop();
let item = items.objectAt(itemIndex);
itemKey = identity(item);
style = this._cellLayout.formatItemStyle(itemIndex, this._clientWidth, this._clientHeight);
set(cell, 'style', style);
set(cell, 'key', itemKey);
set(cell, 'index', itemIndex);
set(cell, 'item', item);
set(cell, 'hidden', false);
cellMap[itemKey] = cell;
} else {
set(cell, 'hidden', true);
set(cell, 'style', 'height: 0; display: none;');
}
}
}
for (i=0; i<newItems.length; i++) {
itemIndex = newItems[i];
let item = items.objectAt(itemIndex);
itemKey = identity(item);
style = this._cellLayout.formatItemStyle(itemIndex, this._clientWidth, this._clientHeight);
cell = new Cell(itemKey, item, itemIndex, style);
cellMap[itemKey] = cell;
this._cells.pushObject(cell);
}
this._cellMap = cellMap;
},
_isGlimmer2() {
return !this._renderNode;
},
@action
scrollChange(scrollLeft, scrollTop) {
if (this._scrollChange) {
this._scrollChange(scrollLeft, scrollTop);
} else {
if (scrollLeft !== this._scrollLeft ||
scrollTop !== this._scrollTop) {
set(this, '_scrollLeft', scrollLeft);
set(this, '_scrollTop', scrollTop);
this._needsRevalidate();
}
}
},
@action
clientSizeChange(clientWidth, clientHeight) {
if (this._clientWidth !== clientWidth ||
this._clientHeight !== clientHeight) {
set(this, '_clientWidth', clientWidth);
set(this, '_clientHeight', clientHeight);
this._needsRevalidate();
}
}
});