Commit d0781023 authored by Vladimir Baranov's avatar Vladimir Baranov

rref #5392. Stop loading if all messages loaded, keep scroll position when new messages loaded

parent 4e1ce1bd
...@@ -45,6 +45,7 @@ angular.module("ulakbus.messaging") ...@@ -45,6 +45,7 @@ angular.module("ulakbus.messaging")
function reset(){ function reset(){
iScope.selectedChannel = null; iScope.selectedChannel = null;
iScope.allMessagesLoaded = false;
iScope.publicChannels = []; iScope.publicChannels = [];
iScope.notificationsChannel = []; iScope.notificationsChannel = [];
iScope.directChannels = []; iScope.directChannels = [];
...@@ -270,6 +271,8 @@ angular.module("ulakbus.messaging") ...@@ -270,6 +271,8 @@ angular.module("ulakbus.messaging")
} }
iScope.selectChannel = function(channel, silent){ iScope.selectChannel = function(channel, silent){
// enable channel history loading
iScope.allMessagesLoaded = false;
var channelKey = getKey(channel); var channelKey = getKey(channel);
selectChannel(channelKey, silent).then(function(result){ selectChannel(channelKey, silent).then(function(result){
iScope.selectedChannel = result; iScope.selectedChannel = result;
...@@ -367,11 +370,18 @@ angular.module("ulakbus.messaging") ...@@ -367,11 +370,18 @@ angular.module("ulakbus.messaging")
}; };
iScope.loadMore = function(){ iScope.loadMore = function(){
if (iScope.allMessagesLoaded) return;
if (iScope.selectedChannel.messages.length > 0){ if (iScope.selectedChannel.messages.length > 0){
var first = iScope.selectedChannel.messages[0]; var first = iScope.selectedChannel.messages[0];
return MessagingService.channel_history(iScope.selectedChannel, first.timestamp) return MessagingService.channel_history(getKey(iScope.selectedChannel), first.timestamp)
.then(function(result){ .then(function(result){
console.error("RES: ", result); var messages = iScope.selectedChannel.messages;
if (result.messages.length == 0){
iScope.allMessagesLoaded = true;
return;
}
// prepend loaded messages to current channel messages list
messages.unshift.apply(messages, result.messages);
}); });
} }
}; };
...@@ -433,6 +443,7 @@ angular.module("ulakbus.messaging") ...@@ -433,6 +443,7 @@ angular.module("ulakbus.messaging")
}) })
.directive("loadMoreTop", function($compile, $timeout, $q) { .directive("loadMoreTop", function($compile, $timeout, $q) {
// centered loader
var loaderTpl = $compile('<div class="loader" style="float: none; margin: auto; margin-top: 10px;" ng-show="loading"></div>'); var loaderTpl = $compile('<div class="loader" style="float: none; margin: auto; margin-top: 10px;" ng-show="loading"></div>');
return { return {
scope: { scope: {
...@@ -442,18 +453,31 @@ angular.module("ulakbus.messaging") ...@@ -442,18 +453,31 @@ angular.module("ulakbus.messaging")
var elem = $(iElem); var elem = $(iElem);
iElem.prepend(angular.element(loaderTpl(iScope))); iElem.prepend(angular.element(loaderTpl(iScope)));
iScope.loading = false; iScope.loading = false;
elem.scroll(function(e){
function onScroll(){
var scrollTop = elem.scrollTop(); var scrollTop = elem.scrollTop();
if (scrollTop <= 0 && !iScope.loading){ if (scrollTop <= 0 && !iScope.loading){
if (iScope.loadMoreCallback){ if (iScope.loadMoreCallback){
// save last top element with id position
var id = elem.find("[id]").first().attr('id');
$timeout(function(){iScope.loading = true}); $timeout(function(){iScope.loading = true});
$q.when(iScope.loadMoreCallback()) $q.when(iScope.loadMoreCallback())
.finally(function(){ .finally(function(){
$timeout(function(){iScope.loading = false}); $timeout(function(){
iScope.loading = false;
// try to restore last scroll position;
var lastTopElem = elem.find("#"+id);
if (lastTopElem){
var top = lastTopElem.offset().top - elem.offset().top - 100;
elem.scrollTop(top);
}
});
}) })
} }
} }
}); }
elem.scroll(onScroll);
} }
} }
}) })
......
...@@ -343,6 +343,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -343,6 +343,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
}; };
return wsRequest(outgoing).then(function(result){ return wsRequest(outgoing).then(function(result){
$log.info("Load channel ", channelKey, "history: ", result); $log.info("Load channel ", channelKey, "history: ", result);
prepareMessages(result.messages);
return result; return result;
}) })
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment