www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit a750203f4fa3a9ade79cc57dde9d96e7f147c899
parent 283dba78567ecf18d35760fd5a8ae47bfcffaebf
Author: Simon Kornblith <simon@simonster.com>
Date:   Sat,  9 Jun 2012 14:01:02 -0400

Hook up attachment progress notifications in Firefox UI, and simplify a lot of old code.

Closes #3

Diffstat:
Mchrome/content/zotero/browser.js | 104++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mchrome/content/zotero/xpcom/connector/translate_item.js | 74+++-----------------------------------------------------------------------
Mchrome/content/zotero/xpcom/progressWindow.js | 292++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mchrome/content/zotero/xpcom/server_connector.js | 19++++++++++++-------
Mchrome/content/zotero/xpcom/translation/translate.js | 141++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mchrome/content/zotero/xpcom/translation/translate_item.js | 2--
Mchrome/content/zotero/xpcom/utilities.js | 11+++++++++++
Mchrome/skin/default/zotero/zotero.css | 4++--
8 files changed, 330 insertions(+), 317 deletions(-)

diff --git a/chrome/content/zotero/browser.js b/chrome/content/zotero/browser.js @@ -50,8 +50,6 @@ var Zotero_Browser = new function() { this.tabClose = tabClose; this.resize = resize; this.updateStatus = updateStatus; - this.finishScraping = finishScraping; - this.itemDone = itemDone; this.tabbrowser = null; this.appcontent = null; @@ -447,51 +445,6 @@ var Zotero_Browser = new function() { } } - /* - * Callback to be executed when scraping is complete - */ - function finishScraping(obj, returnValue) { - if(!returnValue) { - Zotero_Browser.progress.show(); - Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapeError")); - // Include link to Known Translator Issues page - var url = "http://www.zotero.org/documentation/known_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(); - } - Zotero_Browser.isScraping = false; - } - - - /* - * Callback to be executed when an item has been finished - */ - function itemDone(obj, dbItem, item, collection) { - var title = item.title; - var icon = Zotero.ItemTypes.getImageSrc(item.itemType); - Zotero_Browser.progress.show(); - Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scraping")); - Zotero_Browser.progress.addLines([title], [icon]); - - // add item to collection, if one was specified - if(collection) { - collection.addItem(dbItem.id); - } - - if(Zotero_Browser.isScraping) { - // initialize close timer between item saves in case translator doesn't call done - Zotero_Browser.progress.startCloseTimer(10000); // is this long enough? - } else { - // if we aren't supposed to be scraping now, the translator is broken; assume we're - // done - Zotero_Browser.progress.startCloseTimer(); - } - } - /** * Called when status bar icon is right-clicked */ @@ -551,9 +504,9 @@ var Zotero_Browser = new function() { function _constructLookupFunction(tab, success) { return function(e) { tab.page.translate.setTranslator(tab.page.translators[0]); - tab.page.translate.clearHandlers("itemsDone"); + tab.page.translate.clearHandlers("done"); tab.page.translate.clearHandlers("itemDone"); - tab.page.translate.setHandler("itemsDone", function(obj, status) { + tab.page.translate.setHandler("done", function(obj, status) { if(status) { success(e, obj); Zotero_Browser.progress.close(); @@ -716,6 +669,7 @@ Zotero_Browser.Tab.prototype._attemptLocalFileImport = function(doc) { Zotero_Browser.Tab.prototype.translate = function(libraryID, collectionID, translator) { if(this.page.translators && this.page.translators.length) { Zotero_Browser.progress.show(); + Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scraping")); Zotero_Browser.isScraping = true; if(collectionID) { @@ -730,11 +684,57 @@ Zotero_Browser.Tab.prototype.translate = function(libraryID, collectionID, trans // use first translator available this.page.translate.setTranslator(translator ? translator : this.page.translators[0]); - this.page.translate.clearHandlers("itemsDone"); + this.page.translate.clearHandlers("done"); this.page.translate.clearHandlers("itemDone"); - this.page.translate.setHandler("itemsDone", function(obj, item) { Zotero_Browser.finishScraping(obj, item) }); - this.page.translate.setHandler("itemDone", function(obj, dbItem, item) { Zotero_Browser.itemDone(obj, dbItem, item, collection) }); + this.page.translate.setHandler("done", function(obj, returnValue) { + if(!returnValue) { + Zotero_Browser.progress.show(); + Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapeError")); + // Include link to Known Translator Issues page + var url = "http://www.zotero.org/documentation/known_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(); + } + Zotero_Browser.isScraping = false; + }); + + var attachmentsMap = new WeakMap(); + + this.page.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)); + } + + // add item to collection, if one was specified + if(collection) { + collection.addItem(dbItem.id); + } + }); + + this.page.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)); + } + } + }); this.page.translate.translate(libraryID); } diff --git a/chrome/content/zotero/xpcom/connector/translate_item.js b/chrome/content/zotero/xpcom/connector/translate_item.js @@ -79,23 +79,10 @@ Zotero.Translate.ItemSaver.prototype = { payload.cookie = this._cookie; } - Zotero.Connector.callMethod("saveItems", payload, function(data, status) { - if(data !== false) { + Zotero.Connector.callMethod("saveItems", payload, function(success, status) { + if(success !== false) { Zotero.debug("Translate: Save via Standalone succeeded"); - var haveAttachments = false; - if(data.items) { - for(var i=0; i<data.items.length; i++) { - var attachments = items[i].attachments = data.items[i].attachments; - for(var j=0; j<attachments.length; j++) { - if(attachments[j].id) { - attachmentCallback(attachments[j], 0); - haveAttachments = true; - } - } - } - } callback(true, items); - if(haveAttachments) me._pollForProgress(items, attachmentCallback); } else if(Zotero.isFx) { callback(false, new Error("Save via Standalone failed with "+status)); } else { @@ -105,60 +92,6 @@ Zotero.Translate.ItemSaver.prototype = { }, /** - * Polls for updates to attachment progress - * @param items Items in Zotero.Item.toArray() format - * @param {Function} attachmentCallback A callback that receives information about attachment - * save progress. The callback will be called as attachmentCallback(attachment, false, error) - * on failure or attachmentCallback(attachment, progressPercent) periodically during saving. - * attachmentCallback() will be called with all attachments that will be saved - */ - "_pollForProgress":function(items, attachmentCallback) { - var attachments = []; - var progressIDs = []; - var previousStatus = []; - for(var i=0; i<items.length; i++) { - var itemAttachments = items[i].attachments; - for(var j=0; j<itemAttachments.length; j++) { - if(itemAttachments[j].id) { - attachments.push(itemAttachments[j]); - progressIDs.push(itemAttachments[j].id); - previousStatus.push(0); - } - } - } - - var nPolls = 0; - var poll = function() { - Zotero.Connector.callMethod("attachmentProgress", progressIDs, function(currentStatus, status) { - if(currentStatus) { - for(var i=0; i<attachments.length; i++) { - if(currentStatus[i] === 100 || currentStatus[i] === false) { - attachmentCallback(attachments[i], currentStatus[i]); - attachments.splice(i, 1); - progressIDs.splice(i, 1); - previousStatus.splice(i, 1); - currentStatus.splice(i, 1); - i--; - } else if(currentStatus[i] !== previousStatus[i]) { - attachmentCallback(attachments[i], currentStatus[i]); - previousStatus[i] = currentStatus[i]; - } - } - - if(nPolls++ < 60 && attachments.length) { - setTimeout(poll, 1000); - } - } else { - for(var i=0; i<attachments.length; i++) { - attachmentCallback(attachments[i], false, "Lost connection to Zotero Standalone"); - } - } - }); - }; - poll(); - }, - - /** * Saves items to server * @param items Items in Zotero.Item.toArray() format * @param {Function} callback A callback to be executed when saving is complete. If saving @@ -168,7 +101,6 @@ Zotero.Translate.ItemSaver.prototype = { * @param {Function} attachmentCallback A callback that receives information about attachment * save progress. The callback will be called as attachmentCallback(attachment, false, error) * on failure or attachmentCallback(attachment, progressPercent) periodically during saving. - * attachmentCallback() will be called with all attachments that will be saved */ "_saveToServer":function(items, callback, attachmentCallback) { var newItems = [], typedArraysSupported = false; @@ -183,7 +115,7 @@ Zotero.Translate.ItemSaver.prototype = { if(typedArraysSupported) { // Get rid of attachments that we won't be able to save properly and add ids for(var j=0; j<item.attachments.length; j++) { - if(!item.attachments[j].url || item.attachments[j].mimeType === "text/html") { + if(item.attachments[j].url && item.attachments[j].mimeType !== "text/html") { item.attachments.splice(j--, 1); } else { item.attachments[j].id = Zotero.Utilities.randomString(); diff --git a/chrome/content/zotero/xpcom/progressWindow.js b/chrome/content/zotero/xpcom/progressWindow.js @@ -107,30 +107,22 @@ Zotero.ProgressWindowSet = new function() { * Pass the active window into the constructor */ Zotero.ProgressWindow = function(_window){ - this.show = show; - this.changeHeadline = changeHeadline; - this.addLines = addLines; - this.addDescription = addDescription; - this.startCloseTimer = startCloseTimer; - this.close = close; + var self = this, + _window = null, + _progressWindow = null, + _windowLoaded = false, + _windowLoading = false, + _timeoutID = false, + _closing = false, + _mouseWasOver = false, + _deferredUntilWindowLoad = [], + _deferredUntilWindowLoadThis = [], + _deferredUntilWindowLoadArgs = []; - var _window = null; - - var _progressWindow = null; - var _windowLoaded = false; - var _windowLoading = false; - var _timeoutID = false; - var _mouseWasOver = false - - // keep track of all of these things in case they're called before we're - // done loading the progress window - var _loadHeadline = ''; - var _loadLines = []; - var _loadIcons = []; - var _loadDescription = null; - - - function show() { + /** + * Shows the progress window + */ + this.show = function show() { if(_windowLoading || _windowLoaded) { // already loading or loaded return false; } @@ -164,89 +156,66 @@ Zotero.ProgressWindow = function(_window){ return true; } - function changeHeadline(headline) { - if(_windowLoaded) { - _progressWindow.document.getElementById("zotero-progress-text-headline").value = headline; - } else { - _loadHeadline = headline; - } - } + /** + * Changes the "headline" shown at the top of the progress window + */ + this.changeHeadline = _deferUntilWindowLoad(function changeHeadline(headline) { + _progressWindow.document.getElementById("zotero-progress-text-headline").value = headline; + }); - function addLines(labels, icons) { - if(_windowLoaded) { - for (var i in labels) { - var newText = _progressWindow.document.createElement("description"); - newText.appendChild( - _progressWindow.document.createTextNode(labels[i]) - ); - newText.setAttribute("class", "zotero-progress-item-label"); - newText.setAttribute("crop", "end"); - - var newImageHolder = _progressWindow.document.createElement("vbox"); - var newImage = _progressWindow.document.createElement("image"); - newImage.setAttribute("class", "zotero-progress-item-icon"); - newImage.setAttribute("src", icons[i]); - newImage.setAttribute("flex", 0); - newImage.setAttribute("orient", "horizontal"); - newImage.setAttribute("pack", "start"); - newImageHolder.appendChild(newImage); - - var newHB = _progressWindow.document.createElement("hbox"); - newHB.setAttribute("class", "zotero-progress-item-hbox"); - - newHB.appendChild(newImageHolder); - newHB.appendChild(newText); - - _progressWindow.document.getElementById("zotero-progress-text-box").appendChild(newHB); - } - - _move(); - } else { - _loadLines = _loadLines.concat(labels); - _loadIcons = _loadIcons.concat(icons); + /** + * Adds a line to the progress window with the specified icon + */ + this.addLines = _deferUntilWindowLoad(function addLines(labels, icons) { + for (var i in labels) { + new this.ItemProgress(icons[i], labels[i]); } - } - + + _move(); + }); - /* + /** * Add a description to the progress window * * <a> elements are turned into XUL links */ - function addDescription(text) { - if(_windowLoaded) { - var newHB = _progressWindow.document.createElement("hbox"); - newHB.setAttribute("class", "zotero-progress-item-hbox"); - var newDescription = _progressWindow.document.createElement("description"); - - var parts = Zotero.Utilities.parseMarkup(text); - for each(var part in parts) { - if (part.type == 'text') { - var elem = _progressWindow.document.createTextNode(part.text); - } - else if (part.type == 'link') { - var elem = _progressWindow.document.createElement('label'); - elem.setAttribute('value', part.text); - elem.setAttribute('class', 'zotero-text-link'); - for (var i in part.attributes) { - elem.setAttribute(i, part.attributes[i]); - } + this.addDescription = _deferUntilWindowLoad(function addDescription(text) { + var newHB = _progressWindow.document.createElement("hbox"); + newHB.setAttribute("class", "zotero-progress-item-hbox"); + var newDescription = _progressWindow.document.createElement("description"); + + var parts = Zotero.Utilities.parseMarkup(text); + for each(var part in parts) { + if (part.type == 'text') { + var elem = _progressWindow.document.createTextNode(part.text); + } + else if (part.type == 'link') { + var elem = _progressWindow.document.createElement('label'); + elem.setAttribute('value', part.text); + elem.setAttribute('class', 'zotero-text-link'); + for (var i in part.attributes) { + elem.setAttribute(i, part.attributes[i]); } - - newDescription.appendChild(elem); } - newHB.appendChild(newDescription); - _progressWindow.document.getElementById("zotero-progress-text-box").appendChild(newHB); - - _move(); - } else { - _loadDescription = text; + newDescription.appendChild(elem); } - } - + + newHB.appendChild(newDescription); + _progressWindow.document.getElementById("zotero-progress-text-box").appendChild(newHB); + + _move(); + }); - function startCloseTimer(ms, requireMouseOver) { + /** + * Sets a timer to close the progress window. If a previous close timer was set, + * clears it. + * @param {Integer} ms The number of milliseconds to wait before closing the progress + * window. + * @param {Boolean} [requireMouseOver] If true, wait until the mouse has touched the + * window before closing. + */ + this.startCloseTimer = function startCloseTimer(ms, requireMouseOver) { if (_windowLoaded || _windowLoading) { if (requireMouseOver && !_mouseWasOver) { return; @@ -261,10 +230,14 @@ Zotero.ProgressWindow = function(_window){ } _timeoutID = _progressWindow.setTimeout(_timeout, ms); + _closing = true; } } - function close() { + /** + * Immediately closes the progress window if it is open. + */ + this.close = function close() { _disableTimeout(); _windowLoaded = false; _windowLoading = false; @@ -275,23 +248,102 @@ Zotero.ProgressWindow = function(_window){ } catch(ex) {} } + /** + * Creates a new object representing a line in the progressWindow. This is the OO + * version of addLines() above. + */ + this.ItemProgress = _deferUntilWindowLoad(function(iconSrc, title, parentItemProgress) { + this._itemText = _progressWindow.document.createElement("description"); + this._itemText.appendChild(_progressWindow.document.createTextNode(title)); + this._itemText.setAttribute("class", "zotero-progress-item-label"); + this._itemText.setAttribute("crop", "end"); + + this._image = _progressWindow.document.createElement("hbox"); + this._image.setAttribute("class", "zotero-progress-item-icon"); + this._image.setAttribute("flex", 0); + this._image.style.width = "16px"; + this._image.style.backgroundRepeat = "no-repeat"; + this.setIcon(iconSrc); + + this._hbox = _progressWindow.document.createElement("hbox"); + this._hbox.setAttribute("class", "zotero-progress-item-hbox"); + if(parentItemProgress) { + this._hbox.style.marginLeft = "16px"; + this._hbox.zoteroIsChildItem; + } + this._hbox.style.opacity = "0.5"; + + this._hbox.appendChild(this._image); + this._hbox.appendChild(this._itemText); + + var container = _progressWindow.document.getElementById("zotero-progress-text-box"); + if(parentItemProgress) { + var nextItem = parentItemProgress._hbox.nextSibling; + while(nextItem && nextItem.zoteroIsChildItem) { + nextItem = nextItem.nextSibling; + } + container.insertBefore(this._hbox, nextItem); + } else { + container.appendChild(this._hbox); + } + + _move(); + }); + + /** + * Sets the current save progress for this item. + * @param {Integer} percent A percentage from 0 to 100. + */ + this.ItemProgress.prototype.setProgress = _deferUntilWindowLoad(function(percent) { + if(percent != 0 && percent != 100) { + // Indication of partial progress, so we will use the circular indicator + this._image.style.backgroundImage = "url('chrome://zotero/skin/progress_arcs.png')"; + this._image.style.backgroundPosition = "-"+(Math.round(percent/100*nArcs)*16)+"px 0"; + this._hbox.style.opacity = percent/200+.5; + this._hbox.style.filter = "alpha(opacity = "+(percent/2+50)+")"; + } else if(percent == 100) { + this._image.style.backgroundImage = "url('"+this._iconSrc+"')"; + this._image.style.backgroundPosition = ""; + this._hbox.style.opacity = "1"; + this._hbox.style.filter = ""; + } + }); + + /** + * Sets the icon for this item. + * @param {Integer} percent A percentage from 0 to 100. + */ + this.ItemProgress.prototype.setIcon = _deferUntilWindowLoad(function(iconSrc) { + this._image.style.backgroundImage = "url('"+iconSrc+"')"; + this._image.style.backgroundPosition = ""; + this._iconSrc = iconSrc; + }); + + /** + * Indicates that an error occurred saving this item. + */ + this.ItemProgress.prototype.setError = _deferUntilWindowLoad(function() { + this._image.style.backgroundImage = "url('chrome://zotero/skin/cross.png')"; + this._image.style.backgroundPosition = ""; + this._itemText.style.color = "red"; + this._hbox.style.opacity = "1"; + this._hbox.style.filter = ""; + }); + function _onWindowLoaded() { _windowLoading = false; _windowLoaded = true; _move(); + // do things we delayed because the window was loading - changeHeadline(_loadHeadline); - addLines(_loadLines, _loadIcons); - if (_loadDescription) { - addDescription(_loadDescription); + for(var i=0; i<_deferredUntilWindowLoad.length; i++) { + _deferredUntilWindowLoad[i].apply(_deferredUntilWindowLoadThis[i], + _deferredUntilWindowLoadArgs[i]); } - - // reset parameters - _loadHeadline = ''; - _loadLines = []; - _loadIcons = []; - _loadDescription = null; + _deferredUntilWindowLoad = []; + _deferredUntilWindowLoadThis = []; + _deferredUntilWindowLoadArgs = []; } function _move() { @@ -303,8 +355,8 @@ Zotero.ProgressWindow = function(_window){ } function _timeout() { - close(); // could check to see if we're really supposed to close yet - // (in case multiple scrapers are operating at once) + self.close(); // could check to see if we're really supposed to close yet + // (in case multiple scrapers are operating at once) _timeoutID = false; } @@ -319,7 +371,6 @@ Zotero.ProgressWindow = function(_window){ _timeoutID = false; } - /* * Disable the close timer when the mouse is over the window */ @@ -328,8 +379,7 @@ Zotero.ProgressWindow = function(_window){ _disableTimeout(); } - - /* + /** * Start the close timer when the mouse leaves the window * * Note that this onmouseout doesn't work correctly on popups in Fx2, @@ -345,11 +395,27 @@ Zotero.ProgressWindow = function(_window){ && (e.screenY >= top) && e.screenY <= (top + this.outerHeight)) { return; } - startCloseTimer(); + if(_closing) self.startCloseTimer(); } - function _onMouseUp(e) { - close(); + self.close(); + } + + /** + * Wraps a function to ensure it isn't called until the window is loaded + */ + function _deferUntilWindowLoad(fn) { + return function() { + if(_window.closed) return; + + if(_windowLoaded) { + fn.apply(this, Array.prototype.slice.call(arguments)); + } else { + _deferredUntilWindowLoad.push(fn); + _deferredUntilWindowLoadThis.push(this); + _deferredUntilWindowLoadArgs.push(Array.prototype.slice.call(arguments)); + } + } } } diff --git a/chrome/content/zotero/xpcom/server_connector.js b/chrome/content/zotero/xpcom/server_connector.js @@ -33,16 +33,20 @@ Zotero.Server.Connector.AttachmentProgressManager = new function() { i = 1; /** + * Adds attachments to attachment progress manager + */ + this.add = function(attachments) { + for(var i=0; i<attachments.length; i++) { + var attachment = attachments[i]; + attachmentsInProgress.set(attachment, (attachment.id = i++)); + } + } + + /** * Called on attachment progress */ this.onProgress = function(attachment, progress, error) { - var progressID = attachmentsInProgress.get(attachment); - if(!progressID) { - progressID = attachment.id = i++; - attachmentsInProgress.set(attachment, progressID); - } - - attachmentProgress[progressID] = progress; + attachmentProgress[attachmentsInProgress.get(attachment)] = progress; }; /** @@ -278,6 +282,7 @@ Zotero.Server.Connector.SavePage.prototype = { Zotero.Server.Connector.AttachmentProgressManager.onProgress(attachment, progress, error); }); translate.setHandler("itemsDone", function(obj, item) { + Zotero.Server.Connector.AttachmentProgressManager.add(item.attachments); Zotero.Browser.deleteHiddenBrowser(me._browser); if(jsonItems.length || me.selectedItems === false) { me.sendResponse(201, "application/json", JSON.stringify({"items":jsonItems})); diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js @@ -130,28 +130,16 @@ Zotero.Translate.Sandbox = { } } } + + // Fire itemSaving event + translate._runHandler("itemSaving", item); if(translate instanceof Zotero.Translate.Web) { // For web translators, we queue saves translate.saveQueue.push(item); - translate._runHandler("itemSaving", item); } else { - var newItem; - translate._itemSaver.saveItems([item], function(returnValue, data) { - if(returnValue) { - newItem = data[0]; - translate.newItems.push(newItem); - } else { - translate.complete(false, data); - throw data; - } - }, function(arg1, arg2, arg3) { - translate._attachmentProgress(arg1, arg2, arg3); - }); - - translate._runHandler("itemSaving", item); - // pass both the saved item and the original JS array item - translate._runHandler("itemDone", newItem, item); + // Save items + translate._saveItems([item]); } }, @@ -1014,7 +1002,8 @@ Zotero.Translate.Base.prototype = { this._libraryID = libraryID; this._saveAttachments = saveAttachments === undefined || saveAttachments; - this._attachmentsSaving = []; + this._savingAttachments = []; + this._savingItems = 0; var me = this; if(typeof this.translator[0] === "object") { @@ -1143,14 +1132,11 @@ Zotero.Translate.Base.prototype = { if(returnValue) { if(this.saveQueue.length) { - var me = this; - this._itemSaver.saveItems(this.saveQueue.slice(), - function(returnValue, data) { me._itemsSaved(returnValue, data); }, - function(arg1, arg2, arg3) { me._attachmentProgress(arg1, arg2, arg3); }); + this._saveItems(this.saveQueue); + this.saveQueue = []; return; - } else { - this._debug("Translation successful"); } + this._debug("Translation successful"); } else { if(error) { // report error to console @@ -1176,61 +1162,76 @@ Zotero.Translate.Base.prototype = { }, /** - * Callback executed when items have been saved (which may happen asynchronously, if in - * connector) - * - * @param {Boolean} returnValue Whether saving was successful - * @param {Zotero.Item[]|Error} data If returnValue is true, this will be an array of - * Zotero.Item objects. If returnValue is false, this will - * be a string error message. + * Saves items to the database, taking care to defer attachmentProgress notifications + * until after save */ - "_itemsSaved":function(returnValue, data) { - if(returnValue) { - // trigger deferred itemDone events - var nItems = data.length; - for(var i=0; i<nItems; i++) { - this._runHandler("itemDone", data[i], this.saveQueue[i]); + "_saveItems":function(items) { + var me = this, + itemDoneEventsDispatched = false, + deferredProgress = [], + attachmentsWithProgress = []; + + this._savingItems++; + this._itemSaver.saveItems(items.slice(), function(returnValue, newItems) { + if(returnValue) { + // Remove attachments not being saved from item.attachments + for(var i=0; i<items.length; i++) { + var item = items[i]; + for(var j=0; j<item.attachments.length; j++) { + if(attachmentsWithProgress.indexOf(item.attachments[j]) === -1) { + item.attachments.splice(j--, 1); + } + } + } + + // Trigger itemDone events + for(var i=0, nItems = items.length; i<nItems; i++) { + me._runHandler("itemDone", newItems[i], items[i]); + } + + // Specify that itemDone event was dispatched, so that we don't defer + // attachmentProgress notifications anymore + itemDoneEventsDispatched = true; + + // Run deferred attachmentProgress notifications + for(var i=0; i<deferredProgress.length; i++) { + me._runHandler("attachmentProgress", deferredProgress[i][0], + deferredProgress[i][1], deferredProgress[i][2]); + } + + me._savingItems--; + me._checkIfDone(); + } else { + Zotero.logError(newItems); + me.complete(returnValue, newItems); + } + }, + function(attachment, progress, error) { + var attachmentIndex = me._savingAttachments.indexOf(attachment); + if((progress === false || progress === 100) && attachmentIndex !== -1) { + me._savingAttachments.splice(attachmentIndex, 1); + } else if(attachmentIndex === -1) { + me._savingAttachments.push(attachment); } - this.saveQueue = []; - } else { - Zotero.logError(data); - } - - if(returnValue) { - this._checkIfDone(); - } else { - this._runHandler("done", returnValue); - } - }, - - /** - * Callback for attachment progress, passed as third argument to Zotero.ItemSaver#saveItems - * - * @param {Object} attachment Attachment object to be saved. Should remain the same between - * repeated calls to callback. - * @param {Boolean|Number} progress Percent complete, or false if an error occurred. - * @param {Error} [error] Error, if an error occurred during saving. - */ - "_attachmentProgress":function(attachment, progress, error) { - Zotero.debug("Attachment progress (progress = "+progress+")"); - Zotero.debug(attachment); - var attachmentIndex = this._attachmentsSaving.indexOf(attachment); - if((progress === false || progress === 100) && attachmentIndex !== -1) { - this._attachmentsSaving.splice(attachmentIndex, 1); - } else if(attachmentIndex === -1) { - this._attachmentsSaving.push(attachment); - } - - this._runHandler("attachmentProgress", attachment, progress, error); - this._checkIfDone(); + if(itemDoneEventsDispatched) { + // itemDone event has already fired, so we can fire attachmentProgress + // notifications + me._runHandler("attachmentProgress", attachment, progress, error); + me._checkIfDone(); + } else { + // Defer until after we fire the itemDone event + deferredProgress.push([attachment, progress, error]); + attachmentsWithProgress.push(attachment); + } + }); }, /** * Checks if saving done, and if so, fires done event */ "_checkIfDone":function() { - if(!this._attachmentsSaving.length) { + if(!this._savingItems && !this._savingAttachments.length && !this._currentState) { this._runHandler("done", true); } }, diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -151,8 +151,6 @@ Zotero.Translate.ItemSaver.prototype = { var newAttachment = this._saveAttachment(item.attachments[i], myID, attachmentCallback); if(typeof newAttachment === "object") { this._saveTags(item.attachments[i], newAttachment); - } else if(!newAttachment) { - item.attachments.splice(i--, 1); } } } diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js @@ -1575,5 +1575,16 @@ Zotero.Utilities = { } } return length; + }, + + /** + * Gets the icon for a JSON-style attachment + */ + "determineAttachmentIcon":function(attachment) { + if(attachment.linkMode === "linked_url") { + return Zotero.ItemTypes.getImageSrc("attachment-web-link"); + } + return Zotero.ItemTypes.getImageSrc(attachment.mimeType === "application/pdf" + ? "attachment-pdf" : "attachment-snapshot"); } } diff --git a/chrome/skin/default/zotero/zotero.css b/chrome/skin/default/zotero/zotero.css @@ -237,8 +237,8 @@ label.zotero-text-link { .zotero-progress-item-hbox { padding-left: 5px; - margin-top: 3px; - margin-bottom: 3px; + margin-top: 1px; + margin-bottom: 1px; } .zotero-progress-item-label