portals-list: more cleanups

- columns have a default sort order which is always used when first selecting one - a second select toggles the order
- display '-' instead of '0' for no links/fields (any suggestions for resonator count, to make botu '8' and '0' visibly distinct?)
- pre-convert portal names to lower case, to avoid ecxessive conversion in soting
This commit is contained in:
Jon Atkins 2013-12-17 06:05:36 +00:00
parent 18d5708043
commit 2f4aa2ae11

View File

@ -45,6 +45,7 @@
window.plugin.portalslist = function() {}; window.plugin.portalslist = function() {};
window.plugin.portalslist.listPortals = []; window.plugin.portalslist.listPortals = [];
window.plugin.portalslist.sortBy = 'level';
window.plugin.portalslist.sortOrder = -1; window.plugin.portalslist.sortOrder = -1;
window.plugin.portalslist.enlP = 0; window.plugin.portalslist.enlP = 0;
window.plugin.portalslist.resP = 0; window.plugin.portalslist.resP = 0;
@ -82,6 +83,7 @@ window.plugin.portalslist.getPortals = function() {
'guid': i, 'guid': i,
'teamN': teamN, 'teamN': teamN,
'name': d.title, 'name': d.title,
'nameLower': d.title.toLowerCase(),
'team': d.team, 'team': d.team,
'level': portal.options.level, 'level': portal.options.level,
'health': d.health, 'health': d.health,
@ -100,12 +102,14 @@ window.plugin.portalslist.getPortals = function() {
window.plugin.portalslist.displayPL = function() { window.plugin.portalslist.displayPL = function() {
var html = ''; var html = '';
window.plugin.portalslist.sortBy = 'level';
window.plugin.portalslist.sortOrder = -1; window.plugin.portalslist.sortOrder = -1;
window.plugin.portalslist.enlP = 0; window.plugin.portalslist.enlP = 0;
window.plugin.portalslist.resP = 0; window.plugin.portalslist.resP = 0;
window.plugin.portalslist.filter = 0;
if (window.plugin.portalslist.getPortals()) { if (window.plugin.portalslist.getPortals()) {
html += window.plugin.portalslist.portalTable('level', window.plugin.portalslist.sortOrder,window.plugin.portalslist.filter); html += window.plugin.portalslist.portalTable(window.plugin.portalslist.sortBy, window.plugin.portalslist.sortOrder,window.plugin.portalslist.filter);
} else { } else {
html = '<table><tr><td>Nothing to show!</td></tr></table>'; html = '<table><tr><td>Nothing to show!</td></tr></table>';
}; };
@ -124,26 +128,31 @@ window.plugin.portalslist.displayPL = function() {
} }
window.plugin.portalslist.portalTable = function(sortBy, sortOrder, filter) { window.plugin.portalslist.portalTable = function(sortBy, sortOrder, filter) {
// sortOrder <0 ==> desc, >0 ==> asc, i use sortOrder * -1 to change the state // save the sortBy/sortOrder/filter
window.plugin.portalslist.sortBy = sortBy;
window.plugin.portalslist.sortOrder = sortOrder;
window.plugin.portalslist.filter = filter; window.plugin.portalslist.filter = filter;
var portals=window.plugin.portalslist.listPortals; var portals=window.plugin.portalslist.listPortals;
//Array sort //Array sort
window.plugin.portalslist.listPortals.sort(function(a, b) { window.plugin.portalslist.listPortals.sort(function(a, b) {
var retVal = 0; var retVal = 0;
switch (sortBy) {
case 'names': var aComp = a[sortBy];
retVal = a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1; var bComp = b[sortBy];
break;
default: if (aComp < bComp) {
retVal = b[sortBy] - a[sortBy]; retVal = -1;
break; } else if (aComp > bComp) {
retVal = 1;
} else {
// equal - compare GUIDs to ensure consistant (but arbitary) order
retVal = a.guid < b.guid ? -1 : 1;
} }
// break sort ties by comparing guids - ensures consistant sort order // sortOrder is 1 (normal) or -1 (reversed)
if (retVal == 0) retVal = a.guid < b.guid ? -1 : 1; retVal = retVal * sortOrder;
if (sortOrder > 0) retVal = -retVal; //thx @jonatkins
return retVal; return retVal;
}); });
@ -152,13 +161,13 @@ window.plugin.portalslist.portalTable = function(sortBy, sortOrder, filter) {
html += '<table>' html += '<table>'
+ '<tr>' + '<tr>'
+ '<th>#</th>' + '<th>#</th>'
+ '<th ' + sortAttr('names', sortAttr, 1, 'portalTitle') + '>Portal</th>' + '<th ' + sortAttr('nameLower', sortBy, 1, 'portalTitle') + '>Portal Name</th>'
+ '<th ' + sortAttr('level', sortAttr, -1) + '>Level</th>' + '<th ' + sortAttr('level', sortBy, -1) + '>Level</th>'
+ '<th ' + sortAttr('teamN', sortAttr, 1) + '>Team</th>' + '<th ' + sortAttr('teamN', sortBy, 1) + '>Team</th>'
+ '<th ' + sortAttr('health', sortAttr, -1) + '>Health</th>' + '<th ' + sortAttr('health', sortBy, -1) + '>Health</th>'
+ '<th ' + sortAttr('resCount', sortAttr, -1) + '>Resonators</th>' + '<th ' + sortAttr('resCount', sortBy, -1) + '>Resonators</th>'
+ '<th ' + sortAttr('linkCount', sortAttr, -1) + '>Links</th>' + '<th ' + sortAttr('linkCount', sortBy, -1) + '>Links</th>'
+ '<th ' + sortAttr('fieldCount', sortAttr, -1) + '>Fields</th>' + '<th ' + sortAttr('fieldCount', sortBy, -1) + '>Fields</th>'
var rowNum = 1; var rowNum = 1;
@ -173,8 +182,8 @@ window.plugin.portalslist.portalTable = function(sortBy, sortOrder, filter) {
html += '<td style="cursor:help" title="'+ portal.health +'">' + portal.health + '</td>' html += '<td style="cursor:help" title="'+ portal.health +'">' + portal.health + '</td>'
+ '<td>' + portal.resCount + '</td>' + '<td>' + portal.resCount + '</td>'
+ '<td title="In: ' + portal.link.in.length + ' Out: ' + portal.link.out.length + '">' + portal.linkCount + '</td>' + '<td title="In: ' + portal.link.in.length + ' Out: ' + portal.link.out.length + '">' + (portal.linkCount?portal.linkCount:'-') + '</td>'
+ '<td>' + portal.fieldCount + '</td>'; + '<td>' + (portal.fieldCount?portal.fieldCount:'-') + '</td>';
html+= '</tr>'; html+= '</tr>';
@ -184,10 +193,8 @@ window.plugin.portalslist.portalTable = function(sortBy, sortOrder, filter) {
html += '</table>'; html += '</table>';
html += '<div class="disclaimer">Click on portals table headers to sort by that column. ' html += '<div class="disclaimer">Click on portals table headers to sort by that column. '
+ 'Click on <b>All Portals, Resistance Portals, Enlightened Portals</b> to filter<br>' + 'Click on <b>All Portals, Resistance Portals, Enlightened Portals</b> to filter</div>';
+ 'Thanks to @vita10gy & @xelio for their IITC plugins who inspired me. A <a href="https://plus.google.com/113965246471577467739">@teo96</a> production. Vive la Résistance !</div>';
window.plugin.portalslist.sortOrder = window.plugin.portalslist.sortOrder*-1;
return html; return html;
} }
@ -204,7 +211,7 @@ window.plugin.portalslist.stats = function(sortBy) {
// A little helper function so the above isn't so messy // A little helper function so the above isn't so messy
window.plugin.portalslist.portalTableHeaderSortAttr = function(name, by, defOrder, extraClass) { window.plugin.portalslist.portalTableHeaderSortAttr = function(name, by, defOrder, extraClass) {
// data-sort attr: used by jquery .data('sort') below // data-sort attr: used by jquery .data('sort') below
var retVal = 'data-sort="' + name + '" class="'+(extraClass?extraClass+' ':'')+'sortable'+(name==by?' sorted':'')+'"'; var retVal = 'data-sort="'+name+'" data-defaultorder="'+defOrder+'" class="'+(extraClass?extraClass+' ':'')+'sortable'+(name==by?' sorted':'')+'"';
return retVal; return retVal;
}; };
@ -270,17 +277,20 @@ var setup = function() {
// Setup sorting // Setup sorting
$(document).on('click.portalslist', '#portalslist table th.sortable', function() { $(document).on('click.portalslist', '#portalslist table th.sortable', function() {
$('#portalslist').html(window.plugin.portalslist.portalTable($(this).data('sort'),window.plugin.portalslist.sortOrder,window.plugin.portalslist.filter)); var sortBy = $(this).data('sort');
// if this is the currently selected column, toggle the sort order - otherwise use the columns default sort order
var sortOrder = sortBy == window.plugin.portalslist.sortBy ? window.plugin.portalslist.sortOrder*-1 : parseInt($(this).data('defaultorder'));
$('#portalslist').html(window.plugin.portalslist.portalTable(sortBy,sortOrder,window.plugin.portalslist.filter));
}); });
$(document).on('click.portalslist', '#portalslist .filterAll', function() { $(document).on('click.portalslist', '#portalslist .filterAll', function() {
$('#portalslist').html(window.plugin.portalslist.portalTable($(this).data('sort'),window.plugin.portalslist.sortOrder,0)); $('#portalslist').html(window.plugin.portalslist.portalTable(window.plugin.portalslist.sortBy,window.plugin.portalslist.sortOrder,0));
}); });
$(document).on('click.portalslist', '#portalslist .filterRes', function() { $(document).on('click.portalslist', '#portalslist .filterRes', function() {
$('#portalslist').html(window.plugin.portalslist.portalTable($(this).data('sort'),window.plugin.portalslist.sortOrder,1)); $('#portalslist').html(window.plugin.portalslist.portalTable(window.plugin.portalslist.sortBy,window.plugin.portalslist.sortOrder,1));
}); });
$(document).on('click.portalslist', '#portalslist .filterEnl', function() { $(document).on('click.portalslist', '#portalslist .filterEnl', function() {
$('#portalslist').html(window.plugin.portalslist.portalTable($(this).data('sort'),window.plugin.portalslist.sortOrder,2)); $('#portalslist').html(window.plugin.portalslist.portalTable(window.plugin.portalslist.sortBy,window.plugin.portalslist.sortOrder,2));
}); });
} }