Skip to content

Commit 3c31814

Browse files
committed
smarter attribute injection logic
1 parent 8c714d5 commit 3c31814

File tree

4 files changed

+109
-68
lines changed

4 files changed

+109
-68
lines changed

dist/ui-scroll-grid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* angular-ui-scroll
33
* https://github.com/angular-ui/ui-scroll.git
4-
* Version: 1.4.1 -- 2016-05-26T23:58:09.875Z
4+
* Version: 1.4.1 -- 2016-05-31T21:50:01.591Z
55
* License: MIT
66
*/
77

dist/ui-scroll-jqlite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* angular-ui-scroll
33
* https://github.com/angular-ui/ui-scroll.git
4-
* Version: 1.4.1 -- 2016-05-26T17:11:13.078Z
4+
* Version: 1.4.1 -- 2016-05-31T21:50:01.591Z
55
* License: MIT
66
*/
77

dist/ui-scroll.js

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* angular-ui-scroll
33
* https://github.com/angular-ui/ui-scroll.git
4-
* Version: 1.4.1 -- 2016-05-26T17:11:13.078Z
4+
* Version: 1.4.1 -- 2016-05-31T21:50:01.591Z
55
* License: MIT
66
*/
77

@@ -423,12 +423,12 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
423423
var _this = this;
424424

425425
var viewportScope = viewport.scope() || $rootScope;
426-
var setTopVisible = $attr.topVisible ? $parse($attr.topVisible).assign : angular.noop;
427-
var setTopVisibleElement = $attr.topVisibleElement ? $parse($attr.topVisibleElement).assign : angular.noop;
428-
var setTopVisibleScope = $attr.topVisibleScope ? $parse($attr.topVisibleScope).assign : angular.noop;
429-
var setIsLoading = $attr.isLoading ? $parse($attr.isLoading).assign : angular.noop;
430426
var disabled = false;
431427

