Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

Sort Exceptions listbox, auto-select added items #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions src/content/dlg-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ NoSquint.dialogs.global = NoSquint.ns(function() { with (NoSquint) {

// Exceptions tab
$('copyURL-button').style.display = this.url ? '' : 'none';
for (let exc in iter(NSQ.prefs.exceptions))
this.exceptionsListAdd(exc[0].replace(/%20/g, ' '), false);
var sortedExceptions = NSQ.prefs.exceptions;
for (let exc in iter(sortedExceptions))
exc[0] = exc[0].replace(/%20/g, ' ');
sortedExceptions.sort(function(a, b) {
if (a[0] < b[0]) { return -1; }
if (a[0] > b[0]) { return 1; }
return 0;
});
for (let exc in iter(sortedExceptions))
this.exceptionsListAdd(exc[0], false, -1);
$('exceptionsList').setUserData('nosquint.changed', false, null);
this.excListSelect();
};
Expand Down Expand Up @@ -161,12 +169,12 @@ NoSquint.dialogs.global = NoSquint.ns(function() { with (NoSquint) {
* Exceptions tab functions
*/

this.exceptionsListAdd = function(pattern, check_dupe) {
this.exceptionsListAdd = function(pattern, manual_add, insert_before) {
var listbox = $('exceptionsList');
// Strip URI scheme from pattern (if it exists)
pattern = pattern.replace(/^\w+:\/\//, '');

if (check_dupe) {
if (manual_add) {
for (let node in iter(listbox.childNodes)) {
if (node.childNodes[0].getAttribute('label') == pattern)
return;
Expand All @@ -178,7 +186,26 @@ NoSquint.dialogs.global = NoSquint.ns(function() { with (NoSquint) {
var li1 = document.createElement("listcell");
li1.setAttribute('label', pattern);
node.appendChild(li1);
listbox.appendChild(node);

if (insert_before == -1) // insert at end
{
listbox.appendChild(node);
}
else {
var items = listbox.childNodes;
if (insert_before >= 0 && insert_before < items.length) {
listbox.insertBefore(node, items.item(insert_before));
}
else {
console.error("insert_before value " + insert_before + " is out of the listbox bounds! Item '" + pattern + "' has not been added.");
return;
}
}
if (manual_add) {
listbox.ensureElementIsVisible(node);
listbox.selectItem(node);
}

node.addEventListener('dblclick', function() NSQ.dialogs.global.buttonEditException(), false);
// Mark the listbox as having been changed from stored prefs.
listbox.setUserData('nosquint.changed', true, null);
Expand Down Expand Up @@ -221,7 +248,22 @@ NoSquint.dialogs.global = NoSquint.ns(function() { with (NoSquint) {
};

this.buttonAddException = function() {
this.exceptionsListAdd($('pattern').value, true);
// Since the listbox should always be sorted already, we can simply scan to see where to add the new one.
var listbox = $('exceptionsList');
var value = $('pattern').value;
var children = listbox.childNodes;
var insertbefore = -1;
for (var i = 0; i < children.length; i++) {
var item = children.item(i);
if (item.nodeName == "listitem") {
var label = item.childNodes.item(0).getAttribute("label");
if (label > value) {
insertbefore = i;
break;
}
}
}
this.exceptionsListAdd(value, true, insertbefore);
$('pattern').value = '';
this.textPatternChange();
};
Expand Down