cslpreview.js (3981B)
1 /* 2 ***** BEGIN LICENSE BLOCK ***** 3 4 Copyright © 2009 Center for History and New Media 5 George Mason University, Fairfax, Virginia, USA 6 http://zotero.org 7 8 This file is part of Zotero. 9 10 Zotero is free software: you can redistribute it and/or modify 11 it under the terms of the GNU Affero General Public License as published by 12 the Free Software Foundation, either version 3 of the License, or 13 (at your option) any later version. 14 15 Zotero is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU Affero General Public License for more details. 19 20 You should have received a copy of the GNU Affero General Public License 21 along with Zotero. If not, see <http://www.gnu.org/licenses/>. 22 23 ***** END LICENSE BLOCK ***** 24 Contributed by Julian Onions 25 */ 26 27 var Zotero_CSL_Preview = new function() { 28 this.init = init; 29 this.refresh = refresh; 30 this.generateBibliography = generateBibliography; 31 32 function init() { 33 var menulist = document.getElementById("locale-menu"); 34 35 Zotero.Styles.populateLocaleList(menulist); 36 menulist.value = Zotero.Prefs.get('export.lastLocale');; 37 38 var iframe = document.getElementById('zotero-csl-preview-box'); 39 iframe.contentDocument.documentElement.innerHTML = '<html><head><title></title></head><body><p>' + Zotero.getString('styles.preview.instructions') + '</p></body></html>'; 40 } 41 function refresh() { 42 var iframe = document.getElementById('zotero-csl-preview-box'); 43 var items = Zotero.getActiveZoteroPane().getSelectedItems(); 44 if (items.length === 0) { 45 iframe.contentDocument.documentElement.innerHTML = '<html><head><title></title></head><body><p style="color: red">' + Zotero.getString('styles.editor.warning.noItems') + '</p></body></html>'; 46 return; 47 } 48 var progressWin = new Zotero.ProgressWindow(); 49 // XXX needs its own string really! 50 progressWin.changeHeadline(Zotero.getString("pane.items.menu.createBib.multiple")); 51 var icon = 'chrome://zotero/skin/treeitem-attachment-file.png'; 52 progressWin.addLines(document.title, icon); 53 progressWin.show(); 54 progressWin.startCloseTimer(); 55 var f = function() { 56 var styles = Zotero.Styles.getVisible(); 57 // XXX needs its own string really for the title! 58 var str = '<html><head><title></title></head><body>'; 59 for (let style of styles) { 60 if (style.source) { 61 continue; 62 } 63 Zotero.debug("Generate Bib for " + style.title); 64 var cite = generateBibliography(style); 65 if (cite) { 66 str += '<h3>' + style.title + '</h3>'; 67 str += cite; 68 str += '<hr>'; 69 } 70 } 71 72 str += '</body></html>'; 73 iframe.contentDocument.documentElement.innerHTML = str; 74 }; 75 // Give progress window time to appear 76 setTimeout(f, 100); 77 } 78 79 function generateBibliography(style) { 80 var iframe = document.getElementById('zotero-csl-preview-box'); 81 82 var items = Zotero.getActiveZoteroPane().getSelectedItems(); 83 if (items.length === 0) { 84 return ''; 85 } 86 87 var citationFormat = document.getElementById("citation-format").selectedItem.value; 88 if (citationFormat != "all" && citationFormat != style.categories) { 89 Zotero.debug("CSL IGNORE: citation format is " + style.categories); 90 return ''; 91 } 92 93 var locale = document.getElementById("locale-menu").value; 94 var styleEngine = style.getCiteProc(locale); 95 96 // Generate multiple citations 97 var citations = styleEngine.previewCitationCluster( 98 { 99 citationItems: items.map(item => ({ id: item.id })), 100 properties: {} 101 }, 102 [], [], "html" 103 ); 104 105 // Generate bibliography 106 var bibliography = ''; 107 if(style.hasBibliography) { 108 styleEngine.updateItems(items.map(item => item.id)); 109 bibliography = Zotero.Cite.makeFormattedBibliography(styleEngine, "html"); 110 } 111 112 return '<p>' + citations + '</p>' + bibliography; 113 } 114 115 116 }();