Skip to content

Commit

Permalink
try harder to pick an appropriate sort column
Browse files Browse the repository at this point in the history
  • Loading branch information
haarg committed May 15, 2018
1 parent a5cdb03 commit 9b3dc2b
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions root/static/js/cpan.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,7 @@ $(document).ready(function() {
$('table.tablesorter').each(function() {
var table = $(this);

var sortid = (
MetaCPAN.storage.getItem("tablesorter:" + table.attr('id')) ||
table.attr('data-default-sort') || '0,0');
sortid = JSON.parse("[" + sortid + "]");

var cfg = {
sortList: [sortid],
textExtraction: function(node) {
var $node = $(node);
var sort = $node.attr("sort");
Expand All @@ -141,13 +135,47 @@ $(document).ready(function() {
headers: {}
};

var sortable = [];
table.find('thead th').each(function(i, el) {
if ($(el).hasClass('no-sort')) {
cfg.headers[i] = {
sorter: false
};
} else {
sortable.push(i);
}
});

var sortid;
if (table.attr('id')) {
sortid = MetaCPAN.storage.getItem("tablesorter:" + table.attr('id'));
}
if (!sortid && table.attr('data-default-sort')) {
sortid = table.attr('data-default-sort');
}
if (!sortid) {
sortid = '0,0';
}
try {
sortid = JSON.parse('[' + sortid + ']');
} catch (e) {
sortid = [0, 0];
}

var found;
$(sortable).each(function(i, col) {
if (sortid[0] == col) {
found = true;
return false;
}
});
if (found) {
cfg.sortList = [sortid];
} else if (sortable.length) {
cfg.sortList = [
[sortable[0], 0]
];
}

table.tablesorter(cfg);
});
Expand Down

0 comments on commit 9b3dc2b

Please sign in to comment.