handle clearing differently to avoid clearing text before new one is available. Also throw some more magic at scrolling in chat so it works better.

This commit is contained in:
Stefan Breunig
2013-02-04 15:03:24 +01:00
parent f85a3b2443
commit 302de091e6
2 changed files with 107 additions and 75 deletions

View File

@ -17,37 +17,36 @@ window.chat.getNewestTimestamp = function(public) {
return chat['_new'+(public ? 'Public' : 'Faction')+'Timestamp']; return chat['_new'+(public ? 'Public' : 'Faction')+'Timestamp'];
} }
window.chat._needsClearing = false; window.chat.clearIfRequired = function(elm) {
window.chat.clear = function() { if(!elm.data('needsClearing')) return;
console.log('clearing now'); elm.data('ignoreNextScroll', true).data('needsClearing', false).html('');
window.chat._displayedFactionGuids = [];
window.chat._displayedPublicGuids = [];
window.chat._displayedPlayerActionTime = {};
window.chat._oldFactionTimestamp = -1;
window.chat._newFactionTimestamp = -1;
window.chat._oldPublicTimestamp = -1;
window.chat._newPublicTimestamp = -1;
$('#chatfaction, #chatpublic, #chatautomated').data('ignoreNextScroll', true).html('');
} }
window.chat.clearIfRequired = function() {
if(!chat._needsClearing) return;
chat.clear();
chat._needsClearing = false;
}
window.chat._oldBBox = null; window.chat._oldBBox = null;
window.chat.genPostData = function(public, getOlderMsgs) { window.chat.genPostData = function(public, getOlderMsgs) {
if(typeof public !== 'boolean') throw('Need to know if public or faction chat.'); if(typeof public !== 'boolean') throw('Need to know if public or faction chat.');
chat._localRangeCircle.setLatLng(map.getCenter()); chat._localRangeCircle.setLatLng(map.getCenter());
var b = map.getBounds().extend(chat._localRangeCircle.getBounds()); var b = map.getBounds().extend(chat._localRangeCircle.getBounds());
var ne = b.getNorthEast();
var sw = b.getSouthWest();
var bbs = b.toBBoxString(); // round bounds in order to ignore rounding errors
chat._needsClearing = chat._needsClearing || chat._oldBBox && chat._oldBBox !== bbs; var bbs = $.map([ne.lat, ne.lng, sw.lat, sw.lng], function(x) { return Math.round(x*1E4) }).join();
if(chat._needsClearing) console.log('Bounding Box changed, chat will be cleared (old: '+chat._oldBBox+' ; new: '+bbs+' )'); if(chat._oldBBox && chat._oldBBox !== bbs) {
$('#chat > div').data('needsClearing', true);
console.log('Bounding Box changed, chat will be cleared (old: '+chat._oldBBox+' ; new: '+bbs+' )');
// need to reset these flags now because clearing will only occur
// after the request is finished i.e. there would be one almost
// useless request.
chat._displayedFactionGuids = [];
chat._displayedPublicGuids = [];
chat._displayedPlayerActionTime = {};
chat._oldFactionTimestamp = -1;
chat._newFactionTimestamp = -1;
chat._oldPublicTimestamp = -1;
chat._newPublicTimestamp = -1;
}
chat._oldBBox = bbs; chat._oldBBox = bbs;
var ne = b.getNorthEast(); var ne = b.getNorthEast();
@ -157,15 +156,14 @@ window.chat.handleFaction = function(data, textStatus, jqXHR, isOldMsgs) {
return console.warn('faction chat error. Waiting for next auto-refresh.'); return console.warn('faction chat error. Waiting for next auto-refresh.');
} }
chat.clearIfRequired(); var c = $('#chatfaction');
chat.clearIfRequired(c);
if(data.result.length === 0) return; if(data.result.length === 0) return;
chat._newFactionTimestamp = data.result[0][1]; chat._newFactionTimestamp = data.result[0][1];
chat._oldFactionTimestamp = data.result[data.result.length-1][1]; chat._oldFactionTimestamp = data.result[data.result.length-1][1];
var c = $('#chatfaction');
var scrollBefore = scrollBottom(c); var scrollBefore = scrollBottom(c);
chat.renderPlayerMsgsTo(true, data, isOldMsgs, chat._displayedFactionGuids); chat.renderPlayerMsgsTo(true, data, isOldMsgs, chat._displayedFactionGuids);
chat.keepScrollPosition(c, scrollBefore, isOldMsgs); chat.keepScrollPosition(c, scrollBefore, isOldMsgs);
@ -242,22 +240,25 @@ window.chat.handlePublic = function(data, textStatus, jqXHR, isOldMsgs) {
return console.warn('public chat error. Waiting for next auto-refresh.'); return console.warn('public chat error. Waiting for next auto-refresh.');
} }
chat.clearIfRequired(); var ca = $('#chatautomated');
var cp = $('#chatpublic');
chat.clearIfRequired(ca);
chat.clearIfRequired(cp);
if(data.result.length === 0) return; if(data.result.length === 0) return;
chat._newPublicTimestamp = data.result[0][1]; chat._newPublicTimestamp = data.result[0][1];
chat._oldPublicTimestamp = data.result[data.result.length-1][1]; chat._oldPublicTimestamp = data.result[data.result.length-1][1];
var c = $('#chatautomated');
var scrollBefore = scrollBottom(c);
chat.handlePublicAutomated(data);
chat.keepScrollPosition(c, scrollBefore, isOldMsgs);
c = $('#chatpublic'); var scrollBefore = scrollBottom(ca);
var scrollBefore = scrollBottom(c); chat.handlePublicAutomated(data);
chat.keepScrollPosition(ca, scrollBefore, isOldMsgs);
var scrollBefore = scrollBottom(cp);
chat.renderPlayerMsgsTo(false, data, isOldMsgs, chat._displayedPublicGuids); chat.renderPlayerMsgsTo(false, data, isOldMsgs, chat._displayedPublicGuids);
chat.keepScrollPosition(c, scrollBefore, isOldMsgs); chat.keepScrollPosition(cp, scrollBefore, isOldMsgs);
if(data.result.length >= CHAT_PUBLIC_ITEMS) chat.needMoreMessages(); if(data.result.length >= CHAT_PUBLIC_ITEMS) chat.needMoreMessages();
} }
@ -429,7 +430,7 @@ window.chat.toggle = function() {
c.removeClass('expand'); c.removeClass('expand');
var div = $('#chat > div:visible'); var div = $('#chat > div:visible');
div.data('ignoreNextScroll', true); div.data('ignoreNextScroll', true);
div.scrollTop(9999999999999); // scroll to bottom div.scrollTop(99999999); // scroll to bottom
} else { } else {
$('#chatcontrols a:first').text('shrink'); $('#chatcontrols a:first').text('shrink');
c.addClass('expand'); c.addClass('expand');
@ -468,27 +469,36 @@ window.chat.chooser = function(event) {
$('#chat > div').hide(); $('#chat > div').hide();
var elm;
switch(tt) { switch(tt) {
case 'faction': case 'faction':
span.css('color', ''); span.css('color', '');
span.text('tell faction:'); span.text('tell faction:');
$('#chatfaction').show(); elm = $('#chatfaction');
break; break;
case 'public': case 'public':
span.css('cssText', 'color: red !important'); span.css('cssText', 'color: red !important');
span.text('tell public:'); span.text('tell public:');
$('#chatpublic').show(); elm = $('#chatpublic');
break; break;
case 'automated': case 'automated':
span.css('cssText', 'color: #bbb !important'); span.css('cssText', 'color: #bbb !important');
span.text('tell Jarvis:'); span.text('tell Jarvis:');
chat.renderAutomatedMsgsTo(); chat.renderAutomatedMsgsTo();
$('#chatautomated').show(); elm = $('#chatautomated');
break; break;
} }
elm.show();
if(elm.data('needsScrollTop')) {
elm.data('ignoreNextScroll', true);
elm.scrollTop(elm.data('needsScrollTop'));
elm.data('needsScrollTop', null);
}
chat.needMoreMessages(); chat.needMoreMessages();
} }
@ -500,6 +510,12 @@ window.chat.keepScrollPosition = function(box, scrollBefore, isOldMsgs) {
// when old messages are added. New messages added at the bottom dont // when old messages are added. New messages added at the bottom dont
// change the view and enabling this would make the chat scroll down // change the view and enabling this would make the chat scroll down
// for every added message, even if the user wants to read old stuff. // for every added message, even if the user wants to read old stuff.
if(box.is(':hidden') && !isOldMsgs) {
box.data('needsScrollTop', 99999999);
return;
}
if(scrollBefore === 0 || isOldMsgs) { if(scrollBefore === 0 || isOldMsgs) {
box.data('ignoreNextScroll', true); box.data('ignoreNextScroll', true);
box.scrollTop(box.scrollTop() + (scrollBottom(box)-scrollBefore)); box.scrollTop(box.scrollTop() + (scrollBottom(box)-scrollBefore));

View File

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @id ingress-intel-total-conversion@breunigs // @id ingress-intel-total-conversion@breunigs
// @name intel map total conversion // @name intel map total conversion
// @version 0.2-2013-02-04-142146 // @version 0.2-2013-02-04-150321
// @namespace https://github.com/breunigs/ingress-intel-total-conversion // @namespace https://github.com/breunigs/ingress-intel-total-conversion
// @updateURL https://raw.github.com/breunigs/ingress-intel-total-conversion/gh-pages/total-conversion-build.user.js // @updateURL https://raw.github.com/breunigs/ingress-intel-total-conversion/gh-pages/total-conversion-build.user.js
// @downloadURL https://raw.github.com/breunigs/ingress-intel-total-conversion/gh-pages/total-conversion-build.user.js // @downloadURL https://raw.github.com/breunigs/ingress-intel-total-conversion/gh-pages/total-conversion-build.user.js
@ -879,37 +879,36 @@ window.chat.getNewestTimestamp = function(public) {
return chat['_new'+(public ? 'Public' : 'Faction')+'Timestamp']; return chat['_new'+(public ? 'Public' : 'Faction')+'Timestamp'];
} }
window.chat._needsClearing = false; window.chat.clearIfRequired = function(elm) {
window.chat.clear = function() { if(!elm.data('needsClearing')) return;
console.log('clearing now'); elm.data('ignoreNextScroll', true).data('needsClearing', false).html('');
window.chat._displayedFactionGuids = [];
window.chat._displayedPublicGuids = [];
window.chat._displayedPlayerActionTime = {};
window.chat._oldFactionTimestamp = -1;
window.chat._newFactionTimestamp = -1;
window.chat._oldPublicTimestamp = -1;
window.chat._newPublicTimestamp = -1;
$('#chatfaction, #chatpublic, #chatautomated').data('ignoreNextScroll', true).html('');
} }
window.chat.clearIfRequired = function() {
if(!chat._needsClearing) return;
chat.clear();
chat._needsClearing = false;
}
window.chat._oldBBox = null; window.chat._oldBBox = null;
window.chat.genPostData = function(public, getOlderMsgs) { window.chat.genPostData = function(public, getOlderMsgs) {
if(typeof public !== 'boolean') throw('Need to know if public or faction chat.'); if(typeof public !== 'boolean') throw('Need to know if public or faction chat.');
chat._localRangeCircle.setLatLng(map.getCenter()); chat._localRangeCircle.setLatLng(map.getCenter());
var b = map.getBounds().extend(chat._localRangeCircle.getBounds()); var b = map.getBounds().extend(chat._localRangeCircle.getBounds());
var ne = b.getNorthEast();
var sw = b.getSouthWest();
var bbs = b.toBBoxString(); // round bounds in order to ignore rounding errors
chat._needsClearing = chat._needsClearing || chat._oldBBox && chat._oldBBox !== bbs; var bbs = $.map([ne.lat, ne.lng, sw.lat, sw.lng], function(x) { return Math.round(x*1E4) }).join();
if(chat._needsClearing) console.log('Bounding Box changed, chat will be cleared (old: '+chat._oldBBox+' ; new: '+bbs+' )'); if(chat._oldBBox && chat._oldBBox !== bbs) {
$('#chat > div').data('needsClearing', true);
console.log('Bounding Box changed, chat will be cleared (old: '+chat._oldBBox+' ; new: '+bbs+' )');
// need to reset these flags now because clearing will only occur
// after the request is finished i.e. there would be one almost
// useless request.
chat._displayedFactionGuids = [];
chat._displayedPublicGuids = [];
chat._displayedPlayerActionTime = {};
chat._oldFactionTimestamp = -1;
chat._newFactionTimestamp = -1;
chat._oldPublicTimestamp = -1;
chat._newPublicTimestamp = -1;
}
chat._oldBBox = bbs; chat._oldBBox = bbs;
var ne = b.getNorthEast(); var ne = b.getNorthEast();
@ -1019,15 +1018,14 @@ window.chat.handleFaction = function(data, textStatus, jqXHR, isOldMsgs) {
return console.warn('faction chat error. Waiting for next auto-refresh.'); return console.warn('faction chat error. Waiting for next auto-refresh.');
} }
chat.clearIfRequired(); var c = $('#chatfaction');
chat.clearIfRequired(c);
if(data.result.length === 0) return; if(data.result.length === 0) return;
chat._newFactionTimestamp = data.result[0][1]; chat._newFactionTimestamp = data.result[0][1];
chat._oldFactionTimestamp = data.result[data.result.length-1][1]; chat._oldFactionTimestamp = data.result[data.result.length-1][1];
var c = $('#chatfaction');
var scrollBefore = scrollBottom(c); var scrollBefore = scrollBottom(c);
chat.renderPlayerMsgsTo(true, data, isOldMsgs, chat._displayedFactionGuids); chat.renderPlayerMsgsTo(true, data, isOldMsgs, chat._displayedFactionGuids);
chat.keepScrollPosition(c, scrollBefore, isOldMsgs); chat.keepScrollPosition(c, scrollBefore, isOldMsgs);
@ -1104,22 +1102,25 @@ window.chat.handlePublic = function(data, textStatus, jqXHR, isOldMsgs) {
return console.warn('public chat error. Waiting for next auto-refresh.'); return console.warn('public chat error. Waiting for next auto-refresh.');
} }
chat.clearIfRequired(); var ca = $('#chatautomated');
var cp = $('#chatpublic');
chat.clearIfRequired(ca);
chat.clearIfRequired(cp);
if(data.result.length === 0) return; if(data.result.length === 0) return;
chat._newPublicTimestamp = data.result[0][1]; chat._newPublicTimestamp = data.result[0][1];
chat._oldPublicTimestamp = data.result[data.result.length-1][1]; chat._oldPublicTimestamp = data.result[data.result.length-1][1];
var c = $('#chatautomated');
var scrollBefore = scrollBottom(c);
chat.handlePublicAutomated(data);
chat.keepScrollPosition(c, scrollBefore, isOldMsgs);
c = $('#chatpublic'); var scrollBefore = scrollBottom(ca);
var scrollBefore = scrollBottom(c); chat.handlePublicAutomated(data);
chat.keepScrollPosition(ca, scrollBefore, isOldMsgs);
var scrollBefore = scrollBottom(cp);
chat.renderPlayerMsgsTo(false, data, isOldMsgs, chat._displayedPublicGuids); chat.renderPlayerMsgsTo(false, data, isOldMsgs, chat._displayedPublicGuids);
chat.keepScrollPosition(c, scrollBefore, isOldMsgs); chat.keepScrollPosition(cp, scrollBefore, isOldMsgs);
if(data.result.length >= CHAT_PUBLIC_ITEMS) chat.needMoreMessages(); if(data.result.length >= CHAT_PUBLIC_ITEMS) chat.needMoreMessages();
} }
@ -1291,7 +1292,7 @@ window.chat.toggle = function() {
c.removeClass('expand'); c.removeClass('expand');
var div = $('#chat > div:visible'); var div = $('#chat > div:visible');
div.data('ignoreNextScroll', true); div.data('ignoreNextScroll', true);
div.scrollTop(9999999999999); // scroll to bottom div.scrollTop(99999999); // scroll to bottom
} else { } else {
$('#chatcontrols a:first').text('shrink'); $('#chatcontrols a:first').text('shrink');
c.addClass('expand'); c.addClass('expand');
@ -1330,27 +1331,36 @@ window.chat.chooser = function(event) {
$('#chat > div').hide(); $('#chat > div').hide();
var elm;
switch(tt) { switch(tt) {
case 'faction': case 'faction':
span.css('color', ''); span.css('color', '');
span.text('tell faction:'); span.text('tell faction:');
$('#chatfaction').show(); elm = $('#chatfaction');
break; break;
case 'public': case 'public':
span.css('cssText', 'color: red !important'); span.css('cssText', 'color: red !important');
span.text('tell public:'); span.text('tell public:');
$('#chatpublic').show(); elm = $('#chatpublic');
break; break;
case 'automated': case 'automated':
span.css('cssText', 'color: #bbb !important'); span.css('cssText', 'color: #bbb !important');
span.text('tell Jarvis:'); span.text('tell Jarvis:');
chat.renderAutomatedMsgsTo(); chat.renderAutomatedMsgsTo();
$('#chatautomated').show(); elm = $('#chatautomated');
break; break;
} }
elm.show();
if(elm.data('needsScrollTop')) {
elm.data('ignoreNextScroll', true);
elm.scrollTop(elm.data('needsScrollTop'));
elm.data('needsScrollTop', null);
}
chat.needMoreMessages(); chat.needMoreMessages();
} }
@ -1362,6 +1372,12 @@ window.chat.keepScrollPosition = function(box, scrollBefore, isOldMsgs) {
// when old messages are added. New messages added at the bottom dont // when old messages are added. New messages added at the bottom dont
// change the view and enabling this would make the chat scroll down // change the view and enabling this would make the chat scroll down
// for every added message, even if the user wants to read old stuff. // for every added message, even if the user wants to read old stuff.
if(box.is(':hidden') && !isOldMsgs) {
box.data('needsScrollTop', 99999999);
return;
}
if(scrollBefore === 0 || isOldMsgs) { if(scrollBefore === 0 || isOldMsgs) {
box.data('ignoreNextScroll', true); box.data('ignoreNextScroll', true);
box.scrollTop(box.scrollTop() + (scrollBottom(box)-scrollBefore)); box.scrollTop(box.scrollTop() + (scrollBottom(box)-scrollBefore));