Add a wrapper class for citation and bibliography fields
This commit is contained in:
parent
41c93ab034
commit
52fd0d992d
|
@ -744,10 +744,9 @@ Zotero.Integration.Interface.prototype.editBibliography = function() {
|
||||||
return fieldGetter.get();
|
return fieldGetter.get();
|
||||||
}).then(function(fields) {
|
}).then(function(fields) {
|
||||||
var haveBibliography = false;
|
var haveBibliography = false;
|
||||||
for(var i=fields.length-1; i>=0; i--) {
|
for (let i = fields.length-1; i >= 0; i--) {
|
||||||
var code = fields[i].getCode();
|
let field = Zotero.Integration.Field.loadExisting(fields[i]);
|
||||||
var [type, content] = fieldGetter.getCodeTypeAndContent(code);
|
if (field.type == INTEGRATION_TYPE_BIBLIOGRAPHY) {
|
||||||
if(type == INTEGRATION_TYPE_BIBLIOGRAPHY) {
|
|
||||||
haveBibliography = true;
|
haveBibliography = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -779,10 +778,9 @@ Zotero.Integration.Interface.prototype.addEditBibliography = Zotero.Promise.coro
|
||||||
var fields = yield fieldGetter.get();
|
var fields = yield fieldGetter.get();
|
||||||
|
|
||||||
var haveBibliography = false;
|
var haveBibliography = false;
|
||||||
for (var i=fields.length-1; i>=0; i--) {
|
for (let i = fields.length-1; i >= 0; i--) {
|
||||||
var code = fields[i].getCode();
|
let field = Zotero.Integration.Field.loadExisting(fields[i]);
|
||||||
var [type, content] = fieldGetter.getCodeTypeAndContent(code);
|
if (field.type == INTEGRATION_TYPE_BIBLIOGRAPHY) {
|
||||||
if (type == INTEGRATION_TYPE_BIBLIOGRAPHY) {
|
|
||||||
haveBibliography = true;
|
haveBibliography = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -792,7 +790,7 @@ Zotero.Integration.Interface.prototype.addEditBibliography = Zotero.Promise.coro
|
||||||
yield fieldGetter.updateSession();
|
yield fieldGetter.updateSession();
|
||||||
yield session.editBibliography(this._doc);
|
yield session.editBibliography(this._doc);
|
||||||
} else {
|
} else {
|
||||||
var field = yield fieldGetter.addField();
|
var field = new Zotero.Integration.BibliographyField(yield fieldGetter.addField());
|
||||||
field.setCode("BIBL");
|
field.setCode("BIBL");
|
||||||
yield fieldGetter.updateSession();
|
yield fieldGetter.updateSession();
|
||||||
}
|
}
|
||||||
|
@ -838,91 +836,84 @@ Zotero.Integration.Interface.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.)
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
Zotero.Integration.Document.prototype.setDocPrefs = function() {
|
Zotero.Integration.Interface.prototype.setDocPrefs = Zotero.Promise.coroutine(function* () {
|
||||||
var me = this,
|
var fieldGetter,
|
||||||
fieldGetter,
|
|
||||||
oldData;
|
oldData;
|
||||||
return this._getSession(false, true).then(function(haveSession) {
|
let haveSession = yield this._getSession(false, true);
|
||||||
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;
|
|
||||||
|
|
||||||
// If there are fields, we will have to convert some things; get a list of what
|
fieldGetter = new Zotero.Integration.Fields(this._session, this._doc, Zotero.Integration.onFieldError);
|
||||||
// we need to deal with
|
var setDocPrefs = this._session.setDocPrefs.bind(this._session, this._doc,
|
||||||
var convertBibliographies = oldData === true
|
this._app.primaryFieldType, this._app.secondaryFieldType);
|
||||||
|| 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(convertItems && type === INTEGRATION_TYPE_ITEM) {
|
if(!haveSession) {
|
||||||
var citation = me._session.unserializeCitation(fieldCode);
|
// This is a brand new document; don't try to get fields
|
||||||
if(!citation.properties.dontUpdate) {
|
oldData = yield setDocPrefs();
|
||||||
fieldsToConvert.push(field);
|
} else {
|
||||||
fieldNoteTypes.push(me._session.data.prefs.noteType);
|
// Can get fields while dialog is open
|
||||||
}
|
oldData = yield Zotero.Promise.all([
|
||||||
} else if(convertBibliographies
|
fieldGetter.get(),
|
||||||
&& type === INTEGRATION_TYPE_BIBLIOGRAPHY) {
|
setDocPrefs()
|
||||||
|
]).spread(function (fields, setDocPrefs) {
|
||||||
|
// Only return value from setDocPrefs
|
||||||
|
return setDocPrefs;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write document data to document
|
||||||
|
this._doc.setDocumentData(this._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;
|
||||||
|
|
||||||
|
// Perform noteType or fieldType conversion
|
||||||
|
let fields = yield fieldGetter.get();
|
||||||
|
|
||||||
|
var convertBibliographies = oldData.prefs.fieldType != this._session.data.prefs.fieldType;
|
||||||
|
var convertItems = convertBibliographies
|
||||||
|
|| oldData.prefs.noteType != this._session.data.prefs.noteType;
|
||||||
|
var fieldsToConvert = new Array();
|
||||||
|
var fieldNoteTypes = new Array();
|
||||||
|
for (var i=0, n=fields.length; i<n; i++) {
|
||||||
|
let field = Zotero.Integration.Field.loadExisting(fields[i]);
|
||||||
|
|
||||||
|
if (convertItems && field.type === INTEGRATION_TYPE_ITEM) {
|
||||||
|
var citation = this._session.unserializeCitation(field.code);
|
||||||
|
if (!citation.properties.dontUpdate) {
|
||||||
fieldsToConvert.push(field);
|
fieldsToConvert.push(field);
|
||||||
fieldNoteTypes.push(0);
|
fieldNoteTypes.push(this._session.data.prefs.noteType);
|
||||||
}
|
}
|
||||||
|
} else if(convertBibliographies
|
||||||
|
&& type === INTEGRATION_TYPE_BIBLIOGRAPHY) {
|
||||||
|
fieldsToConvert.push(field);
|
||||||
|
fieldNoteTypes.push(0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if(fieldsToConvert.length) {
|
|
||||||
// Pass to conversion function
|
if(fieldsToConvert.length) {
|
||||||
me._doc.convert(new Zotero.Integration.Document.JSEnumerator(fieldsToConvert),
|
// Pass to conversion function
|
||||||
me._session.data.prefs.fieldType, fieldNoteTypes,
|
this._doc.convert(new Zotero.Integration.JSEnumerator(fieldsToConvert),
|
||||||
fieldNoteTypes.length);
|
this._session.data.prefs.fieldType, fieldNoteTypes,
|
||||||
}
|
fieldNoteTypes.length);
|
||||||
|
}
|
||||||
// Refresh contents
|
|
||||||
fieldGetter = new Zotero.Integration.Fields(me._session, me._doc, Zotero.Integration.onFieldError);
|
// Refresh contents
|
||||||
fieldGetter.ignoreEmptyBibliography = false;
|
fieldGetter = new Zotero.Integration.Fields(this._session, this._doc, Zotero.Integration.onFieldError);
|
||||||
return fieldGetter.updateSession().then(fieldGetter.updateDocument.bind(
|
fieldGetter.ignoreEmptyBibliography = false;
|
||||||
fieldGetter, FORCE_CITATIONS_RESET_TEXT, true, true));
|
return fieldGetter.updateSession().then(fieldGetter.updateDocument.bind(
|
||||||
});
|
fieldGetter, FORCE_CITATIONS_RESET_TEXT, true, true));
|
||||||
}
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An exceedingly simple nsISimpleEnumerator implementation
|
* An exceedingly simple nsISimpleEnumerator implementation
|
||||||
*/
|
*/
|
||||||
Zotero.Integration.Document.JSEnumerator = function(objArray) {
|
Zotero.Integration.JSEnumerator = function(objArray) {
|
||||||
this.objArray = objArray;
|
this.objArray = objArray;
|
||||||
}
|
}
|
||||||
Zotero.Integration.Document.JSEnumerator.prototype.hasMoreElements = function() {
|
Zotero.Integration.JSEnumerator.prototype.hasMoreElements = function() {
|
||||||
return this.objArray.length;
|
return this.objArray.length;
|
||||||
}
|
}
|
||||||
Zotero.Integration.Document.JSEnumerator.prototype.getNext = function() {
|
Zotero.Integration.JSEnumerator.prototype.getNext = function() {
|
||||||
return this.objArray.shift();
|
return this.objArray.shift();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -984,27 +975,6 @@ Zotero.Integration.Fields.prototype.addField = function(note) {
|
||||||
return Zotero.Promise.resolve(field);
|
return Zotero.Promise.resolve(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the type and content of a field object
|
|
||||||
*/
|
|
||||||
Zotero.Integration.Fields.prototype.getCodeTypeAndContent = function(rawCode) {
|
|
||||||
for (let code of ["ITEM", "CITATION"]) {
|
|
||||||
if(rawCode.substr(0, code.length) === code) {
|
|
||||||
return [INTEGRATION_TYPE_ITEM, rawCode.substr(code.length+1)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(rawCode.substr(0, 4) === "BIBL") {
|
|
||||||
return [INTEGRATION_TYPE_BIBLIOGRAPHY, rawCode.substr(5)];
|
|
||||||
}
|
|
||||||
|
|
||||||
if(rawCode.substr(0, 4) === "TEMP") {
|
|
||||||
return [INTEGRATION_TYPE_TEMP, rawCode.substr(5)];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [null, rawCode];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all fields for a document
|
* Gets all fields for a document
|
||||||
* @return {Promise} Promise resolved with field list.
|
* @return {Promise} Promise resolved with field list.
|
||||||
|
@ -1147,22 +1117,11 @@ Zotero.Integration.Fields.prototype._processFields = Zotero.Promise.coroutine(fu
|
||||||
if(!i) i = 0;
|
if(!i) i = 0;
|
||||||
|
|
||||||
for(var n = this._fields.length; i<n; i++) {
|
for(var n = this._fields.length; i<n; i++) {
|
||||||
var field = this._fields[i];
|
let field = Zotero.Integration.Field.loadExisting(this._fields[i]);
|
||||||
|
if (field.type === INTEGRATION_TYPE_ITEM) {
|
||||||
try {
|
|
||||||
var fieldCode = field.getCode();
|
|
||||||
} catch(e) {
|
|
||||||
var corruptFieldException = new Zotero.Integration.CorruptFieldException(
|
|
||||||
"Field code not retrievable", e);
|
|
||||||
corruptFieldException.setContext(this, i);
|
|
||||||
throw corruptFieldException;
|
|
||||||
}
|
|
||||||
|
|
||||||
var [type, content] = this.getCodeTypeAndContent(fieldCode);
|
|
||||||
if(type === INTEGRATION_TYPE_ITEM) {
|
|
||||||
var noteIndex = field.getNoteIndex();
|
var noteIndex = field.getNoteIndex();
|
||||||
try {
|
try {
|
||||||
yield this._session.addCitation(i, noteIndex, content);
|
yield this._session.addCitation(i, noteIndex, field.code);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
var removeCode = false;
|
var removeCode = false;
|
||||||
|
|
||||||
|
@ -1185,13 +1144,13 @@ Zotero.Integration.Fields.prototype._processFields = Zotero.Promise.coroutine(fu
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if(type === INTEGRATION_TYPE_BIBLIOGRAPHY) {
|
} else if (field.type === INTEGRATION_TYPE_BIBLIOGRAPHY) {
|
||||||
if(this.ignoreEmptyBibliography && field.getText().trim() === "") {
|
if (this.ignoreEmptyBibliography && field.text.trim() === "") {
|
||||||
this._removeCodeFields[i] = true;
|
this._removeCodeFields[i] = true;
|
||||||
} else {
|
} else {
|
||||||
this._bibliographyFields.push(field);
|
this._bibliographyFields.push(field);
|
||||||
if(!this._session.bibliographyData && !this._bibliographyData) {
|
if(!this._session.bibliographyData && !this._bibliographyData) {
|
||||||
this._bibliographyData = content;
|
this._bibliographyData = field.code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1378,93 +1337,84 @@ Zotero.Integration.Fields.prototype._updateDocument = function* (forceCitations,
|
||||||
* 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 = Zotero.Promise.coroutine(function* (field) {
|
Zotero.Integration.Fields.prototype.addEditCitation = Zotero.Promise.coroutine(function* (field) {
|
||||||
var newField, citation, fieldIndex, session = this._session;
|
var newField, citation, session = this._session;
|
||||||
|
|
||||||
// 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) {
|
||||||
|
field = Zotero.Integration.Field.loadExisting(field);
|
||||||
|
if (field.type != INTEGRATION_TYPE_ITEM) {
|
||||||
|
throw new Zotero.Exception.Alert("integration.error.notInCitation");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var code = field.getCode();
|
citation = session.unserializeCitation(field.code);
|
||||||
} catch(e) {}
|
} catch(e) {}
|
||||||
|
|
||||||
if(code) {
|
if (citation) {
|
||||||
var [type, content] = this.getCodeTypeAndContent(code);
|
|
||||||
if(type != INTEGRATION_TYPE_ITEM) {
|
|
||||||
throw new Zotero.Exception.Alert("integration.error.notInCitation");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
citation = session.unserializeCitation(content);
|
yield session.lookupItems(citation);
|
||||||
} catch(e) {}
|
} catch(e) {
|
||||||
|
if(e instanceof Zotero.Integration.MissingItemException) {
|
||||||
if(citation) {
|
citation.citationItems = [];
|
||||||
try {
|
} else {
|
||||||
yield session.lookupItems(citation);
|
throw e;
|
||||||
} catch(e) {
|
|
||||||
if(e instanceof Zotero.Integration.MissingItemException) {
|
|
||||||
citation.citationItems = [];
|
|
||||||
} else {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(citation.properties.dontUpdate
|
|
||||||
|| (citation.properties.plainCitation
|
|
||||||
&& field.getText() !== citation.properties.plainCitation)) {
|
|
||||||
this._doc.activate();
|
|
||||||
Zotero.debug("[addEditCitation] Attempting to update manually modified citation.\n"
|
|
||||||
+ "citation.properties.dontUpdate: " + citation.properties.dontUpdate + "\n"
|
|
||||||
+ "Original: " + citation.properties.plainCitation + "\n"
|
|
||||||
+ "Current: " + field.getText()
|
|
||||||
);
|
|
||||||
if(!this._doc.displayAlert(Zotero.getString("integration.citationChanged.edit"),
|
|
||||||
DIALOG_ICON_WARNING, DIALOG_BUTTONS_OK_CANCEL)) {
|
|
||||||
throw new Zotero.Exception.UserCancelled("editing citation");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure it's going to get updated
|
|
||||||
delete citation.properties["formattedCitation"];
|
|
||||||
delete citation.properties["plainCitation"];
|
|
||||||
delete citation.properties["dontUpdate"];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(citation.properties.dontUpdate
|
||||||
|
|| (citation.properties.plainCitation
|
||||||
|
&& field.getText() !== citation.properties.plainCitation)) {
|
||||||
|
this._doc.activate();
|
||||||
|
Zotero.debug("[addEditCitation] Attempting to update manually modified citation.\n"
|
||||||
|
+ "citation.properties.dontUpdate: " + citation.properties.dontUpdate + "\n"
|
||||||
|
+ "Original: " + citation.properties.plainCitation + "\n"
|
||||||
|
+ "Current: " + field.getText()
|
||||||
|
);
|
||||||
|
if(!this._doc.displayAlert(Zotero.getString("integration.citationChanged.edit"),
|
||||||
|
DIALOG_ICON_WARNING, DIALOG_BUTTONS_OK_CANCEL)) {
|
||||||
|
throw new Zotero.Exception.UserCancelled("editing citation");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure it's going to get updated
|
||||||
|
delete citation.properties["formattedCitation"];
|
||||||
|
delete citation.properties["plainCitation"];
|
||||||
|
delete citation.properties["dontUpdate"];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
newField = true;
|
newField = true;
|
||||||
field = this.addField(true);
|
field = new Zotero.Integration.CitationField(yield this.addField(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
var me = this;
|
if (!citation) {
|
||||||
return Zotero.Promise.resolve(field).then(function(field) {
|
field.setCode("TEMP");
|
||||||
if(!citation) {
|
citation = {"citationItems":[], "properties":{}};
|
||||||
field.setCode("TEMP");
|
}
|
||||||
citation = {"citationItems":[], "properties":{}};
|
|
||||||
}
|
var io = new Zotero.Integration.CitationEditInterface(citation, field, this, session);
|
||||||
|
|
||||||
var io = new Zotero.Integration.CitationEditInterface(citation, field, me, session);
|
if (Zotero.Prefs.get("integration.useClassicAddCitationDialog")) {
|
||||||
|
Zotero.Integration.displayDialog(this._doc,
|
||||||
if(Zotero.Prefs.get("integration.useClassicAddCitationDialog")) {
|
'chrome://zotero/content/integration/addCitationDialog.xul', 'alwaysRaised,resizable',
|
||||||
Zotero.Integration.displayDialog(me._doc,
|
io);
|
||||||
'chrome://zotero/content/integration/addCitationDialog.xul', 'alwaysRaised,resizable',
|
} else {
|
||||||
io);
|
var mode = (!Zotero.isMac && Zotero.Prefs.get('integration.keepAddCitationDialogRaised')
|
||||||
} else {
|
? 'popup' : 'alwaysRaised')+',resizable=false';
|
||||||
var mode = (!Zotero.isMac && Zotero.Prefs.get('integration.keepAddCitationDialogRaised')
|
Zotero.Integration.displayDialog(this._doc,
|
||||||
? 'popup' : 'alwaysRaised')+',resizable=false';
|
'chrome://zotero/content/integration/quickFormat.xul', mode, io);
|
||||||
Zotero.Integration.displayDialog(me._doc,
|
}
|
||||||
'chrome://zotero/content/integration/quickFormat.xul', mode, io);
|
|
||||||
}
|
if (newField) {
|
||||||
|
return io.promise.catch(function(e) {
|
||||||
if(newField) {
|
// Try to delete new field on failure
|
||||||
return io.promise.catch(function(e) {
|
try {
|
||||||
// Try to delete new field on failure
|
field.delete();
|
||||||
try {
|
} catch(e) {}
|
||||||
field.delete();
|
throw e;
|
||||||
} catch(e) {}
|
});
|
||||||
throw e;
|
} else {
|
||||||
});
|
return io.promise;
|
||||||
} else {
|
}
|
||||||
return io.promise;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2090,31 +2040,31 @@ Zotero.Integration.Session.prototype.lookupItems = Zotero.Promise.coroutine(func
|
||||||
/**
|
/**
|
||||||
* Unserializes a JSON citation into a citation object (sans items)
|
* Unserializes a JSON citation into a citation object (sans items)
|
||||||
*/
|
*/
|
||||||
Zotero.Integration.Session.prototype.unserializeCitation = function(arg, index) {
|
Zotero.Integration.Session.prototype.unserializeCitation = function(code, index) {
|
||||||
var firstBracket = arg.indexOf("{");
|
var firstBracket = code.indexOf("{");
|
||||||
if(firstBracket !== -1) { // JSON field
|
if (firstBracket !== -1) { // JSON field
|
||||||
arg = arg.substr(firstBracket);
|
code = code.substr(firstBracket);
|
||||||
|
|
||||||
// fix for corrupted fields
|
// fix for corrupted fields
|
||||||
var lastBracket = arg.lastIndexOf("}");
|
var lastBracket = code.lastIndexOf("}");
|
||||||
if(lastBracket+1 != arg.length) {
|
if(lastBracket+1 != code.length) {
|
||||||
if(index) this.updateIndices[index] = true;
|
if(index) this.updateIndices[index] = true;
|
||||||
arg = arg.substr(0, lastBracket+1);
|
code = code.substr(0, lastBracket+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// get JSON
|
// get JSON
|
||||||
try {
|
try {
|
||||||
var citation = JSON.parse(arg);
|
var citation = JSON.parse(code);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
// fix for corrupted fields (corrupted by Word, somehow)
|
// fix for corrupted fields (corrupted by Word, somehow)
|
||||||
try {
|
try {
|
||||||
var citation = JSON.parse(arg.substr(0, arg.length-1));
|
var citation = JSON.parse(code.substr(0, code.length-1));
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
// another fix for corrupted fields (corrupted by 2.1b1)
|
// another fix for corrupted fields (corrupted by 2.1b1)
|
||||||
try {
|
try {
|
||||||
var citation = JSON.parse(arg.replace(/{{((?:\s*,?"unsorted":(?:true|false)|\s*,?"custom":"(?:(?:\\")?[^"]*\s*)*")*)}}/, "{$1}"));
|
var citation = JSON.parse(code.replace(/{{((?:\s*,?"unsorted":(?:true|false)|\s*,?"custom":"(?:(?:\\")?[^"]*\s*)*")*)}}/, "{$1}"));
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
throw new Zotero.Integration.CorruptFieldException(arg, e);
|
throw new Zotero.Integration.CorruptFieldException(code, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2180,14 +2130,14 @@ Zotero.Integration.Session.prototype.unserializeCitation = function(arg, index)
|
||||||
}
|
}
|
||||||
if(!citation.citationID) citation.citationID = Zotero.randomString();
|
if(!citation.citationID) citation.citationID = Zotero.randomString();
|
||||||
|
|
||||||
citation.properties.field = arg;
|
citation.properties.field = code;
|
||||||
} else { // ye olde style field
|
} else { // ye olde style field
|
||||||
var underscoreIndex = arg.indexOf("_");
|
var underscoreIndex = code.indexOf("_");
|
||||||
var itemIDs = arg.substr(0, underscoreIndex).split("|");
|
var itemIDs = code.substr(0, underscoreIndex).split("|");
|
||||||
|
|
||||||
var lastIndex = arg.lastIndexOf("_");
|
var lastIndex = code.lastIndexOf("_");
|
||||||
if(lastIndex != underscoreIndex+1) {
|
if(lastIndex != underscoreIndex+1) {
|
||||||
var locatorString = arg.substr(underscoreIndex+1, lastIndex-underscoreIndex-1);
|
var locatorString = code.substr(underscoreIndex+1, lastIndex-underscoreIndex-1);
|
||||||
var locators = locatorString.split("|");
|
var locators = locatorString.split("|");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2871,3 +2821,121 @@ Zotero.Integration.URIMap.prototype.getZoteroItemForURIs = Zotero.Promise.corout
|
||||||
|
|
||||||
return [zoteroItem, needUpdate];
|
return [zoteroItem, needUpdate];
|
||||||
});
|
});
|
||||||
|
Zotero.Integration.Field = function(field) {
|
||||||
|
this.dirty = false;
|
||||||
|
this._field = field;
|
||||||
|
this.type = INTEGRATION_TYPE_TEMP;
|
||||||
|
|
||||||
|
return new Proxy(this, Zotero.Integration.Field.proxyHandler);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Proxy properties through to the integration field
|
||||||
|
Zotero.Integration.Field.proxyHandler = {
|
||||||
|
get: function(target, name) {
|
||||||
|
if (name in target) {
|
||||||
|
return target[name];
|
||||||
|
}
|
||||||
|
return target._field[name];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load existing field in document and return correct instance of field type
|
||||||
|
* @param docField
|
||||||
|
* @param rawCode
|
||||||
|
* @param idx
|
||||||
|
* @returns {Zotero.Integration.Field|Zotero.Integration.CitationField|Zotero.Integration.BibliographyField}
|
||||||
|
*/
|
||||||
|
Zotero.Integration.Field.loadExisting = function(docField) {
|
||||||
|
var field;
|
||||||
|
let rawCode = docField.getCode();
|
||||||
|
|
||||||
|
// ITEM/CITATION CSL_ITEM {json: 'data'}
|
||||||
|
for (let type of ["ITEM", "CITATION"]) {
|
||||||
|
if (rawCode.substr(0, type.length) === type) {
|
||||||
|
field = new Zotero.Integration.CitationField(docField);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// BIBL {json: 'data'} CSL_BIBLIOGRAPHY
|
||||||
|
if (rawCode.substr(0, 4) === "BIBL") {
|
||||||
|
field = new Zotero.Integration.BibliographyField(docField);
|
||||||
|
}
|
||||||
|
if (field) {
|
||||||
|
let start = rawCode.indexOf('{');
|
||||||
|
if (start != -1) {
|
||||||
|
field._code = rawCode.substr(start, start + rawCode.lastIndexOf('}'));
|
||||||
|
} else {
|
||||||
|
field._code = rawCode.substr(rawCode.indexOf(' ')+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rawCode.substr(0, 4) === "TEMP") {
|
||||||
|
field = new Zotero.Integration.Field(docField);
|
||||||
|
field._code = rawCode.substr(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
return field;
|
||||||
|
};
|
||||||
|
|
||||||
|
Zotero.defineProperty(Zotero.Integration.Field.prototype, 'text', {
|
||||||
|
get: () => 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);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user