diff options
| -rw-r--r-- | src/widgets.js | 15 | ||||
| -rw-r--r-- | test/widgetsSpec.js | 69 |
2 files changed, 80 insertions, 4 deletions
diff --git a/src/widgets.js b/src/widgets.js index 155b8c08..63ddaf36 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -43,6 +43,13 @@ * instance of angular.module.ng.$rootScope.Scope to set the HTML fragment to. * @param {string=} onload Expression to evaluate when a new partial is loaded. * + * @param {string=} autoscroll Whether `ng:include` should call {@link angular.module.ng.$autoScroll + * $autoScroll} to scroll the viewport after the content is loaded. + * + * - If the attribute is not set, disable scrolling. + * - If the attribute is set without value, enable scrolling. + * - Otherwise enable scrolling only if the expression evaluates to truthy value. + * * @example <doc:example> <doc:source jsfiddle="false"> @@ -84,7 +91,9 @@ angularWidget('ng:include', function(element){ var compiler = this, srcExp = element.attr("src"), scopeExp = element.attr("scope") || '', - onloadExp = element[0].getAttribute('onload') || ''; //workaround for jquery bug #7537 + onloadExp = element[0].getAttribute('onload') || '', //workaround for jquery bug #7537 + autoScrollExp = element.attr('autoscroll'); + if (element[0]['ng:compiled']) { this.descend(true); this.directives(true); @@ -123,7 +132,9 @@ angularWidget('ng:include', function(element){ if (childScope) childScope.$destroy(); childScope = useScope ? useScope : scope.$new(); compiler.compile(element)(childScope); - $autoScroll(); + if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) { + $autoScroll(); + } scope.$eval(onloadExp); } }).error(clearContent); diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index 633ec0e0..e8ff4b27 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -55,7 +55,7 @@ describe('widget', function() { }); - describe('ng:include', inject(function($rootScope, $compile) { + describe('ng:include', function() { function putIntoCache(url, content) { return function($templateCache) { @@ -227,9 +227,74 @@ describe('widget', function() { expect(log.join('; ')).toEqual('url2; url2'); // it's here twice because we go through at // least two digest cycles })); - })); + describe('autoscoll', function() { + var autoScrollSpy; + + function spyOnAutoScroll() { + return function($provide) { + autoScrollSpy = jasmine.createSpy('$autoScroll'); + $provide.value('$autoScroll', autoScrollSpy); + }; + } + + function compileAndLink(tpl) { + return function($compile, $rootScope) { + $compile(tpl)($rootScope); + }; + } + + function changeTplAndValueTo(template, value) { + return function($rootScope, $browser) { + $rootScope.$apply(function() { + $rootScope.tpl = template; + $rootScope.value = value; + }); + $browser.defer.flush(); + }; + } + + beforeEach(inject( + spyOnAutoScroll(), + putIntoCache('template.html', 'CONTENT'), + putIntoCache('another.html', 'CONTENT'))); + + + it('should call $autoScroll if autoscroll attribute is present', inject( + compileAndLink('<ng:include src="tpl" autoscroll></ng:include>'), + changeTplAndValueTo('template.html'), function() { + expect(autoScrollSpy).toHaveBeenCalledOnce(); + })); + + + it('should call $autoScroll if autoscroll evaluates to true', inject( + compileAndLink('<ng:include src="tpl" autoscroll="value"></ng:include>'), + changeTplAndValueTo('template.html', true), + changeTplAndValueTo('another.html', 'some-string'), + changeTplAndValueTo('template.html', 100), function() { + expect(autoScrollSpy).toHaveBeenCalled(); + expect(autoScrollSpy.callCount).toBe(3); + })); + + + it('should not call $autoScroll if autoscroll attribute is not present', inject( + compileAndLink('<ng:include src="tpl"></ng:include>'), + changeTplAndValueTo('template.html'), function() { + expect(autoScrollSpy).not.toHaveBeenCalled(); + })); + + + it('should not call $autoScroll if autoscroll evaluates to false', inject( + compileAndLink('<ng:include src="tpl" autoscroll="value"></ng:include>'), + changeTplAndValueTo('template.html', false), + changeTplAndValueTo('template.html', undefined), + changeTplAndValueTo('template.html', null), function() { + expect(autoScrollSpy).not.toHaveBeenCalled(); + })); + }); + }); + describe('a', function() { it('should prevent default action to be executed when href is empty', inject(function($rootScope, $compile) { |
