From 52fd0d992dee8e4a4f3b8e786abbbf708987897c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Wed, 17 May 2017 17:15:01 +0300 Subject: [PATCH] Add a wrapper class for citation and bibliography fields --- chrome/content/zotero/xpcom/integration.js | 484 ++++++++++++--------- 1 file changed, 276 insertions(+), 208 deletions(-) diff --git a/chrome/content/zotero/xpcom/integration.js b/chrome/content/zotero/xpcom/integration.js index 3a1a4a2838..ab028ab2d4 100644 --- a/chrome/content/zotero/xpcom/integration.js +++ b/chrome/content/zotero/xpcom/integration.js @@ -744,10 +744,9 @@ Zotero.Integration.Interface.prototype.editBibliography = function() { return fieldGetter.get(); }).then(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) { + for (let i = fields.length-1; i >= 0; i--) { + let field = Zotero.Integration.Field.loadExisting(fields[i]); + if (field.type == INTEGRATION_TYPE_BIBLIOGRAPHY) { haveBibliography = true; break; } @@ -779,10 +778,9 @@ Zotero.Integration.Interface.prototype.addEditBibliography = Zotero.Promise.coro var fields = yield fieldGetter.get(); 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) { + for (let i = fields.length-1; i >= 0; i--) { + let field = Zotero.Integration.Field.loadExisting(fields[i]); + if (field.type == INTEGRATION_TYPE_BIBLIOGRAPHY) { haveBibliography = true; break; } @@ -792,7 +790,7 @@ Zotero.Integration.Interface.prototype.addEditBibliography = Zotero.Promise.coro yield fieldGetter.updateSession(); yield session.editBibliography(this._doc); } else { - var field = yield fieldGetter.addField(); + var field = new Zotero.Integration.BibliographyField(yield fieldGetter.addField()); field.setCode("BIBL"); yield fieldGetter.updateSession(); } @@ -838,91 +836,84 @@ Zotero.Integration.Interface.prototype.removeCodes = function() { * Displays a dialog to set document preferences (style, footnotes/endnotes, etc.) * @return {Promise} */ -Zotero.Integration.Document.prototype.setDocPrefs = function() { - var me = this, - fieldGetter, +Zotero.Integration.Interface.prototype.setDocPrefs = Zotero.Promise.coroutine(function* () { + var fieldGetter, oldData; - return this._getSession(false, true).then(function(haveSession) { - fieldGetter = new Zotero.Integration.Fields(me._session, me._doc, Zotero.Integration.onFieldError); - var setDocPrefs = me._session.setDocPrefs.bind(me._session, me._doc, - me._app.primaryFieldType, me._app.secondaryFieldType); - if(!haveSession) { - // This is a brand new document; don't try to get fields - return setDocPrefs(); - } else { - // Can get fields while dialog is open - return Zotero.Promise.all([ - fieldGetter.get(), - setDocPrefs() - ]).spread(function (fields, setDocPrefs) { - // Only return value from setDocPrefs - return setDocPrefs; - }); - } - }).then(function(aOldData) { // After setDocPrefs call - oldData = aOldData; - - // Write document data to document - me._doc.setDocumentData(me._session.data.serialize()); - - // If oldData is null, then there was no document data, so we don't need to update - // fields - if(!oldData) return false; - return fieldGetter.get(); - }).then(function(fields) { - if(!fields || !fields.length) return; + let haveSession = yield this._getSession(false, true); - // 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 this._text = this._text ? this._text : this.getText(), + set: (v) => {this._text = v; this.dirty = true} +}); + +Zotero.defineProperty(Zotero.Integration.Field.prototype, 'code', { + get: () => this._code = this._code ? this._code : this.getCode(), + set: (v) => {this._code = v; this.dirty = true;} +}); + +Zotero.Integration.Field.prototype = { + writeToDoc: function(doc) { + this.dirty = false; + + let text = this._text; + let isRich = false; + // If RTF wrap with RTF tags + if (text.indexOf("\\") !== -1) { + text = "{\\rtf "+text+"}"; + isRich = true; + } + this._field.setText(text, isRich); + + // Boo. Inconsistent. + if (this.type == INTEGRATION_TYPE_ITEM) { + this._field.setCode(`ITEM CSL_CITATION ${JSON.stringify(this._data)}`); + } else if (this.type == INTEGRATION_TYPE_BIBLIOGRAPHY) { + this._field.setCode(`BIBL ${JSON.stringify(this._data)} CSL_BIBLIOGRAPHY`); + } + + // Retrigger retrieval from doc. + this._text = null; + this._code = null; + }, + + getCode: function() { + let code = this._field.getCode(); + let start = code.indexOf('{'); + if (start == -1) { + return '{}'; + } + return code.substr(start, start + code.indexOf('}')); + } +}; + + +Zotero.Integration.CitationField = function(field) { + Zotero.Integration.Field.call(this, field); + this.type = INTEGRATION_TYPE_ITEM; + + return new Proxy(this, Zotero.Integration.Field.proxyHandler); +}; +Zotero.extendClass(Zotero.Integration.Field, Zotero.Integration.CitationField); + +Zotero.Integration.BibliographyField = function(field) { + Zotero.Integration.Field.call(this, field); + this.type = INTEGRATION_TYPE_BIBLIOGRAPHY; + + return new Proxy(this, Zotero.Integration.Field.proxyHandler); +}; +Zotero.extendClass(Zotero.Integration.Field, Zotero.Integration.BibliographyField);