Put JavaScript code from Style Editor and Preview into separate files

This commit is contained in:
rmzelle 2015-01-14 21:13:51 -05:00
parent 6fd3b298de
commit eec1ba4c73
4 changed files with 331 additions and 288 deletions

View file

@ -0,0 +1,222 @@
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2009 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
var Zotero_CSL_Editor = new function() {
this.init = init;
this.handleKeyPress = handleKeyPress;
this.loadCSL = loadCSL;
this.generateBibliography = generateBibliography;
this.refresh = refresh;
function init() {
var cslList = document.getElementById('zotero-csl-list');
if (cslList.getAttribute('initialized') == 'true') {
if (currentStyle) {
loadCSL(currentStyle);
refresh();
}
return;
}
var rawDefaultStyle = Zotero.Prefs.get('export.quickCopy.setting');
var defaultStyle = Zotero.QuickCopy.stripContentType(rawDefaultStyle);
var styles = Zotero.Styles.getAll();
var currentStyle = null;
var listPos = 0;
for each(var style in styles) {
if (style.source) {
continue;
}
var item = cslList.appendItem(style.title, style.styleID);
if (!currentStyle || defaultStyle == ('bibliography=' + style.styleID)) {
currentStyle = style.styleID;
cslList.selectedIndex = listPos;
}
listPos += 1;
}
if (currentStyle) {
loadCSL(currentStyle);
refresh();
}
var pageList = document.getElementById('zotero-csl-page-type');
var locators = Zotero.Cite.labels;
for each(var type in locators) {
var locator = type;
locator = locator[0].toUpperCase()+locator.substr(1);
pageList.appendItem(locator, type);
}
pageList.selectedIndex = 0;
cslList.setAttribute('initialized', true)
}
function refresh() {
var editor = document.getElementById('zotero-csl-editor')
generateBibliography(editor.value);
}
this.save = function() {
var editor = document.getElementById('zotero-csl-editor')
var style = editor.value;
const nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(window, "Save Citation Style", nsIFilePicker.modeSave);
fp.appendFilter("Citation Style Language", "*.csl");
//get the filename from the id; we could consider doing even more here like creating the id from filename.
var parser = new DOMParser();
var doc = parser.parseFromString(style, 'text/xml');
var filename = doc.getElementsByTagName("id");
if (filename) {
filename = filename[0].textContent;
fp.defaultString = filename.replace(/.+\//, "") + ".csl";
}
else {
fp.defaultString = "untitled.csl";
}
var rv = fp.show();
if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
var outputFile = fp.file;
Zotero.File.putContents(outputFile, style);
}
}
function handleKeyPress(event) {
if (event.keyCode == 9 &&
(!event.shiftKey && !event.metaKey && !event.altKey && !event.ctrlKey)) {
_insertText("\t");
event.preventDefault();
}
}
function loadCSL(cslID) {
var editor = document.getElementById('zotero-csl-editor')
var style = Zotero.Styles.get(cslID);
editor.value = Zotero.File.getContents(style.file);
editor.cslID = cslID;
editor.doCommand();
document.getElementById('zotero-csl-list').value = cslID;
}
function generateBibliography(str) {
var editor = document.getElementById('zotero-csl-editor')
var iframe = document.getElementById('zotero-csl-preview-box');
var items = Zotero.getActiveZoteroPane().getSelectedItems();
if (items.length == 0) {
iframe.contentDocument.documentElement.innerHTML = '<html><head><title></title></head><body><p style="color: red">No references selected in Zotero.</p></body></html>';
return;
}
var styleObject, styleEngine;
if (str.indexOf("<defaults") != -1) {
iframe.contentDocument.documentElement.innerHTML =
'<div>'
+ "Old-style CSLs are no longer supported."
+ '</div>';
return;
}
else {
try {
styleObject = new Zotero.Style(str);
styleEngine = styleObject.getCiteProc();
} catch(e) {
iframe.contentDocument.documentElement.innerHTML = '<div>Error parsing '+
'style: </div><div>'+e+'</div>';
throw e;
}
}
var itemIds = [items[i].id for (i in items)];
styleEngine.updateItems(itemIds);
// Generate multiple citations
var citation = {};
citation.citationItems = [];
citation.properties = {};
citation.properties.noteIndex = 1;
for (var i = 0, ilen = items.length; i < ilen; i += 1) {
citation.citationItems.push({id:itemIds[i]});
}
// Generate single citations
var author = document.getElementById("preview-suppress-author").checked;
var search = document.getElementById('preview-pages');
var loc = document.getElementById('zotero-csl-page-type');
var pos = document.getElementById('zotero-ref-position').selectedItem.value;
var citations = '<h1>Single Citations</h1>';
for (var i=0; i<citation.citationItems.length; i++) {
citation.citationItems[i]['suppress-author'] = author;
if (search.value != '') {
citation.citationItems[i].locator = search.value;
citation.citationItems[i].label = loc.selectedItem.value;
}
if (pos == 4) {
//near note is a subsequent citation with near note set to true;
citation.citationItems[i].position = 1;
citation.citationItems[i]["near-note"] = true;
}
else {
citation.citationItems[i].position = parseInt(pos, 10);
}
var subcitation = [citation.citationItems[i]];
citations += styleEngine.makeCitationCluster(subcitation) + '<br />';
}
try {
var multCitations = '<hr><h1>Multi Citations <span style="font-size:smaller;">(all with position "first")</span></h1>' +
styleEngine.previewCitationCluster(citation, [], [], "html");
// Generate bibliography
styleEngine.updateItems(itemIds);
var bibliography = '<hr/><h1>Bibliography</h1>' +
Zotero.Cite.makeFormattedBibliography(styleEngine, "html");
iframe.contentDocument.documentElement.innerHTML =
'<div style="white-space: pre-wrap">'
+ citations + multCitations + bibliography
+ '</div>';
} catch(e) {
iframe.contentDocument.documentElement.innerHTML = '<div>Error generating citations '+
'and bibliography: </div><div>'+e+'</div>';
throw e;
}
}
// From http://kb.mozillazine.org/Inserting_text_at_cursor
function _insertText(text) {
var command = "cmd_insertText";
var controller = document.commandDispatcher.getControllerForCommand(command);
if (controller && controller.isCommandEnabled(command)) {
controller = controller.QueryInterface(Components.interfaces.nsICommandController);
var params = Components.classes["@mozilla.org/embedcomp/command-params;1"];
params = params.createInstance(Components.interfaces.nsICommandParams);
params.setStringValue("state_data", "\t");
controller.doCommandWithParams(command, params);
}
}
}

