Skip to content

WIP: simplify $anchorScroll offset feature #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/app/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ angular.module('docsApp', [
'search',
'tutorials',
'versions',
'scrollOffset',
'bootstrap',
'ui.bootstrap.dropdown'
])

.config(['$locationProvider', function($locationProvider) {
$locationProvider.html5Mode(true).hashPrefix('!');
}]);
}]);
8 changes: 7 additions & 1 deletion docs/app/src/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ angular.module('directives', [])
element.html(window.prettyPrintOne(html, lang, linenums));
}
};
});
})

.directive('scrollYOffsetElement', ['$anchorScroll', function($anchorScroll) {
return function(scope, element) {
$anchorScroll.yOffset = element;
};
}]);
39 changes: 0 additions & 39 deletions docs/app/src/scroll-offset.js

This file was deleted.

2 changes: 1 addition & 1 deletion docs/config/templates/indexPage.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</head>
<body>
<div id="wrapper">
<header class="header header-fixed" scroll-offset-element>
<header scroll-y-offset-element class="header header-fixed">
<section class="navbar navbar-inverse docs-navbar-primary" ng-controller="DocsSearchCtrl">
<div class="container">
<div class="row">
Expand Down
40 changes: 24 additions & 16 deletions src/ng/anchorScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,12 @@ function $AnchorScrollProvider() {
// TODO(gkalpak): The $anchorScrollProvider should be documented as well
// (under the providers section).

var DEFAULT_OFFSET = 0;

var autoScrollingEnabled = true;
var scrollOffsetGetter = function() { return DEFAULT_OFFSET; };

this.disableAutoScrolling = function() {
autoScrollingEnabled = false;
};

this.setScrollOffset = function(newScrollOffset) {
if (isFunction(newScrollOffset)) {
scrollOffsetGetter = function() { return newScrollOffset(); };
} else if (isNumber(newScrollOffset)) {
scrollOffsetGetter = function() { return newScrollOffset; };
}
};

this.$get = ['$window', '$location', '$rootScope', function($window, $location, $rootScope) {
var document = $window.document;

Expand All @@ -148,14 +137,33 @@ function $AnchorScrollProvider() {
return result;
}

function getYOffset() {

var offset = scroll.yOffset;

if (isElement(offset)) {

var style = $window.getComputedStyle(scroll.yOffset[0]);
var top = parseInt(style.top,10);
var height = parseInt(style.height,10);
return style.position === 'fixed' ? (top + height) : 0;

} else if (isFunction(offset)) {
return offset();

} else if (isNumber(offset)) {
return offset;

} else {
return 0;
}

}

function scrollTo(elem) {
if (elem) {
elem.scrollIntoView();
var offset = scrollOffsetGetter();
var actualOffset = offset && (offset - (elem.offsetTop - document.body.scrollTop));
if (actualOffset) {
$window.scrollBy(0, -1 * actualOffset);
}
$window.scrollBy(0, -1 * getYOffset());
} else {
$window.scrollTo(0, 0);
}
Expand Down
1 change: 1 addition & 0 deletions test/ng/anchorScrollSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('$anchorScroll', function() {
elmSpy = {};
$provide.value('$window', {
scrollTo: jasmine.createSpy('$window.scrollTo'),
scrollBy: jasmine.createSpy('$window.scrollBy'),
document: document,
navigator: {}
});
Expand Down