Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/scrollglue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

(function(angular, undefined){
'use strict';

angular.module('luegg.directives', [])
.directive('scrollGlue', ['$parse', function($parse){
function unboundState(initValue){
Expand Down Expand Up @@ -58,20 +58,41 @@
link: function(scope, $el, attrs){
var el = $el[0],
activationState = createActivationState(attrs.scrollGlue, scope);

var scrollParent = false;
var height = 0;
function scrollToBottom(){
if (scrollParent) {
if (el.parentElement.scrollHeight > 0) {
el.parentElement.scrollTop = height;
} else {
document.body.scrollTop = height;
}
} else {
el.scrollTop = el.scrollHeight;
}
}

function onScopeChanges(scope){
if(activationState.getValue() && !shouldActivateAutoScroll()){
if(activationState.getValue() && shouldActivateAutoScroll()){
scrollToBottom();
}
}

function shouldActivateAutoScroll(){
// + 1 catches off by one errors in chrome
return el.scrollTop + el.clientHeight + 1 >= el.scrollHeight;
var result = el.scrollTop + el.offsetHeight + 1 <= el.scrollHeight;
if (!result) {
var scrollHeight = el.parentElement.scrollHeight;
if (scrollHeight === 0) {
scrollHeight = document.body.scrollHeight;
}
height = scrollHeight - window.innerHeight;
result = height !== 0;
scrollParent = true;
} else {
scrollParent = false;
}
return result;
}

function onScroll(){
Expand All @@ -83,4 +104,4 @@
}
};
}]);
}(angular));
}(angular));