From 1e59c5ab7e57c1f1ccc2e2762c2a827d9ec3a22b Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 15 Mar 2013 04:13:14 -0400 Subject: [PATCH] More tags box fixes (follow-up to f932f312ebbc) Use the Notifier for all tags box updates to ensure that it always updates. Also fix the tag count and some other things. Post-tab focus still isn't 100% correct in all situations, but it's real close. --- chrome/content/zotero/bindings/tagsbox.xml | 395 ++++++++++++--------- chrome/content/zotero/xpcom/data/item.js | 4 +- 2 files changed, 226 insertions(+), 173 deletions(-) diff --git a/chrome/content/zotero/bindings/tagsbox.xml b/chrome/content/zotero/bindings/tagsbox.xml index 4a5630ee7..4a8a84b4a 100644 --- a/chrome/content/zotero/bindings/tagsbox.xml +++ b/chrome/content/zotero/bindings/tagsbox.xml @@ -118,7 +118,7 @@ this.mode = this.getAttribute('mode'); } - this._notifierID = Zotero.Notifier.registerObserver(this, ['setting']); + this._notifierID = Zotero.Notifier.registerObserver(this, ['item-tag', 'setting']); ]]> @@ -142,6 +142,55 @@ } return; } + else if (type == 'item-tag') { + let itemID, tagID; + + for (var i=0; i newTabIndex) { + this._lastTabIndex++; + } + } + else if (this._tabDirection == 1) { + if (this._lastTabIndex > newTabIndex) { + this._lastTabIndex++; + } + } + } + else if (event == 'remove') { + var oldTabIndex = this.remove(tagID); + if (oldTabIndex == -1) { + return; + } + if (this._tabDirection == -1) { + if (this._lastTabIndex > oldTabIndex) { + this._lastTabIndex--; + } + } + else if (this._tabDirection == 1) { + if (this._lastTabIndex >= oldTabIndex) { + this._lastTabIndex--; + } + } + } + } + + this.updateCount(); + } + else if (type == 'tag') { + if (event == 'modify') { + this.reload(); + } + } ]]> @@ -166,14 +215,11 @@ while(rows.hasChildNodes()) { rows.removeChild(rows.firstChild); } - var tags = self.item.getTags(); - if (tags) { - for (var i=0; i + @@ -234,10 +283,13 @@ + - @@ -583,21 +633,11 @@ 1) { - var lastTag = row == row.parentNode.lastChild; - - Zotero.DB.beginTransaction(); - - // If old tag isn't in array, remove it - if (id) { - var oldValue = Zotero.Tags.getName(id); - if (tagArray.indexOf(oldValue) == -1) { - this.item.removeTag(id); - } - } - - this.item.addTags(tagArray); - - Zotero.DB.commitTransaction(); - - // TODO: get tab index right - - if (lastTag) { - this._lastTabIndex = this.item.getTags().length; - } - - this.reload(); - return; - } - // Single tag at end + // Existing tag cleared else { - id = tagsbox.add(value); - // New tag - if (id) { - // Stay put, since a tag was added above - if (this._tabDirection == -1) { - this._tabDirection = false; - } + this.item.removeTag(oldTagID); + } + } + // Multiple tags + else if (tagArray.length > 1) { + var lastTag = row == row.parentNode.lastChild; + + Zotero.DB.beginTransaction(); + + if (oldTagID) { + var oldValue = Zotero.Tags.getName(oldTagID); + // If old tag isn't in array, remove it + if (tagArray.indexOf(oldValue) == -1) { + this.item.removeTag(oldTagID); } - // Already exists + // If old tag is staying, restore the textbox + // immediately. This isn't strictly necessary, but it + // makes the transition nicer. else { - // Go back one, since we'll remove this below - if (this._tabDirection == 1) { - this._lastTabIndex--; - } + textbox.value = textbox.getAttribute('value'); + this.textboxToLabel(textbox); } } - } - - if (id) { - var elem = this.createValueElement( - value, - tabindex - ); - var row = textbox.parentNode; - row.replaceChild(elem, textbox); + this.item.addTags(tagArray); - this.updateRow(row, Zotero.Tags.get(id), tabindex); + Zotero.DB.commitTransaction(); - if (!unchanged) { - // Move row to appropriate place, alphabetically - var collation = Zotero.getLocaleCollation(); - var rows = row.parentNode; - var labels = rows.getElementsByAttribute('fieldname', 'tag'); - - rows.removeChild(row); - var currentTabIndex = elem.getAttribute('ztabindex'); - - var before = null; - var inserted = false; - for (var i=0; i 0) { - labels[i].setAttribute('ztabindex', newTabIndex); - continue; - } - - elem.setAttribute('ztabindex', newTabIndex); - rows.insertBefore(row, labels[i].parentNode); - inserted = true; - - // Adjust last tab index - if (this._tabDirection == -1) { - if (this._lastTabIndex > newTabIndex) { - this._lastTabIndex++; - } - } - else if (this._tabDirection == 1) { - if (this._lastTabIndex < newTabIndex) { - this._lastTabIndex--; - } - } - } - if (!inserted) { - elem.setAttribute('ztabindex', i + 1); - rows.appendChild(row); - } + if (lastTag) { + this._lastTabIndex = this.item.getTags().length; } + + this.reload(); } + // Single tag at end else { - // Just remove the row - // - // If there's an open popup, this throws NODE CANNOT BE FOUND - try { - var row = rows.removeChild(row); - } - catch (e) {} + row.parentNode.removeChild(row); + this.item.addTag(value); } ]]> @@ -769,16 +729,84 @@ + + + + + + - - - + - + + if (collation.compareString(1, name, labels[i].textContent) > 0) { + labels[i].setAttribute('ztabindex', index); + continue; + } + + elem.setAttribute('ztabindex', index); + rowsElement.insertBefore(row, labels[i].parentNode); + newTabIndex = index; + inserted = true; + } + if (!inserted) { + newTabIndex = i + 1; + elem.setAttribute('ztabindex', newTabIndex); + rowsElement.appendChild(row); + } + + return newTabIndex; + ]]> @@ -803,12 +831,35 @@ - - - + @@ -816,7 +867,7 @@