2014-07-08 07:02:26 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
|
|
|
App.directive "infiniteScroll", () ->
|
|
|
|
return {
|
|
|
|
link: (scope, element, attrs, ctrl) ->
|
|
|
|
innerElement = element.find(".infinite-scroll-inner")
|
|
|
|
element.css 'overflow-y': 'auto'
|
|
|
|
|
|
|
|
atEndOfListView = () ->
|
2014-07-15 13:25:12 -04:00
|
|
|
if attrs.infiniteScrollUpwards?
|
|
|
|
atTopOfListView()
|
|
|
|
else
|
2014-07-28 12:28:52 -04:00
|
|
|
atBottomOfListView()
|
2014-07-15 13:25:12 -04:00
|
|
|
|
|
|
|
atTopOfListView = () ->
|
|
|
|
element.scrollTop() < 30
|
|
|
|
|
|
|
|
atBottomOfListView = () ->
|
2014-07-08 07:02:26 -04:00
|
|
|
element.scrollTop() + element.height() >= innerElement.height() - 30
|
|
|
|
|
|
|
|
listShorterThanContainer = () ->
|
2014-07-15 13:25:12 -04:00
|
|
|
element.height() > innerElement.height()
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
loadUntilFull = () ->
|
|
|
|
if (listShorterThanContainer() or atEndOfListView()) and not scope.$eval(attrs.infiniteScrollDisabled)
|
|
|
|
promise = scope.$eval(attrs.infiniteScroll)
|
|
|
|
promise.then () ->
|
2015-02-13 10:53:59 -05:00
|
|
|
setTimeout () ->
|
|
|
|
loadUntilFull()
|
|
|
|
, 0
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
element.on "scroll", (event) ->
|
|
|
|
loadUntilFull()
|
|
|
|
|
|
|
|
scope.$watch attrs.infiniteScrollInitialize, (value) ->
|
|
|
|
if value
|
|
|
|
loadUntilFull()
|
|
|
|
|
|
|
|
}
|