Skip to content

Allow specifying target id with expression. #170

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 15 additions & 4 deletions angular-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var duScroll = angular.module('duScroll', [
.value('duScrollDuration', 350)
//Scrollspy debounce interval, set to 0 to disable
.value('duScrollSpyWait', 100)
//Scrollspy forced refresh interval, use if your content changes or reflows without scrolling.
//0 to disable
.value('duScrollSpyRefreshInterval', 0)
//Wether or not multiple scrollspies can be active at once
.value('duScrollGreedy', false)
//Default offset for smoothScroll directive
Expand Down Expand Up @@ -254,7 +257,7 @@ angular.module('duScroll.requestAnimation', ['duScroll.polyfill'])


angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
.factory('spyAPI', ["$rootScope", "$timeout", "$window", "$document", "scrollContainerAPI", "duScrollGreedy", "duScrollSpyWait", "duScrollBottomSpy", "duScrollActiveClass", function($rootScope, $timeout, $window, $document, scrollContainerAPI, duScrollGreedy, duScrollSpyWait, duScrollBottomSpy, duScrollActiveClass) {
.factory('spyAPI', ["$rootScope", "$timeout", "$interval", "$window", "$document", "scrollContainerAPI", "duScrollGreedy", "duScrollSpyWait", "duScrollSpyRefreshInterval", "duScrollBottomSpy", "duScrollActiveClass", function($rootScope, $timeout, $interval, $window, $document, scrollContainerAPI, duScrollGreedy, duScrollSpyWait, duScrollSpyRefreshInterval, duScrollBottomSpy, duScrollActiveClass) {
'use strict';

var createScrollHandler = function(context) {
Expand Down Expand Up @@ -283,7 +286,7 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
for(i = 0; i < spies.length; i++) {
spy = spies[i];
pos = spy.getTargetPosition();
if (!pos) continue;
if (!pos || !spy.$element) continue;

if((duScrollBottomSpy && bottomReached) || (pos.top + spy.offset - containerOffset < 20 && (duScrollGreedy || pos.top*-1 + containerOffset) < pos.height)) {
//Find the one closest the viewport top or the page bottom if it's reached
Expand All @@ -300,7 +303,7 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
toBeActive = toBeActive.spy;
}
if(currentlyActive === toBeActive || (duScrollGreedy && !toBeActive)) return;
if(currentlyActive) {
if(currentlyActive && currentlyActive.$element) {
currentlyActive.$element.removeClass(duScrollActiveClass);
$rootScope.$broadcast(
'duScrollspy:becameInactive',
Expand Down Expand Up @@ -360,6 +363,9 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
var destroyContext = function($scope) {
var id = $scope.$id;
var context = contexts[id], container = context.container;
if(context.intervalPromise) {
$interval.cancel(context.intervalPromise);
}
if(container) {
container.off('scroll', context.handler);
}
Expand Down Expand Up @@ -411,6 +417,9 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
context.container.off('scroll', context.handler);
}
context.container = scrollContainerAPI.getContainer(spy.$scope);
if (duScrollSpyRefreshInterval && !context.intervalPromise) {
context.intervalPromise = $interval(context.handler, duScrollSpyRefreshInterval, 0, false);
}
context.container.on('scroll', context.handler).triggerHandler('scroll');
}
};
Expand Down Expand Up @@ -595,12 +604,14 @@ angular.module('duScroll.scrollspy', ['duScroll.spyAPI'])
};

return {
scope: { duScrollspy: '=' },
link: function ($scope, $element, $attr) {
var href = $attr.ngHref || $attr.href;
var targetId;

if (href && href.indexOf('#') !== -1) {
targetId = href.replace(/.*(?=#[^\s]+$)/, '').substring(1);
} else if($scope.duScrollspy) {
targetId = $scope.duScrollspy;
} else if($attr.duScrollspy) {
targetId = $attr.duScrollspy;
} else if($attr.duSmoothScroll) {
Expand Down
Loading