From c1fac59618294c3da8a747c2cfdd387b161c6433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Sat, 28 Jan 2012 15:40:33 +0100 Subject: [PATCH] Automagically disable scrolling when user scrolls --- file/js/TimWolla.WCF.Chat.coffee | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/file/js/TimWolla.WCF.Chat.coffee b/file/js/TimWolla.WCF.Chat.coffee index 425d2ba..3215b90 100644 --- a/file/js/TimWolla.WCF.Chat.coffee +++ b/file/js/TimWolla.WCF.Chat.coffee @@ -18,6 +18,7 @@ TimWolla.WCF ?= {} isActive: true autocompleteOffset: 0 autocompleteValue: null + scrollHeightTopDiff: null events: newMessage: $.Callbacks() userMenu: $.Callbacks() @@ -72,6 +73,7 @@ TimWolla.WCF ?= {} , @ $('#chatInput').keydown $.proxy (event) -> + # tab key if event.keyCode is 9 event.preventDefault() if @autocompleteValue is null @@ -104,6 +106,12 @@ TimWolla.WCF ?= {} element.data 'status', 1 icon.attr 'src', icon.attr('src').replace /disabled(\d?).([a-z]{3})$/, 'enabled$1.$2' element.attr 'title', element.data 'disableMessage' + + $('#chatAutoscroll').click (event) -> + $(this).removeClass('hot') + if $(this).data 'status' + $('.chatMessageContainer').scrollTop $('.chatMessageContainer ul').height() + if typeof window.webkitNotifications isnt 'undefined' $('#chatNotify').click (event) -> window.webkitNotifications.requestPermission() if $(this).data 'status' @@ -182,7 +190,7 @@ TimWolla.WCF ?= {} fish.animate top: '+=' + top left: '+=' + left - , 1000 + , 1e3 , 1.5e3); ### # Loads new messages. @@ -201,6 +209,13 @@ TimWolla.WCF ?= {} # @param array messages ### handleMessages: (messages) -> + # disable scrolling automagically when user manually scrolled + if @scrollHeightTopDiff isnt null + if @scrollHeightTopDiff isnt $('.chatMessageContainer ul').height() - $('.chatMessageContainer').scrollTop() + if $('#chatAutoscroll').data('status') is 1 + $('#chatAutoscroll').click() + $('#chatAutoscroll').addClass('hot').fadeOut('slow').fadeIn('slow') + for message in messages @events.newMessage.fire message @@ -211,9 +226,11 @@ TimWolla.WCF ?= {} li.append output li.appendTo $ '.chatMessageContainer ul' - $('.chatMessageContainer').animate - scrollTop: $('.chatMessageContainer ul').height() - , 1000 + + # autoscroll down + if $('#chatAutoscroll').data('status') is 1 + $('.chatMessageContainer').scrollTop $('.chatMessageContainer ul').height() + @scrollHeightTopDiff = $('.chatMessageContainer ul').height() - $('.chatMessageContainer').scrollTop() handleUsers: (users) -> foundUsers = {} for user in users