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

Commit 15fd735

Browse files
committed
refactor($autoScroll): rename to $anchorScroll and allow disabling auto scrolling (links)
Now, that we have autoscroll attribute on ng:include, there is no reason to disable the service completely, so $anchorScrollProvider.disableAutoScrolling() means it won't be scrolling when $location.hash() changes. And then, it's not $autoScroll at all, it actually scrolls to anchor when it's called, so I renamed it to $anchorScroll.
1 parent 985d3d7 commit 15fd735

File tree

6 files changed

+41
-49
lines changed

6 files changed

+41
-49
lines changed

angularFiles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ angularFiles = {
99
'src/sanitizer.js',
1010
'src/jqLite.js',
1111
'src/apis.js',
12-
'src/service/autoScroll.js',
12+
'src/service/anchorScroll.js',
1313
'src/service/browser.js',
1414
'src/service/cacheFactory.js',
1515
'src/service/compiler.js',

src/AngularPublic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function publishExternalAPI(angular){
6666
$provide.value('$directive', angularDirective);
6767
$provide.value('$widget', angularWidget);
6868

69-
$provide.service('$autoScroll', $AutoScrollProvider);
69+
$provide.service('$anchorScroll', $AnchorScrollProvider);
7070
$provide.service('$browser', $BrowserProvider);
7171
$provide.service('$cacheFactory', $CacheFactoryProvider);
7272
$provide.service('$compile', $CompileProvider);

src/service/autoScroll.js renamed to src/service/anchorScroll.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @ngdoc function
3-
* @name angular.module.ng.$autoScroll
3+
* @name angular.module.ng.$anchorScroll
44
* @requires $window
55
* @requires $location
66
* @requires $rootScope
@@ -11,14 +11,14 @@
1111
* {@link http://dev.w3.org/html5/spec/Overview.html#the-indicated-part-of-the-document Html5 spec}.
1212
*
1313
* It also watches the `$location.hash()` and scroll whenever it changes to match any anchor.
14-
*
15-
* You can disable `$autoScroll` service by calling `disable()` on `$autoScrollProvider`.
16-
* Note: disabling is only possible before the service is instantiated !
14+
* This can be disabled by calling `$anchorScrollProvider.disableAutoScrolling()`.
1715
*/
18-
function $AutoScrollProvider() {
16+
function $AnchorScrollProvider() {
17+
18+
var autoScrollingEnabled = true;
1919

20-
this.disable = function() {
21-
this.$get = function() {return noop;};
20+
this.disableAutoScrolling = function() {
21+
autoScrollingEnabled = false;
2222
};
2323

2424
this.$get = ['$window', '$location', '$rootScope', function($window, $location, $rootScope) {
@@ -54,9 +54,11 @@ function $AutoScrollProvider() {
5454

5555
// does not scroll when user clicks on anchor link that is currently on
5656
// (no url change, no $locaiton.hash() change), browser native does scroll
57-
$rootScope.$watch(function() {return $location.hash();}, function() {
58-
$rootScope.$evalAsync(scroll);
59-
});
57+
if (autoScrollingEnabled) {
58+
$rootScope.$watch(function() {return $location.hash();}, function() {
59+
$rootScope.$evalAsync(scroll);
60+
});
61+
}
6062

6163
return scroll;
6264
}];

src/widgets.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
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.
46+
* @param {string=} autoscroll Whether `ng:include` should call {@link angular.module.ng.$anchorScroll
47+
* $anchorScroll} to scroll the viewport after the content is loaded.
4848
*
4949
* - If the attribute is not set, disable scrolling.
5050
* - If the attribute is set without value, enable scrolling.
@@ -99,8 +99,8 @@ angularWidget('ng:include', function(element){
9999
this.directives(true);
100100
} else {
101101
element[0]['ng:compiled'] = true;
102-
return ['$http', '$templateCache', '$autoScroll', '$element',
103-
function($http, $templateCache, $autoScroll, element) {
102+
return ['$http', '$templateCache', '$anchorScroll', '$element',
103+
function($http, $templateCache, $anchorScroll, element) {
104104
var scope = this,
105105
changeCounter = 0,
106106
childScope;
@@ -133,7 +133,7 @@ angularWidget('ng:include', function(element){
133133
childScope = useScope ? useScope : scope.$new();
134134
compiler.compile(element)(childScope);
135135
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
136-
$autoScroll();
136+
$anchorScroll();
137137
}
138138
scope.$eval(onloadExp);
139139
}
@@ -568,8 +568,8 @@ angularWidget('ng:view', function(element) {
568568

569569
if (!element[0]['ng:compiled']) {
570570
element[0]['ng:compiled'] = true;
571-
return ['$http', '$templateCache', '$route', '$autoScroll', '$element',
572-
function($http, $templateCache, $route, $autoScroll, element) {
571+
return ['$http', '$templateCache', '$route', '$anchorScroll', '$element',
572+
function($http, $templateCache, $route, $anchorScroll, element) {
573573
var template;
574574
var changeCounter = 0;
575575

@@ -593,7 +593,7 @@ angularWidget('ng:view', function(element) {
593593
if (newChangeCounter == changeCounter) {
594594
element.html(response);
595595
compiler.compile(element)($route.current.scope);
596-
$autoScroll();
596+
$anchorScroll();
597597
}
598598
}).error(clearContent);
599599
} else {

test/service/autoScrollSpec.js renamed to test/service/anchorScrollSpec.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe('$autoScroll', function() {
1+
describe('$anchorScroll', function() {
22

33
var elmSpy;
44

@@ -18,9 +18,9 @@ describe('$autoScroll', function() {
1818
}
1919

2020
function changeHashAndScroll(hash) {
21-
return function($location, $autoScroll) {
21+
return function($location, $anchorScroll) {
2222
$location.hash(hash);
23-
$autoScroll();
23+
$anchorScroll();
2424
};
2525
}
2626

@@ -46,12 +46,6 @@ describe('$autoScroll', function() {
4646
return expectScrollingTo(NaN);
4747
}
4848

49-
function disableScroller() {
50-
return function($autoScrollProvider) {
51-
$autoScrollProvider.disable();
52-
};
53-
}
54-
5549

5650
beforeEach(module(function($provide) {
5751
elmSpy = {};
@@ -108,16 +102,6 @@ describe('$autoScroll', function() {
108102
expectScrollingTo('id=top')));
109103

110104

111-
it('should not scroll when disabled', function() {
112-
module(disableScroller());
113-
inject(
114-
addElements('id=fake', 'a name=fake', 'input name=fake'),
115-
changeHashAndScroll('fake'),
116-
expectNoScrolling()
117-
);
118-
});
119-
120-
121105
describe('watcher', function() {
122106

123107
function initLocation(config) {
@@ -128,13 +112,19 @@ describe('$autoScroll', function() {
128112
}
129113

130114
function changeHashTo(hash) {
131-
return function ($location, $rootScope, $autoScroll) {
115+
return function ($location, $rootScope, $anchorScroll) {
132116
$rootScope.$apply(function() {
133117
$location.hash(hash);
134118
});
135119
};
136120
}
137121

122+
function disableAutoScrolling() {
123+
return function($anchorScrollProvider) {
124+
$anchorScrollProvider.disableAutoScrolling();
125+
};
126+
}
127+
138128
afterEach(inject(function($document) {
139129
dealoc($document);
140130
}));
@@ -182,7 +172,7 @@ describe('$autoScroll', function() {
182172

183173
it('should not scroll when disabled', function() {
184174
module(
185-
disableScroller(),
175+
disableAutoScrolling(),
186176
initLocation({html5Mode: false, historyApi: false})
187177
);
188178
inject(

test/widgetsSpec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ describe('widget', function() {
225225
describe('autoscoll', function() {
226226
var autoScrollSpy;
227227

228-
function spyOnAutoScroll() {
228+
function spyOnAnchorScroll() {
229229
return function($provide) {
230-
autoScrollSpy = jasmine.createSpy('$autoScroll');
231-
$provide.value('$autoScroll', autoScrollSpy);
230+
autoScrollSpy = jasmine.createSpy('$anchorScroll');
231+
$provide.value('$anchorScroll', autoScrollSpy);
232232
};
233233
}
234234

@@ -247,20 +247,20 @@ describe('widget', function() {
247247
};
248248
}
249249

250-
beforeEach(module(spyOnAutoScroll()));
250+
beforeEach(module(spyOnAnchorScroll()));
251251
beforeEach(inject(
252252
putIntoCache('template.html', 'CONTENT'),
253253
putIntoCache('another.html', 'CONTENT')));
254254

255255

256-
it('should call $autoScroll if autoscroll attribute is present', inject(
256+
it('should call $anchorScroll if autoscroll attribute is present', inject(
257257
compileAndLink('<ng:include src="tpl" autoscroll></ng:include>'),
258258
changeTplAndValueTo('template.html'), function() {
259259
expect(autoScrollSpy).toHaveBeenCalledOnce();
260260
}));
261261

262262

263-
it('should call $autoScroll if autoscroll evaluates to true', inject(
263+
it('should call $anchorScroll if autoscroll evaluates to true', inject(
264264
compileAndLink('<ng:include src="tpl" autoscroll="value"></ng:include>'),
265265
changeTplAndValueTo('template.html', true),
266266
changeTplAndValueTo('another.html', 'some-string'),
@@ -270,14 +270,14 @@ describe('widget', function() {
270270
}));
271271

272272

273-
it('should not call $autoScroll if autoscroll attribute is not present', inject(
273+
it('should not call $anchorScroll if autoscroll attribute is not present', inject(
274274
compileAndLink('<ng:include src="tpl"></ng:include>'),
275275
changeTplAndValueTo('template.html'), function() {
276276
expect(autoScrollSpy).not.toHaveBeenCalled();
277277
}));
278278

279279

280-
it('should not call $autoScroll if autoscroll evaluates to false', inject(
280+
it('should not call $anchorScroll if autoscroll evaluates to false', inject(
281281
compileAndLink('<ng:include src="tpl" autoscroll="value"></ng:include>'),
282282
changeTplAndValueTo('template.html', false),
283283
changeTplAndValueTo('template.html', undefined),

0 commit comments

Comments
 (0)