commit f95832d4a950b4c2dc6de9757256f5022ae05860
parent 22026e5cfb3ab4671a886b991b86277f97dd72b2
Author: Simon Kornblith <simon@simonster.com>
Date: Sat, 5 Mar 2016 19:29:24 -0500
Make citing work via right-click
Drag and drop and WP integration are still TODO
Diffstat:
3 files changed, 72 insertions(+), 51 deletions(-)
diff --git a/chrome/content/zotero/fileInterface.js b/chrome/content/zotero/fileInterface.js
@@ -126,7 +126,6 @@ var Zotero_File_Interface = new function() {
this.exportItems = exportItems;
this.bibliographyFromCollection = bibliographyFromCollection;
this.bibliographyFromItems = bibliographyFromItems;
- this.copyItemsToClipboard = copyItemsToClipboard;
this.copyCitationToClipboard = copyCitationToClipboard;
/**
@@ -408,7 +407,7 @@ var Zotero_File_Interface = new function() {
*
* Does not check that items are actual references (and not notes or attachments)
*/
- function copyItemsToClipboard(items, style, locale, asHTML, asCitations) {
+ this.copyItemsToClipboard = Zotero.Promise.coroutine(function* (items, style, locale, asHTML, asCitations) {
// copy to clipboard
var transferable = Components.classes["@mozilla.org/widget/transferable;1"].
createInstance(Components.interfaces.nsITransferable);
@@ -418,7 +417,7 @@ var Zotero_File_Interface = new function() {
var cslEngine = style.getCiteProc(locale);
// add HTML
- var bibliography = Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, items, "html", asCitations);
+ var bibliography = yield Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, items, "html", asCitations);
var str = Components.classes["@mozilla.org/supports-string;1"].
createInstance(Components.interfaces.nsISupportsString);
str.data = bibliography;
@@ -428,7 +427,7 @@ var Zotero_File_Interface = new function() {
// add text (or HTML source)
if(!asHTML) {
cslEngine = style.getCiteProc(locale);
- var bibliography = Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, items, "text", asCitations);
+ var bibliography = yield Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, items, "text", asCitations);
}
var str = Components.classes["@mozilla.org/supports-string;1"].
createInstance(Components.interfaces.nsISupportsString);
@@ -437,7 +436,7 @@ var Zotero_File_Interface = new function() {
transferable.setTransferData("text/unicode", str, bibliography.length*2);
clipboardService.setData(transferable, null, Components.interfaces.nsIClipboard.kGlobalClipboard);
- }
+ });
/*
@@ -484,7 +483,7 @@ var Zotero_File_Interface = new function() {
/*
* Shows bibliography options and creates a bibliography
*/
- function _doBibliographyOptions(name, items) {
+ let _doBibliographyOptions = Zotero.Promise.coroutine(function* (name, items) {
// make sure at least one item is not a standalone note or attachment
var haveRegularItem = false;
for each(var item in items) {
@@ -516,12 +515,12 @@ var Zotero_File_Interface = new function() {
// generate bibliography
try {
if(io.method == 'copy-to-clipboard') {
- Zotero_File_Interface.copyItemsToClipboard(items, io.style, locale, false, io.mode === "citations");
+ yield Zotero_File_Interface.copyItemsToClipboard(items, io.style, locale, false, io.mode === "citations");
}
else {
var style = Zotero.Styles.get(io.style);
var cslEngine = style.getCiteProc(locale);
- var bibliography = Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine,
+ var bibliography = yield Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine,
items, format, io.mode === "citations");
}
} catch(e) {
@@ -599,7 +598,7 @@ var Zotero_File_Interface = new function() {
fStream.close();
}
}
- }
+ });
function _saveBibliography(name, format) {
diff --git a/chrome/content/zotero/xpcom/cite.js b/chrome/content/zotero/xpcom/cite.js
@@ -71,9 +71,9 @@ Zotero.Cite = {
* @param {String} format The format of the output (html, text, or rtf)
* @return {String} Bibliography or item list in specified format
*/
- "makeFormattedBibliographyOrCitationList":function(cslEngine, items, format, asCitationList) {
+ "makeFormattedBibliographyOrCitationList":Zotero.Promise.coroutine(function* (cslEngine, items, format, asCitationList) {
cslEngine.setOutputFormat(format);
- cslEngine.updateItems(items.map(item => item.id));
+ yield cslEngine.updateItems(items.map(item => item.id));
if(!asCitationList) {
var bibliography = Zotero.Cite.makeFormattedBibliography(cslEngine, format);
@@ -84,7 +84,7 @@ Zotero.Cite = {
var citations=[];
for (var i=0, ilen=items.length; i<ilen; i++) {
var item = items[i];
- var outList = cslEngine.appendCitationCluster({"citationItems":[{"id":item.id}], "properties":{}}, true);
+ var outList = yield cslEngine.appendCitationCluster({"citationItems":[{"id":item.id}], "properties":{}}, true);
for (var j=0, jlen=outList.length; j<jlen; j++) {
var citationPos = outList[j][0];
citations[citationPos] = outList[j][1];
@@ -124,7 +124,7 @@ Zotero.Cite = {
return "<\\rtf \n"+citations.join("\\\n")+"\n}";
}
}
- },
+ }),
/**
* Makes a formatted bibliography
@@ -492,21 +492,54 @@ Zotero.Cite.System = function(automaticJournalAbbreviations) {
if(automaticJournalAbbreviations) {
this.getAbbreviation = Zotero.Cite.getAbbreviation;
}
+ this.items = {};
}
Zotero.Cite.System.prototype = {
/**
+ * Asynchronously fetch item and convert to CSL JSON
+ */
+ "addItem":Zotero.Promise.coroutine(function* (zoteroItem) {
+ if (typeof(zoteroItem) != "object") {
+ if (this.items.hasOwnProperty(zoteroItem)) return;
+ zoteroItem = yield Zotero.Items.getAsync(zoteroItem);
+ }
+ if (this.items.hasOwnProperty(zoteroItem.id)) return;
+ let item = yield Zotero.Utilities.itemToCSLJSON(zoteroItem);
+ item.id = zoteroItem.id;
+
+ if (!Zotero.Prefs.get("export.citePaperJournalArticleURL")) {
+ var itemType = Zotero.ItemTypes.getName(zoteroItem.itemTypeID);
+ // don't return URL or accessed information for journal articles if a
+ // pages field exists
+ if (["journalArticle", "newspaperArticle", "magazineArticle"].indexOf(itemType) !== -1
+ && item.pages
+ ) {
+ delete item.URL;
+ delete item.accessed;
+ }
+ }
+ this.items[item.id] = item;
+ }),
+
+ /**
+ * Asynchronously fetch items and convert them to CSL JSON
+ */
+ "addItems":Zotero.Promise.coroutine(function* (items) {
+ for (let item of items) {
+ yield this.addItem(item);
+ }
+ }),
+
+ /**
* citeproc-js system function for getting items
* See http://gsl-nagoya-u.net/http/pub/citeproc-doc.html#retrieveitem
* @param {String|Integer} Item ID, or string item for embedded citations
* @return {Object} citeproc-js item
*/
"retrieveItem":function retrieveItem(item) {
- var zoteroItem, slashIndex;
- if(typeof item === "object" && item !== null && item instanceof Zotero.Item) {
- //if(this._cache[item.id]) return this._cache[item.id];
- zoteroItem = item;
- } else if(typeof item === "string" && (slashIndex = item.indexOf("/")) !== -1) {
+ let slashIndex;
+ if(typeof item === "string" && (slashIndex = item.indexOf("/")) !== -1) {
// is an embedded item
var sessionID = item.substr(0, slashIndex);
var session = Zotero.Integration.sessions[sessionID]
@@ -517,37 +550,8 @@ Zotero.Cite.System.prototype = {
return embeddedCitation;
}
}
- } else {
- // is an item ID
- //if(this._cache[item]) return this._cache[item];
- try {
- zoteroItem = Zotero.Items.get(item);
- } catch(e) {}
}
-
- if(!zoteroItem) {
- throw "Zotero.Cite.System.retrieveItem called on non-item "+item;
- }
-
- throw new Error("Unimplemented");
- var cslItem = Zotero.Utilities.itemToCSLJSON(zoteroItem);
-
- // TEMP: citeproc-js currently expects the id property to be the item DB id
- cslItem.id = zoteroItem.id;
-
- if (!Zotero.Prefs.get("export.citePaperJournalArticleURL")) {
- var itemType = Zotero.ItemTypes.getName(zoteroItem.itemTypeID);
- // don't return URL or accessed information for journal articles if a
- // pages field exists
- if (["journalArticle", "newspaperArticle", "magazineArticle"].indexOf(itemType) !== -1
- && zoteroItem.getField("pages")
- ) {
- delete cslItem.URL;
- delete cslItem.accessed;
- }
- }
-
- return cslItem;
+ return this.items[item];
},
/**
@@ -575,3 +579,20 @@ Zotero.Cite.System.prototype = {
return str.value;
}
};
+
+Zotero.Cite.AsyncCiteProc = function() {
+ Zotero.CiteProc.CSL.Engine.apply(this, arguments);
+}
+Zotero.Cite.AsyncCiteProc.prototype = Object.create(Zotero.CiteProc.CSL.Engine.prototype);
+Zotero.Cite.AsyncCiteProc.prototype.updateItems = Zotero.Promise.coroutine(function*(items) {
+ yield this.sys.addItems(items);
+ Zotero.CiteProc.CSL.Engine.prototype.updateItems.call(this, items);
+});
+Zotero.Cite.AsyncCiteProc.prototype.appendCitationCluster = Zotero.Promise.coroutine(function*(citation, isRegistered) {
+ if (!isRegistered) {
+ for (let citationItem of citation.citationItems) {
+ yield this.sys.addItem(citationItem.id);
+ }
+ }
+ return Zotero.CiteProc.CSL.Engine.prototype.appendCitationCluster.call(this, citation, isRegistered);
+});
diff --git a/chrome/content/zotero/xpcom/style.js b/chrome/content/zotero/xpcom/style.js
@@ -437,7 +437,7 @@ Zotero.Styles = new function() {
yield Zotero.File.putContentsAsync(destFile, style);
yield Zotero.Styles.reinit();
-
+
// Refresh preferences windows
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
getService(Components.interfaces.nsIWindowMediator);
@@ -691,7 +691,7 @@ Zotero.Style.prototype.getCiteProc = function(locale, automaticJournalAbbreviati
}
try {
- var citeproc = new Zotero.CiteProc.CSL.Engine(
+ var citeproc = new Zotero.Cite.AsyncCiteProc(
new Zotero.Cite.System(automaticJournalAbbreviations),
xml,
locale,
@@ -832,4 +832,4 @@ Zotero.Style.prototype.remove = Zotero.Promise.coroutine(function* () {
}
return Zotero.Styles.reinit();
-});
+});
+\ No newline at end of file