Automatically switch to My Library on connector save in noneditable view

This commit is contained in:
Dan Stillman 2018-04-27 18:27:06 -04:00
parent c55b355548
commit d0e42a1186
2 changed files with 92 additions and 42 deletions

View file

@ -28,19 +28,46 @@ Zotero.Server.Connector = {
_waitingForSelection: {}, _waitingForSelection: {},
getSaveTarget: function () { getSaveTarget: function () {
var zp = Zotero.getActiveZoteroPane(), var zp = Zotero.getActiveZoteroPane();
library = null, var library = null;
collection = null, var collection = null;
editable = true; var editable = null;
try {
library = Zotero.Libraries.get(zp.getSelectedLibraryID()); if (zp && zp.collectionsView) {
collection = zp.getSelectedCollection(); if (zp.collectionsView.editable) {
editable = zp.collectionsView.editable; library = Zotero.Libraries.get(zp.getSelectedLibraryID());
collection = zp.getSelectedCollection();
editable = true;
}
// If not editable, switch to My Library if it exists and is editable
else {
let userLibrary = Zotero.Libraries.userLibrary;
if (userLibrary && userLibrary.editable) {
Zotero.debug("Save target isn't editable -- switching to My Library");
// Don't wait for this, because we don't want to slow down all conenctor
// requests by making this function async
zp.collectionsView.selectByID(userLibrary.treeViewID);
library = userLibrary;
collection = null;
editable = true;
}
}
} }
catch (e) { else {
let id = Zotero.Prefs.get('lastViewedFolder'); let id = Zotero.Prefs.get('lastViewedFolder');
if (id) { if (id) {
({ library, collection, editable } = this.resolveTarget(id)); ({ library, collection, editable } = this.resolveTarget(id));
if (!editable) {
let userLibrary = Zotero.Libraries.userLibrary;
if (userLibrary && userLibrary.editable) {
Zotero.debug("Save target isn't editable -- switching to My Library");
let treeViewID = userLibrary.treeViewID;
Zotero.Prefs.set('lastViewedFolder', treeViewID);
({ library, collection, editable } = this.resolveTarget(treeViewID));
}
}
} }
} }
@ -48,7 +75,7 @@ Zotero.Server.Connector = {
// (which should never be the case anymore) // (which should never be the case anymore)
if (!library) { if (!library) {
let userLibrary = Zotero.Libraries.userLibrary; let userLibrary = Zotero.Libraries.userLibrary;
if (userLibrary) { if (userLibrary && userLibrary.editable) {
library = userLibrary; library = userLibrary;
} }
} }
@ -152,9 +179,9 @@ Zotero.Server.Connector.SaveSession.prototype.update = async function (targetID,
this._currentTags = tags || ""; this._currentTags = tags || "";
// Select new destination in collections pane // Select new destination in collections pane
var win = Zotero.getActiveZoteroPane(); var zp = Zotero.getActiveZoteroPane();
if (win && win.collectionsView) { if (zp && zp.collectionsView) {
await win.collectionsView.selectByID(targetID); await zp.collectionsView.selectByID(targetID);
} }
// If window is closed, select target collection re-open // If window is closed, select target collection re-open
else { else {
@ -189,12 +216,12 @@ Zotero.Server.Connector.SaveSession.prototype.update = async function (targetID,
await this._updateItems(this._items); await this._updateItems(this._items);
// If a single item was saved, select it (or its parent, if it now has one) // If a single item was saved, select it (or its parent, if it now has one)
if (win && win.collectionsView && this._items.size == 1) { if (zp && zp.collectionsView && this._items.size == 1) {
let item = Array.from(this._items)[0]; let item = Array.from(this._items)[0];
item = item.isTopLevelItem() ? item : item.parentItem; item = item.isTopLevelItem() ? item : item.parentItem;
// Don't select if in trash // Don't select if in trash
if (!item.deleted) { if (!item.deleted) {
await win.selectItem(item.id); await zp.selectItem(item.id);
} }
} }
}; };
@ -485,6 +512,8 @@ Zotero.Server.Connector.SavePage.prototype = {
*/ */
init: function(url, data, sendResponseCallback) { init: function(url, data, sendResponseCallback) {
var { library, collection, editable } = Zotero.Server.Connector.getSaveTarget(); var { library, collection, editable } = Zotero.Server.Connector.getSaveTarget();
// Shouldn't happen as long as My Library exists
if (!library.editable) { if (!library.editable) {
Zotero.logError("Can't add item to read-only library " + library.name); Zotero.logError("Can't add item to read-only library " + library.name);
return sendResponseCallback(500, "application/json", JSON.stringify({ libraryEditable: false })); return sendResponseCallback(500, "application/json", JSON.stringify({ libraryEditable: false }));
@ -601,7 +630,7 @@ Zotero.Server.Connector.SaveItems.prototype = {
} }
yield session.update(targetID); yield session.update(targetID);
// TODO: Default to My Library root, since it's changeable // Shouldn't happen as long as My Library exists
if (!library.editable) { if (!library.editable) {
Zotero.logError("Can't add item to read-only library " + library.name); Zotero.logError("Can't add item to read-only library " + library.name);
return [500, "application/json", JSON.stringify({ libraryEditable: false })]; return [500, "application/json", JSON.stringify({ libraryEditable: false })];
@ -719,7 +748,7 @@ Zotero.Server.Connector.SaveSnapshot.prototype = {
} }
await session.update(collection ? collection.treeViewID : library.treeViewID); await session.update(collection ? collection.treeViewID : library.treeViewID);
// TODO: Default to My Library root, since it's changeable // Shouldn't happen as long as My Library exists
if (!library.editable) { if (!library.editable) {
Zotero.logError("Can't add item to read-only library " + library.name); Zotero.logError("Can't add item to read-only library " + library.name);
return [500, "application/json", JSON.stringify({ libraryEditable: false })]; return [500, "application/json", JSON.stringify({ libraryEditable: false })];
@ -955,6 +984,8 @@ Zotero.Server.Connector.Import.prototype = {
translate.setTranslator(translators[0]); translate.setTranslator(translators[0]);
var { library, collection, editable } = Zotero.Server.Connector.getSaveTarget(); var { library, collection, editable } = Zotero.Server.Connector.getSaveTarget();
var libraryID = library.libraryID; var libraryID = library.libraryID;
// Shouldn't happen as long as My Library exists
if (!library.editable) { if (!library.editable) {
Zotero.logError("Can't import into read-only library " + library.name); Zotero.logError("Can't import into read-only library " + library.name);
return [500, "application/json", JSON.stringify({ libraryEditable: false })]; return [500, "application/json", JSON.stringify({ libraryEditable: false })];

View file

@ -163,7 +163,7 @@ describe("Connector Server", function () {
}); });
it("should respond with 500 if read-only library is selected", function* () { it("should switch to My Library if read-only library is selected", function* () {
var group = yield createGroup({ var group = yield createGroup({
editable: false editable: false
}); });
@ -188,7 +188,8 @@ describe("Connector Server", function () {
uri: "http://example.com" uri: "http://example.com"
}; };
var req = yield Zotero.HTTP.request( var promise = waitForItemEvent('add');
var reqPromise = Zotero.HTTP.request(
'POST', 'POST',
connectorServerPath + "/connector/saveItems", connectorServerPath + "/connector/saveItems",
{ {
@ -200,13 +201,19 @@ describe("Connector Server", function () {
} }
); );
assert.equal(req.status, 500); // My Library be selected, and the item should be in it
assert.isFalse(JSON.parse(req.responseText).libraryEditable); var ids = yield promise;
// The selection should remain
assert.equal( assert.equal(
win.ZoteroPane.collectionsView.getSelectedLibraryID(), group.libraryID win.ZoteroPane.collectionsView.getSelectedLibraryID(),
Zotero.Libraries.userLibraryID
); );
assert.lengthOf(ids, 1);
var item = Zotero.Items.get(ids[0]);
assert.equal(item.libraryID, Zotero.Libraries.userLibraryID);
assert.equal(Zotero.ItemTypes.getName(item.itemTypeID), 'newspaperArticle');
var req = yield reqPromise;
assert.equal(req.status, 201);
}); });
it("should use the provided proxy to deproxify item url", function* () { it("should use the provided proxy to deproxify item url", function* () {
@ -355,14 +362,15 @@ describe("Connector Server", function () {
stub.restore(); stub.restore();
}); });
it("should respond with 500 if a read-only library is selected", function* () { it("should switch to My Library if a read-only library is selected", function* () {
var group = yield createGroup({ var group = yield createGroup({
editable: false editable: false
}); });
yield selectLibrary(win, group.libraryID); yield selectLibrary(win, group.libraryID);
yield waitForItemsLoad(win); yield waitForItemsLoad(win);
var req = yield Zotero.HTTP.request( var promise = waitForItemEvent('add');
var reqPromise = Zotero.HTTP.request(
'POST', 'POST',
connectorServerPath + "/connector/saveSnapshot", connectorServerPath + "/connector/saveSnapshot",
{ {
@ -377,13 +385,18 @@ describe("Connector Server", function () {
} }
); );
assert.equal(req.status, 500); // My Library be selected, and the item should be in it
assert.isFalse(JSON.parse(req.responseText).libraryEditable); var ids = yield promise;
// The selection should remain
assert.equal( assert.equal(
win.ZoteroPane.collectionsView.getSelectedLibraryID(), group.libraryID win.ZoteroPane.collectionsView.getSelectedLibraryID(),
Zotero.Libraries.userLibraryID
); );
assert.lengthOf(ids, 1);
var item = Zotero.Items.get(ids[0]);
assert.equal(item.libraryID, Zotero.Libraries.userLibraryID);
var req = yield reqPromise;
assert.equal(req.status, 201);
}); });
}); });
@ -714,14 +727,15 @@ describe("Connector Server", function () {
var collection = yield createDataObject('collection'); var collection = yield createDataObject('collection');
yield waitForItemsLoad(win); yield waitForItemsLoad(win);
var addedItemIDPromise = waitForItemEvent('add');
var resource = `@book{test1, var resource = `@book{test1,
title={Test1}, title={Test1},
author={Owl}, author={Owl},
year={1000}, year={1000},
publisher={Curly Braces Publishing} publisher={Curly Braces Publishing}
}`; }`;
var response = yield Zotero.HTTP.request(
var addedItemIDsPromise = waitForItemEvent('add');
var req = yield Zotero.HTTP.request(
'POST', 'POST',
endpoint, endpoint,
{ {
@ -729,15 +743,15 @@ describe("Connector Server", function () {
body: resource body: resource
} }
); );
assert.equal(response.status, 201); assert.equal(req.status, 201);
assert.equal(JSON.parse(response.responseText)[0].title, 'Test1'); assert.equal(JSON.parse(req.responseText)[0].title, 'Test1');
let itemId = yield addedItemIDPromise; let itemIDs = yield addedItemIDsPromise;
assert.isTrue(collection.hasItem(itemId[0])); assert.isTrue(collection.hasItem(itemIDs[0]));
}); });
it('should respond with 500 if read-only library is selected', function* () { it('should switch to My Library if read-only library is selected', function* () {
var group = yield createGroup({ var group = yield createGroup({
editable: false editable: false
}); });
@ -750,6 +764,8 @@ describe("Connector Server", function () {
year={1000}, year={1000},
publisher={Curly Braces Publishing} publisher={Curly Braces Publishing}
}`; }`;
var addedItemIDsPromise = waitForItemEvent('add');
var req = yield Zotero.HTTP.request( var req = yield Zotero.HTTP.request(
'POST', 'POST',
endpoint, endpoint,
@ -759,13 +775,16 @@ describe("Connector Server", function () {
successCodes: false successCodes: false
} }
); );
assert.equal(req.status, 500);
assert.isFalse(JSON.parse(req.responseText).libraryEditable);
// The selection should remain assert.equal(req.status, 201);
assert.equal( assert.equal(
win.ZoteroPane.collectionsView.getSelectedLibraryID(), group.libraryID win.ZoteroPane.collectionsView.getSelectedLibraryID(),
Zotero.Libraries.userLibraryID
); );
let itemIDs = yield addedItemIDsPromise;
var item = Zotero.Items.get(itemIDs[0]);
assert.equal(item.libraryID, Zotero.Libraries.userLibraryID);
}); });
}); });
}); });