make faction chat display work completely

This commit is contained in:
Stefan Breunig
2013-02-03 14:20:21 +01:00
parent cd57c5a6d3
commit a7db7e656a
3 changed files with 101 additions and 15 deletions

View File

@ -1,6 +1,7 @@
window.chat = function() {};
window.chat.getOldestTimestamp = function(public) {
if(chat._needsClearing) return -1;
if(public) {
var a = $('#chatpublic time:first').data('timestamp');
var b = $('#chatbot time:first').data('timestamp');
@ -12,6 +13,7 @@ window.chat.getOldestTimestamp = function(public) {
}
window.chat.getNewestTimestamp = function(public) {
if(chat._needsClearing) return -1;
if(public) {
var a = $('#chatpublic time:last').data('timestamp');
var b = $('#chatbot time:last').data('timestamp');
@ -22,13 +24,21 @@ window.chat.getNewestTimestamp = function(public) {
}
}
window.chat._needsClearing = false;
window.chat._oldBBox = null;
window.chat.genPostData = function(public, getOlderMsgs) {
if(typeof public !== 'boolean') throw('Need to know if public or faction chat.');
var b = map.getBounds();
chat._localRangeCircle.setLatLng(map.getCenter());
var b = map.getBounds().extend(chat._localRangeCircle.getBounds());
var bbs = b.toBBoxString();
chat._needsClearing = chat._oldBBox && chat._oldBBox !== bbs;
if(chat._needsClearing) console.log('Bounding Box changed, chat will be cleared (old: '+chat._oldBBox+' ; new: '+bbs+' )');
chat._oldBBox = bbs;
var ne = b.getNorthEast();
var sw = b.getSouthWest();
var data = {
desiredNumItems: 10,
minLatE6: Math.round(sw.lat*1E6),
@ -124,7 +134,12 @@ window.chat.handleNewFaction = function(data, textStatus, jqXHR) {
window.chat._displayedFactionGuids = [];
window.chat.handleFaction = function(data, textStatus, jqXHR, isOldMsgs) {
if(!data || !data.result) return console.warn('Couldnt get chat data. Pausing chat for now.');
if(!data || !data.result) {
window.failedRequestCount++;
return console.warn('Couldnt get chat data. Waiting for next auto-refresh.');
}
chat.clearIfRequired();
var msgs = '';
var prevTime = null;
@ -143,14 +158,20 @@ window.chat.handleFaction = function(data, textStatus, jqXHR, isOldMsgs) {
var nowTime = new Date(time).toLocaleDateString();
if(prevTime && prevTime !== nowTime)
msgs += '<summary>'+nowTime+'</summary>';
msgs += chat.renderDivider(nowTime);
msgs += chat.renderMsg(msg, nick, time, team);
prevTime = nowTime;
});
// if there is a change of day between two requests, handle the
// divider insertion here.
if(isOldMsgs) {
var nextTime = new Date($('#chatfaction time:last').data('timestamp')).toLocaleDateString();
if(prevTime && prevTime !== nextTime)
msgs += chat.renderDivider(nextTime);
}
var c = $('#chatfaction');
var scrollBefore = scrollBottom(c);
if(isOldMsgs)
@ -171,6 +192,19 @@ window.chat.handleFaction = function(data, textStatus, jqXHR, isOldMsgs) {
chat.needMoreMessages();
}
window.chat.clear = function() {
console.log('clearing now');
window.chat._displayedFactionGuids = [];
window.chat._displayedPublicGuids = [];
$('#chatfaction, #chatpublic, #chatbot').data('ignoreNextScroll', true).html('');
}
window.chat.clearIfRequired = function() {
if(!chat._needsClearing) return;
chat.clear();
chat._needsClearing = false;
}
window.chat.toggle = function() {
var c = $('#chat, #chatcontrols');
if(c.hasClass('expand')) {
@ -215,6 +249,8 @@ window.chat.setupTime = function() {
}
window.chat.setup = function() {
window.chat._localRangeCircle = L.circle(map.getCenter(), CHAT_MIN_RANGE*1000);
$('#chatcontrols, #chat, #chatinput').show();
$('#chatcontrols a:first').click(window.chat.toggle);
@ -238,7 +274,6 @@ window.chat.setup = function() {
if(scrollBottom(t) === 0) chat.requestNewPublic();
});
chat.requestNewFaction();
window.addResumeFunction(chat.request);
window.requests.addRefreshFunction(chat.request);
@ -252,3 +287,7 @@ window.chat.renderMsg = function(msg, nick, time, team) {
var s = 'style="color:'+COLORS[team]+'"';
return '<p>'+t+'<mark '+s+'>'+nick+'</mark><span>'+msg+'</span></p>';
}
window.chat.renderDivider = function(text) {
return '<summary>─ '+text+' ────────────────────────────────────────────────────────────────────────────</summary>';
}