Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 5c19766

Browse files
committed
feat(ng:include): enable/disable scrolling through autoscroll attribute
1 parent f2119c7 commit 5c19766

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

src/widgets.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@
4343
* instance of angular.module.ng.$rootScope.Scope to set the HTML fragment to.
4444
* @param {string=} onload Expression to evaluate when a new partial is loaded.
4545
*
46+
* @param {string=} autoscroll Whether `ng:include` should call {@link angular.module.ng.$autoScroll
47+
* $autoScroll} to scroll the viewport after the content is loaded.
48+
*
49+
* - If the attribute is not set, disable scrolling.
50+
* - If the attribute is set without value, enable scrolling.
51+
* - Otherwise enable scrolling only if the expression evaluates to truthy value.
52+
*
4653
* @example
4754
<doc:example>
4855
<doc:source jsfiddle="false">
@@ -84,7 +91,9 @@ angularWidget('ng:include', function(element){
8491
var compiler = this,
8592
srcExp = element.attr("src"),
8693
scopeExp = element.attr("scope") || '',
87-
onloadExp = element[0].getAttribute('onload') || ''; //workaround for jquery bug #7537
94+
onloadExp = element[0].getAttribute('onload') || '', //workaround for jquery bug #7537
95+
autoScrollExp = element.attr('autoscroll');
96+
8897
if (element[0]['ng:compiled']) {
8998
this.descend(true);
9099
this.directives(true);
@@ -123,7 +132,9 @@ angularWidget('ng:include', function(element){
123132
if (childScope) childScope.$destroy();
124133
childScope = useScope ? useScope : scope.$new();
125134
compiler.compile(element)(childScope);
126-
$autoScroll();
135+
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
136+
$autoScroll();
137+
}
127138
scope.$eval(onloadExp);
128139
}
129140
}).error(clearContent);

test/widgetsSpec.js

+67-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('widget', function() {
5555
});
5656

5757

58-
describe('ng:include', inject(function($rootScope, $compile) {
58+
describe('ng:include', function() {
5959

6060
function putIntoCache(url, content) {
6161
return function($templateCache) {
@@ -227,9 +227,74 @@ describe('widget', function() {
227227
expect(log.join('; ')).toEqual('url2; url2'); // it's here twice because we go through at
228228
// least two digest cycles
229229
}));
230-
}));
231230

232231

232+
describe('autoscoll', function() {
233+
var autoScrollSpy;
234+
235+
function spyOnAutoScroll() {
236+
return function($provide) {
237+
autoScrollSpy = jasmine.createSpy('$autoScroll');
238+
$provide.value('$autoScroll', autoScrollSpy);
239+
};
240+
}
241+
242+
function compileAndLink(tpl) {
243+
return function($compile, $rootScope) {
244+
$compile(tpl)($rootScope);
245+
};
246+
}
247+
248+
function changeTplAndValueTo(template, value) {
249+
return function($rootScope, $browser) {
250+
$rootScope.$apply(function() {
251+
$rootScope.tpl = template;
252+
$rootScope.value = value;
253+
});
254+
$browser.defer.flush();
255+
};
256+
}
257+
258+
beforeEach(inject(
259+
spyOnAutoScroll(),
260+
putIntoCache('template.html', 'CONTENT'),
261+
putIntoCache('another.html', 'CONTENT')));
262+
263+
264+
it('should call $autoScroll if autoscroll attribute is present', inject(
265+
compileAndLink('<ng:include src="tpl" autoscroll></ng:include>'),
266+
changeTplAndValueTo('template.html'), function() {
267+
expect(autoScrollSpy).toHaveBeenCalledOnce();
268+
}));
269+
270+
271+
it('should call $autoScroll if autoscroll evaluates to true', inject(
272+
compileAndLink('<ng:include src="tpl" autoscroll="value"></ng:include>'),
273+
changeTplAndValueTo('template.html', true),
274+
changeTplAndValueTo('another.html', 'some-string'),
275+
changeTplAndValueTo('template.html', 100), function() {
276+
expect(autoScrollSpy).toHaveBeenCalled();
277+
expect(autoScrollSpy.callCount).toBe(3);
278+
}));
279+
280+
281+
it('should not call $autoScroll if autoscroll attribute is not present', inject(
282+
compileAndLink('<ng:include src="tpl"></ng:include>'),
283+
changeTplAndValueTo('template.html'), function() {
284+
expect(autoScrollSpy).not.toHaveBeenCalled();
285+
}));
286+
287+
288+
it('should not call $autoScroll if autoscroll evaluates to false', inject(
289+
compileAndLink('<ng:include src="tpl" autoscroll="value"></ng:include>'),
290+
changeTplAndValueTo('template.html', false),
291+
changeTplAndValueTo('template.html', undefined),
292+
changeTplAndValueTo('template.html', null), function() {
293+
expect(autoScrollSpy).not.toHaveBeenCalled();
294+
}));
295+
});
296+
});
297+
233298
describe('a', function() {
234299
it('should prevent default action to be executed when href is empty',
235300
inject(function($rootScope, $compile) {

0 commit comments

Comments
 (0)