overleaf/services/web/public/coffee/ide/history/directives/infiniteScroll.coffee

40 lines
1 KiB
CoffeeScript
Raw Normal View History

2014-07-08 11:02:26 +00: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 17:25:12 +00:00
if attrs.infiniteScrollUpwards?
atTopOfListView()
else
2014-07-28 16:28:52 +00:00
atBottomOfListView()
2014-07-15 17:25:12 +00:00
atTopOfListView = () ->
element.scrollTop() < 30
atBottomOfListView = () ->
2014-07-08 11:02:26 +00:00
element.scrollTop() + element.height() >= innerElement.height() - 30
listShorterThanContainer = () ->
2014-07-15 17:25:12 +00:00
element.height() > innerElement.height()
2014-07-08 11:02:26 +00:00
loadUntilFull = () ->
if (listShorterThanContainer() or atEndOfListView()) and not scope.$eval(attrs.infiniteScrollDisabled)
promise = scope.$eval(attrs.infiniteScroll)
promise.then () ->
setTimeout () ->
loadUntilFull()
, 0
2014-07-08 11:02:26 +00:00
element.on "scroll", (event) ->
loadUntilFull()
scope.$watch attrs.infiniteScrollInitialize, (value) ->
if value
loadUntilFull()
}