overleaf/services/web/public/coffee/app/directives/scrollGlue.coffee
Henry Oswald 581d9ec925 - hooked up chat throughout the stack
- removed lots of console.logs
- working on the auto scrol, doesn't quite work yet
2014-07-08 10:08:38 +01:00

34 lines
743 B
CoffeeScript

define [
"base"
], (App) ->
fakeNgModel = (initValue) ->
$setViewValue: (value) ->
@$viewValue = value
return
$viewValue: initValue
App.directive "scrollGlue", ->
return {
priority: 1
require: ["?ngModel"]
restrict: "A"
link: (scope, $el, attrs, ctrls) ->
scrollToBottom = ->
el.scrollTop = el.scrollHeight
shouldActivateAutoScroll = ->
el.scrollTop + el.clientHeight + 1 >= el.scrollHeight
el = $el[0]
ngModel = ctrls[0] or fakeNgModel(true)
scope.$watch ->
scrollToBottom() if ngModel.$viewValue
$el.bind "scroll", ->
activate = shouldActivateAutoScroll()
scope.$apply ngModel.$setViewValue.bind(ngModel, activate) if activate isnt ngModel.$viewValue
}