From 2a55e56f3fee41f2f3526f21a64eb6eb349ec052 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 29 Jun 2016 05:24:14 -0400 Subject: [PATCH] Save open field when selecting from an item pane context menu If a field is open and the user right-clicks on another field (e.g., swap names, creator type, transform text), any changed value in the open field was lost. Also: - Don't show swap-names menu in single-field mode I can't quite get programmatic access to context menus to work correctly, so tests are disabled for now. (They work individually, but not together.) --- chrome/content/zotero/bindings/itembox.xml | 78 +++++++++++--------- test/tests/itemPaneTest.js | 83 ++++++++++++++++++++++ 2 files changed, 129 insertions(+), 32 deletions(-) diff --git a/chrome/content/zotero/bindings/itembox.xml b/chrome/content/zotero/bindings/itembox.xml index 0fb381911..66ad49263 100644 --- a/chrome/content/zotero/bindings/itembox.xml +++ b/chrome/content/zotero/bindings/itembox.xml @@ -786,7 +786,7 @@ firstlast.lastChild.setAttribute('hidden', true); } - if (this.editable) { + if (this.editable && fieldMode == 0) { firstlast.setAttribute('contextmenu', 'zotero-creator-transform-menu'); } @@ -2021,28 +2021,32 @@ - - - + @@ -2083,7 +2087,7 @@ @@ -2117,6 +2129,7 @@ @return {Promise} --> + - - - + }, this); + ]]> @@ -2394,7 +2408,7 @@ var hideTransforms = !exists || !!fieldMode; return !hideTransforms;"> + oncommand="document.getBindingParent(this).swapNames(event);"/> diff --git a/test/tests/itemPaneTest.js b/test/tests/itemPaneTest.js index 58704979a..1528e8668 100644 --- a/test/tests/itemPaneTest.js +++ b/test/tests/itemPaneTest.js @@ -27,6 +27,89 @@ describe("Item pane", function () { yield Zotero.Items.erase(id); }) + + + it.skip("should swap creator names", function* () { + var item = new Zotero.Item('book'); + item.setCreators([ + { + firstName: "First", + lastName: "Last", + creatorType: "author" + } + ]); + yield item.saveTx(); + + var itemBox = doc.getElementById('zotero-editpane-item-box'); + var label = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'creator-0-lastName')[0]; + var parent = label.parentNode; + assert.isTrue(parent.hasAttribute('contextmenu')); + + var menupopup = doc.getAnonymousNodes(itemBox)[0] + .getElementsByAttribute('id', 'zotero-creator-transform-menu')[0]; + // Fake a right-click + doc.popupNode = parent; + menupopup.openPopup( + parent, "after_start", 0, 0, true, false, new MouseEvent('click', { button: 2 }) + ); + var menuitem = menupopup.getElementsByTagName('menuitem')[0]; + menuitem.click(); + yield waitForItemEvent('modify'); + + var creator = item.getCreators()[0]; + assert.propertyVal(creator, 'firstName', 'Last'); + assert.propertyVal(creator, 'lastName', 'First'); + }); + + + it("shouldn't show Swap Names menu for single-field mode", function* () { + var item = new Zotero.Item('book'); + item.setCreators([ + { + name: "Name", + creatorType: "author" + } + ]); + yield item.saveTx(); + + var itemBox = doc.getElementById('zotero-editpane-item-box'); + var label = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'creator-0-lastName')[0]; + assert.isFalse(label.parentNode.hasAttribute('contextmenu')); + }); + + + // Note: This issue applies to all context menus in the item box (text transform, name swap), + // though the others aren't tested. This might go away with the XUL->HTML transition. + it.skip("should save open field after changing creator type", function* () { + var item = new Zotero.Item('book'); + item.setCreators([ + { + firstName: "First", + lastName: "Last", + creatorType: "author" + } + ]); + var id = yield item.saveTx(); + + var itemBox = doc.getElementById('zotero-editpane-item-box'); + var label = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'place')[1]; + label.click(); + var textbox = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'place')[1]; + textbox.value = "Place"; + + var menuLabel = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'creator-0-typeID')[0]; + menuLabel.click(); + var menupopup = itemBox._creatorTypeMenu; + var menuItems = menupopup.getElementsByTagName('menuitem'); + menuItems[1].click(); + yield waitForItemEvent('modify'); + + assert.equal(item.getField('place'), 'Place'); + assert.equal(Zotero.CreatorTypes.getName(item.getCreators()[0].creatorTypeID), 'contributor'); + + // Wait for no-op saveTx() + yield Zotero.Promise.delay(1); + }); }) describe("Attachment pane", function () {