Refactor ProgressWindow translation messages show for feed items

This commit is contained in:
Adomas Venčkauskas 2016-02-09 20:48:27 +00:00 committed by Dan Stillman
parent e206b0af5e
commit 9e88969f3d
5 changed files with 148 additions and 91 deletions

View file

@ -596,14 +596,7 @@ var Zotero_Browser = new function() {
*/
this.performTranslation = Zotero.Promise.coroutine(function* (translate, libraryID, collection) {
if (Zotero.locked) {
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapeError"));
var desc = Zotero.localeJoin([
Zotero.getString('general.operationInProgress'),
Zotero.getString('general.operationInProgress.waitUntilFinishedAndTryAgain')
]);
Zotero_Browser.progress.addDescription(desc);
Zotero_Browser.progress.show();
Zotero_Browser.progress.startCloseTimer(8000);
Zotero_Browser.progress.Translation.operationInProgress();
return;
}
@ -616,11 +609,7 @@ var Zotero_Browser = new function() {
if(libraryID === undefined && ZoteroPane && !Zotero.isConnector) {
try {
if (!ZoteroPane.collectionsView.editable) {
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapeError"));
var desc = Zotero.getString('save.error.cannotMakeChangesToCollection');
Zotero_Browser.progress.addDescription(desc);
Zotero_Browser.progress.show();
Zotero_Browser.progress.startCloseTimer(8000);
Zotero_Browser.progress.cannotEditCollection();
return;
}
@ -632,46 +621,16 @@ var Zotero_Browser = new function() {
}
if (libraryID === Zotero.Libraries.publicationsLibraryID) {
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapeError"));
var desc = Zotero.getString('save.error.cannotAddToMyPublications');
Zotero_Browser.progress.addDescription(desc);
Zotero_Browser.progress.show();
Zotero_Browser.progress.startCloseTimer(8000);
Zotero_Browser.progress.Translation.cannotAddToPublications();
return;
}
if (Zotero.Feeds.get(libraryID)) {
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapeError"));
Zotero_Browser.progress.addDescription(Zotero.getString('save.error.cannotAddToFeed'));
Zotero_Browser.progress.show();
Zotero_Browser.progress.startCloseTimer(8000);
Zotero_Browser.progress.Translation.cannotAddToFeed();
return;
}
if(Zotero.isConnector) {
Zotero.Connector.callMethod("getSelectedCollection", {}, function(response, status) {
if(status !== 200) {
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scraping"));
} else {
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapingTo"),
"chrome://zotero/skin/treesource-"+(response.id ? "collection" : "library")+".png",
response.name+"\u2026");
}
});
} else {
var name;
if(collection) {
name = collection.name;
} else if(libraryID) {
name = Zotero.Libraries.getName(libraryID);
} else {
name = Zotero.getString("pane.collections.library");
}
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapingTo"),
"chrome://zotero/skin/treesource-"+(collection ? "collection" : "library")+".png",
name+"\u2026");
}
Zotero_Browser.progress.Translation.scrapingTo(libraryID, collection);
translate.clearHandlers("done");
translate.clearHandlers("itemDone");
@ -679,48 +638,20 @@ var Zotero_Browser = new function() {
var deferred = Zotero.Promise.defer();
translate.setHandler("done", function(obj, returnValue) {
if(!returnValue) {
Zotero_Browser.progress.show();
Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapeError"));
// Include link to translator troubleshooting page
var url = "https://www.zotero.org/support/troubleshooting_translator_issues";
var linkText = '<a href="' + url + '" tooltiptext="' + url + '">'
+ Zotero.getString('ingester.scrapeErrorDescription.linkText') + '</a>';
Zotero_Browser.progress.addDescription(Zotero.getString("ingester.scrapeErrorDescription", linkText));
Zotero_Browser.progress.startCloseTimer(8000);
} else {
Zotero_Browser.progress.startCloseTimer();
}
translate.setHandler("done", function() {
Zotero_Browser.progress.Translation.doneHandler.apply(Zotero_Browser.progress.Translation, arguments);
Zotero_Browser.isScraping = false;
deferred.resolve();
});
translate.setHandler("itemDone", function(obj, dbItem, item) {
Zotero_Browser.progress.show();
var itemProgress = new Zotero_Browser.progress.ItemProgress(Zotero.ItemTypes.getImageSrc(item.itemType),
item.title);
itemProgress.setProgress(100);
for(var i=0; i<item.attachments.length; i++) {
var attachment = item.attachments[i];
_attachmentsMap.set(attachment,
new Zotero_Browser.progress.ItemProgress(
Zotero.Utilities.determineAttachmentIcon(attachment),
attachment.title, itemProgress));
}
translate.setHandler("itemDone", function() {
let handler = Zotero_Browser.progress.Translation.itemDoneHandler(_attachmentsMap);
handler.apply(Zotero_Browser.progress.Translation, arguments);
});
translate.setHandler("attachmentProgress", function(obj, attachment, progress, error) {
var itemProgress = _attachmentsMap.get(attachment);
if(progress === false) {
itemProgress.setError();
} else {
itemProgress.setProgress(progress);
if(progress === 100) {
itemProgress.setIcon(Zotero.Utilities.determineAttachmentIcon(attachment));
}
}
translate.setHandler("attachmentProgress", function() {
let handler = Zotero_Browser.progress.Translation.attachmentProgressHandler(_attachmentsMap);
handler.apply(Zotero_Browser.progress.Translation, arguments);
});
translate.translate({

View file

@ -206,8 +206,29 @@ Zotero.FeedItem.prototype.forceEraseTx = function(options) {
* @return {Promise<FeedItem|Item>} translated feed item
*/
Zotero.FeedItem.prototype.translate = Zotero.Promise.coroutine(function* (libraryID, collectionID) {
if (Zotero.locked) {
Zotero.debug('Zotero locked, skipping feed item translation');
return;
}
let deferred = Zotero.Promise.defer();
let error = function(e) { Zotero.debug(e, 1); deferred.reject(e); };
let translate = new Zotero.Translate.Web();
if (libraryID) {
// Show progress notifications when scraping to a library
var win = Services.wm.getMostRecentWindow("navigator:browser");
translate.clearHandlers("done");
translate.clearHandlers("itemDone");
translate.setHandler("done", win.Zotero_Browser.progress.Translation.doneHandler);
translate.setHandler("itemDone", win.Zotero_Browser.progress.Translation.itemDoneHandler());
let collection;
if (collectionID) {
collection = yield Zotero.Collections.getAsync(collectionID);
}
win.Zotero_Browser.progress.show();
win.Zotero_Browser.progress.Translation.scrapingTo(libraryID, collection);
}
// Load document
let hiddenBrowser = Zotero.HTTP.processDocuments(
@ -218,7 +239,6 @@ Zotero.FeedItem.prototype.translate = Zotero.Promise.coroutine(function* (librar
let doc = yield deferred.promise;
// Set translate document
let translate = new Zotero.Translate.Web();
translate.setDocument(doc);
// Load translators
@ -235,8 +255,10 @@ Zotero.FeedItem.prototype.translate = Zotero.Promise.coroutine(function* (librar
deferred = Zotero.Promise.defer();
if (libraryID) {
return translate.translate({libraryID, collections: collectionID ? [collectionID] : false})
let result = yield translate.translate({libraryID, collections: collectionID ? [collectionID] : false})
.then(items => items ? items[0] : false);
Zotero.Browser.deleteHiddenBrowser(hiddenBrowser);
return result;
}
// Clear these to prevent saving
@ -255,8 +277,7 @@ Zotero.FeedItem.prototype.translate = Zotero.Promise.coroutine(function* (librar
for (let field of deleteFields) {
delete itemData[field];
}
// TODO: handle no items like the ones in french history studies feed
// set new translated data for item
this.fromJSON(itemData);
this.isTranslated = true;
this.forceSaveTx();

View file

@ -365,6 +365,110 @@ Zotero.ProgressWindow = function(_window = null) {
this._hbox.style.filter = "";
});
this.Translation = {};
this.Translation.operationInProgress = function() {
var desc = Zotero.localeJoin([
Zotero.getString('general.operationInProgress'),
Zotero.getString('general.operationInProgress.waitUntilFinishedAndTryAgain')
]);
self.Translation._scrapeError(desc);
};
this.Translation.cannotEditCollection = function() {
var desc = Zotero.getString('save.error.cannotMakeChangesToCollection');
self.Translation._scrapeError(desc);
};
this.Translation.cannotAddToPublications = function () {
var desc = Zotero.getString('save.error.cannotAddToMyPublications');
self.Translation._scrapeError(desc);
};
this.Translation.cannotAddToFeed = function() {
var desc = Zotero.getString('save.error.cannotAddToFeed');
self.Translation._scrapeError(desc);
};
this.Translation.scrapingTo = function(libraryID, collection) {
if(Zotero.isConnector) {
Zotero.Connector.callMethod("getSelectedCollection", {}, function(response, status) {
if(status !== 200) {
self.changeHeadline(Zotero.getString("ingester.scraping"));
} else {
self.changeHeadline(Zotero.getString("ingester.scrapingTo"),
"chrome://zotero/skin/treesource-"+(response.id ? "collection" : "library")+".png",
response.name+"\u2026");
}
});
} else {
var name;
if(collection) {
name = collection.name;
} else if(libraryID) {
name = Zotero.Libraries.getName(libraryID);
} else {
name = Zotero.getString("pane.collections.library");
}
self.changeHeadline(Zotero.getString("ingester.scrapingTo"),
"chrome://zotero/skin/treesource-"+(collection ? "collection" : "library")+".png",
name+"\u2026");
}
};
this.Translation.doneHandler = function(obj, returnValue) {
if(!returnValue) {
// Include link to translator troubleshooting page
var url = "https://www.zotero.org/support/troubleshooting_translator_issues";
var linkText = '<a href="' + url + '" tooltiptext="' + url + '">'
+ Zotero.getString('ingester.scrapeErrorDescription.linkText') + '</a>';
var desc = Zotero.getString("ingester.scrapeErrorDescription", linkText)
self.Translation._scrapeError(desc);
} else {
self.startCloseTimer();
}
};
this.Translation.itemDoneHandler = function(_attachmentsMap) {
_attachmentsMap = _attachmentsMap || new WeakMap();
return function(obj, dbItem, item) {
self.show();
var itemProgress = new self.ItemProgress(Zotero.ItemTypes.getImageSrc(item.itemType),
item.title);
itemProgress.setProgress(100);
for(var i=0; i<item.attachments.length; i++) {
var attachment = item.attachments[i];
_attachmentsMap.set(attachment,
new self.ItemProgress(
Zotero.Utilities.determineAttachmentIcon(attachment),
attachment.title, itemProgress));
}
}
};
this.Translation.attachmentProgressHandler = function(_attachmentsMap) {
_attachmentsMap = _attachmentsMap || new WeakMap();
return function(obj, attachment, progress, error) {
var itemProgress = _attachmentsMap.get(attachment);
if(progress === false) {
itemProgress.setError();
} else {
itemProgress.setProgress(progress);
if(progress === 100) {
itemProgress.setIcon(Zotero.Utilities.determineAttachmentIcon(attachment));
}
}
}
};
this.Translation._scrapeError = function(description) {
self.changeHeadline(Zotero.getString("ingester.scrapeError"));
self.addDescription(description);
self.show();
self.startCloseTimer(8000)
}
function _onWindowLoaded() {
_windowLoading = false;
_windowLoaded = true;

View file

@ -290,10 +290,10 @@ describe("Zotero.Feed", function() {
assert.equal(feed.unreadCount, 2);
});
it('should not re-add deleted items, but add new ones', function* () {
let feedItems = yield Zotero.FeedItems.getAll(feed.id);
yield feedItems[1].forceEraseTx();
let feedItem = yield Zotero.FeedItems.getAsyncByGUID("http://liftoff.msfc.nasa.gov/2003/05/20.html#item570:"+feed.id);
yield feedItem.forceEraseTx();
feedItems = yield Zotero.FeedItems.getAll(feed.id);
let feedItems = yield Zotero.FeedItems.getAll(feed.id);
for (let feedItem of feedItems) {
feedItem.isRead = true;
yield feedItem.forceSaveTx();

View file

@ -113,7 +113,8 @@ describe("Zotero.Feeds", function () {
yield Zotero.Feeds.scheduleNextFeedCheck();
assert.equal(Zotero.Promise.delay.args[0][0], 1000*60*60);
// Allow a propagation delay of 5000ms
assert.isTrue(Zotero.Promise.delay.args[0][0] - 1000*60*60 <= 5000);
Zotero.Feeds.scheduleNextFeedCheck.restore();
Zotero.Promise.delay.restore();