From b6558c68e830f45418a2429182d1b23c78599e78 Mon Sep 17 00:00:00 2001 From: alexfriesen Date: Thu, 2 Jun 2016 10:18:57 +0200 Subject: [PATCH] only scroll if already on bottom in console and cleaned some code --- js/console.js | 67 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/js/console.js b/js/console.js index abd19c2..699bafc 100644 --- a/js/console.js +++ b/js/console.js @@ -65,23 +65,52 @@ function ConsoleController( $scope, rconService, $timeout ) $scope.OnMessage = function( msg ) { - if ( msg.Message.startsWith( "[rcon] " ) ) return; - if ( msg.Type != "Generic" && msg.Type != "Log" && msg.Type != "Error" && msg.Type != "Warning" ) - { - console.log( msg ); - return; + if ( msg.Message.startsWith( "[rcon] " ) ) { + return; } - msg.Class = msg.Type; - $scope.Output.push( msg ); + switch(msg.Type) { + case 'Generic': + case 'Log': + case 'Error': + case 'Warning': + $scope.addOutput(msg); + break; - $timeout( $scope.ScrollToBottom, 50 ); + default: + console.log( msg ); + return; + } } $scope.ScrollToBottom = function() { - // TODO - don't scroll if we're not at the bottom ! - $( "#ConsoleController .Output" ).scrollTop( $( "#ConsoleController .Output" )[0].scrollHeight ); + var element = $( "#ConsoleController .Output" ); + + $timeout( function() { + element.scrollTop( element.prop('scrollHeight') ); + }, 50 ); + } + + $scope.isOnBottom = function() + { + // get jquery element + var element = $( "#ConsoleController .Output" ); + + // height of the element + var height = element.height(); + + // scroll position from top position + var scrollTop = element.scrollTop(); + + // full height of the element + var scrollHeight = element.prop('scrollHeight'); + + if((scrollTop + height) > (scrollHeight - 10)) { + return true; + } + + return false; } // @@ -90,15 +119,27 @@ function ConsoleController( $scope, rconService, $timeout ) // $scope.GetHistory = function () { - console.log( "GetHistory" ); - rconService.Request( "console.tail 128", $scope, function ( msg ) { var messages = JSON.parse( msg.Message ); - messages.forEach( function ( x ) { $scope.OnMessage( x ); } ); + messages.forEach( function ( msg ) { + $scope.OnMessage( msg ); + }); + + $scope.ScrollToBottom(); } ); } + $scope.addOutput = function (msg) + { + msg.Class = msg.Type; + $scope.Output.push( msg ); + + if($scope.isOnBottom()) { + $scope.ScrollToBottom(); + } + } + rconService.InstallService( $scope, $scope.GetHistory ) } \ No newline at end of file