428+
injectValue($attr.adapter, this);
429+
430+
// Adapter API definition
431+
432432
Object.defineProperty(this, 'disabled', {
433433
get: function get() {
434434
return disabled;
@@ -440,28 +440,6 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
440440

441441
this.isLoading = false;
442442

443-
function applyUpdate(wrapper, newItems) {
444-
if (!angular.isArray(newItems)) {
445-
return;
446-
}
447-
448-
var keepIt = void 0;
449-
var pos = buffer.indexOf(wrapper) + 1;
450-
451-
newItems.reverse().forEach(function (newItem) {
452-
if (newItem === wrapper.item) {
453-
keepIt = true;
454-
pos--;
455-
} else {
456-
buffer.insert(pos, newItem);
457-
}
458-
});
459-
460-
if (!keepIt) {
461-
wrapper.op = 'remove';
462-
}
463-
}
464-
465443
this.applyUpdates = function (arg1, arg2) {
466444
if (angular.isFunction(arg1)) {
467445
// arg1 is the updater function, arg2 is ignored
@@ -498,7 +476,7 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
498476

499477
this.loading = function (value) {
500478
_this.isLoading = value;
501-
setIsLoading(viewportScope, value);
479+
injectValue($attr.isLoading, value);
502480
};
503481

504482
this.calculateProperties = function () {
@@ -523,14 +501,58 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
523501
_this.topVisible = item.item;
524502
_this.topVisibleElement = item.element;
525503
_this.topVisibleScope = item.scope;
526-
setTopVisible(viewportScope, item.item);
527-
setTopVisibleElement(viewportScope, item.element);
528-
setTopVisibleScope(viewportScope, item.scope);
504+
injectValue($attr.topVisible, item.item);
505+
injectValue($attr.topVisibleElement, item.element);
506+
injectValue($attr.topVisibleScope, item.scope);
529507
}
530508
break;
531509
}
532510
}
533511
};
512+
513+
// private function definitions
514+
515+
function injectValue(expression, value) {
516+
if (expression) {
517+
var scope = viewportScope;
518+
var s = viewportScope;
519+
var i = expression.indexOf('.');
520+
if (i > 0) {
521+
var ctrlName = expression.slice(0, i);
522+
while (s !== $rootScope) {
523+
if (s.hasOwnProperty(ctrlName) && angular.isFunction(s[ctrlName])) {
524+
scope = s;
525+
expression = expression.slice(i + 1);
526+
break;
527+
}
528+
s = s.$parent;
529+
}
530+
}
531+
$parse(expression).assign(scope, value);
532+
}
533+
}
534+
535+
function applyUpdate(wrapper, newItems) {
536+
if (!angular.isArray(newItems)) {
537+
return;
538+
}
539+
540+
var keepIt = void 0;
541+
var pos = buffer.indexOf(wrapper) + 1;
542+
543+
newItems.reverse().forEach(function (newItem) {
544+
if (newItem === wrapper.item) {
545+
keepIt = true;
546+
pos--;
547+
} else {
548+
buffer.insert(pos, newItem);
549+
}
550+
});
551+
552+
if (!keepIt) {
553+
wrapper.op = 'remove';
554+
}
555+
}
534556
}
535557

536558
function link($scope, element, $attr, controllers, linker) {
@@ -614,7 +636,6 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
614636
};
615637

616638
adapter.reload = reload;
617-
if ($attr.adapter) $parse($attr.adapter).assign($scope, adapter);
618639

619640
/**
620641
* Build padding elements
@@ -644,7 +665,7 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
644665
reload();
645666
});
646667

647-
/* Function definitions */
668+
/* Private function definitions */
648669

649670
function isInvalid(rid) {
650671
return rid && rid !== ridActual || $scope.$$destroyed;

src/ui-scroll.js

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,11 @@ angular.module('ui.scroll', [])
421421

422422
function Adapter($attr, viewport, buffer, adjustBuffer) {
423423
const viewportScope = viewport.scope() || $rootScope;
424-
const setTopVisible = $attr.topVisible ? $parse($attr.topVisible).assign : angular.noop;
425-
const setTopVisibleElement = $attr.topVisibleElement ? $parse($attr.topVisibleElement).assign : angular.noop;
426-
const setTopVisibleScope = $attr.topVisibleScope ? $parse($attr.topVisibleScope).assign : angular.noop;
427-
const setIsLoading = $attr.isLoading ? $parse($attr.isLoading).assign : angular.noop;
428424
let disabled = false;
429425

430-
if ($attr.adapter)
431-
$parse($attr.adapter).assign(viewportScope, this);
426+
injectValue($attr.adapter, this);
427+
428+
// Adapter API definition
432429

433430
Object.defineProperty(this, 'disabled', {
434431
get: () => disabled,
@@ -437,28 +434,6 @@ angular.module('ui.scroll', [])
437434

438435
this.isLoading = false;
439436

440-
function applyUpdate(wrapper, newItems) {
441-
if (!angular.isArray(newItems)) {
442-
return;
443-
}
444-
445-
let keepIt;
446-
let pos = (buffer.indexOf(wrapper)) + 1;
447-
448-
newItems.reverse().forEach((newItem) => {
449-
if (newItem === wrapper.item) {
450-
keepIt = true;
451-
pos--;
452-
} else {
453-
buffer.insert(pos, newItem);
454-
}
455-
});
456-
457-
if (!keepIt) {
458-
wrapper.op = 'remove';
459-
}
460-
}
461-
462437
this.applyUpdates = (arg1, arg2) => {
463438
if (angular.isFunction(arg1)) {
464439
// arg1 is the updater function, arg2 is ignored
@@ -494,7 +469,7 @@ angular.module('ui.scroll', [])
494469

495470
this.loading = (value) => {
496471
this.isLoading = value;
497-
setIsLoading(viewportScope, value);
472+
injectValue($attr.isLoading, value);
498473
};
499474

500475
this.calculateProperties = () => {
@@ -515,14 +490,59 @@ angular.module('ui.scroll', [])
515490
this.topVisible = item.item;
516491
this.topVisibleElement = item.element;
517492
this.topVisibleScope = item.scope;
518-
setTopVisible(viewportScope, item.item);
519-
setTopVisibleElement(viewportScope, item.element);
520-
setTopVisibleScope(viewportScope, item.scope);
493+
injectValue($attr.topVisible, item.item);
494+
injectValue($attr.topVisibleElement, item.element);
495+
injectValue($attr.topVisibleScope, item.scope);
521496
}
522497
break;
523498
}
524499
}
525500
};
501+
502+
// private function definitions
503+
504+
function injectValue(expression, value) {
505+
if (expression) {
506+
let scope = viewportScope;
507+
let s = viewportScope;
508+
let i = expression.indexOf('.');
509+
if (i>0) {
510+
let ctrlName = expression.slice(0, i);
511+
while (s !== $rootScope) {
512+
if (s.hasOwnProperty(ctrlName) && angular.isFunction(s[ctrlName])) {
513+
scope = s;
514+
expression = expression.slice(i+1);
515+
break;
516+
}
517+
s = s.$parent;
518+
}
519+
}
520+
$parse(expression).assign(scope, value);
521+
}
522+
}
523+
524+
function applyUpdate(wrapper, newItems) {
525+
if (!angular.isArray(newItems)) {
526+
return;
527+
}
528+
529+
let keepIt;
530+
let pos = (buffer.indexOf(wrapper)) + 1;
531+
532+
newItems.reverse().forEach((newItem) => {
533+
if (newItem === wrapper.item) {
534+
keepIt = true;
535+
pos--;
536+
} else {
537+
buffer.insert(pos, newItem);
538+
}
539+
});
540+
541+
if (!keepIt) {
542+
wrapper.op = 'remove';
543+
}
544+
}
545+
526546
}
527547

528548
function link($scope, element, $attr, controllers, linker) {
@@ -631,7 +651,7 @@ angular.module('ui.scroll', [])
631651
reload();
632652
});
633653

634-
/* Function definitions */
654+
/* Private function definitions */
635655

636656
function isInvalid(rid) {
637657
return (rid && rid !== ridActual) || $scope.$$destroyed;

0 commit comments

Comments
 (0)