From 23e01fcefd07a05f3a59e696640bf4994df51ca8 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 9 Apr 2016 18:34:54 -0400 Subject: [PATCH] Fix saving to My Library if Zotero pane hasn't been opened --- chrome/content/zotero/browser.js | 22 ++++++------ chrome/content/zotero/zoteroPane.js | 22 ++++++------ test/tests/attachmentsTest.js | 2 +- test/tests/browserTest.js | 52 ++++++++++++++++++++++++++++- test/tests/data/snapshot/index.html | 22 +++++------- 5 files changed, 84 insertions(+), 36 deletions(-) diff --git a/chrome/content/zotero/browser.js b/chrome/content/zotero/browser.js index 375a18bba..5da833975 100644 --- a/chrome/content/zotero/browser.js +++ b/chrome/content/zotero/browser.js @@ -622,17 +622,19 @@ var Zotero_Browser = new function() { // Get libraryID and collectionID if(libraryID === undefined && ZoteroPane && !Zotero.isConnector) { - try { - if (!ZoteroPane.collectionsView.editable) { - Zotero_Browser.progress.cannotEditCollection(); - return; - } - - libraryID = ZoteroPane.getSelectedLibraryID(); - collection = ZoteroPane.getSelectedCollection(); - } catch(e) { - Zotero.debug(e, 1); + // Save to My Library by default if pane hasn't been opened + if (!ZoteroPane.collectionsView || !ZoteroPane.collectionsView.selectedTreeRow) { + libraryID = Zotero.Libraries.userLibraryID; } + else if (!ZoteroPane.collectionsView.editable) { + Zotero_Browser.progress.Translation.cannotEditCollection(); + return; + } + else { + libraryID = ZoteroPane.getSelectedLibraryID(); + } + + collection = ZoteroPane.getSelectedCollection(); } if (libraryID === Zotero.Libraries.publicationsLibraryID) { diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 8fede563e..82ba1f09a 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -3484,19 +3484,21 @@ var ZoteroPane = new function() return false; } - if (!row && this.collectionsView && this.collectionsView.selection) { + if (row == undefined && this.collectionsView && this.collectionsView.selection) { row = this.collectionsView.selection.currentIndex; } - if (!this.canEdit(row)) { - this.displayCannotEditLibraryMessage(); - return false; - } - - var collectionTreeRow = this.collectionsView.getRow(row); - if (collectionTreeRow.isPublications()) { - this.displayCannotAddToMyPublicationsMessage(); - return false; + if (row !== undefined) { + if (!this.canEdit(row)) { + this.displayCannotEditLibraryMessage(); + return false; + } + + var collectionTreeRow = this.collectionsView.getRow(row); + if (collectionTreeRow.isPublications()) { + this.displayCannotAddToMyPublicationsMessage(); + return false; + } } return this.addItemFromDocument(window.content.document, itemType, saveSnapshot, row); diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index f051d3fa2..6e5735327 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -171,7 +171,7 @@ describe("Zotero.Attachments", function() { assert.equal(attachment.getField('url'), "file://" + uri); // Check indexing - var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'works'); + var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'share your research'); assert.lengthOf(matches, 1); assert.propertyVal(matches[0], 'id', attachment.id); }) diff --git a/test/tests/browserTest.js b/test/tests/browserTest.js index 0e80d7478..10e8f88c2 100644 --- a/test/tests/browserTest.js +++ b/test/tests/browserTest.js @@ -1,10 +1,11 @@ "use strict"; describe("Zotero_Browser", function () { - var win; + var win, collection; before(function* () { win = yield loadBrowserWindow(); + collection = yield createDataObject('collection'); }); after(function* () { @@ -15,6 +16,55 @@ describe("Zotero_Browser", function () { Zotero.ProgressWindowSet.closeAll(); }) + + it("should save webpage to My Library if the Zotero pane hasn't been opened yet in a Firefox window", function* () { + var win = yield loadBrowserWindow(); + + var uri = OS.Path.join(getTestDataDirectory().path, "snapshot", "index.html"); + var deferred = Zotero.Promise.defer(); + win.Zotero_Browser.addDetectCallback(() => deferred.resolve()); + win.loadURI(uri); + yield deferred.promise; + + var promise = waitForWindow('chrome://zotero/content/progressWindow.xul', function (progressWin) { + assert.include( + progressWin.document.documentElement.textContent, + "Test" + ); + }); + yield win.Zotero_Browser.scrapeThisPage(); + yield promise; + + win.close(); + }); + + it("should save journal article to My Library if the Zotero pane hasn't been opened yet in a Firefox window", function* () { + Zotero.Prefs.set('lastViewedFolder', collection.collectionTreeViewID); + + var win = yield loadBrowserWindow(); + + var deferred = Zotero.Promise.defer(); + win.Zotero_Browser.addDetectCallback(() => deferred.resolve()); + var uri = OS.Path.join( + getTestDataDirectory().path, "metadata", "journalArticle-single.html" + ); + win.loadURI(uri); + yield deferred.promise; + + var promise1 = waitForWindow('chrome://zotero/content/progressWindow.xul', function (progressWin) {}); + var promise2 = waitForItemEvent('add'); + yield win.Zotero_Browser.scrapeThisPage(); + yield promise1; + var ids = yield promise2; + var items = Zotero.Items.get(ids); + assert.lengthOf(items, 1); + assert.equal(items[0].libraryID, Zotero.Libraries.userLibraryID); + assert.equal(Zotero.ItemTypes.getName(items[0].itemTypeID), 'journalArticle'); + assert.lengthOf(items[0].getCollections(), 0); + + win.close(); + }); + it("should save webpage to current collection", function* () { var uri = OS.Path.join(getTestDataDirectory().path, "snapshot", "index.html"); var deferred = Zotero.Promise.defer(); diff --git a/test/tests/data/snapshot/index.html b/test/tests/data/snapshot/index.html index 6ae791eae..898d0926d 100644 --- a/test/tests/data/snapshot/index.html +++ b/test/tests/data/snapshot/index.html @@ -1,16 +1,10 @@ - - - - - - - - - -

It works!

-

This is the default web page for this server.

-

The web server software is running but no content has been added, yet.

- - + + + Test + + +

Test

+

Zotero [zoh-TAIR-oh] is a free, easy-to-use tool to help you collect, organize, cite, and share your research sources.

+