View file

@ -33,208 +33,7 @@
title="Zotero Style Editor"> title="Zotero Style Editor">
<script src="chrome://zotero/content/include.js"/> <script src="chrome://zotero/content/include.js"/>
<script src="csledit.js"/>
<script>
<![CDATA[
var Zotero_CSL_Editor = new function() {
this.init = init;
this.handleKeyPress = handleKeyPress;
this.loadCSL = loadCSL;
this.generateBibliography = generateBibliography;
this.refresh = refresh;
function init() {
var cslList = document.getElementById('zotero-csl-list');
if (cslList.getAttribute('initialized') == 'true') {
if (currentStyle) {
loadCSL(currentStyle);
refresh();
}
return;
}
var rawDefaultStyle = Zotero.Prefs.get('export.quickCopy.setting');
var defaultStyle = Zotero.QuickCopy.stripContentType(rawDefaultStyle);
var styles = Zotero.Styles.getAll();
var currentStyle = null;
var listPos = 0;
for each(var style in styles) {
if (style.source) {
continue;
}
var item = cslList.appendItem(style.title, style.styleID);
if (!currentStyle || defaultStyle == ('bibliography=' + style.styleID)) {
currentStyle = style.styleID;
cslList.selectedIndex = listPos;
}
listPos += 1;
}
if (currentStyle) {
loadCSL(currentStyle);
refresh();
}
var pageList = document.getElementById('zotero-csl-page-type');
var locators = Zotero.Cite.labels;
for each(var type in locators) {
var locator = type;
locator = locator[0].toUpperCase()+locator.substr(1);
pageList.appendItem(locator, type);
}
pageList.selectedIndex = 0;
cslList.setAttribute('initialized', true)
}
function refresh() {
var editor = document.getElementById('zotero-csl-editor')
generateBibliography(editor.value);
}
this.save = function() {
var editor = document.getElementById('zotero-csl-editor')
var style = editor.value;
const nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(window, "Save Citation Style", nsIFilePicker.modeSave);
fp.appendFilter("Citation Style Language", "*.csl");
//get the filename from the id; we could consider doing even more here like creating the id from filename.
var parser = new DOMParser();
var doc = parser.parseFromString(style, 'text/xml');
var filename = doc.getElementsByTagName("id");
if (filename) {
filename = filename[0].textContent;
fp.defaultString = filename.replace(/.+\//, "") + ".csl";
}
else {
fp.defaultString = "untitled.csl";
}
var rv = fp.show();
if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
var outputFile = fp.file;
Zotero.File.putContents(outputFile, style);
}
}
function handleKeyPress(event) {
if (event.keyCode == 9 &&
(!event.shiftKey && !event.metaKey && !event.altKey && !event.ctrlKey)) {
_insertText("\t");
event.preventDefault();
}
}
function loadCSL(cslID) {
var editor = document.getElementById('zotero-csl-editor')
var style = Zotero.Styles.get(cslID);
editor.value = Zotero.File.getContents(style.file);
editor.cslID = cslID;
editor.doCommand();
document.getElementById('zotero-csl-list').value = cslID;
}
function generateBibliography(str) {
var editor = document.getElementById('zotero-csl-editor')
var iframe = document.getElementById('zotero-csl-preview-box');
var items = Zotero.getActiveZoteroPane().getSelectedItems();
if (items.length == 0) {
iframe.contentDocument.documentElement.innerHTML = '<html><head><title></title></head><body><p style="color: red">No references selected in Zotero.</p></body></html>';
return;
}
var styleObject, styleEngine;
if (str.indexOf("<defaults") != -1) {
iframe.contentDocument.documentElement.innerHTML =
'<div>'
+ "Old-style CSLs are no longer supported."
+ '</div>';
return;
}
else {
try {
styleObject = new Zotero.Style(str);
styleEngine = styleObject.getCiteProc();
} catch(e) {
iframe.contentDocument.documentElement.innerHTML = '<div>Error parsing '+
'style: </div><div>'+e+'</div>';
throw e;
}
}
var itemIds = [items[i].id for (i in items)];
styleEngine.updateItems(itemIds);
// Generate multiple citations
var citation = {};
citation.citationItems = [];
citation.properties = {};
citation.properties.noteIndex = 1;
for (var i = 0, ilen = items.length; i < ilen; i += 1) {
citation.citationItems.push({id:itemIds[i]});
}
// Generate single citations
var author = document.getElementById("preview-suppress-author").checked;
var search = document.getElementById('preview-pages');
var loc = document.getElementById('zotero-csl-page-type');
var pos = document.getElementById('zotero-ref-position').selectedItem.value;
var citations = '<h1>Single Citations</h1>';
for (var i=0; i<citation.citationItems.length; i++) {
citation.citationItems[i]['suppress-author'] = author;
if (search.value != '') {
citation.citationItems[i].locator = search.value;
citation.citationItems[i].label = loc.selectedItem.value;
}
if (pos == 4) {
//near note is a subsequent citation with near note set to true;
citation.citationItems[i].position = 1;
citation.citationItems[i]["near-note"] = true;
}
else {
citation.citationItems[i].position = parseInt(pos, 10);
}
var subcitation = [citation.citationItems[i]];
citations += styleEngine.makeCitationCluster(subcitation) + '<br />';
}
try {
var multCitations = '<hr><h1>Multi Citations <span style="font-size:smaller;">(all with position "first")</span></h1>' +
styleEngine.previewCitationCluster(citation, [], [], "html");
// Generate bibliography
styleEngine.updateItems(itemIds);
var bibliography = '<hr/><h1>Bibliography</h1>' +
Zotero.Cite.makeFormattedBibliography(styleEngine, "html");
iframe.contentDocument.documentElement.innerHTML =
'<div style="white-space: pre-wrap">'
+ citations + multCitations + bibliography
+ '</div>';
} catch(e) {
iframe.contentDocument.documentElement.innerHTML = '<div>Error generating citations '+
'and bibliography: </div><div>'+e+'</div>';
throw e;
}
}
// From http://kb.mozillazine.org/Inserting_text_at_cursor
function _insertText(text) {
var command = "cmd_insertText";
var controller = document.commandDispatcher.getControllerForCommand(command);
if (controller && controller.isCommandEnabled(command)) {
controller = controller.QueryInterface(Components.interfaces.nsICommandController);
var params = Components.classes["@mozilla.org/embedcomp/command-params;1"];
params = params.createInstance(Components.interfaces.nsICommandParams);
params.setStringValue("state_data", "\t");
controller.doCommandWithParams(command, params);
}
}
}
]]>
</script>
<vbox flex="1"> <vbox flex="1">
<hbox align="center"> <hbox align="center">

