Built motion from commit 943eae279.|1.0.24
[motion.git] / public / bower_components / ngInfiniteScroll / ng-infinite-scroll.js
1 /* ng-infinite-scroll - v1.0.0 - 2013-02-23 */
2 var mod;
3
4 mod = angular.module('infinite-scroll', []);
5
6 mod.directive('infiniteScroll', [
7   '$rootScope', '$window', '$timeout', function($rootScope, $window, $timeout) {
8     return {
9       link: function(scope, elem, attrs) {
10         var checkWhenEnabled, handler, scrollDistance, scrollEnabled;
11         $window = angular.element($window);
12         scrollDistance = 0;
13         if (attrs.infiniteScrollDistance != null) {
14           scope.$watch(attrs.infiniteScrollDistance, function(value) {
15             return scrollDistance = parseInt(value, 10);
16           });
17         }
18         scrollEnabled = true;
19         checkWhenEnabled = false;
20         if (attrs.infiniteScrollDisabled != null) {
21           scope.$watch(attrs.infiniteScrollDisabled, function(value) {
22             scrollEnabled = !value;
23             if (scrollEnabled && checkWhenEnabled) {
24               checkWhenEnabled = false;
25               return handler();
26             }
27           });
28         }
29         handler = function() {
30           var elementBottom, remaining, shouldScroll, windowBottom;
31           windowBottom = $window.height() + $window.scrollTop();
32           elementBottom = elem.offset().top + elem.height();
33           remaining = elementBottom - windowBottom;
34           shouldScroll = remaining <= $window.height() * scrollDistance;
35           if (shouldScroll && scrollEnabled) {
36             if ($rootScope.$$phase) {
37               return scope.$eval(attrs.infiniteScroll);
38             } else {
39               return scope.$apply(attrs.infiniteScroll);
40             }
41           } else if (shouldScroll) {
42             return checkWhenEnabled = true;
43           }
44         };
45         $window.on('scroll', handler);
46         scope.$on('$destroy', function() {
47           return $window.off('scroll', handler);
48         });
49         return $timeout((function() {
50           if (attrs.infiniteScrollImmediateCheck) {
51             if (scope.$eval(attrs.infiniteScrollImmediateCheck)) {
52               return handler();
53             }
54           } else {
55             return handler();
56           }
57         }), 0);
58       }
59     };
60   }
61 ]);