Defer missing item warning until after QuickFormat closes

This commit is contained in:
Simon Kornblith 2012-02-06 22:46:20 -05:00
parent 1c28a23aa6
commit 438e0e3128

View file

@ -1249,33 +1249,39 @@ Zotero.Integration.Fields.prototype._retrieveFields = function() {
* @param {Field} field The Zotero field object * @param {Field} field The Zotero field object
* @param {Integer} i The field index * @param {Integer} i The field index
*/ */
Zotero.Integration.Fields.prototype._showCorruptFieldError = function(e, field, i) { Zotero.Integration.Fields.prototype._showCorruptFieldError = function(e, field, callback, errorCallback, i) {
Zotero.logError(e);
var msg = Zotero.getString("integration.corruptField")+'\n\n'+ var msg = Zotero.getString("integration.corruptField")+'\n\n'+
Zotero.getString('integration.corruptField.description'); Zotero.getString('integration.corruptField.description');
field.select(); field.select();
this._doc.activate();
var result = this._doc.displayAlert(msg, var result = this._doc.displayAlert(msg,
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_CAUTION, Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_CAUTION,
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_YES_NO_CANCEL); Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_YES_NO_CANCEL);
if(result == 0) { if(result == 0) {
throw e; throw new Zotero.Integration.UserCancelledException;
} else if(result == 1) { // No } else if(result == 1) { // No
this._removeCodeFields.push(i); this._removeCodeFields.push(i);
return true;
} else { } else {
// Display reselect edit citation dialog // Display reselect edit citation dialog
var added = this.addEditCitation(field); var me = this;
if(added) { var oldWindow = Zotero.Integration.currentWindow;
this._doc.activate(); this.addEditCitation(field, function() {
} else { Zotero.Integration.currentWindow.close();
throw new Zotero.Integration.UserCancelledException(); Zotero.Integration.currentWindow = oldWindow;
} me.updateSession(callback, errorCallback);
});
return false;
} }
} }
/** /**
* Updates Zotero.Integration.Session attached to Zotero.Integration.Fields in line with document * Updates Zotero.Integration.Session attached to Zotero.Integration.Fields in line with document
*/ */
Zotero.Integration.Fields.prototype.updateSession = function(callback) { Zotero.Integration.Fields.prototype.updateSession = function(callback, errorCallback) {
var me = this; var me = this;
this.get(function(fields) { this.get(function(fields) {
me._session.resetRequest(me._doc); me._session.resetRequest(me._doc);
@ -1300,7 +1306,9 @@ Zotero.Integration.Fields.prototype.updateSession = function(callback) {
try { try {
me._session.loadBibliographyData(me._bibliographyData); me._session.loadBibliographyData(me._bibliographyData);
} catch(e) { } catch(e) {
if(e instanceof Zotero.Integration.CorruptFieldException) { if(errorCallback) {
errorCallback(e);
} else if(e instanceof Zotero.Integration.CorruptFieldException) {
var msg = Zotero.getString("integration.corruptBibliography")+'\n\n'+ var msg = Zotero.getString("integration.corruptBibliography")+'\n\n'+
Zotero.getString('integration.corruptBibliography.description'); Zotero.getString('integration.corruptBibliography.description');
var result = me._doc.displayAlert(msg, var result = me._doc.displayAlert(msg,
@ -1333,16 +1341,16 @@ Zotero.Integration.Fields.prototype.updateSession = function(callback) {
if(callback) callback(me._session); if(callback) callback(me._session);
})); }));
} else { } else {
if(callback) callback(this._session); if(callback) callback(me._session);
} }
}); }, errorCallback);
}); });
} }
/** /**
* Keep processing fields until all have been processed * Keep processing fields until all have been processed
*/ */
Zotero.Integration.Fields.prototype._processFields = function(fields, callback, i) { Zotero.Integration.Fields.prototype._processFields = function(fields, callback, errorCallback, i) {
if(!i) i = 0; if(!i) i = 0;
for(var n = fields.length; i<n; i++) { for(var n = fields.length; i<n; i++) {
@ -1351,7 +1359,7 @@ Zotero.Integration.Fields.prototype._processFields = function(fields, callback,
try { try {
var fieldCode = field.getCode(); var fieldCode = field.getCode();
} catch(e) { } catch(e) {
this._showCorruptFieldError(e, field, i); if(!this._showCorruptFieldError(e, field, callback, errorCallback, i)) return;
} }
var [type, content] = this.getCodeTypeAndContent(fieldCode); var [type, content] = this.getCodeTypeAndContent(fieldCode);
@ -1360,7 +1368,9 @@ Zotero.Integration.Fields.prototype._processFields = function(fields, callback,
try { try {
this._session.addCitation(i, noteIndex, content); this._session.addCitation(i, noteIndex, content);
} catch(e) { } catch(e) {
if(e instanceof Zotero.Integration.MissingItemException) { if(errorCallback) {
errorCallback(e);
} else if(e instanceof Zotero.Integration.MissingItemException) {
// First, check if we've already decided to remove field codes from these // First, check if we've already decided to remove field codes from these
var reselect = true; var reselect = true;
for each(var reselectKey in e.reselectKeys) { for each(var reselectKey in e.reselectKeys) {
@ -1380,6 +1390,7 @@ Zotero.Integration.Fields.prototype._processFields = function(fields, callback,
} }
msg += '\n\n'+Zotero.getString('integration.missingItem.description'); msg += '\n\n'+Zotero.getString('integration.missingItem.description');
field.select(); field.select();
this._doc.activate();
var result = this._doc.displayAlert(msg, 1, 3); var result = this._doc.displayAlert(msg, 1, 3);
if(result == 0) { // Cancel if(result == 0) { // Cancel
throw new Zotero.Integration.UserCancelledException(); throw new Zotero.Integration.UserCancelledException();
@ -1391,16 +1402,18 @@ Zotero.Integration.Fields.prototype._processFields = function(fields, callback,
} else { // Yes } else { // Yes
// Display reselect item dialog // Display reselect item dialog
var me = this; var me = this;
var oldCurrentWindow = Zotero.Integration.currentWindow;
this._session.reselectItem(this._doc, e, function() { this._session.reselectItem(this._doc, e, function() {
// Now try again // Now try again
Zotero.Integration.currentWindow = oldCurrentWindow;
me._doc.activate(); me._doc.activate();
me._processFields(fields, callback, i); me._processFields(fields, callback, errorCallback, i);
}); });
return; return;
} }
} }
} else if(e instanceof Zotero.Integration.CorruptFieldException) { } else if(e instanceof Zotero.Integration.CorruptFieldException) {
this._showCorruptFieldError(e, field, i); if(!this._showCorruptFieldError(e, field, callback, errorCallback, i)) return;
} else { } else {
throw e; throw e;
} }
@ -1598,13 +1611,19 @@ Zotero.Integration.Fields.prototype.addEditCitation = function(field, callback)
// if there's already a citation, make sure we have item IDs in addition to keys // if there's already a citation, make sure we have item IDs in addition to keys
if(field) { if(field) {
try {
var code = field.getCode(); var code = field.getCode();
} catch(e) {}
if(code) {
[type, content] = this.getCodeTypeAndContent(code); [type, content] = this.getCodeTypeAndContent(code);
if(type != INTEGRATION_TYPE_ITEM) { if(type != INTEGRATION_TYPE_ITEM) {
throw new Zotero.Integration.DisplayException("notInCitation"); throw new Zotero.Integration.DisplayException("notInCitation");
} }
try {
citation = session.unserializeCitation(content); citation = session.unserializeCitation(content);
try { try {
session.lookupItems(citation); session.lookupItems(citation);
} catch(e) { } catch(e) {
@ -1627,38 +1646,30 @@ Zotero.Integration.Fields.prototype.addEditCitation = function(field, callback)
delete citation.properties["formattedCitation"]; delete citation.properties["formattedCitation"];
delete citation.properties["plainCitation"]; delete citation.properties["plainCitation"];
delete citation.properties["dontUpdate"]; delete citation.properties["dontUpdate"];
} catch(e) {}
}
} else { } else {
newField = true; newField = true;
var field = this.addField(true); var field = this.addField(true);
field.setCode("TEMP"); }
if(!citation) {
field.setCode("TEMP");
citation = {"citationItems":[], "properties":{}}; citation = {"citationItems":[], "properties":{}};
} }
var io = new Zotero.Integration.CitationEditInterface(citation, field, this, session, newField, callback); var io = new Zotero.Integration.CitationEditInterface(citation, field, this, session, newField, callback);
var doc = this._doc;
function openAddCitationDialog() {
if(Zotero.Prefs.get("integration.useClassicAddCitationDialog")) { if(Zotero.Prefs.get("integration.useClassicAddCitationDialog")) {
Zotero.Integration.displayDialog(doc, Zotero.Integration.displayDialog(this._doc,
'chrome://zotero/content/integration/addCitationDialog.xul', 'alwaysRaised,resizable', 'chrome://zotero/content/integration/addCitationDialog.xul', 'alwaysRaised,resizable',
io, true); io, true);
} else { } else {
var mode = (!Zotero.isMac && Zotero.Prefs.get('integration.keepAddCitationDialogRaised') var mode = (!Zotero.isMac && Zotero.Prefs.get('integration.keepAddCitationDialogRaised')
? 'popup' : 'alwaysRaised') ? 'popup' : 'alwaysRaised')
Zotero.Integration.displayDialog(doc, Zotero.Integration.displayDialog(this._doc,
'chrome://zotero/content/integration/quickFormat.xul', mode, io, true); 'chrome://zotero/content/integration/quickFormat.xul', mode, io, true);
} }
}
if(session.data.prefs.storeReferences) {
// If references are stored in document, don't delay opening dialog to see if there are
// missing references
openAddCitationDialog();
} else {
// Otherwise, read items out of the document before doing anything
io._runWhenSessionUpdated(openAddCitationDialog);
}
} }
/** /**
@ -1718,6 +1729,11 @@ Zotero.Integration.CitationEditInterface.prototype = {
} }
me._sessionUpdated = true; me._sessionUpdated = true;
delete me._sessionCallbackQueue; delete me._sessionCallbackQueue;
}, function(e) {
if(e instanceof Zotero.Integration.MissingItemException
|| e instanceof Zotero.Integration.CorruptFieldException) {
me._errorOccurred = true;
}
}); });
} }
}, },
@ -1750,11 +1766,23 @@ Zotero.Integration.CitationEditInterface.prototype = {
* Receives a number from 0 to 100 indicating current status. * Receives a number from 0 to 100 indicating current status.
*/ */
"accept":function(progressCallback) { "accept":function(progressCallback) {
var me = this;
this._fields.progressCallback = progressCallback; this._fields.progressCallback = progressCallback;
if(this._errorOccurred) {
// If an error occurred updating the session, update it again, this time letting the
// error get displayed
Zotero.setTimeout(function() {
me._fields.updateSession(function() {
me._errorOccurred = false;
me.accept(progressCallback);
})
}, 0);
return;
}
if(this.citation.citationItems.length) { if(this.citation.citationItems.length) {
// Citation added // Citation
var me = this;
this._runWhenSessionUpdated(function() { this._runWhenSessionUpdated(function() {
me._session.addCitation(me._fieldIndex, me._field.getNoteIndex(), me.citation); me._session.addCitation(me._fieldIndex, me._field.getNoteIndex(), me.citation);
me._session.updateIndices[me._fieldIndex] = true; me._session.updateIndices[me._fieldIndex] = true;