View file

@ -0,0 +1,107 @@
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2009 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
Contributed by Julian Onions
*/
var Zotero_CSL_Preview = new function() {
this.init = init;
this.refresh = refresh;
this.generateBibliography = generateBibliography;
function init() {
//refresh();
var iframe = document.getElementById('zotero-csl-preview-box');
iframe.contentDocument.documentElement.innerHTML = '<html><head><title></title></head><body><p>' + Zotero.getString('cslpreview.instruction') + '</p></body></html>';
}
function refresh() {
var iframe = document.getElementById('zotero-csl-preview-box');
var items = Zotero.getActiveZoteroPane().getSelectedItems();
if (items.length === 0) {
iframe.contentDocument.documentElement.innerHTML = '<html><head><title></title></head><body><p style="color: red">' + Zotero.getString('cslpreview.warning.noItems') + '</p></body></html>';
return;
}
var progressWin = new Zotero.ProgressWindow();
// XXX needs its own string really!
progressWin.changeHeadline(Zotero.getString("pane.items.menu.createBib.multiple"));
var icon = 'chrome://zotero/skin/treeitem-attachment-file.png';
progressWin.addLines(document.title, icon);
progressWin.show();
progressWin.startCloseTimer();
var f = function() {
var styles = Zotero.Styles.getAll();
// XXX needs its own string really for the title!
var str = '<html><head><title></title></head><body>';
for each(var style in styles) {
if (style.source) {
continue;
}
Zotero.debug("Generate Bib for " + style.title);
var cite = generateBibliography(style);
if (cite) {
str += '<h3>' + style.title + '</h3>';
str += cite;
str += '<hr>';
}
}
str += '</body></html>';
iframe.contentDocument.documentElement.innerHTML = str;
};
// Give progress window time to appear
setTimeout(f, 100);
}
function generateBibliography(style) {
var iframe = document.getElementById('zotero-csl-preview-box');
var items = Zotero.getActiveZoteroPane().getSelectedItems();
if (items.length === 0) {
return '';
}
var citationFormat = document.getElementById("citation-format").selectedItem.value;
if (citationFormat != "all" && citationFormat != style.categories) {
Zotero.debug("CSL IGNORE: citation format is " + style.categories);
return '';
}
var styleEngine = style.getCiteProc();
// Generate multiple citations
var citations = styleEngine.previewCitationCluster(
{"citationItems":[{"id":item.id} for each(item in items)], "properties":{}},
[], [], "html");
// Generate bibliography
var bibliography = '';
if(style.hasBibliography) {
styleEngine.updateItems([item.id for each(item in items)]);
bibliography = Zotero.Cite.makeFormattedBibliography(styleEngine, "html");
}
return '<p>' + citations + '</p>' + bibliography;
}
}();

