Skip to content

Commit

Permalink
Merge pull request #167 from neoeno/add_manual_trigger_event
Browse files Browse the repository at this point in the history
Add custom event listener to trigger handler manually.
  • Loading branch information
sroze committed Feb 14, 2015
2 parents 78770d7 + b11dbf4 commit 9fcbb88
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
17 changes: 13 additions & 4 deletions build/ng-infinite-scroll.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ng-infinite-scroll - v1.2.0 - 2014-12-02 */
/* ng-infinite-scroll - v1.2.0 - 2015-02-14 */
var mod;

mod = angular.module('infinite-scroll', []);
Expand All @@ -13,17 +13,19 @@ mod.directive('infiniteScroll', [
infiniteScrollContainer: '=',
infiniteScrollDistance: '=',
infiniteScrollDisabled: '=',
infiniteScrollUseDocumentBottom: '='
infiniteScrollUseDocumentBottom: '=',
infiniteScrollListenForEvent: '@'
},
link: function(scope, elem, attrs) {
var changeContainer, checkWhenEnabled, container, handleInfiniteScrollContainer, handleInfiniteScrollDisabled, handleInfiniteScrollDistance, handleInfiniteScrollUseDocumentBottom, handler, height, immediateCheck, offsetTop, pageYOffset, scrollDistance, scrollEnabled, throttle, useDocumentBottom, windowElement;
var changeContainer, checkWhenEnabled, container, handleInfiniteScrollContainer, handleInfiniteScrollDisabled, handleInfiniteScrollDistance, handleInfiniteScrollUseDocumentBottom, handler, height, immediateCheck, offsetTop, pageYOffset, scrollDistance, scrollEnabled, throttle, unregisterEventListener, useDocumentBottom, windowElement;
windowElement = angular.element($window);
scrollDistance = null;
scrollEnabled = null;
checkWhenEnabled = null;
container = null;
immediateCheck = true;
useDocumentBottom = false;
unregisterEventListener = null;
height = function(elem) {
elem = elem[0] || elem;
if (isNaN(elem.offsetHeight)) {
Expand Down Expand Up @@ -110,7 +112,11 @@ mod.directive('infiniteScroll', [
handler = throttle(handler, THROTTLE_MILLISECONDS);
}
scope.$on('$destroy', function() {
return container.unbind('scroll', handler);
container.unbind('scroll', handler);
if (unregisterEventListener != null) {
unregisterEventListener();
return unregisterEventListener = null;
}
});
handleInfiniteScrollDistance = function(v) {
return scrollDistance = parseFloat(v) || 0;
Expand Down Expand Up @@ -141,6 +147,9 @@ mod.directive('infiniteScroll', [
}
};
changeContainer(windowElement);
if (scope.infiniteScrollListenForEvent) {
unregisterEventListener = $rootScope.$on(scope.infiniteScrollListenForEvent, handler);
}
handleInfiniteScrollContainer = function(newContainer) {
if ((newContainer == null) || newContainer.length === 0) {
return;
Expand Down
4 changes: 2 additions & 2 deletions build/ng-infinite-scroll.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/infinite-scroll.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ mod.directive 'infiniteScroll', ['$rootScope', '$window', '$interval', 'THROTTLE
infiniteScrollContainer: '='
infiniteScrollDistance: '='
infiniteScrollDisabled: '='
infiniteScrollUseDocumentBottom: '='
infiniteScrollUseDocumentBottom: '=',
infiniteScrollListenForEvent: '@'

link: (scope, elem, attrs) ->
windowElement = angular.element($window)
Expand All @@ -20,6 +21,7 @@ mod.directive 'infiniteScroll', ['$rootScope', '$window', '$interval', 'THROTTLE
container = null
immediateCheck = true
useDocumentBottom = false
unregisterEventListener = null

height = (elem) ->
elem = elem[0] or elem
Expand Down Expand Up @@ -103,6 +105,9 @@ mod.directive 'infiniteScroll', ['$rootScope', '$window', '$interval', 'THROTTLE

scope.$on '$destroy', ->
container.unbind 'scroll', handler
if unregisterEventListener?
unregisterEventListener()
unregisterEventListener = null

# infinite-scroll-distance specifies how close to the bottom of the page
# the window is allowed to be before we trigger a new scroll. The value
Expand Down Expand Up @@ -155,6 +160,9 @@ mod.directive 'infiniteScroll', ['$rootScope', '$window', '$interval', 'THROTTLE

changeContainer windowElement

if scope.infiniteScrollListenForEvent
unregisterEventListener = $rootScope.$on scope.infiniteScrollListenForEvent, handler

handleInfiniteScrollContainer = (newContainer) ->
# TODO: For some reason newContainer is sometimes null instead
# of the empty array, which Angular is supposed to pass when the
Expand Down
37 changes: 37 additions & 0 deletions test/spec/ng-infinite-scroll.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ getTemplate = (angularVersion, container, attrs, throttle) ->
$rootScope.enable = function () {
$rootScope.busy = false;
};
$rootScope.triggerEvent = function () {
$rootScope.$emit('anEvent');
};
});
</script>
</head>
<body ng-app="app">
<a id="action" ng-click="enable()">Enable</a>
<a id="force" ng-click="loadMore()">Force</a>
<a id="trigger" ng-click="triggerEvent()">Trigger</a>
#{containers[container].start}
<div infinite-scroll="loadMore()" #{containers[container].attr} #{attrs}>
<p ng-repeat='item in items track by $index'>
Expand Down Expand Up @@ -85,6 +90,14 @@ scrollToLastScreenScript = (container, offset) ->
#{calculateChildrenHeightScript(container)} - 2 * #{getElementByIdScript(container)}.offsetHeight + #{offset}
"""

collapseItemsScript = (container) ->
"""
var items = document.getElementsByTagName('p')
for (i = 0; i < items.length; ++i) {
items[i].style.display = 'none'
}
"""

getItems = ->
element.all(By.repeater "item in items")

Expand Down Expand Up @@ -137,6 +150,17 @@ describe "ng-infinite-scroll", ->
browser.driver.executeScript(scrollToLastScreenScript(container, 20))
expect(getItems().count()).toBe 200

describe "with an event handler", ->

it "calls the event handler on an event", ->
replaceIndexFile "infinite-scroll-listen-for-event='anEvent'", throttle
browser.get pathToDocument
expect(getItems().count()).toBe 100
browser.driver.executeScript(collapseItemsScript(container))
expect(getItems().count()).toBe 100
element(By.id("trigger")).click()
expect(getItems().count()).toBe 200

describe "with throttling", ->

throttle = browser.params.testThrottleValue
Expand Down Expand Up @@ -182,3 +206,16 @@ describe "ng-infinite-scroll", ->
expect(getItems().count()).toBe 100
browser.sleep(throttle)
expect(getItems().count()).toBe 200

describe "with an event handler", ->

it "calls the event handler on an event", ->
replaceIndexFile "infinite-scroll-listen-for-event='anEvent'", throttle
browser.get pathToDocument
expect(getItems().count()).toBe 100
browser.driver.executeScript(collapseItemsScript(container))
expect(getItems().count()).toBe 100
element(By.id("trigger")).click()
expect(getItems().count()).toBe 100
browser.sleep(throttle)
expect(getItems().count()).toBe 200

0 comments on commit 9fcbb88

Please sign in to comment.