Eliminate processNextEvent(). This needs a lot of testing.

This commit is contained in:
Simon Kornblith 2011-09-21 03:41:49 +00:00
parent c4bfd17e52
commit ed42e0c22d

View File

@ -480,7 +480,7 @@ Zotero.Integration.Document = function(app, doc) {
*/ */
Zotero.Integration.Document.prototype._createNewSession = function(data) { Zotero.Integration.Document.prototype._createNewSession = function(data) {
data.sessionID = Zotero.randomString(); data.sessionID = Zotero.randomString();
var session = Zotero.Integration.sessions[data.sessionID] = new Zotero.Integration.Session(); var session = Zotero.Integration.sessions[data.sessionID] = new Zotero.Integration.Session(this._doc);
return session; return session;
} }
@ -491,8 +491,9 @@ Zotero.Integration.Document.prototype._createNewSession = function(data) {
* @param dontRunSetDocPrefs {Boolean} Whether to show the Set Document Preferences window if no * @param dontRunSetDocPrefs {Boolean} Whether to show the Set Document Preferences window if no
* preferences exist * preferences exist
*/ */
Zotero.Integration.Document.prototype._getSession = function(require, dontRunSetDocPrefs) { Zotero.Integration.Document.prototype._getSession = function(require, dontRunSetDocPrefs, callback) {
var dataString = this._doc.getDocumentData(); var dataString = this._doc.getDocumentData(),
me = this;
if(!dataString) { if(!dataString) {
var haveFields = false; var haveFields = false;
var data = new Zotero.Integration.DocumentData(); var data = new Zotero.Integration.DocumentData();
@ -519,20 +520,20 @@ Zotero.Integration.Document.prototype._getSession = function(require, dontRunSet
// Set doc prefs if no data string yet // Set doc prefs if no data string yet
this._session = this._createNewSession(data); this._session = this._createNewSession(data);
this._session.setData(data); this._session.setData(data);
if(dontRunSetDocPrefs) return false; if(dontRunSetDocPrefs) {
callback();
try { return;
var ret = this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType);
} finally {
this._doc.activate();
} }
// save doc prefs in doc this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType, function() {
this._doc.setDocumentData(this._session.data.serializeXML()); // save doc prefs in doc
me._doc.setDocumentData(me._session.data.serializeXML());
if(haveFields) { if(haveFields) {
this._session.reload = true; me._session.reload = true;
} }
callback();
});
} else { } else {
var data = new Zotero.Integration.DocumentData(dataString); var data = new Zotero.Integration.DocumentData(dataString);
if(data.dataVersion < DATA_VERSION) { if(data.dataVersion < DATA_VERSION) {
@ -565,34 +566,34 @@ Zotero.Integration.Document.prototype._getSession = function(require, dontRunSet
} catch(e) { } catch(e) {
// make sure style is defined // make sure style is defined
if(e instanceof Zotero.Integration.DisplayException && e.name === "invalidStyle") { if(e instanceof Zotero.Integration.DisplayException && e.name === "invalidStyle") {
try { this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType, function() {
this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType); me._doc.setDocumentData(me._session.data.serializeXML());
} finally { me._session.reload = true;
this._doc.activate(); callback(true);
} });
return;
} else { } else {
throw e; throw e;
} }
} }
this._doc.setDocumentData(this._session.data.serializeXML());
this._doc.setDocumentData(this._session.data.serializeXML());
this._session.reload = true; this._session.reload = true;
} }
callback();
} }
return !!dataString;
} }
/** /**
* Adds a citation to the current document. * Adds a citation to the current document.
*/ */
Zotero.Integration.Document.prototype.addCitation = function() { Zotero.Integration.Document.prototype.addCitation = function() {
this._getSession(); var me = this;
this._getSession(false, false, function() {
var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc);
me = this; fieldGetter.addEditCitation(null, function() {
fieldGetter.addEditCitation(null, function() { Zotero.Integration.complete(me._doc);
Zotero.Integration.complete(me._doc); });
}); });
} }
@ -600,17 +601,17 @@ Zotero.Integration.Document.prototype.addCitation = function() {
* Edits the citation at the cursor position. * Edits the citation at the cursor position.
*/ */
Zotero.Integration.Document.prototype.editCitation = function() { Zotero.Integration.Document.prototype.editCitation = function() {
this._getSession(true); var me = this;
this._getSession(true, false, function() {
var field = me._doc.cursorInField(me._session.data.prefs['fieldType'])
if(!field) {
throw new Zotero.Integration.DisplayException("notInCitation");
}
var field = this._doc.cursorInField(this._session.data.prefs['fieldType']) var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc);
if(!field) { fieldGetter.addEditCitation(field, function() {
throw new Zotero.Integration.DisplayException("notInCitation"); Zotero.Integration.complete(me._doc);
} });
var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc),
me = this;
fieldGetter.addEditCitation(field, function() {
Zotero.Integration.complete(me._doc);
}); });
} }
@ -618,53 +619,55 @@ Zotero.Integration.Document.prototype.editCitation = function() {
* Adds a bibliography to the current document. * Adds a bibliography to the current document.
*/ */
Zotero.Integration.Document.prototype.addBibliography = function() { Zotero.Integration.Document.prototype.addBibliography = function() {
this._getSession(true); var me = this;
this._getSession(true, false, function() {
// Make sure we can have a bibliography
if(!me._session.data.style.hasBibliography) {
throw new Zotero.Integration.DisplayException("noBibliography");
}
// Make sure we can have a bibliography var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc),
if(!this._session.data.style.hasBibliography) { field = fieldGetter.addField();
throw new Zotero.Integration.DisplayException("noBibliography"); field.setCode("BIBL");
} fieldGetter.updateSession(function() {
fieldGetter.updateDocument(false, true);
var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), Zotero.Integration.complete(me._doc);
field = fieldGetter.addField(), });
me = this;
field.setCode("BIBL");
fieldGetter.updateSession(function() {
fieldGetter.updateDocument(false, true);
Zotero.Integration.complete(me._doc);
}); });
} }
/** /**
* Edits bibliography metadata. * Edits bibliography metadata.
*/ */
Zotero.Integration.Document.prototype.editBibliography = function() { Zotero.Integration.Document.prototype.editBibliography = function(callback) {
// Make sure we have a bibliography // Make sure we have a bibliography
this._getSession(true); var me = this;
this._getSession(true, false, function() {
var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc);
me = this; fieldGetter.get(function(fields) {
fieldGetter.get(function(fields) { var haveBibliography = false;
var haveBibliography = false; for(var i=fields.length-1; i>=0; i--) {
for(var i=fields.length-1; i>=0; i--) { var code = fields[i].getCode();
var code = fields[i].getCode(); var [type, content] = fieldGetter.getCodeTypeAndContent(code);
var [type, content] = fieldGetter.getCodeTypeAndContent(code); if(type == INTEGRATION_TYPE_BIBLIOGRAPHY) {
if(type == INTEGRATION_TYPE_BIBLIOGRAPHY) { haveBibliography = true;
haveBibliography = true; break;
break; }
} }
}
if(!haveBibliography) { if(!haveBibliography) {
throw new Zotero.Integration.DisplayException("mustInsertBibliography"); throw new Zotero.Integration.DisplayException("mustInsertBibliography");
} }
fieldGetter.updateSession(); fieldGetter.updateSession(function() {
me._session.editBibliography(); me._session.editBibliography(function() {
me._doc.activate(); me._doc.activate();
fieldGetter.updateDocument(false, true); fieldGetter.updateDocument(false, true);
Zotero.Integration.complete(me._doc); Zotero.Integration.complete(me._doc);
});
});
});
}); });
} }
@ -672,14 +675,14 @@ Zotero.Integration.Document.prototype.editBibliography = function() {
* Updates the citation data for all citations and bibliography entries. * Updates the citation data for all citations and bibliography entries.
*/ */
Zotero.Integration.Document.prototype.refresh = function() { Zotero.Integration.Document.prototype.refresh = function() {
this._getSession(true); var me = this;
this._getSession(true, false, function() {
// Send request, forcing update of citations and bibliography // Send request, forcing update of citations and bibliography
var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc);
me = this; fieldGetter.updateSession(function() {
fieldGetter.updateSession(function() { fieldGetter.updateDocument(true, true);
fieldGetter.updateDocument(true, true); Zotero.Integration.complete(me._doc);
Zotero.Integration.complete(me._doc); });
}); });
} }
@ -687,21 +690,21 @@ Zotero.Integration.Document.prototype.refresh = function() {
* Deletes field codes. * Deletes field codes.
*/ */
Zotero.Integration.Document.prototype.removeCodes = function() { Zotero.Integration.Document.prototype.removeCodes = function() {
this._getSession(true); var me = this;
this._getSession(true, false, function() {
var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc);
me = this; fieldGetter.get(function(fields) {
fieldGetter.get(function(fields) { var result = me._doc.displayAlert(Zotero.getString("integration.removeCodesWarning"),
var result = me._doc.displayAlert(Zotero.getString("integration.removeCodesWarning"), Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_WARNING,
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_WARNING, Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL);
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL); if(result) {
if(result) { for(var i=fields.length-1; i>=0; i--) {
for(var i=fields.length-1; i>=0; i--) { fields[i].removeCode();
fields[i].removeCode(); }
} }
}
Zotero.Integration.complete(me._doc); Zotero.Integration.complete(me._doc);
});
}); });
} }
@ -709,65 +712,61 @@ Zotero.Integration.Document.prototype.removeCodes = function() {
* Displays a dialog to set document preferences (style, footnotes/endnotes, etc.) * Displays a dialog to set document preferences (style, footnotes/endnotes, etc.)
*/ */
Zotero.Integration.Document.prototype.setDocPrefs = function() { Zotero.Integration.Document.prototype.setDocPrefs = function() {
this._getSession(false, true); var me = this;
this._getSession(false, true, function() {
var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc);
fieldGetter.get();
var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc); me._session.setDocPrefs(me._app.primaryFieldType, me._app.secondaryFieldType, function(oldData) {
fieldGetter.get(); if(oldData) {
me._doc.setDocumentData(me._session.data.serializeXML());
try { fieldGetter.get(function(fields) {
var oldData = this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType); if(fields && fields.length) {
} finally { // if there are fields, we will have to convert some things; get a list of what we need to deal with
this._doc.activate(); var convertBibliographies = oldData === true || oldData.prefs.fieldType != me._session.data.prefs.fieldType;
} var convertItems = convertBibliographies || oldData.prefs.noteType != me._session.data.prefs.noteType;
var fieldsToConvert = new Array();
var fieldNoteTypes = new Array();
for(var i=0, n=fields.length; i<n; i++) {
var field = fields[i],
fieldCode = field.getCode(),
[type, content] = fieldGetter.getCodeTypeAndContent(fieldCode);
if(oldData) { if(convertItems && type === INTEGRATION_TYPE_ITEM) {
this._doc.setDocumentData(this._session.data.serializeXML()); var citation = me._session.unserializeCitation(fieldCode);
if(!citation.properties.dontUpdate) {
var me = this; fieldsToConvert.push(field);
fieldGetter.get(function(fields) { fieldNoteTypes.push(me._session.data.prefs.noteType);
if(fields && fields.length) { }
// if there are fields, we will have to convert some things; get a list of what we need to deal with } else if(convertBibliographies && type === INTEGRATION_TYPE_BIBLIOGRAPHY) {
var convertBibliographies = oldData === true || oldData.prefs.fieldType != me._session.data.prefs.fieldType; fieldsToConvert.push(field);
var convertItems = convertBibliographies || oldData.prefs.noteType != me._session.data.prefs.noteType; fieldNoteTypes.push(0);
var fieldsToConvert = new Array(); }
var fieldNoteTypes = new Array();
for(var i=0, n=fields.length; i<n; i++) {
var field = fields[i],
fieldCode = field.getCode(),
[type, content] = fieldGetter.getCodeTypeAndContent(fieldCode);
if(convertItems && type === INTEGRATION_TYPE_ITEM) {
var citation = me._session.unserializeCitation(fieldCode);
if(!citation.properties.dontUpdate) {
fieldsToConvert.push(field);
fieldNoteTypes.push(me._session.data.prefs.noteType);
} }
} else if(convertBibliographies && type === INTEGRATION_TYPE_BIBLIOGRAPHY) {
fieldsToConvert.push(field); if(fieldsToConvert.length) {
fieldNoteTypes.push(0); // pass to conversion function
me._doc.convert(new Zotero.Integration.Document.JSEnumerator(fieldsToConvert),
me._session.data.prefs.fieldType, fieldNoteTypes, fieldNoteTypes.length);
// clear fields so that they will get collected again before refresh
me._fields = undefined;
}
// refresh contents
fieldGetter = new Zotero.Integration.Fields(me._session, me._doc);
fieldGetter.updateSession(function() {
fieldGetter.updateDocument(true, true, true);
Zotero.Integration.complete(me._doc);
});
} }
}
if(fieldsToConvert.length) {
// pass to conversion function
me._doc.convert(new Zotero.Integration.Document.JSEnumerator(fieldsToConvert),
me._session.data.prefs.fieldType, fieldNoteTypes, fieldNoteTypes.length);
// clear fields so that they will get collected again before refresh
me._fields = undefined;
}
// refresh contents
fieldGetter = new Zotero.Integration.Fields(me._session, me._doc);
fieldGetter.updateSession(function() {
fieldGetter.updateDocument(true, true, true);
Zotero.Integration.complete(me._doc);
}); });
} else {
Zotero.Integration.complete(me._doc);
} }
}); });
} else { });
Zotero.Integration.complete(this._doc);
}
} }
/** /**
@ -850,8 +849,8 @@ Zotero.Integration.Fields.prototype.get = function(callback) {
callback(this._fields); callback(this._fields);
} }
} catch(e) { } catch(e) {
Zotero.logError(e);
Zotero.Integration.handleError(e, this._doc); Zotero.Integration.handleError(e, this._doc);
throw e;
} }
return; return;
} }
@ -942,25 +941,72 @@ Zotero.Integration.Fields.prototype._showCorruptFieldError = function(e, field,
Zotero.Integration.Fields.prototype.updateSession = function(callback) { Zotero.Integration.Fields.prototype.updateSession = function(callback) {
var me = this; var me = this;
this.get(function(fields) { this.get(function(fields) {
me._updateSession(fields); me._session.resetRequest(me._doc);
if(callback) callback(this._session);
me._deleteKeys = {};
me._deleteFields = [];
me._removeCodeFields = [];
me._bibliographyFields = [];
me._bibliographyData = "";
var collectFieldsTime = (new Date()).getTime();
me._processFields(fields, function() {
var endTime = (new Date()).getTime();
if(Zotero.Debug.enabled) {
Zotero.debug("Integration: Updated session data for "+fields.length+" fields in "+
(endTime-collectFieldsTime)/1000+"; "+
1000/((endTime-collectFieldsTime)/fields.length)+" fields/second");
}
// load uncited items from bibliography
if(me._bibliographyData && !me._session.bibliographyData) {
try {
me._session.loadBibliographyData(me._bibliographyData);
} catch(e) {
if(e instanceof Zotero.Integration.CorruptFieldException) {
var msg = Zotero.getString("integration.corruptBibliography")+'\n\n'+
Zotero.getString('integration.corruptBibliography.description');
var result = me._doc.displayAlert(msg,
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_CAUTION,
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL);
if(result == 0) {
throw e;
} else {
me._bibliographyData = "";
me._session.bibliographyHasChanged = true;
me._session.bibliographyDataHasChanged = true;
}
} else {
throw e;
}
}
}
// if we are reloading this session, assume no item IDs to be updated except for edited items
if(me._session.reload) {
//this._session.restoreProcessorState(); TODO doesn't appear to be working properly
me._session.updateUpdateIndices();
var deleteCitations = me._session.updateCitations();
me._deleteFields = me._deleteFields.concat([i for(i in deleteCitations)]);
me._session.updateIndices = {};
me._session.updateItemIDs = {};
me._session.citationText = {};
me._session.bibliographyHasChanged = false;
delete me._session.reload;
}
if(callback) callback(this._session);
});
}); });
} }
/** /**
* Callback for Zotero.Integration.Fields.get() * Keep processing fields until all have been processed
*/ */
Zotero.Integration.Fields.prototype._updateSession = function(fields) { Zotero.Integration.Fields.prototype._processFields = function(fields, callback, i) {
this._session.resetRequest(this._doc); if(!i) i = 0;
var deleteKeys = {}; for(var n = fields.length; i<n; i++) {
this._deleteFields = [];
this._removeCodeFields = [];
this._bibliographyFields = [];
var bibliographyData = "";
var collectFieldsTime = (new Date()).getTime();
for(var i=0, n=fields.length; i<n; i++) {
var field = fields[i]; var field = fields[i];
try { try {
@ -979,7 +1025,7 @@ Zotero.Integration.Fields.prototype._updateSession = function(fields) {
// 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) {
if(deleteKeys[reselectKey]) { if(this._deleteKeys[reselectKey]) {
this._removeCodeFields.push(i); this._removeCodeFields.push(i);
reselect = false; reselect = false;
break; break;
@ -1000,15 +1046,18 @@ Zotero.Integration.Fields.prototype._updateSession = function(fields) {
throw new Zotero.Integration.UserCancelledException(); throw new Zotero.Integration.UserCancelledException();
} else if(result == 1) { // No } else if(result == 1) { // No
for each(var reselectKey in e.reselectKeys) { for each(var reselectKey in e.reselectKeys) {
deleteKeys[reselectKey] = true; this._deleteKeys[reselectKey] = true;
} }
this._removeCodeFields.push(i); this._removeCodeFields.push(i);
} else { // Yes } else { // Yes
// Display reselect item dialog // Display reselect item dialog
this._session.reselectItem(e); var me = this;
// Now try again this._session.reselectItem(e, function() {
this._session.addCitation(i, field.getNoteIndex(), content); // Now try again
this._doc.activate(); me._doc.activate();
me._processFields(fields, callback, i);
});
return;
} }
} }
} else if(e instanceof Zotero.Integration.CorruptFieldException) { } else if(e instanceof Zotero.Integration.CorruptFieldException) {
@ -1019,53 +1068,13 @@ Zotero.Integration.Fields.prototype._updateSession = function(fields) {
} }
} else if(type === INTEGRATION_TYPE_BIBLIOGRAPHY) { } else if(type === INTEGRATION_TYPE_BIBLIOGRAPHY) {
this._bibliographyFields.push(field); this._bibliographyFields.push(field);
if(!this._session.bibliographyData && !bibliographyData) { if(!this._session.bibliographyData && !this._bibliographyData) {
bibliographyData = content; this._bibliographyData = content;
}
}
}
var endTime = (new Date()).getTime();
if(Zotero.Debug.enabled) {
Zotero.debug("Integration: Updated session data for "+fields.length+" fields in "+
(endTime-collectFieldsTime)/1000+"; "+
1000/((endTime-collectFieldsTime)/fields.length)+" fields/second");
}
// load uncited items from bibliography
if(bibliographyData && !this._session.bibliographyData) {
try {
this._session.loadBibliographyData(bibliographyData);
} catch(e) {
if(e instanceof Zotero.Integration.CorruptFieldException) {
var msg = Zotero.getString("integration.corruptBibliography")+'\n\n'+
Zotero.getString('integration.corruptBibliography.description');
var result = this._doc.displayAlert(msg,
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_CAUTION,
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL);
if(result == 0) {
throw e;
} else {
bibliographyData = "";
this._session.bibliographyHasChanged = true;
this._session.bibliographyDataHasChanged = true;
}
} else {
throw e;
} }
} }
} }
// if we are reloading this session, assume no item IDs to be updated except for edited items callback();
if(this._session.reload) {
//this._session.restoreProcessorState(); TODO doesn't appear to be working properly
this._session.updateUpdateIndices();
var deleteCitations = this._session.updateCitations();
this._deleteFields = this._deleteFields.concat([i for(i in deleteCitations)]);
this._session.updateIndices = {};
this._session.updateItemIDs = {};
this._session.bibliographyHasChanged = false;
delete this._session.reload;
}
} }
/** /**
@ -1222,7 +1231,7 @@ Zotero.Integration.Fields.prototype.updateDocument = function(forceCitations, fo
* Brings up the addCitationDialog, prepopulated if a citation is provided * Brings up the addCitationDialog, prepopulated if a citation is provided
*/ */
Zotero.Integration.Fields.prototype.addEditCitation = function(field, callback) { Zotero.Integration.Fields.prototype.addEditCitation = function(field, callback) {
var newField, citation, var newField, citation, fieldIndex
session = this._session, session = this._session,
me = this, me = this,
io = new function() { this.wrappedJSObject = this; } io = new function() { this.wrappedJSObject = this; }
@ -1276,45 +1285,57 @@ Zotero.Integration.Fields.prototype.addEditCitation = function(field, callback)
// assign preview function // assign preview function
var previewCallbackQueue; var previewCallbackQueue;
function doPreview() {
// fieldIndex will already be set by the time we get here
citation.properties.zoteroIndex = fieldIndex;
citation.properties.noteIndex = field.getNoteIndex();
returnVal = me._session.previewCitation(citation);
for(var i=0, n=previewCallbackQueue.length; i<n; i++) previewCallbackQueue[i](returnVal);
previewCallbackQueue = undefined;
}
io.preview = function(callback) { io.preview = function(callback) {
if(!previewCallbackQueue) previewCallbackQueue = []; if(!previewCallbackQueue) previewCallbackQueue = [];
previewCallbackQueue.push(callback); previewCallbackQueue.push(callback);
var returnVal; var returnVal;
me.get(function() { me.get(function() {
if(!sessionUpdated) { if(sessionUpdated) {
me.updateSession(); doPreview();
sessionUpdated = true; } else {
me.updateSession(function() {
sessionUpdated = true;
doPreview();
});
} }
// fieldIndex will already be set by the time we get here
citation.properties.zoteroIndex = fieldIndex;
citation.properties.noteIndex = field.getNoteIndex();
returnVal = me._session.previewCitation(citation);
for(var i=0, n=previewCallbackQueue.length; i<n; i++) previewCallbackQueue[i](returnVal);
previewCallbackQueue = undefined;
}); });
} }
// assign sort function // assign sort function
io.sort = function() { io.sort = function() {
me._session.previewCitation(io.citation); me._session.previewCitation(io.citation);
} }
// assign accept function // assign accept function
function doAccept() {
Zotero.debug("fieldIndex is "+fieldIndex);
session.addCitation(fieldIndex, field.getNoteIndex(), io.citation);
session.updateIndices[fieldIndex] = true;
me.updateDocument();
callback();
}
io.accept = function(progressCallback) { io.accept = function(progressCallback) {
me._progressCallback = progressCallback; me._progressCallback = progressCallback;
if(io.citation.citationItems.length) { if(io.citation.citationItems.length) {
// Citation added // Citation added
me.get(function() { me.get(function() {
if(!sessionUpdated) me.updateSession(); if(sessionUpdated) {
if(fieldIndex !== undefined) { doAccept();
session.addCitation(fieldIndex, field.getNoteIndex(), io.citation); } else {
me.updateSession(doAccept);
} }
session.updateIndices[fieldIndex] = true;
me.updateDocument();
callback();
}); });
} else if(newField) { } else if(newField) {
// New citation was cancelled // New citation was cancelled
@ -1322,6 +1343,7 @@ Zotero.Integration.Fields.prototype.addEditCitation = function(field, callback)
callback(); callback();
} }
} }
// determine whether citation is sortable in current style // determine whether citation is sortable in current style
io.sortable = session.style.opt.sort_citations; io.sortable = session.style.opt.sort_citations;
@ -1329,11 +1351,11 @@ Zotero.Integration.Fields.prototype.addEditCitation = function(field, callback)
io.style = session.style; io.style = session.style;
// Start finding this citation this citation // Start finding this citation this citation
var fieldIndex;
this.get(function(fields) { this.get(function(fields) {
for(var i=0, n=fields.length; i<n; i++) { for(var i=0, n=fields.length; i<n; i++) {
if(fields[i].equals(field)) { if(fields[i].equals(field)) {
fieldIndex = i; fieldIndex = i;
return;
} }
} }
}); });
@ -1350,13 +1372,13 @@ Zotero.Integration.Fields.prototype.addEditCitation = function(field, callback)
/** /**
* Keeps track of all session-specific variables * Keeps track of all session-specific variables
*/ */
Zotero.Integration.Session = function() { Zotero.Integration.Session = function(doc) {
// holds items not in document that should be in bibliography // holds items not in document that should be in bibliography
this.uncitedItems = {}; this.uncitedItems = {};
this.omittedItems = {}; this.omittedItems = {};
this.customBibliographyText = {}; this.customBibliographyText = {};
this.reselectedItems = {}; this.reselectedItems = {};
this.resetRequest(); this.resetRequest(doc);
} }
/** /**
@ -1427,6 +1449,7 @@ Zotero.Integration.Session.prototype.displayDialog = function(url, options, io,
// without this, Firefox gets raised with our windows under Compiz // without this, Firefox gets raised with our windows under Compiz
if(Zotero.isLinux) allOptions += ',dialog=no'; if(Zotero.isLinux) allOptions += ',dialog=no';
if(options) allOptions += ','+options; if(options) allOptions += ','+options;
if(!async) allOptions += ',modal=yes';
var window = Components.classes["@mozilla.org/embedcomp/window-watcher;1"] var window = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
.getService(Components.interfaces.nsIWindowWatcher) .getService(Components.interfaces.nsIWindowWatcher)
@ -1434,37 +1457,32 @@ Zotero.Integration.Session.prototype.displayDialog = function(url, options, io,
Zotero.Integration.currentWindow = window; Zotero.Integration.currentWindow = window;
Zotero.Integration.activate(window); Zotero.Integration.activate(window);
if(async) { var listener = function() {
var listener = function() { if(window.location.toString() === "about:blank") return;
if(window.location.toString() === "about:blank") return;
if(window.newWindow) { if(window.newWindow) {
window = window.newWindow; window = window.newWindow;
Zotero.Integration.currentWindow = window; Zotero.Integration.currentWindow = window;
window.addEventListener("unload", listener, false); window.addEventListener("unload", listener, false);
return; return;
} }
Zotero.Integration.currentWindow = false;
if(async instanceof Function) async();
}
window.addEventListener("unload", listener, false);
} else {
while(!window.closed) {
Zotero.mainThread.processNextEvent(true);
if(window.newWindow) {
window = window.newWindow;
Zotero.Integration.currentWindow = window;
}
}
Zotero.Integration.currentWindow = false; Zotero.Integration.currentWindow = false;
if(async instanceof Function) {
try {
async();
} catch(e) {
Zotero.Integration.handleError(e, this.doc);
}
}
} }
window.addEventListener("unload", listener, false);
} }
/** /**
* Displays a dialog to set document preferences * Displays a dialog to set document preferences
*/ */
Zotero.Integration.Session.prototype.setDocPrefs = function(primaryFieldType, secondaryFieldType) { Zotero.Integration.Session.prototype.setDocPrefs = function(primaryFieldType, secondaryFieldType, callback) {
var io = new function() { var io = new function() {
this.wrappedJSObject = this; this.wrappedJSObject = this;
}; };
@ -1478,56 +1496,64 @@ Zotero.Integration.Session.prototype.setDocPrefs = function(primaryFieldType, se
io.storeReferences = this.data.prefs.storeReferences; io.storeReferences = this.data.prefs.storeReferences;
} }
this.displayDialog('chrome://zotero/content/integration/integrationDocPrefs.xul', '', io); var me = this;
if(!io.style) throw new Zotero.Integration.UserCancelledException(); this.displayDialog('chrome://zotero/content/integration/integrationDocPrefs.xul', '', io, function() {
if(!io.style) {
Zotero.Integration.activate();
return;
}
// set data // set data
var oldData = this.data; var oldData = me.data;
var data = new Zotero.Integration.DocumentData(); var data = new Zotero.Integration.DocumentData();
data.sessionID = oldData.sessionID; data.sessionID = oldData.sessionID;
data.style.styleID = io.style; data.style.styleID = io.style;
data.prefs.fieldType = io.fieldType; data.prefs.fieldType = io.fieldType;
data.prefs.storeReferences = io.storeReferences; data.prefs.storeReferences = io.storeReferences;
this.setData(data); me.setData(data);
// need to do this after setting the data so that we know if it's a note style // need to do this after setting the data so that we know if it's a note style
this.data.prefs.noteType = this.style && this.styleClass == "note" ? io.useEndnotes+1 : 0; me.data.prefs.noteType = me.style && me.styleClass == "note" ? io.useEndnotes+1 : 0;
if(!oldData || oldData.style.styleID != data.style.styleID if(!oldData || oldData.style.styleID != data.style.styleID
|| oldData.prefs.noteType != data.prefs.noteType || oldData.prefs.noteType != data.prefs.noteType
|| oldData.prefs.fieldType != data.prefs.fieldType) { || oldData.prefs.fieldType != data.prefs.fieldType) {
this.oldCitationIDs = {}; me.oldCitationIDs = {};
} }
return oldData ? oldData : true; callback(oldData ? oldData : true);
});
} }
/** /**
* Reselects an item to replace a deleted item * Reselects an item to replace a deleted item
* @param exception {Zotero.Integration.MissingItemException} * @param exception {Zotero.Integration.MissingItemException}
*/ */
Zotero.Integration.Session.prototype.reselectItem = function(exception) { Zotero.Integration.Session.prototype.reselectItem = function(exception, callback) {
var io = new function() { var io = new function() {
this.wrappedJSObject = this; this.wrappedJSObject = this;
}; },
me = this;
io.addBorder = Zotero.isWin; io.addBorder = Zotero.isWin;
io.singleSelection = true; io.singleSelection = true;
this.displayDialog('chrome://zotero/content/selectItemsDialog.xul', 'resizable', io); this.displayDialog('chrome://zotero/content/selectItemsDialog.xul', 'resizable', io, function() {
if(io.dataOut && io.dataOut.length) {
var itemID = io.dataOut[0];
if(io.dataOut && io.dataOut.length) { // add reselected item IDs to hash, so they can be used
var itemID = io.dataOut[0]; for each(var reselectKey in exception.reselectKeys) {
me.reselectedItems[reselectKey] = itemID;
}
// add old URIs to map, so that they will be included
if(exception.reselectKeyType == RESELECT_KEY_URI) {
me.uriMap.add(itemID, exception.reselectKeys.concat(me.uriMap.getURIsForItemID(itemID)));
}
// flag for update
me.updateItemIDs[itemID] = true;
}
// add reselected item IDs to hash, so they can be used callback();
for each(var reselectKey in exception.reselectKeys) { });
this.reselectedItems[reselectKey] = itemID;
}
// add old URIs to map, so that they will be included
if(exception.reselectKeyType == RESELECT_KEY_URI) {
this.uriMap.add(itemID, exception.reselectKeys.concat(this.uriMap.getURIsForItemID(itemID)));
}
// flag for update
this.updateItemIDs[itemID] = true;
}
} }
/** /**
@ -2197,7 +2223,6 @@ Zotero.Integration.Session.prototype.previewCitation = function(citation) {
try { try {
return this.style.previewCitationCluster(citation, citationsPre, citationsPost, "rtf"); return this.style.previewCitationCluster(citation, citationsPre, citationsPost, "rtf");
} catch(e) { } catch(e) {
Zotero.debug(e);
throw e; throw e;
} }
} }
@ -2205,13 +2230,13 @@ Zotero.Integration.Session.prototype.previewCitation = function(citation) {
/** /**
* Edits integration bibliography * Edits integration bibliography
*/ */
Zotero.Integration.Session.prototype.editBibliography = function() { Zotero.Integration.Session.prototype.editBibliography = function(callback) {
var bibliographyEditor = new Zotero.Integration.Session.BibliographyEditInterface(this); var bibliographyEditor = new Zotero.Integration.Session.BibliographyEditInterface(this);
var io = new function() { this.wrappedJSObject = bibliographyEditor; } var io = new function() { this.wrappedJSObject = bibliographyEditor; }
this.bibliographyDataHasChanged = this.bibliographyHasChanged = true; this.bibliographyDataHasChanged = this.bibliographyHasChanged = true;
this.displayDialog('chrome://zotero/content/integration/editBibliographyDialog.xul', 'resizable', io); this.displayDialog('chrome://zotero/content/integration/editBibliographyDialog.xul', 'resizable', io, callback);
} }
/** /**