diff --git a/chrome/content/zotero/xpcom/integration.js b/chrome/content/zotero/xpcom/integration.js index b6f6307d7..1bf836501 100644 --- a/chrome/content/zotero/xpcom/integration.js +++ b/chrome/content/zotero/xpcom/integration.js @@ -480,7 +480,7 @@ Zotero.Integration.Document = function(app, doc) { */ Zotero.Integration.Document.prototype._createNewSession = function(data) { 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; } @@ -491,8 +491,9 @@ Zotero.Integration.Document.prototype._createNewSession = function(data) { * @param dontRunSetDocPrefs {Boolean} Whether to show the Set Document Preferences window if no * preferences exist */ -Zotero.Integration.Document.prototype._getSession = function(require, dontRunSetDocPrefs) { - var dataString = this._doc.getDocumentData(); +Zotero.Integration.Document.prototype._getSession = function(require, dontRunSetDocPrefs, callback) { + var dataString = this._doc.getDocumentData(), + me = this; if(!dataString) { var haveFields = false; 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 this._session = this._createNewSession(data); this._session.setData(data); - if(dontRunSetDocPrefs) return false; - - try { - var ret = this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType); - } finally { - this._doc.activate(); + if(dontRunSetDocPrefs) { + callback(); + return; } - // save doc prefs in doc - this._doc.setDocumentData(this._session.data.serializeXML()); - - if(haveFields) { - this._session.reload = true; - } + this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType, function() { + // save doc prefs in doc + me._doc.setDocumentData(me._session.data.serializeXML()); + + if(haveFields) { + me._session.reload = true; + } + callback(); + }); } else { var data = new Zotero.Integration.DocumentData(dataString); if(data.dataVersion < DATA_VERSION) { @@ -565,34 +566,34 @@ Zotero.Integration.Document.prototype._getSession = function(require, dontRunSet } catch(e) { // make sure style is defined if(e instanceof Zotero.Integration.DisplayException && e.name === "invalidStyle") { - try { - this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType); - } finally { - this._doc.activate(); - } + this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType, function() { + me._doc.setDocumentData(me._session.data.serializeXML()); + me._session.reload = true; + callback(true); + }); + return; } else { throw e; } } - this._doc.setDocumentData(this._session.data.serializeXML()); + this._doc.setDocumentData(this._session.data.serializeXML()); this._session.reload = true; } + callback(); } - - return !!dataString; } /** * Adds a citation to the current document. */ Zotero.Integration.Document.prototype.addCitation = function() { - this._getSession(); - - var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), - me = this; - fieldGetter.addEditCitation(null, function() { - Zotero.Integration.complete(me._doc); + var me = this; + this._getSession(false, false, function() { + var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc); + fieldGetter.addEditCitation(null, function() { + Zotero.Integration.complete(me._doc); + }); }); } @@ -600,17 +601,17 @@ Zotero.Integration.Document.prototype.addCitation = function() { * Edits the citation at the cursor position. */ Zotero.Integration.Document.prototype.editCitation = function() { - this._getSession(true); - - var field = this._doc.cursorInField(this._session.data.prefs['fieldType']) - if(!field) { - throw new Zotero.Integration.DisplayException("notInCitation"); - } - - var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), - me = this; - fieldGetter.addEditCitation(field, function() { - Zotero.Integration.complete(me._doc); + 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 fieldGetter = new Zotero.Integration.Fields(me._session, me._doc); + 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. */ Zotero.Integration.Document.prototype.addBibliography = function() { - this._getSession(true); - - // Make sure we can have a bibliography - if(!this._session.data.style.hasBibliography) { - throw new Zotero.Integration.DisplayException("noBibliography"); - } - - var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), - field = fieldGetter.addField(), - me = this; - field.setCode("BIBL"); - fieldGetter.updateSession(function() { - fieldGetter.updateDocument(false, true); - Zotero.Integration.complete(me._doc); + 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"); + } + + var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc), + field = fieldGetter.addField(); + field.setCode("BIBL"); + fieldGetter.updateSession(function() { + fieldGetter.updateDocument(false, true); + Zotero.Integration.complete(me._doc); + }); }); } /** * Edits bibliography metadata. */ -Zotero.Integration.Document.prototype.editBibliography = function() { +Zotero.Integration.Document.prototype.editBibliography = function(callback) { // Make sure we have a bibliography - this._getSession(true); - - var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), - me = this; - fieldGetter.get(function(fields) { - var haveBibliography = false; - for(var i=fields.length-1; i>=0; i--) { - var code = fields[i].getCode(); - var [type, content] = fieldGetter.getCodeTypeAndContent(code); - if(type == INTEGRATION_TYPE_BIBLIOGRAPHY) { - haveBibliography = true; - break; + var me = this; + this._getSession(true, false, function() { + var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc); + fieldGetter.get(function(fields) { + var haveBibliography = false; + for(var i=fields.length-1; i>=0; i--) { + var code = fields[i].getCode(); + var [type, content] = fieldGetter.getCodeTypeAndContent(code); + if(type == INTEGRATION_TYPE_BIBLIOGRAPHY) { + haveBibliography = true; + break; + } } - } - - if(!haveBibliography) { - throw new Zotero.Integration.DisplayException("mustInsertBibliography"); - } - - fieldGetter.updateSession(); - me._session.editBibliography(); - me._doc.activate(); - fieldGetter.updateDocument(false, true); - - Zotero.Integration.complete(me._doc); + + if(!haveBibliography) { + throw new Zotero.Integration.DisplayException("mustInsertBibliography"); + } + + fieldGetter.updateSession(function() { + me._session.editBibliography(function() { + me._doc.activate(); + fieldGetter.updateDocument(false, true); + + 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. */ Zotero.Integration.Document.prototype.refresh = function() { - this._getSession(true); - - // Send request, forcing update of citations and bibliography - var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), - me = this; - fieldGetter.updateSession(function() { - fieldGetter.updateDocument(true, true); - Zotero.Integration.complete(me._doc); + var me = this; + this._getSession(true, false, function() { + // Send request, forcing update of citations and bibliography + var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc); + fieldGetter.updateSession(function() { + fieldGetter.updateDocument(true, true); + Zotero.Integration.complete(me._doc); + }); }); } @@ -687,21 +690,21 @@ Zotero.Integration.Document.prototype.refresh = function() { * Deletes field codes. */ Zotero.Integration.Document.prototype.removeCodes = function() { - this._getSession(true); - - var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc), - me = this; - fieldGetter.get(function(fields) { - var result = me._doc.displayAlert(Zotero.getString("integration.removeCodesWarning"), - Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_WARNING, - Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL); - if(result) { - for(var i=fields.length-1; i>=0; i--) { - fields[i].removeCode(); + var me = this; + this._getSession(true, false, function() { + var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc); + fieldGetter.get(function(fields) { + var result = me._doc.displayAlert(Zotero.getString("integration.removeCodesWarning"), + Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_WARNING, + Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL); + if(result) { + for(var i=fields.length-1; i>=0; i--) { + 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.) */ Zotero.Integration.Document.prototype.setDocPrefs = function() { - this._getSession(false, true); - - var fieldGetter = new Zotero.Integration.Fields(this._session, this._doc); - fieldGetter.get(); - - try { - var oldData = this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType); - } finally { - this._doc.activate(); - } - - if(oldData) { - this._doc.setDocumentData(this._session.data.serializeXML()); + var me = this; + this._getSession(false, true, function() { + var fieldGetter = new Zotero.Integration.Fields(me._session, me._doc); + fieldGetter.get(); - var me = this; - fieldGetter.get(function(fields) { - 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 - 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