Plugin AP List: Add sorting function

Clicking header of AP or EL will sort by correspond column.
This commit is contained in:
Xelio 2013-03-26 02:15:10 +08:00
parent 440a199b5b
commit 6897da0cb8

View File

@ -31,6 +31,13 @@ window.plugin.apList.sides = new Array(2);
window.plugin.apList.sortedPortals = new Array(2); window.plugin.apList.sortedPortals = new Array(2);
window.plugin.apList.playerApGainFunc = new Array(2); window.plugin.apList.playerApGainFunc = new Array(2);
window.plugin.apList.SORT_BY_AP = 'AP';
window.plugin.apList.SORT_BY_EL = 'EL';
window.plugin.apList.sortBy = window.plugin.apList.SORT_BY_AP;
window.plugin.apList.SORT_ASC = 1;
window.plugin.apList.SORT_DESC = -1;
window.plugin.apList.sortOptions = {};
window.plugin.apList.currentPage = [1,1]; window.plugin.apList.currentPage = [1,1];
window.plugin.apList.totalPage = [1,1]; window.plugin.apList.totalPage = [1,1];
window.plugin.apList.portalPerPage = 10; window.plugin.apList.portalPerPage = 10;
@ -80,8 +87,10 @@ window.plugin.apList.tableHeaderBuilder = function(side) {
$.each(plugin.apList.tableColumns[side], function(ind, column) { $.each(plugin.apList.tableColumns[side], function(ind, column) {
var cssClass = column.headerTooltip ? (column.cssClass + ' help') : column.cssClass; var cssClass = column.headerTooltip ? (column.cssClass + ' help') : column.cssClass;
var title = column.headerTooltip ? column.headerTooltip : ''; var title = column.headerTooltip ? column.headerTooltip : '';
var onclick = column.headerOnClick ? column.headerOnClick: '';
headerRow += '<td class="' + cssClass + '" ' headerRow += '<td class="' + cssClass + '" '
+ 'title="' + title + '" ' + 'title="' + title + '" '
+ 'onclick="' + onclick + '" '
+ '>' + '>'
+ column.header + column.header
+ '</td>'; + '</td>';
@ -244,9 +253,7 @@ window.plugin.apList.updateSortedPortals = function() {
plugin.apList.sortedPortals[side].push(portal); plugin.apList.sortedPortals[side].push(portal);
}); });
$.each(plugin.apList.sides, function(ind, side) { $.each(plugin.apList.sides, function(ind, side) {
plugin.apList.sortedPortals[side].sort(function(a, b) { plugin.apList.sortedPortals[side].sort(plugin.apList.comparePortal);
return b.playerApGain.totalAp - a.playerApGain.totalAp;
});
}); });
// Modify sortedPortals if any portal selected for destroy // Modify sortedPortals if any portal selected for destroy
@ -322,9 +329,7 @@ window.plugin.apList.handleDestroyPortal = function() {
}); });
// Sorting portals with updated AP // Sorting portals with updated AP
plugin.apList.sortedPortals[enemy].sort(function(a, b) { plugin.apList.sortedPortals[enemy].sort(plugin.apList.comparePortal);
return b.playerApGain.totalAp - a.playerApGain.totalAp;
});
} }
window.plugin.apList.updateTotalPages = function() { window.plugin.apList.updateTotalPages = function() {
@ -407,9 +412,6 @@ window.plugin.apList.getDeployOrUpgradeApGain = function(d) {
upgradedReso.push(targetReso); upgradedReso.push(targetReso);
// Counting upgrade or deploy // Counting upgrade or deploy
(targetReso.level === 0) ? deployCount++ : upgradedCount++; (targetReso.level === 0) ? deployCount++ : upgradedCount++;
totalAp += (targetReso.level === 0)
? DEPLOY_RESONATOR
: UPGRADE_ANOTHERS_RESONATOR;
availableCount--; availableCount--;
} }
@ -537,6 +539,29 @@ window.plugin.apList.getShieldsMitigation = function(portal) {
return shieldsMitigation; return shieldsMitigation;
} }
// For using in .sort(func) of sortedPortals
window.plugin.apList.comparePortal = function(a,b) {
var result = 0;
var options = plugin.apList.sortOptions[plugin.apList.sortBy];
$.each(options, function(indO, option) {
var aProperty = a;
var bProperty = b;
// Walking down the chain
$.each(option.chain, function(indPN, propertyName) {
aProperty = aProperty[propertyName];
bProperty = bProperty[propertyName];
});
// compare next porperty if equal
if(aProperty === bProperty) return true;
result = (aProperty > bProperty ? 1 : -1) * option.order;
return false;
});
return result;
}
// FEATURE TOGGLES AND INTERACTION HANDLER /////////////////////////////////////// // FEATURE TOGGLES AND INTERACTION HANDLER ///////////////////////////////////////
window.plugin.apList.enableCache = function() { window.plugin.apList.enableCache = function() {
@ -598,7 +623,7 @@ window.plugin.apList.animPortalLocationIndicator = function() {
window.plugin.apList.changePage = function(step, toEnd) { window.plugin.apList.changePage = function(step, toEnd) {
var side = plugin.apList.displaySide; var side = plugin.apList.displaySide;
var originPage = plugin.apList.currentPage[side]; var oldPage = plugin.apList.currentPage[side];
if(toEnd) { if(toEnd) {
if(step < 0) plugin.apList.currentPage[side] = 1; if(step < 0) plugin.apList.currentPage[side] = 1;
@ -611,10 +636,19 @@ window.plugin.apList.changePage = function(step, toEnd) {
plugin.apList.currentPage[side] = plugin.apList.totalPage[side]; plugin.apList.currentPage[side] = plugin.apList.totalPage[side];
} }
if(plugin.apList.currentPage[side] !== originPage) if(plugin.apList.currentPage[side] !== oldPage)
plugin.apList.updatePortalTable(side); plugin.apList.updatePortalTable(side);
} }
window.plugin.apList.changeSorting = function(sortBy) {
var oldSortBy = plugin.apList.sortBy;
plugin.apList.sortBy = sortBy;
if(plugin.apList.sortBy !== oldSortBy) {
plugin.apList.updateSortedPortals();
plugin.apList.updatePortalTable(plugin.apList.displaySide);
}
}
window.plugin.apList.destroyPortal = function(guid) { window.plugin.apList.destroyPortal = function(guid) {
// Add to destroyPortalsGuid if not yet added, remove if already added // Add to destroyPortalsGuid if not yet added, remove if already added
var portalIndex = plugin.apList.destroyPortalIndex(guid); var portalIndex = plugin.apList.destroyPortalIndex(guid);
@ -687,6 +721,23 @@ window.plugin.apList.setupVar = function() {
= "#ap-list-eny"; = "#ap-list-eny";
} }
window.plugin.apList.setupSorting = function() {
var optionELAsc = {
order: plugin.apList.SORT_ASC,
chain: ['effectiveLevel','effectiveLevel']};
var optionAPDesc = {
order: plugin.apList.SORT_DESC,
chain: ['playerApGain','totalAp']};
var optionGuidDesc = {
order: plugin.apList.SORT_DESC,
chain: ['guid']};
// order by EL -> AP -> guid
plugin.apList.sortOptions[plugin.apList.SORT_BY_EL] = [optionELAsc, optionAPDesc, optionGuidDesc];
// order by AP -> EL -> guid
plugin.apList.sortOptions[plugin.apList.SORT_BY_AP] = [optionAPDesc, optionELAsc, optionGuidDesc];
}
// Setup table columns for header builder and row builder // Setup table columns for header builder and row builder
window.plugin.apList.setupTableColumns = function() { window.plugin.apList.setupTableColumns = function() {
var enemyColumns = new Array(); var enemyColumns = new Array();
@ -695,12 +746,15 @@ window.plugin.apList.setupTableColumns = function() {
// AP and Eff. LV columns are same in enemy and friendly table // AP and Eff. LV columns are same in enemy and friendly table
var apColumn = { var apColumn = {
header: 'AP', header: 'AP',
headerOnClick: 'plugin.apList.changeSorting(plugin.apList.SORT_BY_AP);',
headerTooltip: 'Click to sort by AP',
cssClass: 'ap-list-td-ap', cssClass: 'ap-list-td-ap',
contentFunction: plugin.apList.getPortalApText contentFunction: plugin.apList.getPortalApText
}; };
var effectiveLevelColumn = { var effectiveLevelColumn = {
header: 'EL', header: 'EL',
headerTooltip: 'Effective Level', headerOnClick: 'plugin.apList.changeSorting(plugin.apList.SORT_BY_EL);',
headerTooltip: 'Effective Level\nClick to sort by EL',
cssClass: 'ap-list-td-eff-lv', cssClass: 'ap-list-td-eff-lv',
contentFunction: plugin.apList.getPortalEffectiveLvText contentFunction: plugin.apList.getPortalEffectiveLvText
}; };
@ -817,6 +871,7 @@ window.plugin.apList.setupMapEvent = function() {
var setup = function() { var setup = function() {
window.plugin.apList.setupVar(); window.plugin.apList.setupVar();
window.plugin.apList.setupSorting();
window.plugin.apList.setupTableColumns(); window.plugin.apList.setupTableColumns();
window.plugin.apList.setupCSS(); window.plugin.apList.setupCSS();
window.plugin.apList.setupList(); window.plugin.apList.setupList();