2006-10-04 17:16:56 +00:00
|
|
|
/*
|
|
|
|
***** BEGIN LICENSE BLOCK *****
|
2009-08-20 05:02:55 +00:00
|
|
|
|
2009-08-20 08:25:01 +00:00
|
|
|
Copyright (c) 2009 Zotero
|
|
|
|
Center for History and New Media
|
2009-08-20 05:02:55 +00:00
|
|
|
George Mason University, Fairfax, Virginia, USA
|
2009-08-20 08:25:01 +00:00
|
|
|
http://zotero.org
|
2009-08-20 05:02:55 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-10-04 17:16:56 +00:00
|
|
|
|
|
|
|
***** END LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
const RESELECT_KEY_URI = 1;
|
|
|
|
const RESELECT_KEY_ITEM_KEY = 2;
|
|
|
|
const RESELECT_KEY_ITEM_ID = 3;
|
2007-10-23 07:11:59 +00:00
|
|
|
|
2006-10-02 23:15:27 +00:00
|
|
|
Zotero.Integration = new function() {
|
2009-08-20 05:02:55 +00:00
|
|
|
var _fifoFile, _osascriptFile;
|
Merged revisions 2710-2712,2714-2716,2718-2728,2730-2731,2734,2736-2738,2740-2750,2752-2753,2755,2758-2768,2770-2779,2782,2789-2790,2794,2797-2802,2804,2808-2810,2812,2814-2824,2826-2832,2834-2835 via svnmerge from 1.0 branch
2008-06-11 08:55:59 +00:00
|
|
|
|
2008-07-10 11:05:43 +00:00
|
|
|
this.sessions = {};
|
|
|
|
|
2008-11-30 20:18:48 +00:00
|
|
|
this.__defineGetter__("usePopup", function () {
|
2008-12-29 07:57:20 +00:00
|
|
|
return Zotero.isWin && !Zotero.Prefs.get("integration.realWindow");
|
2008-11-30 20:18:48 +00:00
|
|
|
});
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Initializes the pipe used for integration on non-Windows platforms.
|
2006-08-28 05:38:37 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
this.init = function() {
|
|
|
|
if(!Zotero.isWin) {
|
|
|
|
// create a new file representing the pipe
|
|
|
|
_fifoFile = Components.classes["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Components.interfaces.nsIProperties).
|
|
|
|
get("Home", Components.interfaces.nsIFile);
|
|
|
|
_fifoFile.append(".zoteroIntegrationPipe");
|
2006-10-03 21:05:48 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
// destroy old pipe, if one exists
|
|
|
|
if(_fifoFile.exists()) _fifoFile.remove(false);
|
|
|
|
|
|
|
|
// make a new pipe
|
|
|
|
var mkfifo = Components.classes["@mozilla.org/file/local;1"].
|
|
|
|
createInstance(Components.interfaces.nsILocalFile);
|
|
|
|
mkfifo.initWithPath("/usr/bin/mkfifo");
|
|
|
|
if(!mkfifo.exists()) mkfifo.initWithPath("/bin/mkfifo");
|
|
|
|
if(!mkfifo.exists()) mkfifo.initWithPath("/usr/local/bin/mkfifo");
|
|
|
|
|
|
|
|
if(mkfifo.exists()) {
|
|
|
|
var main = Components.classes["@mozilla.org/thread-manager;1"].getService().mainThread;
|
|
|
|
var background = Components.classes["@mozilla.org/thread-manager;1"].getService().newThread(0);
|
2006-08-28 05:38:37 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
var me = this;
|
|
|
|
function mainThread(agent, cmd) {
|
|
|
|
this.agent = agent;
|
|
|
|
this.cmd = cmd;
|
|
|
|
}
|
|
|
|
mainThread.prototype.run = function() {
|
|
|
|
me.execCommand(this.agent, this.cmd);
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
function fifoThread() {}
|
|
|
|
fifoThread.prototype.run = function() {
|
|
|
|
var proc = Components.classes["@mozilla.org/process/util;1"].
|
|
|
|
createInstance(Components.interfaces.nsIProcess);
|
|
|
|
proc.init(mkfifo);
|
|
|
|
proc.run(true, [_fifoFile.path], 1);
|
|
|
|
|
|
|
|
if(!_fifoFile.exists()) Zotero.debug("Could not initialize Zotero integration pipe");
|
|
|
|
|
|
|
|
var fifoStream = Components.classes["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Components.interfaces.nsIFileInputStream);
|
|
|
|
var line = {};
|
|
|
|
while(true) {
|
|
|
|
fifoStream.QueryInterface(Components.interfaces.nsIFileInputStream);
|
|
|
|
fifoStream.init(_fifoFile, -1, 0, 0);
|
|
|
|
fifoStream.QueryInterface(Components.interfaces.nsILineInputStream);
|
|
|
|
fifoStream.readLine(line);
|
|
|
|
fifoStream.close();
|
|
|
|
|
|
|
|
var spaceIndex = line.value.indexOf(" ");
|
|
|
|
var agent = line.value.substr(0, spaceIndex);
|
|
|
|
var cmd = line.value.substr(spaceIndex+1);
|
|
|
|
if(agent == "Zotero" && cmd == "shutdown") return;
|
|
|
|
main.dispatch(new mainThread(agent, cmd), background.DISPATCH_NORMAL);
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
fifoThread.prototype.QueryInterface = mainThread.prototype.QueryInterface = function(iid) {
|
|
|
|
if (iid.equals(Components.interfaces.nsIRunnable) ||
|
|
|
|
iid.equals(Components.interfaces.nsISupports)) return this;
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
2006-09-04 04:13:12 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
background.dispatch(new fifoThread(), background.DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
var observerService = Components.classes["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIObserverService);
|
|
|
|
observerService.addObserver({
|
|
|
|
observe: me.destroy
|
|
|
|
}, "quit-application", false);
|
2006-08-28 05:38:37 +00:00
|
|
|
} else {
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.debug("mkfifo not found -- not initializing integration pipe");
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
// initialize SOAP server just to throw version errors
|
|
|
|
Zotero.Integration.Compat.init();
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
Merged revisions 2710-2712,2714-2716,2718-2728,2730-2731,2734,2736-2738,2740-2750,2752-2753,2755,2758-2768,2770-2779,2782,2789-2790,2794,2797-2802,2804,2808-2810,2812,2814-2824,2826-2832,2834-2835 via svnmerge from 1.0 branch
2008-06-11 08:55:59 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Executes an integration command.
|
|
|
|
*/
|
|
|
|
this.execCommand = function execCommand(agent, command) {
|
|
|
|
var componentClass = "@zotero.org/Zotero/integration/application?agent="+agent+";1";
|
|
|
|
Zotero.debug("Integration: Instantiating "+componentClass+" for command "+command);
|
|
|
|
var application = Components.classes[componentClass]
|
|
|
|
.getService(Components.interfaces.zoteroIntegrationApplication);
|
|
|
|
var integration = new Zotero.Integration.Document(application);
|
|
|
|
try {
|
|
|
|
integration[command]();
|
|
|
|
} catch(e) {
|
2009-08-21 08:41:07 +00:00
|
|
|
if(!(e instanceof Zotero.Integration.UserCancelledException)) {
|
|
|
|
integration._doc.displayAlert(Zotero.getString("integration.error.generic"),
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_STOP,
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK);
|
|
|
|
throw e;
|
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
} finally {
|
|
|
|
integration.cleanup();
|
Merged revisions 2710-2712,2714-2716,2718-2728,2730-2731,2734,2736-2738,2740-2750,2752-2753,2755,2758-2768,2770-2779,2782,2789-2790,2794,2797-2802,2804,2808-2810,2812,2814-2824,2826-2832,2834-2835 via svnmerge from 1.0 branch
2008-06-11 08:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-28 05:38:37 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Destroys the integration pipe.
|
2006-08-28 05:38:37 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
this.destroy = function() {
|
|
|
|
// send shutdown message to fifo thread
|
|
|
|
var oStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
|
|
|
|
getService(Components.interfaces.nsIFileOutputStream);
|
|
|
|
oStream.init(_fifoFile, 0x02 | 0x10, 0, 0);
|
|
|
|
var cmd = "Zotero shutdown\n";
|
|
|
|
oStream.write(cmd, cmd.length);
|
|
|
|
oStream.close();
|
|
|
|
_fifoFile.remove(false);
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
Merged revisions 2710-2712,2714-2716,2718-2728,2730-2731,2734,2736-2738,2740-2750,2752-2753,2755,2758-2768,2770-2779,2782,2789-2790,2794,2797-2802,2804,2808-2810,2812,2814-2824,2826-2832,2834-2835 via svnmerge from 1.0 branch
2008-06-11 08:55:59 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Activates Firefox
|
|
|
|
*/
|
|
|
|
this.activate = function() {
|
|
|
|
if(Zotero.isMac) {
|
|
|
|
if(_osascriptFile === undefined) {
|
|
|
|
_osascriptFile = Components.classes["@mozilla.org/file/local;1"].
|
|
|
|
createInstance(Components.interfaces.nsILocalFile);
|
|
|
|
_osascriptFile.initWithPath("/usr/bin/osascript");
|
|
|
|
if(!_osascriptFile.exists()) _osascriptFile = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_osascriptFile) {
|
|
|
|
var proc = Components.classes["@mozilla.org/process/util;1"].
|
|
|
|
createInstance(Components.interfaces.nsIProcess);
|
|
|
|
proc.init(_osascriptFile);
|
2009-08-20 08:25:01 +00:00
|
|
|
proc.run(false, ['-e', 'tell application id "org.mozilla.firefox" to activate'], 2);
|
2009-08-20 05:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
Merged revisions 2710-2712,2714-2716,2718-2728,2730-2731,2734,2736-2738,2740-2750,2752-2753,2755,2758-2768,2770-2779,2782,2789-2790,2794,2797-2802,2804,2808-2810,2812,2814-2824,2826-2832,2834-2835 via svnmerge from 1.0 branch
2008-06-11 08:55:59 +00:00
|
|
|
}
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* An exception thrown when a document contains an item that no longer exists in the current document.
|
|
|
|
*
|
|
|
|
* @param reselectKeys {Array} Keys representing the missing item
|
|
|
|
* @param reselectKeyType {Integer} The type of the keys (see RESELECT_KEY_* constants)
|
|
|
|
* @param citationIndex {Integer} The index of the missing item within the citation cluster
|
|
|
|
* @param citationLength {Integer} The number of items cited in this citation cluster
|
2006-08-28 05:38:37 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.Integration.MissingItemException = function(reselectKeys, reselectKeyType, citationIndex, citationLength) {
|
|
|
|
this.reselectKeys = reselectKeys;
|
|
|
|
this.reselectKeyType = reselectKeyType;
|
|
|
|
this.citationIndex = citationIndex;
|
|
|
|
this.citationLength = citationLength;
|
|
|
|
}
|
|
|
|
Zotero.Integration.MissingItemException.prototype.name = "MissingItemException";
|
|
|
|
Zotero.Integration.MissingItemException.prototype.message = "An item in this document is missing from your Zotero library.";
|
2009-08-21 08:41:07 +00:00
|
|
|
Zotero.Integration.MissingItemException.prototype.toString = function() { return this.name; };
|
|
|
|
|
|
|
|
Zotero.Integration.UserCancelledException = function() {};
|
|
|
|
Zotero.Integration.UserCancelledException.prototype.name = "UserCancelledException";
|
|
|
|
Zotero.Integration.UserCancelledException.prototype.message = "User cancelled document update.";
|
|
|
|
Zotero.Integration.UserCancelledException.prototype.toString = function() { return this.name; };
|
2006-08-28 05:38:37 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
|
|
|
|
// Field code for an item
|
2009-08-21 08:41:07 +00:00
|
|
|
const ITEM_CODE = "ITEM";
|
2009-08-20 05:02:55 +00:00
|
|
|
// Field code for a bibliography
|
2009-08-21 08:41:07 +00:00
|
|
|
const BIBLIOGRAPHY_CODE = "BIBL";
|
2009-08-20 05:02:55 +00:00
|
|
|
// Placeholder for an empty bibliography
|
2009-08-21 08:41:07 +00:00
|
|
|
const BIBLIOGRAPHY_PLACEHOLDER = "{Bibliography}";
|
2009-08-20 05:02:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2006-08-28 05:38:37 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.Integration.Document = function(app) {
|
|
|
|
this._app = app;
|
|
|
|
this._doc = app.getActiveDocument();
|
|
|
|
}
|
2006-08-28 05:38:37 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Creates a new session
|
|
|
|
* @param data {Zotero.Integration.DocumentData} Document data for new session
|
2006-08-28 05:38:37 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.Integration.Document.prototype._createNewSession = function(data) {
|
|
|
|
data.sessionID = Zotero.randomString();
|
|
|
|
var session = Zotero.Integration.sessions[data.sessionID] = new Zotero.Integration.Session();
|
|
|
|
session.setData(data);
|
|
|
|
return session;
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Gets preferences for a document
|
|
|
|
* @param require {Boolean} Whether an error should be thrown if no preferences exist (otherwise,
|
|
|
|
* the set doc prefs dialog is shown)
|
|
|
|
* @param dontRunSetDocPrefs {Boolean} Whether to show the Set Document Preferences window if no
|
|
|
|
* preferences exist
|
2006-08-28 05:38:37 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.Integration.Document.prototype._getSession = function(require, dontRunSetDocPrefs) {
|
|
|
|
var dataString = this._doc.getDocumentData();
|
|
|
|
if(!dataString) {
|
|
|
|
if(require) {
|
|
|
|
this._doc.displayAlert(Zotero.getString("integration.error.mustInsertCitation"),
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_STOP,
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK);
|
|
|
|
} else {
|
|
|
|
// Set doc prefs if no data string yet
|
|
|
|
this._session = this._createNewSession(new Zotero.Integration.DocumentData());
|
|
|
|
if(dontRunSetDocPrefs) return false;
|
2006-08-28 05:38:37 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
var ret = this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType);
|
|
|
|
if(!ret) return false;
|
|
|
|
// save doc prefs in doc
|
|
|
|
this._doc.setDocumentData(this._session.data.serializeXML());
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
} else {
|
2009-08-21 08:41:07 +00:00
|
|
|
if(dataString[0] != "<") {
|
|
|
|
var warning = this._doc.displayAlert(Zotero.getString("integration.upgradeWarning"),
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_WARNING,
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL);
|
|
|
|
if(!warning) throw new Zotero.Integration.UserCancelledException();
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
var data = new Zotero.Integration.DocumentData(dataString);
|
|
|
|
if(Zotero.Integration.sessions[data.sessionID]) {
|
|
|
|
this._session = Zotero.Integration.sessions[data.sessionID];
|
|
|
|
} else {
|
|
|
|
this._session = this._createNewSession(data);
|
2006-08-28 05:38:37 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
// make sure style is defined
|
|
|
|
if(!this._session.style) {
|
|
|
|
this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType);
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
this._doc.setDocumentData(this._session.data.serializeXML());
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
|
|
|
|
this._session.resetRequest();
|
|
|
|
return true;
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Gets all fields for a document
|
|
|
|
* @param require {Boolean} Whether an error should be thrown if no fields exist
|
2006-08-28 05:38:37 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.Integration.Document.prototype._getFields = function(require, onlyCheck) {
|
|
|
|
if(this._fields) return true;
|
|
|
|
if(!this._session && !this._getSession(require, true)) return false;
|
|
|
|
|
|
|
|
var fields = this._doc.getFields(this._session.data.prefs['fieldType']);
|
|
|
|
this._fields = [];
|
|
|
|
while(fields.hasMoreElements()) {
|
|
|
|
this._fields.push(fields.getNext().QueryInterface(Components.interfaces.zoteroIntegrationField));
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
|
|
|
|
if(require && !this._fields.length) {
|
|
|
|
this._doc.displayAlert(Zotero.getString("integration.error.mustInsertCitation"),
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_STOP,
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK);
|
|
|
|
return false;
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
|
|
|
|
return true;
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Checks that it is appropriate to add fields to the current document at the current
|
|
|
|
* positon, then adds one.
|
2006-08-28 05:38:37 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.Integration.Document.prototype._addField = function(note) {
|
|
|
|
// Get citation types if necessary
|
|
|
|
if(!this._doc.canInsertField(this._session.data.prefs['fieldType'])) {
|
|
|
|
this._doc.displayAlert(Zotero.getString("integration.error.cannotInsertHere"),
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_STOP,
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK)
|
|
|
|
return false;
|
|
|
|
}
|
2006-08-28 05:38:37 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
var field = this._doc.cursorInField(this._session.data.prefs['fieldType']);
|
|
|
|
if(field) {
|
|
|
|
if(!this._doc.displayAlert(Zotero.getString("integration.replace"),
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_STOP,
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL)) return false;
|
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
if(!field) {
|
|
|
|
var field = this._doc.insertField(this._session.data.prefs['fieldType'],
|
|
|
|
(note ? this._session.data.prefs["noteType"] : 0));
|
2006-09-06 03:49:41 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
|
|
|
|
return field;
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Loads existing citations and bibliographies out of a document, and creates or edits fields
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.prototype._updateSession = function(editField) {
|
|
|
|
var deleteKeys = {};
|
|
|
|
this._deleteFields = [];
|
|
|
|
this._removeCodeFields = [];
|
|
|
|
this._bibliographyFields = [];
|
|
|
|
var bibliographyData = "";
|
|
|
|
|
|
|
|
// first collect entire bibliography
|
|
|
|
this._getFields();
|
|
|
|
var editFieldIndex = false;
|
|
|
|
for(var i in this._fields) {
|
|
|
|
var field = this._fields[i];
|
|
|
|
|
|
|
|
if(editField && field.equals(editField)) {
|
|
|
|
editFieldIndex = i;
|
2008-09-23 23:11:27 +00:00
|
|
|
} else {
|
2009-08-20 05:02:55 +00:00
|
|
|
var fieldCode = field.getCode();
|
|
|
|
|
|
|
|
if(fieldCode.substr(0, ITEM_CODE.length) == ITEM_CODE) {
|
|
|
|
try {
|
|
|
|
this._session.addCitation(i, fieldCode.substr(ITEM_CODE.length+1));
|
|
|
|
} catch(e) {
|
|
|
|
if(e instanceof Zotero.Integration.MissingItemException) {
|
|
|
|
// First, check if we've already decided to remove field codes from these
|
|
|
|
var reselect = true;
|
|
|
|
for each(var reselectKey in e.reselectKeys) {
|
|
|
|
if(deleteKeys[reselectKey]) {
|
|
|
|
this._removeCodeFields.push(i);
|
|
|
|
reselect = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(reselect) {
|
|
|
|
// Ask user what to do with this item
|
|
|
|
if(e.citationLength == 1) {
|
|
|
|
var msg = Zotero.getString("integration.missingItem.single");
|
|
|
|
} else {
|
|
|
|
var msg = Zotero.getString("integration.missingItem.multiple", e.citationIndex.toString());
|
|
|
|
}
|
|
|
|
msg += '\n\n'+Zotero.getString('integration.missingItem.description');
|
|
|
|
field.select();
|
|
|
|
var result = this._doc.displayAlert(msg, 1, 3);
|
|
|
|
if(result == 0) { // Cancel
|
2009-08-21 08:41:07 +00:00
|
|
|
throw new Zotero.Integration.UserCancelledException();
|
2009-08-20 05:02:55 +00:00
|
|
|
} else if(result == 1) { // No
|
|
|
|
for each(var reselectKey in e.reselectKeys) {
|
|
|
|
deleteKeys[reselectKey] = true;
|
|
|
|
}
|
|
|
|
this._removeCodeFields.push(i);
|
|
|
|
} else { // Yes
|
|
|
|
// Display reselect item dialog
|
|
|
|
Zotero.Integration.activate();
|
|
|
|
this._session.reselectItem(e);
|
|
|
|
// Now try again
|
|
|
|
this._session.addCitation(i, fieldCode.substr(ITEM_CODE.length+1));
|
|
|
|
this._doc.activate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(fieldCode.substr(0, BIBLIOGRAPHY_CODE.length) == BIBLIOGRAPHY_CODE) {
|
|
|
|
this._bibliographyFields.push(field);
|
|
|
|
if(!this._session.bibliographyData && !bibliographyData) {
|
|
|
|
bibliographyData = field.getCode().substr(BIBLIOGRAPHY_CODE.length+1);
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// load uncited items from bibliography
|
|
|
|
if(bibliographyData && !this._session.bibliographyData) {
|
|
|
|
this._session.loadBibliographyData(bibliographyData);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._session.updateItemSet();
|
|
|
|
|
|
|
|
// create new citation or edit existing citation
|
|
|
|
if(editFieldIndex) {
|
|
|
|
this._session.updateCitations(editFieldIndex-1);
|
|
|
|
var editFieldCode = editField.getCode().substr(ITEM_CODE.length+1);
|
|
|
|
var editCitation = editFieldCode ? this._session.unserializeCitation(editFieldCode, editFieldIndex) : null;
|
2008-07-10 11:05:43 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.Integration.activate();
|
|
|
|
var added = this._session.editCitation(editFieldIndex, editCitation);
|
|
|
|
this._doc.activate();
|
2008-07-10 11:05:43 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
if(!added) {
|
|
|
|
if(editFieldCode) { // cancelled editing; just add as if nothing happened
|
|
|
|
this._session.addCitation(editFieldIndex, editCitation);
|
|
|
|
} else { // cancelled creation; delete the citation
|
|
|
|
this._session.deleteCitation(editFieldIndex);
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-08-20 05:02:55 +00:00
|
|
|
* Updates bibliographies and fields within a document
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.prototype._updateDocument = function(forceCitations, forceBibliography) {
|
|
|
|
// update bibliographies
|
|
|
|
var output = new Array();
|
|
|
|
if(this._bibliographyFields.length // if blbliography exists
|
|
|
|
&& (this._session.bibliographyHasChanged // and bibliography changed
|
|
|
|
|| forceBibliography)) { // or if we should generate regardless of changes
|
|
|
|
if(this._session.bibliographyDataHasChanged) {
|
|
|
|
var bibliographyData = this._session.getBibliographyData();
|
|
|
|
for each(var field in this._bibliographyFields) {
|
|
|
|
field.setCode(BIBLIOGRAPHY_CODE+" "+bibliographyData);
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 11:05:43 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
var bibliographyText = this._session.getBibliography();
|
|
|
|
for each(var field in this._bibliographyFields) {
|
|
|
|
field.setText(bibliographyText, true);
|
|
|
|
}
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
// update citations
|
|
|
|
this._session.updateUpdateIndices(forceCitations);
|
|
|
|
for(var i in this._session.updateIndices) {
|
|
|
|
citation = this._session.citationsByIndex[i];
|
|
|
|
if(!citation) continue;
|
2008-07-10 11:05:43 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
if(citation.properties["delete"]) {
|
|
|
|
// delete citation
|
|
|
|
this._deleteFields.push(i);
|
|
|
|
} else if(!this.haveMissing) {
|
|
|
|
var fieldCode = this._session.getCitationField(citation);
|
|
|
|
if(fieldCode != citation.properties.field) {
|
|
|
|
this._fields[citation.properties.index].setCode(ITEM_CODE+" "+fieldCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(citation.properties.custom) {
|
|
|
|
var citationText = citation.properties.custom;
|
|
|
|
// XML uses real RTF, rather than the format used for
|
|
|
|
// integration, so we have to escape things properly
|
|
|
|
citationText = citationText.replace(/[\x7F-\uFFFF]/g,
|
|
|
|
Zotero.Integration.Session._rtfEscapeFunction).
|
|
|
|
replace("\t", "\\tab ", "g");
|
|
|
|
} else {
|
|
|
|
var citationText = this._session.style.formatCitation(citation, "RTF");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(citationText.indexOf("\\") !== -1) {
|
|
|
|
// need to set text as RTF
|
|
|
|
this._fields[citation.properties.index].setText("{\\rtf "+citationText+"}", true);
|
|
|
|
} else {
|
|
|
|
// set text as plain
|
|
|
|
this._fields[citation.properties.index].setText(citationText, false);
|
|
|
|
}
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
// do this operations in reverse in case plug-ins care about order
|
|
|
|
for(var i=(this._deleteFields.length-1); i>=0; i--) {
|
|
|
|
this._fields[this._deleteFields[i]].delete();
|
|
|
|
}
|
|
|
|
for(var i=(this._removeCodeFields.length-1); i>=0; i--) {
|
|
|
|
this._fields[this._removeCodeFields[i]].removeCode();
|
|
|
|
}
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
|
2008-07-10 11:05:43 +00:00
|
|
|
/**
|
2009-08-20 05:02:55 +00:00
|
|
|
* Adds a citation to the current document.
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.prototype.addCitation = function() {
|
|
|
|
if(!this._getSession()) return;
|
2008-07-10 11:05:43 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
var field = this._addField(true);
|
|
|
|
if(!field) return;
|
2008-07-10 11:05:43 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
this._updateSession(field);
|
|
|
|
this._updateDocument();
|
|
|
|
}
|
2008-07-10 11:05:43 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Edits the citation at the cursor position.
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.prototype.editCitation = function() {
|
|
|
|
if(!this._getSession(true)) return;
|
|
|
|
|
|
|
|
var field = this._doc.cursorInField(this._session.data.prefs['fieldType'])
|
|
|
|
if(!field) {
|
|
|
|
this._doc.displayAlert(Zotero.getString("integration.error.notInCitation"),
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_STOP,
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK);
|
|
|
|
return;
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
|
|
|
|
this._updateSession(field);
|
|
|
|
this._updateDocument(false, false);
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 23:11:27 +00:00
|
|
|
/**
|
2009-08-20 05:02:55 +00:00
|
|
|
* Adds a bibliography to the current document.
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.prototype.addBibliography = function() {
|
|
|
|
if(!this._getSession(true)) return;
|
|
|
|
|
|
|
|
// Make sure we can have a bibliography
|
|
|
|
if(!this._session.style.hasBibliography) {
|
|
|
|
this._doc.displayAlert(Zotero.getString("integration.error.noBibliography"),
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_STOP,
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK);
|
|
|
|
return;
|
|
|
|
}
|
2008-09-23 23:11:27 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
// Make sure we have some citations
|
|
|
|
if(!this._getFields(true)) return;
|
2008-09-23 23:11:27 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
var field = this._addField();
|
|
|
|
if(!field) return;
|
|
|
|
var bibliographyData = this._session.getBibliographyData();
|
|
|
|
field.setCode(BIBLIOGRAPHY_CODE+" "+bibliographyData);
|
|
|
|
this._fields.push(field);
|
2008-09-23 23:11:27 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
this._updateSession();
|
|
|
|
this._updateDocument(false, true);
|
2008-09-23 23:11:27 +00:00
|
|
|
}
|
|
|
|
|
2008-07-10 11:05:43 +00:00
|
|
|
/**
|
2009-08-20 05:02:55 +00:00
|
|
|
* Edits bibliography metadata.
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.prototype.editBibliography = function() {
|
|
|
|
// Make sure we have a bibliography
|
|
|
|
if(!this._getFields(true)) return false;
|
|
|
|
var haveBibliography = false;
|
|
|
|
for(var i=this._fields.length-1; i>=0; i++) {
|
|
|
|
if(this._fields[i].getCode().substr(0, BIBLIOGRAPHY_CODE.length) == BIBLIOGRAPHY_CODE) {
|
|
|
|
haveBibliography = true;
|
|
|
|
break;
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
if(!haveBibliography) {
|
|
|
|
this._doc.displayAlert(Zotero.getString("integration.error.mustInsertBibliography"),
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_STOP,
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK);
|
|
|
|
return;
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
this._updateSession();
|
|
|
|
Zotero.Integration.activate();
|
|
|
|
this._session.editBibliography();
|
|
|
|
this._doc.activate();
|
|
|
|
this._updateDocument(false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the citation data for all citations and bibliography entries.
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.prototype.refresh = function() {
|
|
|
|
if(!this._getFields(true)) return false;
|
2008-07-10 11:05:43 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
// Send request, forcing update of citations and bibliography
|
|
|
|
this._updateSession();
|
|
|
|
this._updateDocument(true, true);
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Deletes field codes.
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.prototype.removeCodes = function() {
|
|
|
|
if(!this._getFields(true)) return false;
|
|
|
|
|
|
|
|
var result = this._doc.displayAlert(Zotero.getString("integration.removeCodesWarning"),
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_ICON_WARNING,
|
|
|
|
Components.interfaces.zoteroIntegrationDocument.DIALOG_BUTTONS_OK_CANCEL);
|
|
|
|
if(result) {
|
|
|
|
for(var i=this._fields.length-1; i>=0; i--) {
|
|
|
|
this._fields[i].removeCode();
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays a dialog to set document preferences (style, footnotes/endnotes, etc.)
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.prototype.setDocPrefs = function() {
|
|
|
|
if(this._getSession(false, true)) this._getFields();
|
|
|
|
var oldData = this._session.setDocPrefs(this._app.primaryFieldType, this._app.secondaryFieldType);
|
|
|
|
if(oldData) {
|
|
|
|
this._doc.setDocumentData(this._session.data.serializeXML());
|
|
|
|
if(this._fields && this._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 != 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 each(var field in this._fields) {
|
|
|
|
var fieldCode = field.getCode();
|
|
|
|
|
|
|
|
if(convertItems && fieldCode.substr(0, ITEM_CODE.length) == ITEM_CODE) {
|
|
|
|
fieldsToConvert.push(field);
|
|
|
|
fieldNoteTypes.push(this._session.data.prefs.noteType);
|
|
|
|
} else if(convertBibliographies && fieldCode.substr(0, BIBLIOGRAPHY_CODE.length) == BIBLIOGRAPHY_CODE) {
|
|
|
|
fieldsToConvert.push(field);
|
|
|
|
fieldNoteTypes.push(0);
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
2006-09-04 04:13:12 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
|
|
|
|
if(fieldsToConvert.length) {
|
|
|
|
// pass to conversion function
|
|
|
|
this._doc.convert(new Zotero.Integration.Document.JSEnumerator(fieldsToConvert),
|
|
|
|
this._session.data.prefs.fieldType, fieldNoteTypes, fieldNoteTypes.length);
|
|
|
|
|
|
|
|
// clear fields so that they will get collected again before refresh
|
|
|
|
this._fields = undefined;
|
2006-08-29 04:24:11 +00:00
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
// refresh contents
|
|
|
|
this.refresh();
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
2006-08-28 05:38:37 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
}
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Cleans up any changes made before returning, even if an error occurred
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.prototype.cleanup = function() {
|
|
|
|
this._doc.cleanup()
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* An exceedingly simple nsISimpleEnumerator implementation
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Document.JSEnumerator = function(objArray) {
|
|
|
|
this.objArray = objArray;
|
|
|
|
}
|
|
|
|
Zotero.Integration.Document.JSEnumerator.prototype.hasMoreElements = function() {
|
|
|
|
return this.objArray.length;
|
|
|
|
}
|
|
|
|
Zotero.Integration.Document.JSEnumerator.prototype.getNext = function() {
|
|
|
|
return this.objArray.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keeps track of all session-specific variables
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
|
|
|
Zotero.Integration.Session = function() {
|
|
|
|
// holds items not in document that should be in bibliography
|
|
|
|
this.uncitedItems = new Object();
|
2009-08-20 05:02:55 +00:00
|
|
|
this.reselectedItems = new Object();
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Changes the Session style and data
|
|
|
|
* @param data {Zotero.Integration.DocumentData}
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.Integration.Session.prototype.setData = function(data) {
|
|
|
|
var oldStyleID = (this.data && this.data.style.styleID ? this.data.style.styleID : false);
|
|
|
|
this.data = data;
|
|
|
|
if(data.style.styleID && oldStyleID != data.style.styleID) {
|
|
|
|
this.styleID = data.style.styleID;
|
2008-07-10 11:05:43 +00:00
|
|
|
try {
|
2009-08-20 05:02:55 +00:00
|
|
|
this.style = Zotero.Styles.get(data.style.styleID).csl;
|
2008-07-10 11:05:43 +00:00
|
|
|
this.dateModified = new Object();
|
|
|
|
|
|
|
|
this.itemSet = this.style.createItemSet();
|
|
|
|
this.loadUncitedItems();
|
|
|
|
} catch(e) {
|
|
|
|
Zotero.debug(e)
|
2009-08-20 05:02:55 +00:00
|
|
|
data.style.styleID = undefined;
|
2008-07-10 11:05:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Displays a dialog to set document preferences
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Session.prototype.setDocPrefs = function(primaryFieldType, secondaryFieldType) {
|
|
|
|
var io = new function() {
|
|
|
|
this.wrappedJSObject = this;
|
|
|
|
};
|
|
|
|
|
|
|
|
if(this.data) {
|
|
|
|
io.style = this.data.style.styleID;
|
|
|
|
io.useEndnotes = this.data.prefs.noteType == 0 ? 0 : this.data.prefs.noteType-1;
|
|
|
|
io.fieldType = this.data.prefs.fieldType;
|
|
|
|
io.primaryFieldType = primaryFieldType;
|
|
|
|
io.secondaryFieldType = secondaryFieldType;
|
|
|
|
}
|
|
|
|
|
|
|
|
Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
|
|
|
|
.getService(Components.interfaces.nsIWindowWatcher)
|
|
|
|
.openWindow(null, 'chrome://zotero/content/integrationDocPrefs.xul', '',
|
|
|
|
'chrome,modal,centerscreen' + (Zotero.isWin ? ',popup' : ''), io, true);
|
|
|
|
if(!io.style) return false;
|
|
|
|
|
|
|
|
// set data
|
|
|
|
var oldData = this.data;
|
|
|
|
var data = new Zotero.Integration.DocumentData();
|
|
|
|
data.sessionID = oldData.sessionID;
|
|
|
|
data.style.styleID = io.style;
|
|
|
|
data.prefs.fieldType = io.fieldType;
|
|
|
|
this.setData(data);
|
|
|
|
// 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.style.class == "note" ? io.useEndnotes+1 : 0;
|
|
|
|
|
|
|
|
if(!oldData || oldData.style.styleID != data.style.styleID
|
|
|
|
|| oldData.prefs.noteType != data.prefs.noteType
|
|
|
|
|| oldData.prefs.fieldType != data.prefs.fieldType) {
|
|
|
|
this.regenerateAll = this.bibliographyHasChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldData ? oldData : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reselects an item to replace a deleted item
|
|
|
|
* @param exception {Zotero.Integration.MissingItemException}
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Session.prototype.reselectItem = function(exception) {
|
|
|
|
var io = new function() {
|
|
|
|
this.wrappedJSObject = this;
|
|
|
|
};
|
|
|
|
io.addBorder = Zotero.isWin;
|
|
|
|
io.singleSelection = true;
|
|
|
|
|
|
|
|
Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
|
|
|
|
.getService(Components.interfaces.nsIWindowWatcher)
|
|
|
|
.openWindow(null,'chrome://zotero/content/selectItemsDialog.xul', '',
|
|
|
|
'chrome,modal,centerscreen,resizable=yes' + (Zotero.isWin ? ',popup' : ''), io, true);
|
|
|
|
|
|
|
|
if(io.dataOut && io.dataOut.length) {
|
|
|
|
var itemID = io.dataOut[0];
|
|
|
|
|
|
|
|
// add reselected item IDs to hash, so they can be used
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets per-request variables in the CitationSet
|
2006-09-10 17:38:17 +00:00
|
|
|
*/
|
2007-10-23 07:11:59 +00:00
|
|
|
Zotero.Integration.Session.prototype.resetRequest = function() {
|
|
|
|
this.citationsByItemID = new Object();
|
|
|
|
this.citationsByIndex = new Array();
|
2009-05-24 04:38:57 +00:00
|
|
|
this.uriMap = new Zotero.Integration.URIMap(this);
|
2007-10-23 07:11:59 +00:00
|
|
|
|
2008-09-23 23:11:27 +00:00
|
|
|
this.haveMissing = false;
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
this.regenerateAll = false;
|
|
|
|
this.bibliographyHasChanged = false;
|
2008-07-10 11:05:43 +00:00
|
|
|
this.bibliographyDataHasChanged = false;
|
2007-10-23 07:11:59 +00:00
|
|
|
this.updateItemIDs = new Object();
|
2008-09-23 23:11:27 +00:00
|
|
|
this.updateIndices = new Object();
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Generates a field from a citation object
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
|
|
|
Zotero.Integration.Session._acceptableTypes = ["string", "boolean", "number"];
|
2008-09-23 02:11:56 +00:00
|
|
|
Zotero.Integration.Session._saveProperties = ["custom", "sort"];
|
2007-10-23 07:11:59 +00:00
|
|
|
Zotero.Integration.Session.prototype.getCitationField = function(citation) {
|
|
|
|
var type, field = "";
|
|
|
|
|
|
|
|
for(var j=0; j<Zotero.Integration.Session._saveProperties.length; j++) {
|
|
|
|
var property = Zotero.Integration.Session._saveProperties[j];
|
2008-09-23 02:11:56 +00:00
|
|
|
if(citation.properties[property] || citation.properties[property] === false) {
|
2007-10-23 07:11:59 +00:00
|
|
|
field += ',"'+property+'":'+Zotero.JSON.serialize(citation.properties[property]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var citationItems = "";
|
|
|
|
for(var j=0; j<citation.citationItems.length; j++) {
|
|
|
|
var citationItem = "";
|
2008-07-24 22:25:03 +00:00
|
|
|
|
2009-05-24 04:38:57 +00:00
|
|
|
// save citationItem properties
|
2007-10-23 07:11:59 +00:00
|
|
|
for(var k in citation.citationItems[j]) {
|
|
|
|
type = typeof(citation.citationItems[j][k]);
|
2009-05-24 04:38:57 +00:00
|
|
|
if(citation.citationItems[j][k] && k != "itemID" && k != "key"
|
|
|
|
&& Zotero.Integration.Session._acceptableTypes.indexOf(type) !== -1) {
|
2007-10-23 07:11:59 +00:00
|
|
|
citationItem += ',"'+k+'":'+Zotero.JSON.serialize(citation.citationItems[j][k]);
|
|
|
|
}
|
|
|
|
}
|
2009-05-24 04:38:57 +00:00
|
|
|
|
|
|
|
// save URI
|
|
|
|
citationItem += ',"uri":'+Zotero.JSON.serialize(this.uriMap.getURIsForItemID(citation.citationItems[j].itemID));
|
2007-10-23 07:11:59 +00:00
|
|
|
citationItems += ",{"+citationItem.substr(1)+"}";
|
|
|
|
}
|
|
|
|
field += ',"citationItems":['+citationItems.substr(1)+"]";
|
|
|
|
|
|
|
|
return "{"+field.substr(1)+"}";
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Adds a citation based on a serialized Word field
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
|
|
|
Zotero.Integration._oldCitationLocatorMap = {
|
|
|
|
p:Zotero.CSL.LOCATOR_PAGES,
|
|
|
|
g:Zotero.CSL.LOCATOR_PARAGRAPH,
|
|
|
|
l:Zotero.CSL.LOCATOR_LINE
|
|
|
|
};
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Gets a Zotero.CSL.Citation object given a field name
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
|
|
|
Zotero.Integration.Session.prototype.addCitation = function(index, arg) {
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
var index = parseInt(index, 10);
|
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
if(typeof(arg) == "string") { // text field
|
|
|
|
if(arg == "!" || arg == "X") return;
|
|
|
|
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
var citation = this.unserializeCitation(arg, index);
|
2007-10-23 07:11:59 +00:00
|
|
|
} else { // a citation already
|
|
|
|
var citation = arg;
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
this.completeCitation(citation);
|
2007-10-23 07:11:59 +00:00
|
|
|
|
|
|
|
// add to citationsByItemID and citationsByIndex
|
2009-08-20 05:02:55 +00:00
|
|
|
for(var i=0; i<citation.citationItems.length; i++) {
|
|
|
|
var citationItem = citation.citationItems[i];
|
|
|
|
if(!this.citationsByItemID[citationItem.itemID]) {
|
|
|
|
this.citationsByItemID[citationItem.itemID] = [citation];
|
|
|
|
} else {
|
|
|
|
var byItemID = this.citationsByItemID[citationItem.itemID];
|
|
|
|
if(byItemID[byItemID.length-1].properties.index < index) {
|
|
|
|
// if index is greater than the last index, add to end
|
|
|
|
byItemID.push(citation);
|
2007-10-23 07:11:59 +00:00
|
|
|
} else {
|
2009-08-20 05:02:55 +00:00
|
|
|
// otherwise, splice in at appropriate location
|
|
|
|
for(var j=0; byItemID[j].properties.index < index && j<byItemID.length-1; j++) {}
|
|
|
|
byItemID.splice(j, 0, citation);
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
citation.properties.index = index;
|
|
|
|
this.citationsByIndex[index] = citation;
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Adds items to a citation whose citationItems contain only item IDs
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
|
|
|
Zotero.Integration.Session.prototype.completeCitation = function(object) {
|
|
|
|
// replace item IDs with real items
|
2009-08-20 05:02:55 +00:00
|
|
|
var err;
|
2007-10-23 07:11:59 +00:00
|
|
|
for(var i=0; i<object.citationItems.length; i++) {
|
|
|
|
var citationItem = object.citationItems[i];
|
2006-09-10 17:38:17 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
// get Zotero item
|
2009-05-24 04:38:57 +00:00
|
|
|
var zoteroItem = false;
|
|
|
|
if(citationItem.uri) {
|
2009-08-11 01:11:17 +00:00
|
|
|
var needUpdate = false;
|
|
|
|
[zoteroItem, needUpdate] = this.uriMap.getZoteroItemForURIs(citationItem.uri);
|
|
|
|
if(needUpdate) this.updateItemIDs[zoteroItem.id] = true;
|
2008-07-24 22:25:03 +00:00
|
|
|
} else {
|
|
|
|
if(citationItem.key) {
|
|
|
|
zoteroItem = Zotero.Items.getByKey(citationItem.key);
|
2009-05-24 04:38:57 +00:00
|
|
|
} else if(citationItem.itemID) {
|
2008-07-24 22:25:03 +00:00
|
|
|
zoteroItem = Zotero.Items.get(citationItem.itemID);
|
|
|
|
}
|
2009-05-24 04:38:57 +00:00
|
|
|
|
|
|
|
if(zoteroItem) this.updateItemIDs[zoteroItem.id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no item, check if it was already reselected and otherwise handle as a missing item
|
2009-08-20 05:02:55 +00:00
|
|
|
if(!zoteroItem) {
|
2009-05-24 04:38:57 +00:00
|
|
|
if(citationItem.uri) {
|
2009-08-20 05:02:55 +00:00
|
|
|
var reselectKeys = citationItem.uri;
|
|
|
|
var reselectKeyType = RESELECT_KEY_URI;
|
2009-05-24 04:38:57 +00:00
|
|
|
} else if(citationItem.key) {
|
2009-08-20 05:02:55 +00:00
|
|
|
var reselectKeys = citationItem.key;
|
|
|
|
var reselectKeyType = RESELECT_KEY_ITEM_KEY;
|
2009-05-24 04:38:57 +00:00
|
|
|
} else {
|
2009-08-20 05:02:55 +00:00
|
|
|
var reselectKeys = citationItem.itemID;
|
|
|
|
var reselectKeyType = RESELECT_KEY_ITEM_ID;
|
2009-05-24 04:38:57 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
// look to see if item has already been reselected
|
|
|
|
for each(var reselectKey in reselectKeys) {
|
|
|
|
if(this.reselectedItems[reselectKey]) {
|
|
|
|
zoteroItem = Zotero.Items.get(this.reselectedItems[reselectKey]);
|
|
|
|
break;
|
2009-05-24 04:38:57 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if not already reselected, throw a MissingItemException
|
|
|
|
if(!zoteroItem) {
|
|
|
|
throw(new Zotero.Integration.MissingItemException(
|
|
|
|
reselectKeys, reselectKeyType, i, object.citationItems.length));
|
2008-09-23 23:11:27 +00:00
|
|
|
}
|
2009-05-24 04:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get CSL item
|
|
|
|
var item = this.itemSet.getItemsByIds([zoteroItem.id])[0];
|
|
|
|
if(!item) {
|
2008-07-24 22:25:03 +00:00
|
|
|
item = this.itemSet.add([zoteroItem])[0];
|
2007-10-23 07:11:59 +00:00
|
|
|
|
|
|
|
this.dateModified[citationItem.itemID] = item.zoteroItem.getField("dateModified", true, true);
|
|
|
|
this.updateItemIDs[citationItem.itemID] = true;
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
this.bibliographyHasChanged = true;
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
citationItem.item = item;
|
2008-07-24 22:25:03 +00:00
|
|
|
if(!citationItem.itemID) citationItem.itemID = item.id;
|
2006-09-11 01:05:26 +00:00
|
|
|
}
|
2009-05-24 04:38:57 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
return null;
|
2006-09-11 01:05:26 +00:00
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Unserializes a JSON citation into a citation object (sans items)
|
2006-09-11 01:05:26 +00:00
|
|
|
*/
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
Zotero.Integration.Session.prototype.unserializeCitation = function(arg, index) {
|
2007-10-23 07:11:59 +00:00
|
|
|
if(arg[0] == "{") { // JSON field
|
|
|
|
// create citation
|
|
|
|
var citation = this.style.createCitation();
|
|
|
|
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
// fix for corrupted fields
|
|
|
|
var lastBracket = arg.lastIndexOf("}");
|
|
|
|
if(lastBracket+1 != arg.length) {
|
|
|
|
arg = arg.substr(0, lastBracket+1);
|
|
|
|
this.updateIndices[index] = true;
|
|
|
|
} else {
|
|
|
|
citation.properties.field = arg;
|
|
|
|
}
|
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// get JSON
|
|
|
|
var object = Zotero.JSON.unserialize(arg);
|
|
|
|
|
2008-06-17 20:50:44 +00:00
|
|
|
// Fix uppercase citation codes
|
|
|
|
if(object.CITATIONITEMS) {
|
|
|
|
object.citationItems = [];
|
|
|
|
for (var i=0; i<object.CITATIONITEMS.length; i++) {
|
|
|
|
for (var j in object.CITATIONITEMS[i]) {
|
|
|
|
switch (j) {
|
|
|
|
case 'ITEMID':
|
|
|
|
var field = 'itemID';
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 'position', 'custom'
|
|
|
|
default:
|
|
|
|
var field = j.toLowerCase();
|
|
|
|
}
|
|
|
|
if (!object.citationItems[i]) {
|
|
|
|
object.citationItems[i] = {};
|
|
|
|
}
|
|
|
|
object.citationItems[i][field] = object.CITATIONITEMS[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// copy properties
|
|
|
|
for(var i in object) {
|
|
|
|
if(Zotero.Integration.Session._saveProperties.indexOf(i) != -1) {
|
|
|
|
citation.properties[i] = object[i];
|
|
|
|
} else {
|
|
|
|
citation[i] = object[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // ye olde style field
|
|
|
|
var underscoreIndex = arg.indexOf("_");
|
|
|
|
var itemIDs = arg.substr(0, underscoreIndex).split("|");
|
|
|
|
|
|
|
|
var lastIndex = arg.lastIndexOf("_");
|
|
|
|
if(lastIndex != underscoreIndex+1) {
|
|
|
|
var locatorString = arg.substr(underscoreIndex+1, lastIndex-underscoreIndex-1);
|
|
|
|
var locators = locatorString.split("|");
|
|
|
|
}
|
|
|
|
|
|
|
|
var citationItems = new Array();
|
|
|
|
for(var i=0; i<itemIDs.length; i++) {
|
|
|
|
var citationItem = {itemID:itemIDs[i]};
|
|
|
|
if(locators) {
|
|
|
|
citationItem.locator = locators[i].substr(1);
|
|
|
|
citationItem.locatorType = Zotero.Integration._oldCitationLocatorMap[locators[i][0]];
|
|
|
|
}
|
|
|
|
citationItems.push(citationItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
var citation = this.style.createCitation(citationItems);
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
this.updateIndices[index] = true;
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return citation;
|
2006-09-11 01:05:26 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* <arks a citation for removal
|
2006-09-11 01:05:26 +00:00
|
|
|
*/
|
2009-05-24 04:38:57 +00:00
|
|
|
Zotero.Integration.Session.prototype.deleteCitation = function(index) {
|
2007-10-23 07:11:59 +00:00
|
|
|
this.citationsByIndex[index] = {properties:{"delete":true}};
|
|
|
|
this.updateIndices[index] = true;
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Returns a preview, given a citation object (whose citationItems lack item
|
2007-10-23 07:11:59 +00:00
|
|
|
* and position) and an index
|
|
|
|
*/
|
|
|
|
Zotero.Integration.Session.prototype.previewCitation = function(citation) {
|
|
|
|
// get length of item set, so we can tell how many items we've added
|
|
|
|
var itemSetLength = this.itemSet.items.length;
|
|
|
|
// add citation items
|
|
|
|
this.completeCitation(citation);
|
|
|
|
// get list of items we later have to delete
|
|
|
|
var deleteItems = this.itemSet.items.slice(itemSetLength, this.itemSet.items.length);
|
|
|
|
// get position
|
|
|
|
this.getCitationPositions(citation);
|
|
|
|
// sort item set
|
|
|
|
this.sortItemSet();
|
|
|
|
// sort citation if desired
|
|
|
|
if(citation.properties.sort) {
|
|
|
|
citation.sort();
|
|
|
|
}
|
|
|
|
// get preview citation
|
|
|
|
var text = this.style.formatCitation(citation, "Integration");
|
2008-07-10 11:05:43 +00:00
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// delete from item set
|
|
|
|
if(deleteItems.length) {
|
|
|
|
this.itemSet.remove(deleteItems);
|
2006-09-11 01:05:26 +00:00
|
|
|
}
|
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
return text;
|
2006-09-11 01:05:26 +00:00
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
|
2006-09-11 01:05:26 +00:00
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Brings up the addCitationDialog, prepopulated if a citation is provided
|
2006-09-11 01:05:26 +00:00
|
|
|
*/
|
2007-10-23 07:11:59 +00:00
|
|
|
Zotero.Integration.Session.prototype.editCitation = function(index, citation) {
|
|
|
|
var me = this;
|
|
|
|
var io = new function() { this.wrappedJSObject = this; }
|
|
|
|
|
2008-08-21 21:30:36 +00:00
|
|
|
// if there's already a citation, make sure we have item IDs in addition to keys
|
|
|
|
if(citation) {
|
2009-08-11 01:11:17 +00:00
|
|
|
var zoteroItem;
|
2008-08-21 21:30:36 +00:00
|
|
|
for each(var citationItem in citation.citationItems) {
|
2009-05-24 04:38:57 +00:00
|
|
|
var item = false;
|
|
|
|
if(!citationItem.itemID) {
|
2009-08-11 01:11:17 +00:00
|
|
|
zoteroItem = false;
|
2009-05-24 04:38:57 +00:00
|
|
|
if(citationItem.uri) {
|
2009-08-11 01:11:17 +00:00
|
|
|
[zoteroItem, ] = this.uriMap.getZoteroItemForURIs(citationItem.uri);
|
2009-05-24 04:38:57 +00:00
|
|
|
} else if(citationItem.key) {
|
2009-08-11 01:11:17 +00:00
|
|
|
zoteroItem = Zotero.Items.getByKey(citationItem.key);
|
2009-05-24 04:38:57 +00:00
|
|
|
}
|
|
|
|
if(zoteroItem) citationItem.itemID = zoteroItem.id;
|
2008-08-21 21:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// create object to hold citation
|
|
|
|
io.citation = (citation ? citation.clone() : this.style.createCitation());
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
io.citation.properties.index = parseInt(index, 10);
|
2007-10-23 07:11:59 +00:00
|
|
|
// assign preview function
|
|
|
|
io.previewFunction = function() {
|
|
|
|
return me.previewCitation(io.citation);
|
|
|
|
}
|
2006-09-11 01:05:26 +00:00
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
|
2008-11-30 20:18:48 +00:00
|
|
|
.getService(Components.interfaces.nsIWindowWatcher)
|
|
|
|
.openWindow(
|
|
|
|
null, 'chrome://zotero/content/addCitationDialog.xul', '',
|
|
|
|
'chrome,modal,centerscreen,resizable=yes'
|
|
|
|
+ (Zotero.Integration.usePopup ? ',popup' : ''),
|
|
|
|
io
|
|
|
|
);
|
2009-08-20 05:02:55 +00:00
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
if(citation && !io.citation.citationItems.length) {
|
|
|
|
io.citation = citation;
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
|
|
|
|
if(io.citation.citationItems.length) { // we have an item
|
|
|
|
this.addCitation(index, io.citation);
|
|
|
|
this.updateIndices[index] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// resort item set if necessary
|
|
|
|
this.sortItemSet();
|
|
|
|
|
|
|
|
return !!io.citation.citationItems.length;
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Sets position attribute on a citation
|
2006-09-10 17:38:17 +00:00
|
|
|
*/
|
2007-10-23 07:11:59 +00:00
|
|
|
Zotero.Integration.Session.prototype.getCitationPositions = function(citation, update) {
|
|
|
|
for(var previousIndex = citation.properties.index-1;
|
|
|
|
previousIndex != -1
|
|
|
|
&& (!this.citationsByIndex[previousIndex]
|
2008-09-23 23:11:27 +00:00
|
|
|
|| this.citationsByIndex[previousIndex].properties["delete"]);
|
2007-10-23 07:11:59 +00:00
|
|
|
previousIndex--) {}
|
|
|
|
var previousCitation = (previousIndex == -1 ? false : this.citationsByIndex[previousIndex]);
|
2006-09-04 04:13:12 +00:00
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// if only one source, and it's the same as the last, use ibid
|
|
|
|
if( // there must be a previous citation with one item, and this citation
|
|
|
|
// may only have one item
|
|
|
|
previousCitation && citation.citationItems.length == 1
|
|
|
|
&& previousCitation.citationItems.length == 1
|
|
|
|
// the previous citation must have been a citation of the same item
|
|
|
|
&& citation.citationItems[0].item == previousCitation.citationItems[0].item
|
|
|
|
// and if the previous citation had a locator (page number, etc.)
|
|
|
|
// then this citation must have a locator, or else we should do the
|
|
|
|
// full citation (see Chicago Manual of Style)
|
|
|
|
&& (!previousCitation.citationItems[0].locator || citation.citationItems[0].locator)) {
|
|
|
|
// use ibid, but check whether to use ibid+pages
|
|
|
|
var newPosition = (citation.citationItems[0].locator == previousCitation.citationItems[0].locator
|
|
|
|
&& citation.citationItems[0].locatorType == previousCitation.citationItems[0].locatorType
|
|
|
|
? Zotero.CSL.POSITION_IBID : Zotero.CSL.POSITION_IBID_WITH_LOCATOR);
|
|
|
|
// update if desired
|
|
|
|
if(update && (citation.citationItems[0].position || newPosition) && citation.citationItems[0].position != newPosition) {
|
|
|
|
this.updateIndices[citation.properties.index] = true;
|
|
|
|
}
|
|
|
|
citation.citationItems[0].position = newPosition;
|
|
|
|
} else {
|
|
|
|
// loop through to see which are first citations
|
|
|
|
for(var i=0; i<citation.citationItems.length; i++) {
|
|
|
|
var citationItem = citation.citationItems[i];
|
|
|
|
var newPosition = (!this.citationsByItemID[citationItem.itemID]
|
|
|
|
|| this.citationsByItemID[citationItem.itemID][0].properties.index >= citation.properties.index
|
|
|
|
? Zotero.CSL.POSITION_FIRST : Zotero.CSL.POSITION_SUBSEQUENT);
|
|
|
|
|
|
|
|
// update if desired
|
|
|
|
if(update && (citation.citationItems[i].position || newPosition) && citation.citationItems[i].position != newPosition) {
|
|
|
|
this.updateIndices[citation.properties.index] = true;
|
|
|
|
}
|
|
|
|
citation.citationItems[i].position = newPosition;
|
|
|
|
}
|
|
|
|
}
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Marks citations for update, where necessary
|
2006-09-10 17:38:17 +00:00
|
|
|
*/
|
2007-10-23 07:11:59 +00:00
|
|
|
Zotero.Integration.Session.prototype.updateCitations = function(toIndex) {
|
|
|
|
if(!toIndex) toIndex = this.citationsByIndex.length-1;
|
|
|
|
for(var i=0; i<=toIndex; i++) {
|
|
|
|
var citation = this.citationsByIndex[i];
|
|
|
|
// get position, updating if necesary
|
2008-09-23 23:11:27 +00:00
|
|
|
if(citation && !citation.properties["delete"] && !citation.properties.custom) {
|
2007-10-23 07:11:59 +00:00
|
|
|
this.getCitationPositions(citation, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Updates the ItemSet, adding and deleting bibliography items as appropriate, then re-sorting
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
|
|
|
Zotero.Integration.Session.prototype.updateItemSet = function() {
|
|
|
|
var deleteItems = [];
|
2008-09-23 23:11:27 +00:00
|
|
|
var missingItems = [];
|
2006-09-10 17:38:17 +00:00
|
|
|
|
2008-09-23 23:11:27 +00:00
|
|
|
// see if items were deleted from Zotero
|
2007-10-23 07:11:59 +00:00
|
|
|
for(var i in this.citationsByItemID) {
|
|
|
|
if (!Zotero.Items.get(i)) {
|
2008-09-23 23:11:27 +00:00
|
|
|
deleteItems.push(i);
|
|
|
|
missingItems.push(i);
|
2006-09-04 04:13:12 +00:00
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// see if old items were deleted or changed
|
|
|
|
for each(var item in this.itemSet.items) {
|
2008-07-24 22:25:03 +00:00
|
|
|
var itemID = item.id;
|
2006-09-04 04:13:12 +00:00
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// see if items were removed
|
2009-05-24 04:38:57 +00:00
|
|
|
if(!this.citationsByItemID[itemID] && !this.uncitedItems[itemID]) {
|
2007-10-23 07:11:59 +00:00
|
|
|
deleteItems.push(itemID);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(item.zoteroItem && this.dateModified[itemID] != item.zoteroItem.getField("dateModified", true, true)) {
|
|
|
|
// update date modified
|
|
|
|
this.dateModified[itemID] = item.zoteroItem.getField("dateModified", true, true);
|
|
|
|
// add to list of updated item IDs
|
|
|
|
this.updateItemIDs[itemID] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 23:11:27 +00:00
|
|
|
// delete items from item set
|
2007-10-23 07:11:59 +00:00
|
|
|
if(deleteItems.length) {
|
|
|
|
this.itemSet.remove(deleteItems);
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
this.bibliographyHasChanged = true;
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
this.sortItemSet();
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Sorts the ItemSet
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
|
|
|
Zotero.Integration.Session.prototype.sortItemSet = function() {
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
// save first index
|
|
|
|
for(var itemID in this.citationsByItemID) {
|
|
|
|
if(this.citationsByItemID[itemID]) {
|
|
|
|
var item = this.itemSet.getItemsByIds([itemID])[0];
|
|
|
|
if(item) item.setProperty("index", this.citationsByItemID[itemID][0].properties.index);
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
var citationChanged = this.itemSet.resort();
|
2006-09-10 17:38:17 +00:00
|
|
|
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
// add to list of updated item IDs
|
|
|
|
for each(var item in citationChanged) {
|
2008-07-24 22:25:03 +00:00
|
|
|
this.updateItemIDs[item.id] = true;
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
this.bibliographyHasChanged = true;
|
|
|
|
}
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Edits integration bibliography
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
|
|
|
Zotero.Integration.Session.prototype.editBibliography = function() {
|
|
|
|
var bibliographyEditor = new Zotero.Integration.Session.BibliographyEditInterface(this);
|
|
|
|
var io = new function() { this.wrappedJSObject = bibliographyEditor; }
|
|
|
|
|
2008-07-10 11:05:43 +00:00
|
|
|
this.bibliographyDataHasChanged = this.bibliographyHasChanged = true;
|
2007-10-23 07:11:59 +00:00
|
|
|
|
|
|
|
Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
|
2008-11-30 20:18:48 +00:00
|
|
|
.getService(Components.interfaces.nsIWindowWatcher)
|
|
|
|
.openWindow(
|
|
|
|
null, 'chrome://zotero/content/editBibliographyDialog.xul', '',
|
|
|
|
'chrome,modal,centerscreen,resizable=yes'
|
|
|
|
+ (Zotero.Integration.usePopup ? ',popup' : ''),
|
|
|
|
io,
|
|
|
|
true
|
|
|
|
);
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Gets integration bibliography
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.Integration.Session.prototype.getBibliography = function() {
|
|
|
|
// use real RTF, but chop off the first \n
|
|
|
|
var text = this.style.formatBibliography(this.itemSet, "RTF")
|
|
|
|
var nlIndex = text.indexOf("\n");
|
|
|
|
if(nlIndex !== -1) {
|
|
|
|
return "{\\rtf "+text.substr(text.indexOf("\n"));
|
2008-07-10 11:05:43 +00:00
|
|
|
} else {
|
2009-08-20 05:02:55 +00:00
|
|
|
return "";
|
2008-07-10 11:05:43 +00:00
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Refreshes updateIndices variable to include fields for modified items
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
2009-08-20 05:02:55 +00:00
|
|
|
Zotero.Integration.Session.prototype.updateUpdateIndices = function(regenerateAll) {
|
Merged revisions 1986,1988-1990,1996,1998,2000-2004,2006,2008-2010,2013,2020-2028,2032-2034,2037,2039,2044-2049,2052-2056,2059,2065-2066,2068-2072,2075,2077-2079,2081,2083-2084,2086,2088-2094,2096,2099,2101,2103-2104,2107-2110,2115,2118,2120-2122,2126-2128,2131,2140,2142-2145,2149-2153,2155-2159,2165,2167-2168,2170-2171,2173-2176,2179-2183,2185-2186 via svnmerge from
https://www.zotero.org/svn/extension/branches/1.0
2008-01-30 09:53:19 +00:00
|
|
|
if(regenerateAll || this.regenerateAll) {
|
2007-10-23 07:11:59 +00:00
|
|
|
// update all indices
|
|
|
|
for(var i=0; i<this.citationsByIndex.length; i++) {
|
|
|
|
this.updateIndices[i] = true;
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
} else {
|
|
|
|
// update only item IDs
|
|
|
|
for(var i in this.updateItemIDs) {
|
|
|
|
if(this.citationsByItemID[i] && this.citationsByItemID[i].length) {
|
|
|
|
for(var j=0; j<this.citationsByItemID[i].length; j++) {
|
|
|
|
this.updateIndices[this.citationsByItemID[i][j].properties.index] = true;
|
2006-09-04 04:13:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-10 11:05:43 +00:00
|
|
|
Zotero.Integration.Session._rtfEscapeFunction = function(aChar) {
|
|
|
|
return "{\\uc0\\u"+aChar.charCodeAt(0).toString()+"}"
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Loads document data from a JSON object
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
2008-07-10 11:05:43 +00:00
|
|
|
Zotero.Integration.Session.prototype.loadBibliographyData = function(json) {
|
2007-10-23 07:11:59 +00:00
|
|
|
var documentData = Zotero.JSON.unserialize(json);
|
2006-09-10 17:38:17 +00:00
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// set uncited
|
|
|
|
if(documentData.uncited) {
|
2009-05-24 04:38:57 +00:00
|
|
|
if(documentData.uncited[0]) {
|
|
|
|
// new style array of arrays with URIs
|
2009-08-11 01:11:17 +00:00
|
|
|
var zoteroItem, needUpdate;
|
2009-05-24 04:38:57 +00:00
|
|
|
for each(var uris in documentData.uncited) {
|
2009-08-11 01:11:17 +00:00
|
|
|
[zoteroItem, needUpdate] = this.uriMap.getZoteroItemForURIs(uris);
|
2009-05-24 04:38:57 +00:00
|
|
|
if(zoteroItem) this.uncitedItems[zoteroItem.id] = true;
|
2009-08-11 01:11:17 +00:00
|
|
|
if(needUpdate) this.bibliographyDataHasChanged = true;
|
2009-05-24 04:38:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(var itemID in documentData.uncited) {
|
|
|
|
// if not yet in item set, add to item set
|
|
|
|
if(typeof(itemID) == "string") { // key
|
|
|
|
var zoteroItem = Zotero.getItemByKey(itemID);
|
|
|
|
this.uncitedItems[zoteroItem.id] = true;
|
|
|
|
} else { // item ID
|
|
|
|
this.uncitedItems[itemID] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.bibliographyDataHasChanged = true;
|
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
2009-05-24 04:38:57 +00:00
|
|
|
this.loadUncitedItems();
|
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// set custom bibliography entries
|
|
|
|
if(documentData.custom) {
|
2009-05-24 04:38:57 +00:00
|
|
|
if(documentData.custom[0]) {
|
|
|
|
// new style array of arrays with URIs
|
2009-08-11 01:11:17 +00:00
|
|
|
var zoteroItem, needUpdate;
|
2009-05-24 04:38:57 +00:00
|
|
|
for each(var custom in documentData.custom) {
|
2009-08-11 01:11:17 +00:00
|
|
|
[zoteroItem, needUpdate] = this.uriMap.getZoteroItemForURIs(custom[0]);
|
2009-05-24 04:38:57 +00:00
|
|
|
if(!zoteroItem) continue;
|
2009-08-11 01:11:17 +00:00
|
|
|
if(needUpdate) this.bibliographyDataHasChanged = true;
|
2009-05-24 04:38:57 +00:00
|
|
|
|
|
|
|
var item = this.itemSet.getItemsByIds([zoteroItem.id])[0];
|
|
|
|
if(!item) continue;
|
|
|
|
|
|
|
|
item.setProperty("bibliography-Integration", custom[1]);
|
|
|
|
item.setProperty("bibliography-RTF", custom[1]);
|
2008-07-24 22:25:03 +00:00
|
|
|
}
|
2009-05-24 04:38:57 +00:00
|
|
|
} else {
|
|
|
|
// old style hash
|
|
|
|
for(var itemID in documentData.custom) {
|
|
|
|
if(typeof(itemID) == "string") { // key;
|
|
|
|
var zoteroItem = Zotero.Items.getByKey(itemID);
|
|
|
|
if(!zoteroItem) continue;
|
|
|
|
|
|
|
|
var item = this.itemSet.getItemsByIds([zoteroItem.id])[0];
|
|
|
|
} else { // item
|
|
|
|
var item = this.itemSet.getItemsByIds([itemID])[0];
|
|
|
|
}
|
|
|
|
if (!item) continue;
|
|
|
|
item.setProperty("bibliography-Integration", documentData.custom[itemID]);
|
|
|
|
item.setProperty("bibliography-RTF", documentData.custom[itemID]);
|
Merged revisions 2710-2712,2714-2716,2718-2728,2730-2731,2734,2736-2738,2740-2750,2752-2753,2755,2758-2768,2770-2779,2782,2789-2790,2794,2797-2802,2804,2808-2810,2812,2814-2824,2826-2832,2834-2835 via svnmerge from 1.0 branch
2008-06-11 08:55:59 +00:00
|
|
|
}
|
2009-05-24 04:38:57 +00:00
|
|
|
this.bibliographyDataHasChanged = true;
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
2009-08-20 05:02:55 +00:00
|
|
|
|
|
|
|
this.bibliographyData = json;
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Adds items in this.uncitedItems to itemSet, if they are not already there
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
|
|
|
Zotero.Integration.Session.prototype.loadUncitedItems = function() {
|
|
|
|
for(var itemID in this.uncitedItems) {
|
|
|
|
// skip "undefined"
|
|
|
|
if(!this.uncitedItems[itemID]) continue;
|
2009-05-24 04:38:57 +00:00
|
|
|
var item = this.itemSet.getItemsByIds([itemID])[0];
|
|
|
|
if(!item) {
|
|
|
|
var zoteroItem = Zotero.Items.get(itemID);
|
|
|
|
if(zoteroItem) this.itemSet.add([zoteroItem]);
|
2008-07-24 22:25:03 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* Saves document data from a JSON object
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
2008-07-10 11:05:43 +00:00
|
|
|
Zotero.Integration.Session.prototype.getBibliographyData = function() {
|
|
|
|
var bibliographyData = {};
|
2006-09-10 17:38:17 +00:00
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// add uncited if there is anything
|
2009-05-24 04:38:57 +00:00
|
|
|
for(var item in this.uncitedItems) {
|
2008-07-24 22:25:03 +00:00
|
|
|
if(item) {
|
2009-05-24 04:38:57 +00:00
|
|
|
if(!bibliographyData.uncited) bibliographyData.uncited = [];
|
|
|
|
bibliographyData.uncited.push(this.uriMap.getURIsForItemID(item));
|
2008-07-24 22:25:03 +00:00
|
|
|
}
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
2006-09-10 17:38:17 +00:00
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// look for custom bibliography entries
|
|
|
|
if(this.itemSet.items.length) {
|
|
|
|
for(var i=0; i<this.itemSet.items.length; i++) {
|
2009-04-04 07:10:29 +00:00
|
|
|
var custom = this.itemSet.items[i].getProperty("bibliography-RTF");
|
2007-10-23 07:11:59 +00:00
|
|
|
if(custom !== "") {
|
2009-05-24 04:38:57 +00:00
|
|
|
if(!bibliographyData.custom) bibliographyData.custom = [];
|
|
|
|
bibliographyData.custom.push([
|
|
|
|
this.uriMap.getURIsForItemID(this.itemSet.items[i].id),
|
|
|
|
this.itemSet.items[i].getProperty("bibliography-RTF")]);
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-10 11:05:43 +00:00
|
|
|
if(bibliographyData.uncited || bibliographyData.custom) {
|
|
|
|
return Zotero.JSON.serialize(bibliographyData);
|
2007-10-23 07:11:59 +00:00
|
|
|
} else {
|
2008-07-10 11:05:43 +00:00
|
|
|
return ""; // nothing
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
|
2008-09-11 21:29:05 +00:00
|
|
|
/**
|
|
|
|
* @class Interface for bibliography editor to alter document bibliography
|
|
|
|
* @constructor
|
|
|
|
* Creates a new bibliography editor interface
|
|
|
|
* @param {Zotero.Integration.Session} session
|
2007-10-23 07:11:59 +00:00
|
|
|
*/
|
|
|
|
Zotero.Integration.Session.BibliographyEditInterface = function(session) {
|
|
|
|
this.session = session;
|
|
|
|
}
|
|
|
|
|
2008-09-11 21:29:05 +00:00
|
|
|
/**
|
|
|
|
* Gets the @link {Zotero.CSL.ItemSet} for the bibliography being edited
|
|
|
|
* The item set should not be modified, but may be used to determine what items are in the
|
|
|
|
* bibliography.
|
|
|
|
*/
|
2007-10-23 07:11:59 +00:00
|
|
|
Zotero.Integration.Session.BibliographyEditInterface.prototype.getItemSet = function() {
|
|
|
|
return this.session.itemSet;
|
|
|
|
}
|
|
|
|
|
2008-09-11 21:29:05 +00:00
|
|
|
/**
|
|
|
|
* Checks whether an item is cited in the bibliography being edited
|
|
|
|
*/
|
2007-10-23 07:11:59 +00:00
|
|
|
Zotero.Integration.Session.BibliographyEditInterface.prototype.isCited = function(item) {
|
2008-07-24 22:25:03 +00:00
|
|
|
if(this.session.citationsByItemID[item.id]) return true;
|
2007-10-23 07:11:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-11 21:29:05 +00:00
|
|
|
/**
|
|
|
|
* Checks whether an item is cited in the bibliography being edited
|
|
|
|
*/
|
2007-10-23 07:11:59 +00:00
|
|
|
Zotero.Integration.Session.BibliographyEditInterface.prototype.add = function(item) {
|
|
|
|
// create new item
|
|
|
|
this.session.itemSet.add([item]);
|
2009-05-24 04:38:57 +00:00
|
|
|
this.session.uncitedItems[item.id] = true;
|
2007-10-23 07:11:59 +00:00
|
|
|
this.session.sortItemSet();
|
|
|
|
}
|
|
|
|
|
2008-09-11 21:29:05 +00:00
|
|
|
/**
|
|
|
|
* Removes an item from the bibliography being edited
|
|
|
|
*/
|
2007-10-23 07:11:59 +00:00
|
|
|
Zotero.Integration.Session.BibliographyEditInterface.prototype.remove = function(item) {
|
|
|
|
// create new item
|
|
|
|
this.session.itemSet.remove([item]);
|
|
|
|
this.session.sortItemSet();
|
2006-09-10 17:38:17 +00:00
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// delete citations if necessary
|
2008-07-24 22:25:03 +00:00
|
|
|
var itemID = item.id;
|
2007-10-23 07:11:59 +00:00
|
|
|
if(this.session.citationsByItemID[itemID]) {
|
|
|
|
for(var j=0; j<this.session.citationsByItemID[itemID].length; j++) {
|
|
|
|
var citation = this.session.citationsByItemID[itemID][j];
|
|
|
|
this.session.updateIndices[citation.properties.index] = true;
|
2008-09-23 23:11:27 +00:00
|
|
|
citation.properties["delete"] = true;
|
2007-10-23 07:11:59 +00:00
|
|
|
}
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
|
2007-10-23 07:11:59 +00:00
|
|
|
// delete uncited if neceessary
|
2009-05-24 04:38:57 +00:00
|
|
|
if(this.session.uncitedItems[itemID]) this.session.uncitedItems[itemID] = undefined;
|
2006-09-10 17:38:17 +00:00
|
|
|
}
|
|
|
|
|
2008-09-11 21:29:05 +00:00
|
|
|
/**
|
|
|
|
* Generates a preview of the bibliography entry for a given item
|
|
|
|
*/
|
2007-10-23 07:11:59 +00:00
|
|
|
Zotero.Integration.Session.BibliographyEditInterface.prototype.preview = function(item) {
|
|
|
|
var itemSet = this.session.style.createItemSet([item]);
|
|
|
|
return this.session.style.formatBibliography(itemSet, "Integration");
|
2009-05-24 04:38:57 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:02:55 +00:00
|
|
|
/**
|
|
|
|
* A class for parsing and passing around document-specific data
|
|
|
|
*/
|
|
|
|
Zotero.Integration.DocumentData = function(string) {
|
|
|
|
this.style = {};
|
|
|
|
this.prefs = {};
|
|
|
|
this.sessionID = null;
|
|
|
|
if(string) {
|
|
|
|
this.unserialize(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializes document-specific data as XML
|
|
|
|
*/
|
|
|
|
Zotero.Integration.DocumentData.prototype.serializeXML = function() {
|
|
|
|
var xmlData = <data><session id={this.sessionID} />
|
|
|
|
<style id={this.style.styleID} hasBibliography={this.style.hasBibliography ? 1 : 0}/>
|
|
|
|
<prefs/>
|
|
|
|
</data>;
|
|
|
|
|
|
|
|
for(var pref in this.prefs) {
|
|
|
|
xmlData.prefs.pref += <pref name={pref} value={this.prefs[pref]}/>
|
|
|
|
}
|
|
|
|
|
|
|
|
XML.prettyPrinting = false;
|
|
|
|
var output = xmlData.toXMLString().replace("\n", "", "g");
|
|
|
|
XML.prettyPrinting = true;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unserializes document-specific XML
|
|
|
|
*/
|
|
|
|
Zotero.Integration.DocumentData.prototype.unserializeXML = function(xmlData) {
|
|
|
|
if(typeof xmlData == "string") {
|
|
|
|
var xmlData = new XML(xmlData);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sessionID = xmlData.session.@id.toString();
|
|
|
|
this.style = {"styleID":xmlData.style.@id.toString(),
|
|
|
|
"hasBibliography":(xmlData.style.@hasBibliography.toString() == 1)};
|
|
|
|
this.prefs = {};
|
|
|
|
for each(var pref in xmlData.prefs.children()) {
|
|
|
|
this.prefs[pref.@name.toString()] = pref.@value.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unserializes document-specific data, either as XML or as the string form used previously
|
|
|
|
*/
|
|
|
|
Zotero.Integration.DocumentData.prototype.unserialize = function(input) {
|
2009-08-21 08:41:07 +00:00
|
|
|
if(input[0] == "<") {
|
2009-08-20 05:02:55 +00:00
|
|
|
this.unserializeXML(input);
|
|
|
|
} else {
|
|
|
|
const splitRe = /(^|[^\:])\:([^\:]|$)/;
|
|
|
|
|
2009-08-21 08:41:07 +00:00
|
|
|
var splitOutput = input.split(splitRe);
|
|
|
|
var prefParameters = [splitOutput[0]+splitOutput[1]];
|
|
|
|
for(var i=2; i<splitOutput.length; i+=3) {
|
2009-08-20 05:02:55 +00:00
|
|
|
prefParameters.push((splitOutput[i]+splitOutput[i+1]+splitOutput[i+2]).replace("::", ":", "g"));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sessionID = prefParameters[0];
|
|
|
|
this.style = {"styleID":prefParameters[1],
|
|
|
|
"hasBibliography":(prefParameters[3] == "1" || prefParameters[3] == "True")};
|
|
|
|
this.prefs = {"fieldType":((prefParameters[5] == "1" || prefParameters[5] == "True") ? "Bookmark" : "Field")};
|
|
|
|
if(prefParameters[2] == "note") {
|
|
|
|
if(prefParameters[4] == "1" || prefParameters[4] == "True") {
|
|
|
|
this.prefs.noteType = Components.interfaces.zoteroIntegrationDocument.NOTE_ENDNOTE;
|
|
|
|
} else {
|
|
|
|
this.prefs.noteType = Components.interfaces.zoteroIntegrationDocument.NOTE_FOOTNOTE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.prefs.noteType = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-24 04:38:57 +00:00
|
|
|
/**
|
|
|
|
* Handles mapping of item IDs to URIs
|
|
|
|
*/
|
|
|
|
Zotero.Integration.URIMap = function(session) {
|
|
|
|
this.itemIDURIs = {};
|
|
|
|
this.session = session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a given mapping to the URI map
|
|
|
|
*/
|
|
|
|
Zotero.Integration.URIMap.prototype.add = function(id, uris) {
|
|
|
|
this.itemIDURIs[id] = uris;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets URIs for a given item ID, and adds to map
|
|
|
|
*/
|
|
|
|
Zotero.Integration.URIMap.prototype.getURIsForItemID = function(id) {
|
|
|
|
if(!this.itemIDURIs[id]) {
|
|
|
|
this.itemIDURIs[id] = [Zotero.URI.getItemURI(Zotero.Items.get(id))];
|
|
|
|
}
|
|
|
|
return this.itemIDURIs[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets Zotero item for a given set of URIs
|
|
|
|
*/
|
|
|
|
Zotero.Integration.URIMap.prototype.getZoteroItemForURIs = function(uris) {
|
|
|
|
var zoteroItem = false;
|
2009-08-11 01:11:17 +00:00
|
|
|
var needUpdate = false;
|
|
|
|
|
|
|
|
for(var i in uris) {
|
2009-05-24 04:38:57 +00:00
|
|
|
try {
|
2009-08-11 01:11:17 +00:00
|
|
|
zoteroItem = Zotero.URI.getURIItem(uris[i]);
|
2009-05-24 04:38:57 +00:00
|
|
|
if(zoteroItem) break;
|
|
|
|
} catch(e) {}
|
|
|
|
}
|
2009-08-11 01:11:17 +00:00
|
|
|
|
|
|
|
if(zoteroItem) {
|
|
|
|
// make sure URI is up to date (in case user just began synching)
|
|
|
|
var newURI = Zotero.URI.getItemURI(zoteroItem);
|
|
|
|
if(newURI != uris[i]) {
|
|
|
|
uris[i] = newURI;
|
|
|
|
needUpdate = true;
|
|
|
|
}
|
|
|
|
// cache uris
|
|
|
|
this.itemIDURIs[zoteroItem.id] = uris;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [zoteroItem, needUpdate];
|
2006-08-30 00:16:07 +00:00
|
|
|
}
|