forked from angular-ui/ui-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.js
206 lines (176 loc) · 6.13 KB
/
adapter.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
class Adapter {
constructor($scope, $parse, $attr, viewport, buffer, doAdjust, reload) {
this.$parse = $parse;
this.$attr = $attr;
this.viewport = viewport;
this.buffer = buffer;
this.doAdjust = doAdjust;
this.reload = reload;
this.isLoading = false;
this.disabled = false;
const viewportScope = viewport.getScope();
this.startScope = viewportScope.$parent ? viewportScope : $scope;
this.publicContext = {};
this.assignAdapter($attr.adapter);
this.generatePublicContext();
}
assignAdapter(adapterAttr) {
if (!adapterAttr || !(adapterAttr = adapterAttr.replace(/^\s+|\s+$/gm, ''))) {
return;
}
let adapterOnScope;
try {
this.$parse(adapterAttr).assign(this.startScope, {});
adapterOnScope = this.$parse(adapterAttr)(this.startScope);
}
catch (error) {
error.message = `Angular ui-scroll Adapter assignment exception.\n` +
`Can't parse "${adapterAttr}" expression.\n` +
error.message;
throw error;
}
angular.extend(adapterOnScope, this.publicContext);
this.publicContext = adapterOnScope;
}
generatePublicContext() {
this.hasPublicAttr = false;
// these methods will be accessible out of ui-scroll via user defined adapter
const publicMethods = ['reload', 'applyUpdates', 'append', 'prepend', 'isBOF', 'isEOF', 'isEmpty'];
for (let i = publicMethods.length - 1; i >= 0; i--) {
this.publicContext[publicMethods[i]] = this[publicMethods[i]].bind(this);
}
// these read-only props will be accessible out of ui-scroll via user defined adapter
const publicProps = ['isLoading', 'topVisible', 'topVisibleElement', 'topVisibleScope', 'bottomVisible', 'bottomVisibleElement', 'bottomVisibleScope'];
for (let i = publicProps.length - 1; i >= 0; i--) {
let property, attr = this.$attr[publicProps[i]];
Object.defineProperty(this, publicProps[i], {
get: () => property,
set: (value) => {
property = value;
this.publicContext[publicProps[i]] = value;
if (attr) {
this.$parse(attr).assign(this.startScope, value);
}
}
});
if(attr) this.hasPublicAttr = true;
}
// non-read-only public property
Object.defineProperty(this.publicContext, 'disabled', {
get: () => this.disabled,
set: (value) => (!(this.disabled = value)) ? this.doAdjust() : null
});
}
loading(value) {
this.isLoading = value;
}
isBOF() {
return this.buffer.bof;
}
isEOF() {
return this.buffer.eof;
}
isEmpty() {
return !this.buffer.length;
}
append(newItems) {
this.buffer.append(newItems);
this.doAdjust();
this.viewport.clipTop();
this.viewport.clipBottom();
}
prepend(newItems) {
this.buffer.prepend(newItems);
this.doAdjust();
this.viewport.clipTop();
this.viewport.clipBottom();
}
applyUpdates(arg1, arg2) {
if (typeof arg1 === 'function') {
this.applyUpdatesFunc(arg1);
} else {
this.applyUpdatesIndex(arg1, arg2);
}
this.doAdjust();
}
applyUpdatesFunc(cb) {
this.buffer.slice(0).forEach((wrapper) => {
// we need to do it on the buffer clone, because buffer content
// may change as we iterate through
this.applyUpdate(wrapper, cb(wrapper.item, wrapper.scope, wrapper.element));
});
}
applyUpdatesIndex(index, newItems) {
if (index % 1 !== 0) {
throw new Error('applyUpdates - ' + index + ' is not a valid index (should be an integer)');
}
const _index = index - this.buffer.first;
// apply updates only within buffer
if (_index >= 0 && _index < this.buffer.length) {
this.applyUpdate(this.buffer[_index], newItems);
}
// out-of-buffer case: deletion may affect Paddings
else if(index >= this.buffer.getAbsMinIndex() && index <= this.buffer.getAbsMaxIndex()) {
if(angular.isArray(newItems) && !newItems.length) {
this.viewport.removeCacheItem(index, index === this.buffer.minIndex);
if(index === this.buffer.getAbsMinIndex()) {
this.buffer.incrementMinIndex();
}
else {
this.buffer.decrementMaxIndex();
}
}
}
}
applyUpdate(wrapper, newItems) {
if (!angular.isArray(newItems)) {
return;
}
let position = this.buffer.indexOf(wrapper);
if (!newItems.reverse().some(newItem => newItem === wrapper.item)) {
wrapper.op = 'remove';
if(position === 0 && !newItems.length) {
wrapper._op = 'isTop'; // to catch "first" edge case on remove
}
}
newItems.forEach((newItem) => {
if (newItem === wrapper.item) {
position--;
} else {
// 3 parameter (isTop) is to catch "first" edge case on insert
this.buffer.insert(position + 1, newItem, position === -1);
}
});
}
calculateProperties() {
let rowTop = null, topHeight = 0;
let topDone = false, bottomDone = false;
const length = this.buffer.length;
for (let i = 0; i < length; i++) {
const item = this.buffer[i];
const itemTop = item.element.offset().top;
if (rowTop !== itemTop) { // a new row condition
const itemHeight = item.element.outerHeight(true);
const top = this.viewport.topDataPos() + topHeight + itemHeight;
if (!topDone && top > this.viewport.topVisiblePos()) {
topDone = true;
this['topVisible'] = item.item;
this['topVisibleElement'] = item.element;
this['topVisibleScope'] = item.scope;
}
if (!bottomDone && (top >= this.viewport.bottomVisiblePos() || (i === length - 1 && this.isEOF()))) {
bottomDone = true;
this['bottomVisible'] = item.item;
this['bottomVisibleElement'] = item.element;
this['bottomVisibleScope'] = item.scope;
}
topHeight += itemHeight;
}
rowTop = itemTop;
if (topDone && bottomDone) {
break;
}
}
}
}
export default Adapter;