diff options
Diffstat (limited to 'js/vendor/angular-ui/modules/directives/scrollfix')
-rw-r--r-- | js/vendor/angular-ui/modules/directives/scrollfix/scrollfix.js | 39 | ||||
-rw-r--r-- | js/vendor/angular-ui/modules/directives/scrollfix/test/scrollfixSpec.js | 39 |
2 files changed, 78 insertions, 0 deletions
diff --git a/js/vendor/angular-ui/modules/directives/scrollfix/scrollfix.js b/js/vendor/angular-ui/modules/directives/scrollfix/scrollfix.js new file mode 100644 index 000000000..f613838d9 --- /dev/null +++ b/js/vendor/angular-ui/modules/directives/scrollfix/scrollfix.js @@ -0,0 +1,39 @@ +/*global angular, $, document*/ +/** + * Adds a 'ui-scrollfix' class to the element when the page scrolls past it's position. + * @param [offset] {int} optional Y-offset to override the detected offset. + * Takes 300 (absolute) or -300 or +300 (relative to detected) + */ +angular.module('ui.directives').directive('uiScrollfix', ['$window', function ($window) { + 'use strict'; + return { + link: function (scope, elm, attrs) { + var top = elm.offset().top; + if (!attrs.uiScrollfix) { + attrs.uiScrollfix = top; + } else { + // chartAt is generally faster than indexOf: http://jsperf.com/indexof-vs-chartat + if (attrs.uiScrollfix.charAt(0) === '-') { + attrs.uiScrollfix = top - attrs.uiScrollfix.substr(1); + } else if (attrs.uiScrollfix.charAt(0) === '+') { + attrs.uiScrollfix = top + parseFloat(attrs.uiScrollfix.substr(1)); + } + } + angular.element($window).on('scroll.ui-scrollfix', function () { + // if pageYOffset is defined use it, otherwise use other crap for IE + var offset; + if (angular.isDefined($window.pageYOffset)) { + offset = $window.pageYOffset; + } else { + var iebody = (document.compatMode && document.compatMode !== "BackCompat") ? document.documentElement : document.body; + offset = iebody.scrollTop; + } + if (!elm.hasClass('ui-scrollfix') && offset > attrs.uiScrollfix) { + elm.addClass('ui-scrollfix'); + } else if (elm.hasClass('ui-scrollfix') && offset < attrs.uiScrollfix) { + elm.removeClass('ui-scrollfix'); + } + }); + } + }; +}]); diff --git a/js/vendor/angular-ui/modules/directives/scrollfix/test/scrollfixSpec.js b/js/vendor/angular-ui/modules/directives/scrollfix/test/scrollfixSpec.js new file mode 100644 index 000000000..7ff006cd0 --- /dev/null +++ b/js/vendor/angular-ui/modules/directives/scrollfix/test/scrollfixSpec.js @@ -0,0 +1,39 @@ +/*global describe, beforeEach, module, inject, it, spyOn, expect, $ */ +describe('uiScrollfix', function () { + 'use strict'; + + var scope, $compile, $window; + beforeEach(module('ui.directives')); + beforeEach(inject(function (_$rootScope_, _$compile_, _$window_) { + scope = _$rootScope_.$new(); + $compile = _$compile_; + $window = _$window_; + })); + + describe('compiling this directive', function () { + it('should bind to window "scroll" event with a ui-scrollfix namespace', function () { + spyOn($.fn, 'on'); + $compile('<div ui-scrollfix="100"></div>')(scope); + expect($.fn.on).toHaveBeenCalled(); + expect($.fn.on.mostRecentCall.args[0]).toBe('scroll.ui-scrollfix'); + }); + }); + describe('scrolling the window', function () { + it('should add the ui-scrollfix class if the offset is greater than specified', function () { + var element = $compile('<div ui-scrollfix="-100"></div>')(scope); + $($window).trigger('scroll.ui-scrollfix'); + expect(element.hasClass('ui-scrollfix')).toBe(true); + }); + it('should remove the ui-scrollfix class if the offset is less than specified (using absolute coord)', function () { + var element = $compile('<div ui-scrollfix="100" class="ui-scrollfix"></div>')(scope); + $($window).trigger('scroll.ui-scrollfix'); + expect(element.hasClass('ui-scrollfix')).toBe(false); + + }); + it('should remove the ui-scrollfix class if the offset is less than specified (using relative coord)', function () { + var element = $compile('<div ui-scrollfix="+100" class="ui-scrollfix"></div>')(scope); + $($window).trigger('scroll.ui-scrollfix'); + expect(element.hasClass('ui-scrollfix')).toBe(false); + }); + }); +});
\ No newline at end of file |