Skip to content

Commit 35229c5

Browse files
committed
Refactor Adapter
1 parent ad039eb commit 35229c5

File tree

1 file changed

+51
-58
lines changed

1 file changed

+51
-58
lines changed

src/ui-scroll.js

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -366,106 +366,99 @@ angular.module('ui.scroll', [])
366366
}
367367

368368
function Adapter($attr, viewport, buffer, adjustBuffer) {
369-
var applyUpdate, setIsLoading, setTopVisible, setTopVisibleElement, setTopVisibleScope, viewportScope;
369+
const viewportScope = viewport.scope() || $rootScope;
370+
const setTopVisible = $attr.topVisible ? $parse($attr.topVisible).assign : angular.noop;
371+
const setTopVisibleElement = $attr.topVisibleElement ? $parse($attr.topVisibleElement).assign : angular.noop;
372+
const setTopVisibleScope = $attr.topVisibleScope ? $parse($attr.topVisibleScope).assign : angular.noop;
373+
const setIsLoading = $attr.isLoading ? $parse($attr.isLoading).assign : angular.noop;
374+
370375
this.isLoading = false;
371376

372-
viewportScope = viewport.scope() || $rootScope;
373-
374-
applyUpdate = function (wrapper, newItems) {
375-
var i, j, keepIt, len, newItem, pos, ref;
376-
if (angular.isArray(newItems)) {
377-
pos = (buffer.indexOf(wrapper)) + 1;
378-
ref = newItems.reverse();
379-
for (i = j = 0, len = ref.length; j < len; i = ++j) {
380-
newItem = ref[i];
381-
if (newItem === wrapper.item) {
382-
keepIt = true;
383-
pos--;
384-
} else {
385-
buffer.insert(pos, newItem);
386-
}
387-
}
388-
if (!keepIt) {
389-
return wrapper.op = 'remove';
377+
function applyUpdate(wrapper, newItems) {
378+
if (!angular.isArray(newItems)) {
379+
return;
380+
}
381+
382+
let keepIt;
383+
let pos = (buffer.indexOf(wrapper)) + 1;
384+
385+
newItems.reverse().forEach((newItem) => {
386+
if (newItem === wrapper.item) {
387+
keepIt = true;
388+
pos--;
389+
} else {
390+
buffer.insert(pos, newItem);
390391
}
392+
});
393+
394+
if (!keepIt) {
395+
wrapper.op = 'remove';
391396
}
392-
};
397+
}
393398

394-
this.applyUpdates = function (arg1, arg2) {
395-
var bufferClone, i, j, len, ref, wrapper;
399+
this.applyUpdates = (arg1, arg2) => {
396400
if (angular.isFunction(arg1)) {
397401
// arg1 is the updater function, arg2 is ignored
398-
bufferClone = buffer.slice(0);
399-
for (i = j = 0, len = bufferClone.length; j < len; i = ++j) {
402+
buffer.slice(0).forEach((wrapper) => {
400403
// we need to do it on the buffer clone, because buffer content
401404
// may change as we iterate through
402-
wrapper = bufferClone[i];
403405
applyUpdate(wrapper, arg1(wrapper.item, wrapper.scope, wrapper.element));
404-
}
406+
});
405407
} else {
406408
// arg1 is item index, arg2 is the newItems array
407-
if (arg1 % 1 === 0) {// checking if it is an integer
408-
if ((0 <= (ref = arg1 - buffer.first) && ref < buffer.length)) {
409-
applyUpdate(buffer[arg1 - buffer.first], arg2);
410-
}
411-
} else {
409+
if (arg1 % 1 !== 0) {// checking if it is an integer
412410
throw new Error('applyUpdates - ' + arg1 + ' is not a valid index');
413411
}
412+
413+
const index = arg1 - buffer.first;
414+
if ((index >= 0 && index < buffer.length)) {
415+
applyUpdate(buffer[index], arg2);
416+
}
414417
}
415-
return adjustBuffer();
418+
419+
adjustBuffer();
416420
};
417421

418-
this.append = function (newItems) {
422+
this.append = (newItems) => {
419423
buffer.append(newItems);
420-
return adjustBuffer();
424+
adjustBuffer();
421425
};
422426

423-
this.prepend = function (newItems) {
427+
this.prepend = (newItems) => {
424428
buffer.prepend(newItems);
425-
return adjustBuffer();
426-
};
427-
428-
setTopVisible = $attr.topVisible ? $parse($attr.topVisible).assign : function () {
429-
};
430-
setTopVisibleElement = $attr.topVisibleElement ? $parse($attr.topVisibleElement).assign : function () {
431-
};
432-
setTopVisibleScope = $attr.topVisibleScope ? $parse($attr.topVisibleScope).assign : function () {
433-
};
434-
setIsLoading = $attr.isLoading ? $parse($attr.isLoading).assign : function () {
429+
adjustBuffer();
435430
};
436431

437432
this.loading = function (value) {
438433
this.isLoading = value;
439-
return setIsLoading(viewportScope, value);
434+
setIsLoading(viewportScope, value);
440435
};
441436

442437
this.calculateProperties = function () {
443-
var item, itemHeight, itemTop, j, len, newRow, results, rowTop, topHeight;
438+
let itemHeight, itemTop, isNewRow, rowTop, topHeight;
444439
topHeight = 0;
445-
results = [];
446-
for (j = 0, len = buffer.length; j < len; j++) {
447-
item = buffer[j];
440+
buffer.some((item) => {
448441
itemTop = item.element.offset().top;
449-
newRow = rowTop !== itemTop;
442+
isNewRow = rowTop !== itemTop;
450443
rowTop = itemTop;
451-
if (newRow) {
444+
if (isNewRow) {
452445
itemHeight = item.element.outerHeight(true);
453446
}
454-
if (newRow && (viewport.topDataPos() + topHeight + itemHeight <= viewport.topVisiblePos())) {
455-
results.push(topHeight += itemHeight);
447+
if (isNewRow && (viewport.topDataPos() + topHeight + itemHeight <= viewport.topVisiblePos())) {
448+
topHeight += itemHeight;
456449
} else {
457-
if (newRow) {
450+
if (isNewRow) {
458451
this.topVisible = item.item;
459452
this.topVisibleElement = item.element;
460453
this.topVisibleScope = item.scope;
461454
setTopVisible(viewportScope, item.item);
462455
setTopVisibleElement(viewportScope, item.element);
463456
setTopVisibleScope(viewportScope, item.scope);
464457
}
465-
break;
458+
459+
return true;// Break the loop
466460
}
467-
}
468-
return results;
461+
});
469462
};
470463
}
471464

0 commit comments

Comments
 (0)