commit 5284da05af1a2e92502d74d74f8063b498d1cb13
parent 75374af5afc4c01dce49c8a39038d2808014020f
Author: Simon Kornblith <simon@simonster.com>
Date: Sun, 6 Jun 2010 00:57:47 +0000
- return "Keep Citations Sorted" checkbox
- update to citeproc-js 1.0.22
From Frank's announcement:
- Bypass sorting of citations that have a value for "unsorted"
in citation.properties.
This permits per-citation override of style-driven citation sorts, as
supported by Zotero and required for some common use cases.
Diffstat:
3 files changed, 89 insertions(+), 83 deletions(-)
diff --git a/chrome/content/zotero/integration/addCitationDialog.js b/chrome/content/zotero/integration/addCitationDialog.js
@@ -42,6 +42,7 @@ var Zotero_Citation_Dialog = new function () {
var _autoRegeneratePref;
var _acceptButton;
var _sortCheckbox;
+ var _citationList;
var _originalHTML;
var io;
@@ -71,13 +72,13 @@ var Zotero_Citation_Dialog = new function () {
// find accept button
_acceptButton = document.getElementById("zotero-add-citation-dialog").getButton("accept");
_autoRegeneratePref = Zotero.Prefs.get("integration.autoRegenerate");
+ _citationList = document.getElementById("citation-list");
// if a style with sortable citations, present checkbox
- if(io.citation.sortable) {
+ if(io.sortable) {
_sortCheckbox = document.getElementById("keepSorted");
_sortCheckbox.hidden = false;
- if(io.citation.properties.sort === undefined) io.citation.properties.sort = true;
- _sortCheckbox.checked = io.citation.properties.sort;
+ _sortCheckbox.checked = !io.citation.properties.unsorted;
}
// load locators
@@ -217,30 +218,29 @@ var Zotero_Citation_Dialog = new function () {
_updateAccept();
_updatePreview();
}
- _configListPosition(document.getElementById("citation-list"), true);
+ _configListPosition(true);
}
/*
* called when an item in the selected items list is clicked
*/
function listItemSelected() {
- var itemList = document.getElementById("citation-list");
- var selectedListItem = itemList.getSelectedItem(0);
+ var selectedListItem = _citationList.getSelectedItem(0);
var itemID = (selectedListItem ? selectedListItem.value : false);
_itemSelected(itemID);
- _configListPosition(itemList, !itemID);
+ _configListPosition(!itemID);
document.getElementById("remove").disabled = !itemID;
}
- function _configListPosition(itemList,flag) {
- var selectedIndex = itemList.selectedIndex;
+ function _configListPosition(flag) {
+ var selectedIndex = _citationList.selectedIndex;
if (selectedIndex > 0) {
document.getElementById("up").disabled = flag;
} else {
document.getElementById("up").disabled = true;
}
- if (selectedIndex < (itemList.getRowCount() - 1)) {
+ if (selectedIndex < (_citationList.getRowCount() - 1)) {
document.getElementById("down").disabled = flag;
} else {
document.getElementById("down").disabled = true;
@@ -248,22 +248,27 @@ var Zotero_Citation_Dialog = new function () {
}
function _move(direction) {
+ // automatically uncheck sorted checkbox if user is rearranging citation
+ if(_sortCheckbox && _sortCheckbox.checked) {
+ _sortCheckbox.checked = false;
+ sortCitation();
+ }
+
var insertBeforeItem;
- var itemList = document.getElementById("citation-list");
- var selectedListItem = itemList.getSelectedItem(0);
+ var selectedListItem = _citationList.getSelectedItem(0);
var itemID = selectedListItem.value;
- var selectedListIndex = itemList.selectedIndex;
+ var selectedListIndex = _citationList.selectedIndex;
if (direction === -1) {
insertBeforeItem = selectedListItem.previousSibling;
} else {
insertBeforeItem = selectedListItem.nextSibling.nextSibling;
}
- var listItem = itemList.removeChild(selectedListItem);
- itemList.insertBefore(listItem, insertBeforeItem);
- itemList.selectedIndex = (selectedListIndex + direction);
+ var listItem = _citationList.removeChild(selectedListItem);
+ _citationList.insertBefore(listItem, insertBeforeItem);
+ _citationList.selectedIndex = (selectedListIndex + direction);
_itemSelected(itemID);
_updatePreview();
- _configListPosition(itemList, false);
+ _configListPosition(false);
}
function up() {
@@ -281,6 +286,8 @@ var Zotero_Citation_Dialog = new function () {
var item = itemsView.getSelectedItems()[0]; // treeview from selectItemsDialog.js
_itemSelected(item.getID());
_addItem(item);
+ _citationList.selectedIndex = _citationList.getRowCount()-1;
+ _citationList.focus();
// don't let someone select it again
document.getElementById("add").disabled = true;
@@ -295,8 +302,7 @@ var Zotero_Citation_Dialog = new function () {
* Deletes a citation from the multipleSources list
*/
function remove() {
- var citationList = document.getElementById("citation-list");
- var selectedListItem = citationList.getSelectedItem(0);
+ var selectedListItem = _citationList.getSelectedItem(0);
var itemID = selectedListItem.value;
// remove from _itemData
@@ -312,7 +318,7 @@ var Zotero_Citation_Dialog = new function () {
}
// remove from list
- citationList.removeChild(selectedListItem);
+ _citationList.removeChild(selectedListItem);
_updateAccept();
_updatePreview();
@@ -323,22 +329,29 @@ var Zotero_Citation_Dialog = new function () {
* Sorts the list of citations
*/
function sortCitation() {
- io.citation.properties.sort = _sortCheckbox && _sortCheckbox.checked;
- if(io.citation.properties.sort) {
- _getCitation();
-
- // delete all existing items from list
- _clearCitationList();
-
- // run preview function to re-sort, if it hasn't already been
- // run
- io.previewFunction();
-
- // add items back to list
- for(var i=0; i<io.citation.citationItems.length; i++) {
- var item = Zotero.Items.get(io.citation.citationItems[i].id);
- _addItem(item);
- }
+ if(!_sortCheckbox) return;
+ if(!_sortCheckbox.checked) {
+ io.citation.properties.unsorted = true;
+ return;
+ }
+
+ var selectedItemID = (_citationList.selectedItem ? _citationList.selectedItem.value : null);
+ Zotero.debug("item "+selectedItemID+" selected");
+ _getCitation();
+
+ // delete all existing items from list
+ _clearCitationList();
+
+ // run preview function to re-sort, if it hasn't already been
+ // run
+ io.previewFunction();
+
+ // add items back to list
+ for(var i=0; i<io.citation.sortedItems.length; i++) {
+ var itemID = io.citation.sortedItems[i][0].id;
+ var item = Zotero.Items.get(itemID);
+ _addItem(item);
+ if(itemID == selectedItemID) _citationList.selectedIndex = i;
}
}
@@ -463,7 +476,7 @@ var Zotero_Citation_Dialog = new function () {
*/
function _updateAccept(status) {
if(_multipleSourcesOn) {
- _acceptButton.disabled = !document.getElementById("citation-list").childNodes.length;
+ _acceptButton.disabled = !_citationList.getRowCount();
} else {
_acceptButton.disabled = !itemsView.getSelectedItems().length; // treeview from selectItemsDialog.js
}
@@ -519,15 +532,12 @@ var Zotero_Citation_Dialog = new function () {
var locatorTypeElements = document.getElementById("label").getElementsByTagName("menuitem");
if(_multipleSourcesOn) {
_itemSelected(); // store locator info
-
- var citationList = document.getElementById("citation-list");
- var listLength = citationList.childNodes.length;
-
+ var listLength = _citationList.childNodes.length;
var citationItems = new Array();
if(listLength) {
// generate citationItems
for(var i=0; i<listLength; i++) {
- var itemID = citationList.childNodes[i].value;
+ var itemID = _citationList.childNodes[i].value;
var citationItem = _itemData[itemID];
citationItem.id = itemID;
@@ -560,24 +570,18 @@ var Zotero_Citation_Dialog = new function () {
*/
function _addItem(item) {
var itemNode = document.createElement("listitem");
- var itemList = document.getElementById("citation-list");
itemNode.setAttribute("value", item.getID());
itemNode.setAttribute("label", item.getField("title"));
itemNode.setAttribute("class", "listitem-iconic");
itemNode.setAttribute("image", item.getImageSrc());
- itemList.appendChild(itemNode);
- itemList.focus();
- itemList.selectedIndex = itemList.childNodes.length-1;
- _configListPosition(itemList, false);
+ _citationList.appendChild(itemNode);
+ _configListPosition(false);
}
/*
* Removes all items from the multiple sources list
*/
function _clearCitationList() {
- var citationList = document.getElementById("citation-list");
- while(citationList.firstChild) {
- citationList.removeChild(citationList.firstChild);
- }
+ while(_citationList.firstChild) _citationList.removeChild(_citationList.firstChild);
}
}
\ No newline at end of file
diff --git a/chrome/content/zotero/xpcom/citeproc.js b/chrome/content/zotero/xpcom/citeproc.js
@@ -1432,7 +1432,7 @@ CSL.dateParser = function (txt) {
};
CSL.Engine = function (sys, style, lang, xmlmode) {
var attrs, langspec, localexml, locale;
- this.processor_version = "1.0.21";
+ this.processor_version = "1.0.22";
this.csl_version = "1.0";
this.sys = sys;
this.sys.xml = new CSL.System.Xml.Parsing();
@@ -2229,7 +2229,9 @@ CSL.Engine.prototype.processCitationCluster = function (citation, citationsPre,
for (pos = 0; pos < len; pos += 1) {
sortedItems[pos][1].sortkeys = CSL.getSortKeys.call(this, sortedItems[pos][0], "citation_sort");
}
- sortedItems.sort(this.citation.srt.compareCompositeKeys);
+ if (!citation.properties.unsorted) {
+ sortedItems.sort(this.citation.srt.compareCompositeKeys);
+ }
}
citation.sortedItems = sortedItems;
citationByIndex = [];
@@ -2390,7 +2392,9 @@ CSL.Engine.prototype.processCitationCluster = function (citation, citationsPre,
for (pos = 0; pos < len; pos += 1) {
sortedItems[pos][1].sortkeys = CSL.getSortKeys.call(this, sortedItems[pos][0], "citation_sort");
}
- sortedItems.sort(this.citation.srt.compareCompositeKeys);
+ if (!citation.properties.unsorted) {
+ sortedItems.sort(this.citation.srt.compareCompositeKeys);
+ }
}
for (key in this.tmp.taintedItemIDs) {
if (this.tmp.taintedItemIDs.hasOwnProperty(key)) {
diff --git a/chrome/content/zotero/xpcom/integration.js b/chrome/content/zotero/xpcom/integration.js
@@ -984,7 +984,7 @@ Zotero.Integration.Session.prototype.reselectItem = function(exception) {
* Generates a field from a citation object
*/
Zotero.Integration.Session._acceptableTypes = ["string", "boolean", "number"];
-Zotero.Integration.Session._saveProperties = ["custom", "sort"];
+Zotero.Integration.Session._saveProperties = ["custom", "unsorted"];
Zotero.Integration.Session._saveItems = ["locator", "label", "suppress-author", "author-only", "prefix", "suffix"];
Zotero.Integration.Session.prototype.getCitationField = function(citation) {
var type;
@@ -1148,36 +1148,31 @@ Zotero.Integration.Session.prototype.addCitation = function(index, noteIndex, ar
*/
Zotero.Integration.Session.prototype.unserializeCitation = function(arg, index) {
if(arg[0] == "{") { // JSON field
- // create citation
- var citation = {};
-
- var saveCode = true;
// fix for corrupted fields
var lastBracket = arg.lastIndexOf("}");
if(lastBracket+1 != arg.length) {
- saveCode = false;
this.updateIndices[index] = true;
arg = arg.substr(0, lastBracket+1);
}
// get JSON
try {
- var object = Zotero.JSON.unserialize(arg);
+ var citation = Zotero.JSON.unserialize(arg);
} catch(e) {
+ // fix for corrupted fields
try {
- var object = Zotero.JSON.unserialize(arg.substr(0, arg.length-1));
+ var citation = Zotero.JSON.unserialize(arg.substr(0, arg.length-1));
} catch(e) {
throw new Zotero.Integration.CorruptFieldException(arg);
}
}
// fix for uppercase citation codes
- if(object.CITATIONITEMS) {
- saveCode = false;
+ if(citation.CITATIONITEMS) {
this.updateIndices[index] = true;
- object.citationItems = [];
- for (var i=0; i<object.CITATIONITEMS.length; i++) {
- for (var j in object.CITATIONITEMS[i]) {
+ citation.citationItems = [];
+ for (var i=0; i<citation.CITATIONITEMS.length; i++) {
+ for (var j in citation.CITATIONITEMS[i]) {
switch (j) {
case 'ITEMID':
var field = 'itemID';
@@ -1187,32 +1182,33 @@ Zotero.Integration.Session.prototype.unserializeCitation = function(arg, index)
default:
var field = j.toLowerCase();
}
- if (!object.citationItems[i]) {
- object.citationItems[i] = {};
+ if (!citation.citationItems[i]) {
+ citation.citationItems[i] = {};
}
- object.citationItems[i][field] = object.CITATIONITEMS[i][j];
+ citation.citationItems[i][field] = citation.CITATIONITEMS[i][j];
}
}
}
if(!citation.properties) citation.properties = {};
- // copy properties
- for(var i in object) {
- if(Zotero.Integration.Session._saveProperties.indexOf(i) != -1) {
- citation.properties[i] = object[i];
- } else if(i == "locatorType") {
- citation["label"] = object["locatorType"];
- this.updateIndices[index] = true;
- } else if(i == "suppressAuthor") {
- citation["suppress-author"] = object["suppressAuthor"];
- this.updateIndices[index] = true;
- } else {
- citation[i] = object[i];
+
+ // for upgrade from Zotero 2.0 or earlier
+ for each(var citationItem in citation.citationItems) {
+ if(citationItem.locatorType) {
+ citationItem.label = citationItem.locatorType;
+ delete citationItem.locatorType;
+ } else if(citationItem.suppressAuthor) {
+ citationItem["suppress-author"] = citationItem["suppressAuthor"];
+ delete citationItem.suppressAuthor;
}
}
+ if(citation.sort) {
+ citation.properties.unsorted = !citation.sort;
+ delete citation.sort;
+ }
if(!citation.citationID) citation.citationID = Zotero.randomString();
- if(saveCode) citation.properties.field = arg;
+ citation.properties.field = arg;
} else { // ye olde style field
var underscoreIndex = arg.indexOf("_");
var itemIDs = arg.substr(0, underscoreIndex).split("|");
@@ -1547,6 +1543,8 @@ Zotero.Integration.Session.prototype.editCitation = function(index, noteIndex, c
io.previewFunction = function() {
return me.previewCitation(io.citation);
}
+ // determine whether citation is sortable in current style
+ io.sortable = !this.style.citation.opt["citation-number-sort"] && this.style.citation_sort.tokens.length > 0;
var window = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
.getService(Components.interfaces.nsIWindowWatcher)