From 32bcb1c94319a98f28c646d57669f6e002a990a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Mon, 10 Feb 2014 18:43:52 +0000 Subject: [PATCH 1/6] Fetch more lines on request --- index.html | 6 ++++++ js/glowingbear.js | 39 +++++++++++++++++++++++++++++++++++---- js/models.js | 2 ++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index 4605e00..a7d0234 100644 --- a/index.html +++ b/index.html @@ -282,6 +282,12 @@ $ openssl req -nodes -newkey rsa:2048 -keyout relay.pem -x509 -days 365 -out rel + + + Fetch more lines + Fetching more lines... + +
diff --git a/js/glowingbear.js b/js/glowingbear.js index 4d4af04..62c9a0d 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -24,9 +24,10 @@ weechat.factory('handlers', ['$rootScope', 'models', 'plugins', function($rootSc var handleLine = function(line, initial) { var message = new models.BufferLine(line); + var buffer = models.getBuffer(message.buffer); + buffer.requestedLines++; // Only react to line if its displayed if (message.displayed) { - var buffer = models.getBuffer(message.buffer); message = plugins.PluginManager.contentForMessage(message, $rootScope.visible); buffer.addLine(message); @@ -88,10 +89,11 @@ weechat.factory('handlers', ['$rootScope', 'models', 'plugins', function($rootSc * * (lineinfo) messages are specified by this client. It is request after bufinfo completes */ - var handleLineInfo = function(message) { + var handleLineInfo = function(message, initial) { var lines = message.objects[0].content.reverse(); + if (initial === undefined) initial = true; lines.forEach(function(l) { - handleLine(l, true); + handleLine(l, initial); }); }; @@ -381,13 +383,37 @@ function($rootScope, })); }; + var getMoreLines = function(numLines) { + var buffer = models.getActiveBuffer(); + if (numLines === undefined) { + var numLines = Math.max(40, buffer.requestedLines * 2); + } + + $rootScope.loadingLines = true; + ngWebsockets.send( + weeChat.Protocol.formatHdata({ + path: "buffer:0x" + buffer.id + "/own_lines/last_line(-" + numLines + ")/data", + keys: [] + }) + ).then(function(lineinfo) { + // delete old lines and add new ones + var oldLength = buffer.lines.length; + buffer.lines.length = 0; + buffer.requestedLines = 0; + handlers.handleLineInfo(lineinfo, false); + buffer.lastSeen = buffer.lines.length - oldLength - 1; + $rootScope.loadingLines = false; + }); + } + return { // send: send, connect: connect, disconnect: disconnect, sendMessage: sendMessage, - sendCoreCommand: sendCoreCommand + sendCoreCommand: sendCoreCommand, + getMoreLines: getMoreLines }; }]); @@ -562,6 +588,11 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', } }; + $rootScope.loadingLines = false; + $scope.fetchMoreLines = function(numLines) { + connection.getMoreLines(numLines); + } + $rootScope.scrollWithBuffer = function(nonIncremental) { // First, get scrolling status *before* modification // This is required to determine where we were in the buffer pre-change diff --git a/js/models.js b/js/models.js index 655fd6d..8c3cb38 100644 --- a/js/models.js +++ b/js/models.js @@ -18,6 +18,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) var local_variables = message.local_vars; var notify = 3; // Default 3 == message var lines = []; + var requestedLines = 0; var nicklist = {}; var flatnicklist = []; var history = []; @@ -153,6 +154,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) number: number, title: title, lines: lines, + requestedLines: requestedLines, addLine: addLine, lastSeen: lastSeen, unread: unread, From 5a90e2e830d26bf76e5fea8cd9ca52b69759e545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Mon, 10 Feb 2014 18:43:59 +0000 Subject: [PATCH 2/6] Fix scrolling issue and reduce the number of scrolling events on startup Don't trigger scrolling on each line when batch loading lines --- js/glowingbear.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index 62c9a0d..1a614db 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -22,7 +22,7 @@ weechat.factory('handlers', ['$rootScope', 'models', 'plugins', function($rootSc models.closeBuffer(buffer); }; - var handleLine = function(line, initial) { + var handleLine = function(line, initial, loadingMoreLines) { var message = new models.BufferLine(line); var buffer = models.getBuffer(message.buffer); buffer.requestedLines++; @@ -35,7 +35,7 @@ weechat.factory('handlers', ['$rootScope', 'models', 'plugins', function($rootSc buffer.lastSeen++; } - if (buffer.active) { + if (buffer.active && !initial && !loadingMoreLines) { $rootScope.scrollWithBuffer(); } @@ -89,11 +89,11 @@ weechat.factory('handlers', ['$rootScope', 'models', 'plugins', function($rootSc * * (lineinfo) messages are specified by this client. It is request after bufinfo completes */ - var handleLineInfo = function(message, initial) { + var handleLineInfo = function(message, initial, loadingMoreLines) { var lines = message.objects[0].content.reverse(); if (initial === undefined) initial = true; lines.forEach(function(l) { - handleLine(l, initial); + handleLine(l, initial, loadingMoreLines); }); }; @@ -293,6 +293,7 @@ function($rootScope, }) ).then(function(lineinfo) { handlers.handleLineInfo(lineinfo); + $rootScope.scrollWithBuffer(true); }); ngWebsockets.send( @@ -400,9 +401,10 @@ function($rootScope, var oldLength = buffer.lines.length; buffer.lines.length = 0; buffer.requestedLines = 0; - handlers.handleLineInfo(lineinfo, false); + handlers.handleLineInfo(lineinfo, false, true); buffer.lastSeen = buffer.lines.length - oldLength - 1; $rootScope.loadingLines = false; + $rootScope.scrollWithBuffer(true); }); } @@ -608,7 +610,7 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', var readmarker = document.getElementById('readmarker'); if (nonIncremental && readmarker) { // Switching channels, scroll to read marker - readmarker.scrollIntoView(); + readmarker.scrollIntoViewIfNeeded(); } else { // New message, scroll with buffer (i.e. to bottom) bl.scrollTop = bl.scrollHeight - bl.clientHeight; From 7764fc6910bf7a6ec74f521bdc63999597775be2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Mon, 10 Feb 2014 18:44:15 +0000 Subject: [PATCH 3/6] Dynamically load lines for each buffer on startup Add setting for number of lines to fetch initally. TODO: styling --- index.html | 15 ++++++++++----- js/glowingbear.js | 27 ++++++++++----------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/index.html b/index.html index a7d0234..fe10c2e 100644 --- a/index.html +++ b/index.html @@ -78,11 +78,6 @@ -
- - -

Enter number of lines to sync from WeeChat on connect

-
@@ -250,6 +245,16 @@ $ openssl req -nodes -newkey rsa:2048 -keyout relay.pem -x509 -days 365 -out rel +
  • +
    +
    + +
    +
    +
  • diff --git a/js/glowingbear.js b/js/glowingbear.js index 1a614db..d63dde5 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -286,16 +286,6 @@ function($rootScope, }); // Send all the other commands required for initialization - ngWebsockets.send( - weeChat.Protocol.formatHdata({ - path: "buffer:gui_buffers(*)/own_lines/last_line(-"+storage.get('lines')+")/data", - keys: [] - }) - ).then(function(lineinfo) { - handlers.handleLineInfo(lineinfo); - $rootScope.scrollWithBuffer(true); - }); - ngWebsockets.send( weeChat.Protocol.formatHdata({ path: "hotlist:gui_hotlist(*)", @@ -384,11 +374,9 @@ function($rootScope, })); }; - var getMoreLines = function(numLines) { + var fetchMoreLines = function(numLines) { var buffer = models.getActiveBuffer(); - if (numLines === undefined) { - var numLines = Math.max(40, buffer.requestedLines * 2); - } + numLines = Math.max(numLines, buffer.requestedLines * 2); $rootScope.loadingLines = true; ngWebsockets.send( @@ -415,7 +403,7 @@ function($rootScope, disconnect: disconnect, sendMessage: sendMessage, sendCoreCommand: sendCoreCommand, - getMoreLines: getMoreLines + fetchMoreLines: fetchMoreLines }; }]); @@ -476,6 +464,11 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', $rootScope.scrollWithBuffer(true); var ab = models.getActiveBuffer(); + if (ab.requestedLines < $scope.lines) { + // buffer has not been loaded + // some messages may be present if they arrived after we connected + $scope.fetchMoreLines($scope.lines); + } $rootScope.pageTitle = ab.shortName + ' | ' + ab.title; // If user wants to sync hotlist with weechat @@ -591,8 +584,8 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', }; $rootScope.loadingLines = false; - $scope.fetchMoreLines = function(numLines) { - connection.getMoreLines(numLines); + $scope.fetchMoreLines = function() { + connection.fetchMoreLines($scope.lines); } $rootScope.scrollWithBuffer = function(nonIncremental) { From 40716ca64903676789621f4cf17d6c2c15408fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Mon, 10 Feb 2014 19:24:10 +0000 Subject: [PATCH 4/6] Fix issue with incorrect read marker on startup --- js/glowingbear.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index d63dde5..a4d6ae4 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -390,7 +390,14 @@ function($rootScope, buffer.lines.length = 0; buffer.requestedLines = 0; handlers.handleLineInfo(lineinfo, false, true); - buffer.lastSeen = buffer.lines.length - oldLength - 1; + if (oldLength > 0) { + // We're not initially loading lines into the buffer. + // Set the read marker to the beginning of the newly loaded lines + buffer.lastSeen = buffer.lines.length - oldLength - 1; + } else { + // Initial buffer open, set correct read marker position + buffer.lastSeen += buffer.lines.length; + } $rootScope.loadingLines = false; $rootScope.scrollWithBuffer(true); }); From 56055d61c8c80c0a5460264207396e1a02d3408c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Mon, 10 Feb 2014 19:32:00 +0000 Subject: [PATCH 5/6] Make the code a little prettier --- js/glowingbear.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index a4d6ae4..e53e2ef 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -376,11 +376,15 @@ function($rootScope, var fetchMoreLines = function(numLines) { var buffer = models.getActiveBuffer(); + // Calculate number of lines to fetch, at least as many as the parameter numLines = Math.max(numLines, buffer.requestedLines * 2); + // Indicator that we are loading lines, hides "load more lines" link $rootScope.loadingLines = true; + // Send hdata request to fetch lines for this particular buffer ngWebsockets.send( weeChat.Protocol.formatHdata({ + // "0x" is important, otherwise it won't work path: "buffer:0x" + buffer.id + "/own_lines/last_line(-" + numLines + ")/data", keys: [] }) @@ -390,6 +394,7 @@ function($rootScope, buffer.lines.length = 0; buffer.requestedLines = 0; handlers.handleLineInfo(lineinfo, false, true); + if (oldLength > 0) { // We're not initially loading lines into the buffer. // Set the read marker to the beginning of the newly loaded lines @@ -399,13 +404,13 @@ function($rootScope, buffer.lastSeen += buffer.lines.length; } $rootScope.loadingLines = false; + // Scroll read marker to the center of the screen $rootScope.scrollWithBuffer(true); }); - } + }; return { -// send: send, connect: connect, disconnect: disconnect, sendMessage: sendMessage, @@ -472,8 +477,7 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', var ab = models.getActiveBuffer(); if (ab.requestedLines < $scope.lines) { - // buffer has not been loaded - // some messages may be present if they arrived after we connected + // buffer has not been loaded, but some lines may already be present if they arrived after we connected $scope.fetchMoreLines($scope.lines); } $rootScope.pageTitle = ab.shortName + ' | ' + ab.title; @@ -593,7 +597,7 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', $rootScope.loadingLines = false; $scope.fetchMoreLines = function() { connection.fetchMoreLines($scope.lines); - } + }; $rootScope.scrollWithBuffer = function(nonIncremental) { // First, get scrolling status *before* modification From 8068be8de20ceb70efda39b142f03ac479c5317b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Mon, 10 Feb 2014 20:17:51 +0000 Subject: [PATCH 6/6] Remove setting and calculate number of lines from display properties --- index.html | 18 ++++++------------ js/glowingbear.js | 10 +++++++++- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/index.html b/index.html index fe10c2e..41f8137 100644 --- a/index.html +++ b/index.html @@ -245,16 +245,6 @@ $ openssl req -nodes -newkey rsa:2048 -keyout relay.pem -x509 -days 365 -out rel -
  • -
    -
    - -
    -
    -
  • @@ -289,8 +279,12 @@ $ openssl req -nodes -newkey rsa:2048 -keyout relay.pem -x509 -days 365 -out rel - Fetch more lines - Fetching more lines... + + + diff --git a/js/glowingbear.js b/js/glowingbear.js index e53e2ef..148957b 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -535,7 +535,6 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', $store.bind($scope, "port", "9001"); $store.bind($scope, "proto", "weechat"); $store.bind($scope, "ssl", false); - $store.bind($scope, "lines", "40"); $store.bind($scope, "savepassword", false); if ($scope.savepassword) { $store.bind($scope, "password", ""); @@ -594,6 +593,15 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', } }; + + // Calculate number of lines to fetch + $scope.lines = function() { + var lineHeight = document.querySelector(".bufferline").clientHeight; + // I would have used document.querySelector("#bufferlines").clientHeight and added 5 to the total result, but that provides incorrect values on mobile + var areaHeight = document.body.clientHeight; + return Math.ceil(areaHeight/lineHeight); + }(); + $rootScope.loadingLines = false; $scope.fetchMoreLines = function() { connection.fetchMoreLines($scope.lines);
    + Fetch more lines + Fetching more lines... +