Skip to content

Commit

Permalink
Merge pull request IITC-CE#447 from le-jeu/comm_tabs_refactor
Browse files Browse the repository at this point in the history
Refactor comm tabs code
  • Loading branch information
modos189 authored May 15, 2024
2 parents 0fe373b + 947b226 commit 7ef46bb
Show file tree
Hide file tree
Showing 9 changed files with 1,346 additions and 1,224 deletions.
1,515 changes: 418 additions & 1,097 deletions core/code/chat.js

Large diffs are not rendered by default.

781 changes: 781 additions & 0 deletions core/code/comm.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions core/code/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
* - `fieldRemoved`: Triggered when a field has been removed.
* - `portalDetailsUpdated`: Fired after the details in the sidebar have been (re-)rendered.
* Provides data about the selected portal.
* - `publicChatDataAvailable`: Runs after data for any of the public chats has been received and processed,
* but not yet displayed. Contains both the unprocessed raw AJAX response
* and the processed chat data that is going to be used for display.
* - `commDataAvailable`: Runs after data for any of the chats has been received and processed, but not yet
* been displayed. The data hash contains both the unprocessed raw ajax response as
* well as the chat data that is going to be used for display.
* - `publicChatDataAvailable`: Similar to `chatDataAvailable`, but for all chat only.
* - `factionChatDataAvailable`: Similar to `publicChatDataAvailable`, but for faction chat.
* - `alertsChatDataAvailable`: Similar to `publicChatDataAvailable`, but for alerts chat.
* - `requestFinished`: **Deprecated**. Recommended to use `mapDataRefreshEnd` instead.
Expand Down
27 changes: 13 additions & 14 deletions core/code/panes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ window.show = function(id) {

runHooks("paneChanged", id);

switch(id) {
case 'all':
case 'faction':
case 'alerts':
window.chat.show(id);
break;
case 'map':
window.smartphone.mapButton.click();
$('#portal_highlight_select').show();
$('#farm_level_select').show();
break;
case 'info':
window.smartphone.sideButton.click();
break;
// look for comm tab first
if (window.chat.getChannelDesc(id)) window.chat.show(id);
else {
switch (id) {
case 'map':
window.smartphone.mapButton.click();
$('#portal_highlight_select').show();
$('#farm_level_select').show();
break;
case 'info':
window.smartphone.sideButton.click();
break;
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions core/code/request_handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ window.requests.abort = function () {

window.activeRequests = [];
window.failedRequestCount = 0;
window.chat._requestPublicRunning = false;
window.chat._requestFactionRunning = false;

renderUpdateStatus();
}
Expand Down
9 changes: 9 additions & 0 deletions core/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,15 @@ em {
height: 100%;
}

#chatinput.public mark,
#chatinput.public input {
color: #f66;
}

#chatinput.alerts mark {
color: #bbb;
}

#chat td, #chatinput td {
font-size: 13px;
vertical-align: top;
Expand Down
9 changes: 1 addition & 8 deletions core/total-conversion-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,8 @@ document.body.innerHTML =
'<div id="map">Loading, please wait</div>' +
'<div id="chatcontrols" style="display:none">' +
'<a accesskey="0" title="[0]"><span class="toggle"></span></a>' +
'<a accesskey="1" title="[1]">all</a>' +
'<a accesskey="2" title="[2]" class="active">faction</a>' +
'<a accesskey="3" title="[3]">alerts</a>' +
'</div>' +
'<div id="chat" style="display:none">' +
' <div id="chatfaction"></div>' +
' <div id="chatall"></div>' +
' <div id="chatalerts"></div>' +
'</div>' +
'<div id="chat" style="display:none"></div>' +
'<form id="chatinput" style="display:none"><table><tr>' +
' <td><time></time></td>' +
' <td><mark>tell faction:</mark></td>' +
Expand Down
29 changes: 29 additions & 0 deletions plugins/debug-console.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#chat #chatdebug td:nth-child(-n+2) {
width: 51px
}

#chat #chatdebug td:nth-child(3) {
font-family: monospace
}

