This is a continuation of an old thread http://community.activestate.com/forum/sorting-tabs
First, toddw proposed this code:
// Move the current tab
var view = ko.views.manager.currentView;
var tabbox = view.parentNode;
while (tabbox && tabbox.nodeName != "tabbox" && tabbox.nodeName != "xul:tabbox") {
tabbox = tabbox.parentNode;
}
var index = 1;
var tabToMove = tabbox.selectedTab;
var targetTab = tabbox._tabs.childNodes[index];
tabbox._tabs.insertBefore(tabToMove, targetTab);
Then r.izumita expanded on it with this:
var view = ko.views.manager.currentView;
var tabbox = view.parentNode;
while (tabbox && tabbox.nodeName != "tabbox" && tabbox.nodeName != "xul:tabbox") {
tabbox = tabbox.parentNode;
}
var index = 1;
var childNodes = tabbox._tabs.childNodes
for (var i = 0; i < childNodes.length; i++) {
for (var j = childNodes.length - 1; j > i; j--) {
if (childNodes[j].label < childNodes[j-1].label) {
tabbox._tabs.insertBefore(childNodes[j], childNodes[j-1])
}
}
}
Then r.izumita added case-insesitive sorting
var view = ko.views.manager.currentView;
var tabbox = view.parentNode;
while (tabbox && tabbox.nodeName != "tabbox" && tabbox.nodeName != "xul:tabbox") {
tabbox = tabbox.parentNode;
}
var index = 1;
var childNodes = tabbox._tabs.childNodes
for (var i = 0; i < childNodes.length; i++) {
for (var j = childNodes.length - 1; j > i; j--) {
if (childNodes[j].label.toLowerCase() < childNodes[j-1].label.toLowerCase()) {
tabbox._tabs.insertBefore(childNodes[j], childNodes[j-1])
}
}
}
Then nathan added the ability to group by extension:
var view = ko.views.manager.currentView;
var tabbox = view.parentNode;
while (tabbox && tabbox.nodeName != "tabbox" && tabbox.nodeName != "xul:tabbox") {
tabbox = tabbox.parentNode;
}
var index = 1;
var childNodes = tabbox._tabs.childNodes
for (var i = 0; i < childNodes.length; i++) {
for (var j = childNodes.length - 1; j > i; j--) {
if (childNodes[j].label.toLowerCase() < childNodes[j-1].label.toLowerCase()) {
tabbox._tabs.insertBefore(childNodes[j], childNodes[j-1])
}
}
}
for (var i = 0; i < childNodes.length; i++) {
for (var j = childNodes.length - 1; j > i; j--) {
sExt1 = childNodes[j].label.replace(/.*\./,'');
sExt2 = childNodes[j-1].label.replace(/.*\./,'');
if (sExt1.toLowerCase() < sExt2.toLowerCase()) {
tabbox._tabs.insertBefore(childNodes[j], childNodes[j-1])
}
}
}