Skip to content

Commit

Permalink
Prefix scroll helper functions. Fixes oblador#91. oblador#86
Browse files Browse the repository at this point in the history
  • Loading branch information
oblador committed Nov 28, 2014
1 parent 3fd61b0 commit f4d91e6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Don't forget to add `duScroll` to your module dependencies.
`angular.element` Scroll API
----------------------------

This module extends the `angular.element` object with a few jQuery like functions. Note that `$document` is an `angular.element`, for usage example see below.
This module extends the `angular.element` object with a few jQuery like functions. Note that `$document` is an `angular.element`, for usage example see below. In case of name collisions existing jQuery or jqlite functions will be preserved, just use the prefixed version: ie `.duScrollTo()` instead of `.scrollTo()`.

#### `.scrollTo( left, top [, duration [, easing ] ] )`
Scrolls the element/window to the specified left/top position. If `duration` is specified the scrolling is animated for n milliseconds. If `easing` is ommited the animation will default to the `duScrollEasing` function.
Expand Down
2 changes: 1 addition & 1 deletion src/directives/smooth-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ angular.module('duScroll.smoothScroll', ['duScroll.scrollHelpers', 'duScroll.scr
var duration = $attr.duration ? parseInt($attr.duration, 10) : duScrollDuration;
var container = scrollContainerAPI.getContainer($scope);

container.scrollToElement(
container.duScrollToElement(
angular.element(target),
isNaN(offset) ? 0 : offset,
isNaN(duration) ? 0 : duration
Expand Down
93 changes: 44 additions & 49 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
.run(function($window, $q, cancelAnimation, requestAnimation, duScrollEasing, duScrollDuration, duScrollOffset) {
'use strict';

var proto = angular.element.prototype;
var proto = {};

var isDocument = function(el) {
return (typeof HTMLDocument !== 'undefined' && el instanceof HTMLDocument) || (el.nodeType && el.nodeType === el.DOCUMENT_NODE);
Expand All @@ -16,12 +16,12 @@ angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
return isElement(el) || isDocument(el) ? el : el[0];
};

proto.scrollTo = function(left, top, duration, easing) {
proto.duScrollTo = function(left, top, duration, easing) {
var aliasFn;
if(angular.isElement(left)) {
aliasFn = this.scrollToElement;
aliasFn = this.duScrollToElement;
} else if(duration) {
aliasFn = this.scrollToAnimated;
aliasFn = this.duScrollToAnimated;
}
if(aliasFn) {
return aliasFn.apply(this, arguments);
Expand All @@ -35,12 +35,12 @@ angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
};

var scrollAnimation, deferred;
proto.scrollToAnimated = function(left, top, duration, easing) {
proto.duScrollToAnimated = function(left, top, duration, easing) {
if(duration && !easing) {
easing = duScrollEasing;
}
var startLeft = this.scrollLeft(),
startTop = this.scrollTop(),
var startLeft = this.duScrollLeft(),
startTop = this.duScrollTop(),
deltaLeft = Math.round(left - startLeft),
deltaTop = Math.round(top - startTop);

Expand Down Expand Up @@ -89,72 +89,67 @@ angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
};

//Fix random mobile safari bug when scrolling to top by hitting status bar
el.scrollTo(startLeft, startTop);
el.duScrollTo(startLeft, startTop);

el.bind(cancelOnEvents, cancelScrollAnimation);

scrollAnimation = requestAnimation(animationStep);
return deferred.promise;
};

proto.scrollToElement = function(target, offset, duration, easing) {
proto.duScrollToElement = function(target, offset, duration, easing) {
var el = unwrap(this);
if(!angular.isNumber(offset) || isNaN(offset)) {
offset = duScrollOffset;
}
var top = this.scrollTop() + unwrap(target).getBoundingClientRect().top - offset;
var top = this.duScrollTop() + unwrap(target).getBoundingClientRect().top - offset;
if(isElement(el)) {
top -= el.getBoundingClientRect().top;
}
return this.scrollTo(0, top, duration, easing);
return this.duScrollTo(0, top, duration, easing);
};

var overloaders = {
scrollLeft: function(value, duration, easing) {
if(angular.isNumber(value)) {
return this.scrollTo(value, this.scrollTop(), duration, easing);
}
var el = unwrap(this);
if(isDocument(el)) {
return $window.scrollX || document.documentElement.scrollLeft || document.body.scrollLeft;
}
return el.scrollLeft;
},
scrollTop: function(value, duration, easing) {
if(angular.isNumber(value)) {
return this.scrollTo(this.scrollTop(), value, duration, easing);
}
var el = unwrap(this);
if(isDocument(el)) {
return $window.scrollY || document.documentElement.scrollTop || document.body.scrollTop;
}
return el.scrollTop;
proto.duScrollLeft = function(value, duration, easing) {
if(angular.isNumber(value)) {
return this.duScrollTo(value, this.duScrollTop(), duration, easing);
}
var el = unwrap(this);
if(isDocument(el)) {
return $window.scrollX || document.documentElement.scrollLeft || document.body.scrollLeft;
}
return el.scrollLeft;
};

proto.scrollToElementAnimated = function(target, offset, duration, easing) {
return this.scrollToElement(target, offset, duration || duScrollDuration, easing);
proto.duScrollTop = function(value, duration, easing) {
if(angular.isNumber(value)) {
return this.duScrollTo(this.duScrollLeft(), value, duration, easing);
}
var el = unwrap(this);
if(isDocument(el)) {
return $window.scrollY || document.documentElement.scrollTop || document.body.scrollTop;
}
return el.scrollTop;
};

proto.scrollTopAnimated = function(top, duration, easing) {
return this.scrollTop(top, duration || duScrollDuration, easing);
proto.duScrollToElementAnimated = function(target, offset, duration, easing) {
return this.duScrollToElement(target, offset, duration || duScrollDuration, easing);
};

proto.scrollLeftAnimated = function(left, duration, easing) {
return this.scrollLeft(left, duration || duScrollDuration, easing);
proto.duScrollTopAnimated = function(top, duration, easing) {
return this.duScrollTop(top, duration || duScrollDuration, easing);
};

//Add duration and easing functionality to existing jQuery getter/setters
var overloadScrollPos = function(superFn, overloadFn) {
return function(value, duration, easing) {
if(duration) {
return overloadFn.apply(this, arguments);
}
return superFn.apply(this, arguments);
};
proto.duScrollLeftAnimated = function(left, duration, easing) {
return this.duScrollLeft(left, duration || duScrollDuration, easing);
};

for(var methodName in overloaders) {
proto[methodName] = (proto[methodName] ? overloadScrollPos(proto[methodName], overloaders[methodName]) : overloaders[methodName]);
}
angular.forEach(proto, function(fn, key) {
angular.element.prototype[key] = fn;

//Remove prefix if not already claimed by jQuery / ui.utils
var unprefixed = key.replace(/^duScroll/, 'scroll');
if(angular.isUndefined(angular.element.prototype[unprefixed])) {
angular.element.prototype[unprefixed] = fn;
}
});

});

0 comments on commit f4d91e6

Please sign in to comment.