From 2f4aa2ae1180ddc0ee05e0338b8be6d25a951627 Mon Sep 17 00:00:00 2001 From: Jon Atkins Date: Tue, 17 Dec 2013 06:05:36 +0000 Subject: [PATCH] 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 --- plugins/portals-list.user.js | 78 ++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/plugins/portals-list.user.js b/plugins/portals-list.user.js index 70e4a62c..50e9c8b3 100644 --- a/plugins/portals-list.user.js +++ b/plugins/portals-list.user.js @@ -45,7 +45,8 @@ window.plugin.portalslist = function() {}; window.plugin.portalslist.listPortals = []; -window.plugin.portalslist.sortOrder=-1; +window.plugin.portalslist.sortBy = 'level'; +window.plugin.portalslist.sortOrder = -1; window.plugin.portalslist.enlP = 0; window.plugin.portalslist.resP = 0; window.plugin.portalslist.filter = 0; @@ -66,7 +67,7 @@ window.plugin.portalslist.getPortals = function() { var d = portal.options.data; var teamN = portal.options.team; - switch (teamN){ + switch (teamN) { case TEAM_RES: window.plugin.portalslist.resP++; break; @@ -82,6 +83,7 @@ window.plugin.portalslist.getPortals = function() { 'guid': i, 'teamN': teamN, 'name': d.title, + 'nameLower': d.title.toLowerCase(), 'team': d.team, 'level': portal.options.level, 'health': d.health, @@ -100,12 +102,14 @@ window.plugin.portalslist.getPortals = function() { window.plugin.portalslist.displayPL = function() { var html = ''; - window.plugin.portalslist.sortOrder=-1; + window.plugin.portalslist.sortBy = 'level'; + window.plugin.portalslist.sortOrder = -1; window.plugin.portalslist.enlP = 0; window.plugin.portalslist.resP = 0; + window.plugin.portalslist.filter = 0; 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 { html = '
Nothing to show!
'; }; @@ -124,26 +128,31 @@ window.plugin.portalslist.displayPL = function() { } window.plugin.portalslist.portalTable = function(sortBy, sortOrder, filter) { - // sortOrder <0 ==> desc, >0 ==> asc, i use sortOrder * -1 to change the state - window.plugin.portalslist.filter=filter; + // save the sortBy/sortOrder/filter + window.plugin.portalslist.sortBy = sortBy; + window.plugin.portalslist.sortOrder = sortOrder; + window.plugin.portalslist.filter = filter; + var portals=window.plugin.portalslist.listPortals; //Array sort window.plugin.portalslist.listPortals.sort(function(a, b) { var retVal = 0; - switch (sortBy) { - case 'names': - retVal = a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1; - break; - default: - retVal = b[sortBy] - a[sortBy]; - break; + + var aComp = a[sortBy]; + var bComp = b[sortBy]; + + if (aComp < bComp) { + retVal = -1; + } 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 - if (retVal == 0) retVal = a.guid < b.guid ? -1 : 1; - - if (sortOrder > 0) retVal = -retVal; //thx @jonatkins + // sortOrder is 1 (normal) or -1 (reversed) + retVal = retVal * sortOrder; return retVal; }); @@ -152,13 +161,13 @@ window.plugin.portalslist.portalTable = function(sortBy, sortOrder, filter) { html += '' + '' + '' - + '' - + '' - + '' - + '' - + '' - + '' - + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' var rowNum = 1; @@ -173,8 +182,8 @@ window.plugin.portalslist.portalTable = function(sortBy, sortOrder, filter) { html += '' + '' - + '' - + ''; + + '' + + ''; html+= ''; @@ -184,10 +193,8 @@ window.plugin.portalslist.portalTable = function(sortBy, sortOrder, filter) { html += '
#PortalLevelTeamHealthResonatorsLinksFieldsPortal NameLevelTeamHealthResonatorsLinksFields' + portal.health + '' + portal.resCount + '' + portal.linkCount + '' + portal.fieldCount + '' + (portal.linkCount?portal.linkCount:'-') + '' + (portal.fieldCount?portal.fieldCount:'-') + '
'; html += '
Click on portals table headers to sort by that column. ' - + 'Click on All Portals, Resistance Portals, Enlightened Portals to filter
' - + 'Thanks to @vita10gy & @xelio for their IITC plugins who inspired me. A @teo96 production. Vive la Résistance !
'; + + 'Click on All Portals, Resistance Portals, Enlightened Portals to filter'; - window.plugin.portalslist.sortOrder = window.plugin.portalslist.sortOrder*-1; return html; } @@ -204,7 +211,7 @@ window.plugin.portalslist.stats = function(sortBy) { // A little helper function so the above isn't so messy window.plugin.portalslist.portalTableHeaderSortAttr = function(name, by, defOrder, extraClass) { // 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; }; @@ -270,17 +277,20 @@ var setup = function() { // Setup sorting $(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() { - $('#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() { - $('#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() { - $('#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)); }); }