Skip to content

Commit fc4e686

Browse files
committed
reduce angular specific refactoring
1 parent 1a0d168 commit fc4e686

File tree

6 files changed

+58
-55
lines changed

6 files changed

+58
-55
lines changed

src/modules/adapter.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
class Adapter {
22

3-
constructor(viewport, buffer, adjustBuffer, reload, $attr, $parse, $scope) {
3+
constructor($scope, $parse, $attr, viewport, buffer, doAdjust, reload) {
4+
this.$parse = $parse;
5+
this.$attr = $attr;
46
this.viewport = viewport;
57
this.buffer = buffer;
6-
this.adjustBuffer = adjustBuffer;
8+
9+
this.doAdjust = doAdjust;
710
this.reload = reload;
811

912
this.isLoading = false;
@@ -13,19 +16,19 @@ class Adapter {
1316
this.startScope = viewportScope.$parent ? viewportScope : $scope;
1417

1518
this.publicContext = {};
16-
this.assignAdapter($attr.adapter, $parse);
17-
this.generatePublicContext($attr, $parse);
19+
this.assignAdapter($attr.adapter);
20+
this.generatePublicContext();
1821
}
1922

20-
assignAdapter(adapterAttr, $parse) {
23+
assignAdapter(adapterAttr) {
2124
if (!adapterAttr || !(adapterAttr = adapterAttr.replace(/^\s+|\s+$/gm, ''))) {
2225
return;
2326
}
2427
let adapterOnScope;
2528

2629
try {
27-
$parse(adapterAttr).assign(this.startScope, {});
28-
adapterOnScope = $parse(adapterAttr)(this.startScope);
30+
this.$parse(adapterAttr).assign(this.startScope, {});
31+
adapterOnScope = this.$parse(adapterAttr)(this.startScope);
2932
}
3033
catch (error) {
3134
error.message = `Angular ui-scroll Adapter assignment exception.\n` +
@@ -34,11 +37,11 @@ class Adapter {
3437
throw error;
3538
}
3639

37-
angular.extend(adapterOnScope, this.publicContext);
40+
Object.assign(adapterOnScope, this.publicContext);
3841
this.publicContext = adapterOnScope;
3942
}
4043

41-
generatePublicContext($attr, $parse) {
44+
generatePublicContext() {
4245
// these methods will be accessible out of ui-scroll via user defined adapter
4346
const publicMethods = ['reload', 'applyUpdates', 'append', 'prepend', 'isBOF', 'isEOF', 'isEmpty'];
4447
for (let i = publicMethods.length - 1; i >= 0; i--) {
@@ -48,14 +51,14 @@ class Adapter {
4851
// these read-only props will be accessible out of ui-scroll via user defined adapter
4952
const publicProps = ['isLoading', 'topVisible', 'topVisibleElement', 'topVisibleScope', 'bottomVisible', 'bottomVisibleElement', 'bottomVisibleScope'];
5053
for (let i = publicProps.length - 1; i >= 0; i--) {
51-
let property, attr = $attr[publicProps[i]];
54+
let property, attr = this.$attr[publicProps[i]];
5255
Object.defineProperty(this, publicProps[i], {
5356
get: () => property,
5457
set: (value) => {
5558
property = value;
5659
this.publicContext[publicProps[i]] = value;
5760
if (attr) {
58-
$parse(attr).assign(this.startScope, value);
61+
this.$parse(attr).assign(this.startScope, value);
5962
}
6063
}
6164
});
@@ -64,12 +67,12 @@ class Adapter {
6467
// non-read-only public property
6568
Object.defineProperty(this.publicContext, 'disabled', {
6669
get: () => this.disabled,
67-
set: (value) => (!(this.disabled = value)) ? this.adjustBuffer() : null
70+
set: (value) => (!(this.disabled = value)) ? this.doAdjust() : null
6871
});
6972
}
7073

7174
loading(value) {
72-
this['isLoading'] = value;
75+
this.isLoading = value;
7376
}
7477

7578
isBOF() {
@@ -86,25 +89,25 @@ class Adapter {
8689

8790
append(newItems) {
8891
this.buffer.append(newItems);
89-
this.adjustBuffer();
92+
this.doAdjust();
9093
this.viewport.clipTop();
9194
this.viewport.clipBottom();
9295
}
9396

9497
prepend(newItems) {
9598
this.buffer.prepend(newItems);
96-
this.adjustBuffer();
99+
this.doAdjust();
97100
this.viewport.clipTop();
98101
this.viewport.clipBottom();
99102
}
100103

101104
applyUpdates(arg1, arg2) {
102-
if (angular.isFunction(arg1)) {
105+
if (typeof arg1 === 'function') {
103106
this.applyUpdatesFunc(arg1);
104107
} else {
105108
this.applyUpdatesIndex(arg1, arg2);
106109
}
107-
this.adjustBuffer();
110+
this.doAdjust();
108111
}
109112

110113
applyUpdatesFunc(cb) {
@@ -127,7 +130,7 @@ class Adapter {
127130
}
128131
// out-of-buffer case: deletion may affect Paddings
129132
else if(index >= this.buffer.getAbsMinIndex() && index <= this.buffer.getAbsMaxIndex()) {
130-
if(angular.isArray(newItems) && !newItems.length) {
133+
if(Array.isArray(newItems) && !newItems.length) {
131134
this.viewport.removeCacheItem(index, index === this.buffer.minIndex);
132135
if(index === this.buffer.getAbsMinIndex()) {
133136
this.buffer.incrementMinIndex();
@@ -140,7 +143,7 @@ class Adapter {
140143
}
141144

142145
applyUpdate(wrapper, newItems) {
143-
if (!angular.isArray(newItems)) {
146+
if (!Array.isArray(newItems)) {
144147
return;
145148
}
146149
let position = this.buffer.indexOf(wrapper);

src/modules/buffer.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default function ScrollBuffer(elementRoutines, bufferSize) {
22
const buffer = Object.create(Array.prototype);
33

4-
angular.extend(buffer, {
4+
Object.assign(buffer, {
55
size: bufferSize,
66

77
reset(startIndex) {
@@ -67,12 +67,11 @@ export default function ScrollBuffer(elementRoutines, bufferSize) {
6767

6868
// removes elements from buffer
6969
remove(arg1, arg2) {
70-
if (angular.isNumber(arg1)) {
70+
if (Number.isInteger(arg1)) {
7171
// removes items from arg1 (including) through arg2 (excluding)
7272
for (let i = arg1; i < arg2; i++) {
7373
elementRoutines.removeElement(buffer[i]);
7474
}
75-
7675
return buffer.splice(arg1, arg2 - arg1);
7776
}
7877
// removes single item(wrapper) from the buffer

src/modules/jqLiteExtras.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ export default class JQLiteExtras {
4242
]
4343
}[direction];
4444

45+
const isValueDefined = typeof value !== 'undefined';
4546
if (isWindow(elem)) {
46-
if (angular.isDefined(value)) {
47+
if (isValueDefined) {
4748
return elem.scrollTo(self[preserve].call(self), value);
4849
}
4950
return (prop in elem) ? elem[prop] : elem.document.documentElement[method];
5051
} else {
51-
if (angular.isDefined(value)) {
52+
if (isValueDefined) {
5253
elem[method] = value;
5354
}
5455
return elem[method];
@@ -197,8 +198,8 @@ export default class JQLiteExtras {
197198
height (value){
198199
var self;
199200
self = this;
200-
if (angular.isDefined(value)) {
201-
if (angular.isNumber(value)) {
201+
if (typeof value !== 'undefined') {
202+
if (Number.isInteger(value)) {
202203
value = value + 'px';
203204
}
204205
return css.call(self, 'height', value);

src/modules/viewport.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function Viewport(elementRoutines, buffer, element, viewportContr
1717
return viewport.outerHeight() * padding; // some extra space to initiate preload
1818
}
1919

20-
angular.extend(viewport, {
20+
Object.assign(viewport, {
2121
getScope() {
2222
return scope;
2323
},

src/ui-scroll-grid.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ angular.module('ui.scroll.grid', [])
4444
// controller api methods
4545

4646
this.applyLayout = function(layout) {
47-
this.css = angular.extend({}, layout.css);
47+
this.css = Object.assign({}, layout.css);
4848
this.mapTo = layout.mapTo;
4949
applyCss(this.header, this.css);
5050
};
@@ -158,7 +158,7 @@ angular.module('ui.scroll.grid', [])
158158
let result = [];
159159
columns.forEach((column, index) => result.push({
160160
index: index,
161-
css: angular.extend({}, column.css),
161+
css: Object.assign({}, column.css),
162162
mapTo: column.mapTo
163163
}));
164164
return result;

src/ui-scroll.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ angular.module('ui.scroll', [])
2222
this.viewport = element;
2323
this.scope = scope;
2424

25-
angular.forEach(element.children(), (child => {
25+
angular.forEach(element.children(), child => {
2626
if (child.tagName.toLowerCase() === 'tbody') {
2727
this.viewport = angular.element(child);
2828
}
29-
}));
29+
});
3030

3131
return this;
3232
}
@@ -84,13 +84,15 @@ angular.module('ui.scroll', [])
8484
const elementRoutines = new ElementRoutines($injector, $q);
8585
const buffer = new ScrollBuffer(elementRoutines, bufferSize);
8686
const viewport = new Viewport(elementRoutines, buffer, element, viewportController, $rootScope, padding);
87-
const adapter = new Adapter(viewport, buffer, adjustBuffer, reload, $attr, $parse, $scope);
87+
const adapter = new Adapter($scope, $parse, $attr, viewport, buffer, doAdjust, reload);
8888

8989
if (viewportController) {
9090
viewportController.adapter = adapter;
9191
}
9292

93-
const isDatasourceValid = () => angular.isObject(datasource) && angular.isFunction(datasource.get);
93+
const isDatasourceValid = () =>
94+
Object.prototype.toString.call(datasource) === '[object Object]' && typeof datasource.get === 'function';
95+
9496
datasource = $parse(datasourceName)($scope); // try to get datasource on scope
9597
if (!isDatasourceValid()) {
9698
datasource = $injector.get(datasourceName); // try to inject datasource as service
@@ -101,12 +103,10 @@ angular.module('ui.scroll', [])
101103

102104
let onRenderHandlers = [];
103105
function onRenderHandlersRunner() {
104-
if(onRenderHandlers.length) {
105-
angular.forEach(onRenderHandlers, (handler) => handler());
106-
onRenderHandlers = [];
107-
}
106+
onRenderHandlers.forEach(handler => handler());
107+
onRenderHandlers = [];
108108
}
109-
function preDefineIndexProperty(datasource, propName) {
109+
function persistDatasourceIndex(datasource, propName, doNotDelete) {
110110
let getter;
111111
// need to postpone min/maxIndexUser processing if the view is empty
112112
if(datasource.hasOwnProperty(propName) && !buffer.length) {
@@ -116,13 +116,13 @@ angular.module('ui.scroll', [])
116116
}
117117
}
118118

119-
function defineIndexProperty(datasource, propName, propUserName) {
119+
function defineDatasourceIndex(datasource, propName, propUserName) {
120120
const descriptor = Object.getOwnPropertyDescriptor(datasource, propName);
121121
if (descriptor && (descriptor.set || descriptor.get)) {
122122
return;
123123
}
124124
let getter;
125-
preDefineIndexProperty(datasource, propName);
125+
persistDatasourceIndex(datasource, propName);
126126
Object.defineProperty(datasource, propName, {
127127
set: (value) => {
128128
getter = value;
@@ -137,8 +137,8 @@ angular.module('ui.scroll', [])
137137
});
138138
}
139139

140-
defineIndexProperty(datasource, 'minIndex', 'minIndexUser');
141-
defineIndexProperty(datasource, 'maxIndex', 'maxIndexUser');
140+
defineDatasourceIndex(datasource, 'minIndex', 'minIndexUser');
141+
defineDatasourceIndex(datasource, 'maxIndex', 'maxIndexUser');
142142

143143
const fetchNext = (datasource.get.length !== 2) ?
144144
(success) => datasource.get(buffer.next, bufferSize, success) :
@@ -160,7 +160,7 @@ angular.module('ui.scroll', [])
160160
}, success);
161161
};
162162

163-
const run = () => {
163+
const initialize = () => {
164164
let tryCount = 0;
165165
if(!viewport.applyContainerStyle()) {
166166
const timer = $interval(() => {
@@ -203,7 +203,7 @@ angular.module('ui.scroll', [])
203203

204204
viewport.bind('mousewheel', wheelHandler);
205205

206-
run();
206+
initialize();
207207

208208
/* Private function definitions */
209209

@@ -228,7 +228,7 @@ angular.module('ui.scroll', [])
228228
startIndex = arguments[0];
229229
}
230230
buffer.reset(startIndex);
231-
adjustBuffer();
231+
doAdjust();
232232
}
233233

234234
function isElementVisible(wrapper) {
@@ -238,13 +238,13 @@ angular.module('ui.scroll', [])
238238
function visibilityWatcher(wrapper) {
239239
if (isElementVisible(wrapper)) {
240240
buffer.forEach((item) => {
241-
if (angular.isFunction(item.unregisterVisibilityWatcher)) {
241+
if (typeof item.unregisterVisibilityWatcher === 'function') {
242242
item.unregisterVisibilityWatcher();
243243
delete item.unregisterVisibilityWatcher;
244244
}
245245
});
246246
if (!pending.length) {
247-
$timeout(() => adjustBuffer());
247+
$timeout(() => doAdjust());
248248
}
249249
}
250250
}
@@ -327,11 +327,11 @@ angular.module('ui.scroll', [])
327327
}
328328

329329
function updatePaddings(rid, updates) {
330-
// schedule another adjustBuffer after animation completion
330+
// schedule another doAdjust after animation completion
331331
if (updates.animated.length) {
332332
$q.all(updates.animated).then(() => {
333333
viewport.adjustPaddings();
334-
adjustBuffer(rid);
334+
doAdjust(rid);
335335
});
336336
} else {
337337
viewport.adjustPaddings();
@@ -371,7 +371,7 @@ angular.module('ui.scroll', [])
371371
return updates;
372372
}
373373

374-
function adjustBuffer(rid) {
374+
function doAdjust(rid) {
375375
if (!rid) { // dismiss pending requests
376376
pending = [];
377377
rid = ++ridActual;
@@ -391,7 +391,7 @@ angular.module('ui.scroll', [])
391391
}
392392
}
393393

394-
function adjustBufferAfterFetch(rid) {
394+
function doAdjustAfterFetch(rid) {
395395
const updates = processUpdates();
396396

397397
viewport.onAfterPrepend(updates);
@@ -417,7 +417,7 @@ angular.module('ui.scroll', [])
417417
function fetch(rid) {
418418
if (pending[0]) {// scrolling down
419419
if (buffer.length && !viewport.shouldLoadBottom()) {
420-
adjustBufferAfterFetch(rid);
420+
doAdjustAfterFetch(rid);
421421
} else {
422422
fetchNext((result) => {
423423
if (isInvalid(rid)) {
@@ -433,12 +433,12 @@ angular.module('ui.scroll', [])
433433
buffer.append(result);
434434
}
435435

436-
adjustBufferAfterFetch(rid);
436+
doAdjustAfterFetch(rid);
437437
});
438438
}
439439
} else { // scrolling up
440440
if (buffer.length && !viewport.shouldLoadTop()) {
441-
adjustBufferAfterFetch(rid);
441+
doAdjustAfterFetch(rid);
442442
} else {
443443
fetchPrevious((result) => {
444444
if (isInvalid(rid)) {
@@ -457,7 +457,7 @@ angular.module('ui.scroll', [])
457457
buffer.prepend(result);
458458
}
459459

460-
adjustBufferAfterFetch(rid);
460+
doAdjustAfterFetch(rid);
461461
});
462462
}
463463
}

0 commit comments

Comments
 (0)