From 32b5e396813f7f9899b8a3275120ea886c8ad130 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Tue, 8 Jul 2014 11:56:44 +0100 Subject: [PATCH] chat scoll works fairly well using scroll glue --- .../web/app/views/project/editor/chat.jade | 2 +- .../coffee/app/directives/scroll.coffee | 38 ++++++++++++++----- .../coffee/app/directives/scrollGlue.coffee | 34 ----------------- .../chat/controllers/ChatController.coffee | 1 - 4 files changed, 30 insertions(+), 45 deletions(-) delete mode 100644 services/web/public/coffee/app/directives/scrollGlue.coffee diff --git a/services/web/app/views/project/editor/chat.jade b/services/web/app/views/project/editor/chat.jade index 6ed0e5ac2b..244f15a8f5 100644 --- a/services/web/app/views/project/editor/chat.jade +++ b/services/web/app/views/project/editor/chat.jade @@ -8,7 +8,7 @@ aside.chat( infinite-scroll="loadMore()" infinite-scroll-disabled="chat.loading || chat.atEnd" infinite-scroll-initialize="ui.chatOpen" - scroll-to-bottom-on="message:new" + scroll-glue ) .infinite-scroll-inner ul.list-unstyled diff --git a/services/web/public/coffee/app/directives/scroll.coffee b/services/web/public/coffee/app/directives/scroll.coffee index 43a9e192f0..d3ba6ebe0c 100644 --- a/services/web/public/coffee/app/directives/scroll.coffee +++ b/services/web/public/coffee/app/directives/scroll.coffee @@ -1,14 +1,34 @@ define [ "base" ], (App) -> - App.directive 'scrollToBottomOn', () -> + + fakeNgModel = (initValue) -> + $setViewValue: (value) -> + @$viewValue = value + return + + $viewValue: initValue + + App.directive "scrollGlue", -> return { - link: (scope, element, attrs) -> - element = element[0] - eventToScrollOn = attrs.scrollToBottomOn - console.log eventToScrollOn - scope.$on eventToScrollOn, ()-> - console.log element.scrollHeight, element.scrollTop - element.scrollTop = element.scrollHeight*4 - console.log element.scrollHeight, element.scrollTop + + priority: 1 + require: ["?ngModel"] + restrict: "A" + + link: (scope, $el, attrs, ctrls) -> + scrollToBottom = -> + el.scrollTop = el.scrollHeight + + shouldActivateAutoScroll = -> + el.scrollTop + el.clientHeight + 10 >= 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 } diff --git a/services/web/public/coffee/app/directives/scrollGlue.coffee b/services/web/public/coffee/app/directives/scrollGlue.coffee deleted file mode 100644 index 777ef342e9..0000000000 --- a/services/web/public/coffee/app/directives/scrollGlue.coffee +++ /dev/null @@ -1,34 +0,0 @@ -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 - } diff --git a/services/web/public/coffee/app/ide/chat/controllers/ChatController.coffee b/services/web/public/coffee/app/ide/chat/controllers/ChatController.coffee index 77d95ccea7..d9a41568d8 100644 --- a/services/web/public/coffee/app/ide/chat/controllers/ChatController.coffee +++ b/services/web/public/coffee/app/ide/chat/controllers/ChatController.coffee @@ -6,7 +6,6 @@ define [ $scope.$on "project:joined", => @ide.socket.on "new-chat-message", (message) => - $scope.$emit "message:new" $scope.chat.messages.push(message) $http.get(MESSAGES_URL).success (data, status, headers, config)->