View file

@ -36,92 +36,7 @@
title="&zotero.cslpreview;"> title="&zotero.cslpreview;">
<script src="chrome://zotero/content/include.js"/> <script src="chrome://zotero/content/include.js"/>
<script src="cslpreview.js"/>
<script>
<![CDATA[
var Zotero_CSL_Preview = new function() {
this.init = init;
this.refresh = refresh;
this.generateBibliography = generateBibliography;
function init() {
//refresh();
var iframe = document.getElementById('zotero-csl-preview-box');
iframe.contentDocument.documentElement.innerHTML = '<html><head><title></title></head><body><p>' + Zotero.getString('cslpreview.instruction') + '</p></body></html>';
}
function refresh() {
var iframe = document.getElementById('zotero-csl-preview-box');
var items = Zotero.getActiveZoteroPane().getSelectedItems();
if (items.length === 0) {
iframe.contentDocument.documentElement.innerHTML = '<html><head><title></title></head><body><p style="color: red">' + Zotero.getString('cslpreview.warning.noItems') + '</p></body></html>';
return;
}
var progressWin = new Zotero.ProgressWindow();
// XXX needs its own string really!
progressWin.changeHeadline(Zotero.getString("pane.items.menu.createBib.multiple"));
var icon = 'chrome://zotero/skin/treeitem-attachment-file.png';
progressWin.addLines(document.title, icon);
progressWin.show();
progressWin.startCloseTimer();
var f = function() {
var styles = Zotero.Styles.getAll();
// XXX needs its own string really for the title!
var str = '<html><head><title></title></head><body>';
for each(var style in styles) {
if (style.source) {
continue;
}
Zotero.debug("Generate Bib for " + style.title);
var cite = generateBibliography(style);
if (cite) {
str += '<h3>' + style.title + '</h3>';
str += cite;
str += '<hr>';
}
}
str += '</body></html>';
iframe.contentDocument.documentElement.innerHTML = str;
};
// Give progress window time to appear
setTimeout(f, 100);
}
function generateBibliography(style) {
var iframe = document.getElementById('zotero-csl-preview-box');
var items = Zotero.getActiveZoteroPane().getSelectedItems();
if (items.length === 0) {
return '';
}
var citationFormat = document.getElementById("citation-format").selectedItem.value;
if (citationFormat != "all" && citationFormat != style.categories) {
Zotero.debug("CSL IGNORE: citation format is " + style.categories);
return '';
}
var styleEngine = style.getCiteProc();
// Generate multiple citations
var citations = styleEngine.previewCitationCluster(
{"citationItems":[{"id":item.id} for each(item in items)], "properties":{}},
[], [], "html");
// Generate bibliography
var bibliography = '';
if(style.hasBibliography) {
styleEngine.updateItems([item.id for each(item in items)]);
bibliography = Zotero.Cite.makeFormattedBibliography(styleEngine, "html");
}
return '<p>' + citations + '</p>' + bibliography;
}
}();
]]>
</script>
<vbox flex="1"> <vbox flex="1">
<hbox > <hbox >