#chatdebug td mark {
color: #eee
}

#chatdebug td mark.error {
color: #FF424D
}

#chatdebug td mark.warning {
color: #FFDE42
}

#chatdebug td pre {
display: inline;
white-space: pre-line;
word-break: break-all;
}

#chatinput.debug mark {
color: #bbb;
}
191 changes: 91 additions & 100 deletions plugins/debug-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
// @description Add a debug console tab

/* exported setup, changelog --eslint */
/* global L */

var changelog = [
{
version: '0.2.0',
changes: ['Use channel new API', 'Handle multiline messages'],
},
{
version: '0.1.1',
changes: ['Version upgrade due to a change in the wrapper: added plugin icon'],
Expand All @@ -18,41 +23,36 @@ var debugTab = {};
// DEBUGGING TOOLS ///////////////////////////////////////////////////
// meant to be used from browser debugger tools and the like.


debugTab.renderDetails = function() {
debugTab.console.log('portals: ' + Object.keys(window.portals).length);
debugTab.console.log('links: ' + Object.keys(window.links).length);
debugTab.console.log('fields: ' + Object.keys(window.fields).length);
};

debugTab.printStackTrace = function() {
var e = new Error('dummy');
debugTab.console.error(e.stack);
return e.stack;
};

debugTab.console = {};
debugTab.console.show = function() {
$('#chat, #chatinput').show();
$('#chatinput mark').css('cssText', 'color: #bbb !important').text('debug:');
$('#chat > div').hide();
$('#debugconsole').show();
$('#chatcontrols .active').removeClass('active');
$("#chatcontrols a:contains('debug')").addClass('active');
debugTab.create = function () {
window.chat.addChannel({
id: 'debug',
name: 'Debug',
inputPrompt: 'debug:',
inputClass: 'debug',
sendMessage: function (_, msg) {
var result;
try {
result = eval('(' + msg + ')');
} catch (e) {
if (e.stack) {
console.error(e.stack);
}
throw e; // to trigger native error message
}
if (result !== undefined) {
console.log(result);
}
},
});
};

debugTab.console.renderLine = function(errorType, args) {
debugTab.renderLine = function (errorType, args) {
args = Array.prototype.slice.call(args);
var color = '#eee';
switch (errorType) {
case 'error': color = '#FF424D'; break;
case 'warning': color = '#FFDE42'; break;
}
var text = [];
args.forEach(function (v) {
if (typeof v !== 'string' && typeof v !== 'number') {
var cache = [];
v = JSON.stringify(v, function(key, value) {
v = JSON.stringify(v, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
Expand All @@ -68,37 +68,65 @@ debugTab.console.renderLine = function(errorType, args) {
text.push(v);
});
text = text.join(' ');

// Time
var time = document.createElement('time');
var d = new Date();
var ta = d.toLocaleTimeString(); // print line instead maybe?
var tb = d.toLocaleString();
var t = '<time title="'+tb+'" data-timestamp="'+d.getTime()+'">'+ta+'</time>';
var s = 'style="color:'+color+'"';
var l = '<tr><td>'+t+'</td><td><mark '+s+'>'+errorType+'</mark></td><td>'+text+'</td></tr>';
$('#debugconsole table').prepend(l);
time.textContent = d.toLocaleTimeString();
time.title = d.toLocaleString();
time.dataset.timestamp = d.getTime();

// Type
var type = document.createElement('mark');
type.textContent = errorType;
type.className = errorType;

// Text
var pre = document.createElement('pre');
pre.textContent = text;

// Check if the last message is visible
var debugContainer = document.getElementById('chatdebug');
var isAtBottom = debugContainer.scrollTop >= debugContainer.scrollTopMax;

// Insert Row
var table = document.querySelector('#chatdebug table');
var row = table.insertRow();
row.insertCell().append(time);
row.insertCell().append(type);
row.insertCell().append(pre);

// Auto-scroll to bottom
if (isAtBottom) debugContainer.scrollTo(0, debugContainer.scrollTopMax);
};

debugTab.console.log = function() {
debugTab.console.renderLine('notice', arguments);
debugTab.console = {};
debugTab.console.log = function () {
debugTab.renderLine('notice', arguments);
};

debugTab.console.warn = function () {
debugTab.renderLine('warning', arguments);
};

debugTab.console.warn = function() {
debugTab.console.renderLine('warning', arguments);
debugTab.console.error = function () {
debugTab.renderLine('error', arguments);
};

debugTab.console.error = function() {
debugTab.console.renderLine('error', arguments);
debugTab.console.debug = function () {
debugTab.renderLine('debug', arguments);
};

debugTab.console.debug = function() {
debugTab.console.renderLine('debug', arguments);
debugTab.console.info = function () {
debugTab.renderLine('info', arguments);
};

function overwriteNative() {
var nativeConsole = window.console;
window.console = L.extend({}, window.console);

function overwrite(which) {
window.console[which] = function() {
window.console[which] = function () {
if (nativeConsole) {
nativeConsole[which].apply(nativeConsole, arguments);
}
Expand All @@ -110,82 +138,45 @@ function overwriteNative() {
overwrite('warn');
overwrite('error');
overwrite('debug');
overwrite('info');
}

function setupPosting() {
if (!window.isSmartphone()) {
$('#chatinput input').keydown(function(event) {
var kc = event.keyCode ? event.keyCode : event.which;
if (kc === 13) { // enter
if ($('#chatcontrols .active').text() === 'debug') {
event.preventDefault();
userInput();
}
}
});
}

$('#chatinput').submit(function(event) {
event.preventDefault();
userInput();
});
}

function userInput() {
if ($('#chatcontrols .active').text() !== 'debug') {return;}

var msg = $.trim($('#chatinput input').val());
if (!msg) { return; }

var result;
try {
result = eval(msg);
} catch (e) {
if (e.stack) { debugTab.console.error(e.stack); }
throw e; // to trigger native error message
}
if (result !== undefined) {
debugTab.console.log(result.toString());
}
}


function create() {
if ($('#debugconsole').length) return;
$('#chatcontrols').append('<a>debug</a>');
$('#chatcontrols a:last').click(debugTab.console.show);
$('#chat').append('<div style="display: none" id="debugconsole"><table></table></div>');
// Old API utils
debugTab.renderDetails = function () {
debugTab.console.log('portals: ' + Object.keys(window.portals).length);
debugTab.console.log('links: ' + Object.keys(window.links).length);
debugTab.console.log('fields: ' + Object.keys(window.fields).length);
};

setupPosting();
debugTab.printStackTrace = function () {
var e = new Error('dummy');
debugTab.console.error(e.stack);
return e.stack;
};

if (window.useAndroidPanes()) {
android.addPane('debug', 'Debug', 'ic_action_view_as_list');
window.addHook('paneChanged', function (id) {
if (id === 'debug') {
debugTab.console.show();
}
});
}
}
debugTab.show = function () {
window.chat.show('debug');
};

function setup() {
window.plugin.debug = debugTab;
create();
debugTab.create();
overwriteNative();

$('<style>').prop('type', 'text/css').text('@include_string:debug-console.css@').appendTo('head');

// emulate old API
window.debug = function () {};
window.debug.renderDetails = debugTab.renderDetails;
window.debug.printStackTrace = debugTab.printStackTrace;
window.debug.console = function () {};
window.debug.console.show = debugTab.console.show;
window.debug.console.show = debugTab.show;
window.debug.console.renderLine = function (text, errorType) {
return debugTab.console.renderLine(errorType, [text]);
return debugTab.renderLine(errorType, [text]);
};
window.debug.console.log = debugTab.console.log;
window.debug.console.warn = debugTab.console.warn;
window.debug.console.error = debugTab.console.error;
}

setup.priority = 'boot';

0 comments on commit 7ef46bb

Please sign in to comment.