Merge branch 'attachment-progress'
This commit is contained in:
commit
83aeae942c
2 changed files with 78 additions and 6 deletions
|
@ -79,10 +79,23 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
payload.cookie = this._cookie;
|
payload.cookie = this._cookie;
|
||||||
}
|
}
|
||||||
|
|
||||||
Zotero.Connector.callMethod("saveItems", payload, function(success, status) {
|
Zotero.Connector.callMethod("saveItems", payload, function(data, status) {
|
||||||
if(success !== false) {
|
if(data !== false) {
|
||||||
Zotero.debug("Translate: Save via Standalone succeeded");
|
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);
|
callback(true, items);
|
||||||
|
if(haveAttachments) me._pollForProgress(items, attachmentCallback);
|
||||||
} else if(Zotero.isFx) {
|
} else if(Zotero.isFx) {
|
||||||
callback(false, new Error("Save via Standalone failed with "+status));
|
callback(false, new Error("Save via Standalone failed with "+status));
|
||||||
} else {
|
} else {
|
||||||
|
@ -91,6 +104,60 @@ 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
|
* Saves items to server
|
||||||
* @param items Items in Zotero.Item.toArray() format
|
* @param items Items in Zotero.Item.toArray() format
|
||||||
|
@ -101,6 +168,7 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
* @param {Function} attachmentCallback A callback that receives information about attachment
|
* @param {Function} attachmentCallback A callback that receives information about attachment
|
||||||
* save progress. The callback will be called as attachmentCallback(attachment, false, error)
|
* save progress. The callback will be called as attachmentCallback(attachment, false, error)
|
||||||
* on failure or attachmentCallback(attachment, progressPercent) periodically during saving.
|
* 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) {
|
"_saveToServer":function(items, callback, attachmentCallback) {
|
||||||
var newItems = [], typedArraysSupported = false;
|
var newItems = [], typedArraysSupported = false;
|
||||||
|
@ -115,7 +183,7 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
if(typedArraysSupported) {
|
if(typedArraysSupported) {
|
||||||
// Get rid of attachments that we won't be able to save properly and add ids
|
// 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++) {
|
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);
|
item.attachments.splice(j--, 1);
|
||||||
} else {
|
} else {
|
||||||
item.attachments[j].id = Zotero.Utilities.randomString();
|
item.attachments[j].id = Zotero.Utilities.randomString();
|
||||||
|
|
|
@ -30,7 +30,7 @@ Zotero.Server.Connector.Data = {};
|
||||||
Zotero.Server.Connector.AttachmentProgressManager = new function() {
|
Zotero.Server.Connector.AttachmentProgressManager = new function() {
|
||||||
var attachmentsInProgress = new WeakMap(),
|
var attachmentsInProgress = new WeakMap(),
|
||||||
attachmentProgress = {},
|
attachmentProgress = {},
|
||||||
i = 1;
|
id = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds attachments to attachment progress manager
|
* Adds attachments to attachment progress manager
|
||||||
|
@ -38,9 +38,9 @@ Zotero.Server.Connector.AttachmentProgressManager = new function() {
|
||||||
this.add = function(attachments) {
|
this.add = function(attachments) {
|
||||||
for(var i=0; i<attachments.length; i++) {
|
for(var i=0; i<attachments.length; i++) {
|
||||||
var attachment = attachments[i];
|
var attachment = attachments[i];
|
||||||
attachmentsInProgress.set(attachment, (attachment.id = i++));
|
attachmentsInProgress.set(attachment, (attachment.id = id++));
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called on attachment progress
|
* Called on attachment progress
|
||||||
|
@ -275,6 +275,7 @@ Zotero.Server.Connector.SavePage.prototype = {
|
||||||
if(collection) {
|
if(collection) {
|
||||||
collection.addItem(item.id);
|
collection.addItem(item.id);
|
||||||
}
|
}
|
||||||
|
Zotero.Server.Connector.AttachmentProgressManager.add(jsonItem.attachments);
|
||||||
|
|
||||||
jsonItems.push(jsonItem);
|
jsonItems.push(jsonItem);
|
||||||
});
|
});
|
||||||
|
@ -329,6 +330,9 @@ Zotero.Server.Connector.SaveItem.prototype = {
|
||||||
|
|
||||||
var cookieSandbox = data["uri"] && data["cookie"] ? new Zotero.CookieSandbox(null, data["uri"],
|
var cookieSandbox = data["uri"] && data["cookie"] ? new Zotero.CookieSandbox(null, data["uri"],
|
||||||
data["cookie"]) : null;
|
data["cookie"]) : null;
|
||||||
|
for(var i=0; i<data.items.length; i++) {
|
||||||
|
Zotero.Server.Connector.AttachmentProgressManager.add(data.items[i].attachments);
|
||||||
|
}
|
||||||
|
|
||||||
// save items
|
// save items
|
||||||
var itemSaver = new Zotero.Translate.ItemSaver(libraryID,
|
var itemSaver = new Zotero.Translate.ItemSaver(libraryID,
|
||||||
|
|
Loading…
Add table
Reference in a new issue