Don't rely on object property order to sort tags

It works, but in theory it's not guaranteed in JS.
This commit is contained in:
Dan Stillman 2013-03-15 17:40:59 -04:00
parent 2b4f2e3a0f
commit 2b7d7ebfbf
2 changed files with 16 additions and 11 deletions

View file

@ -226,9 +226,19 @@
// Remove children // Remove children
tagsToggleBox.textContent = ""; tagsToggleBox.textContent = "";
var lastTag; // Sort by name
var orderedTags = [];
var collation = Zotero.getLocaleCollation();
for (let tagID in self._tags) { for (let tagID in self._tags) {
let tagButton = self._makeClickableTag(tagID, lastTag, self.editable); orderedTags.push(self._tags[tagID])
}
orderedTags.sort(function(a, b) {
return collation.compareString(1, a.name, b.name);
});
var lastTag;
for (let i=0; i<orderedTags.length; i++) {
let tagButton = self._makeClickableTag(orderedTags[i], lastTag, self.editable);
if (tagButton) { if (tagButton) {
tagButton.addEventListener('click', function(event) { tagButton.addEventListener('click', function(event) {
self.handleTagClick(event, this); self.handleTagClick(event, this);
@ -741,16 +751,16 @@
<method name="_makeClickableTag"> <method name="_makeClickableTag">
<parameter name="tagID"/> <parameter name="tagObj"/>
<parameter name="lastTag"/> <parameter name="lastTag"/>
<parameter name="editable"/> <parameter name="editable"/>
<body> <body>
<![CDATA[ <![CDATA[
var tagInfo = this._tags[tagID], tagName = tagInfo.name; var tagID = tagObj.id, tagName = tagObj.name, tagType = tagObj.type;
// If the last tag was the same, add this tagID and tagType to it // If the last tag was the same, add this tagID and tagType to it
if(lastTag && lastTag.value === tagName) { if(lastTag && lastTag.value === tagName) {
lastTag.setAttribute('tagID', lastTag.getAttribute('tagID') + '-' + tagID); lastTag.setAttribute('tagID', lastTag.getAttribute('tagID') + '-' + tagID);
lastTag.setAttribute('tagType', lastTag.getAttribute('tagType') + '-' + tagInfo.type); lastTag.setAttribute('tagType', lastTag.getAttribute('tagType') + '-' + tagType);
return false; return false;
} }
@ -759,7 +769,7 @@
label.setAttribute('value', tagName); label.setAttribute('value', tagName);
label.setAttribute('tagID', tagID); label.setAttribute('tagID', tagID);
label.setAttribute('tagType', tagInfo.type); label.setAttribute('tagType', tagType);
if (editable) { if (editable) {
label.setAttribute('context', 'tag-menu'); label.setAttribute('context', 'tag-menu');
} }

View file

@ -186,11 +186,6 @@ Zotero.Tags = new function() {
return {}; return {};
} }
var collation = Zotero.getLocaleCollation();
tags.sort(function(a, b) {
return collation.compareString(1, a.name, b.name);
});
var indexed = {}; var indexed = {};
for (var i=0; i<tags.length; i++) { for (var i=0; i<tags.length; i++) {
var tag = this.get(tags[i].tagID, true); var tag = this.get(tags[i].tagID, true);