bibliography.js (10443B)
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 */ 25 26 ////////////////////////////////////////////////////////////////////////////// 27 // 28 // Zotero_File_Interface_Bibliography 29 // 30 ////////////////////////////////////////////////////////////////////////////// 31 32 // Class to provide options for bibliography 33 // Used by rtfScan.xul, integrationDocPrefs.xul, and bibliography.xul 34 35 var Zotero_File_Interface_Bibliography = new function() { 36 var _io; 37 38 // Only changes when explicitly selected 39 var lastSelectedStyle, 40 lastSelectedLocale; 41 42 var isDocPrefs = false; 43 44 /* 45 * Initialize some variables and prepare event listeners for when chrome is done 46 * loading 47 */ 48 this.init = Zotero.Promise.coroutine(function* () { 49 // Set font size from pref 50 // Affects bibliography.xul and integrationDocPrefs.xul 51 var bibContainer = document.getElementById("zotero-bibliography-container"); 52 if(bibContainer) { 53 Zotero.setFontSize(document.getElementById("zotero-bibliography-container")); 54 } 55 56 if(window.arguments && window.arguments.length) { 57 _io = window.arguments[0]; 58 if(_io.wrappedJSObject) _io = _io.wrappedJSObject; 59 } else { 60 _io = {}; 61 } 62 63 var listbox = document.getElementById("style-listbox"); 64 65 // if no style is requested, get the last style used 66 if(!_io.style) { 67 _io.style = Zotero.Prefs.get("export.lastStyle"); 68 } 69 70 // See note in style.js 71 if (!Zotero.Styles.initialized) { 72 // Initialize styles 73 yield Zotero.Styles.init(); 74 } 75 76 // add styles to list 77 78 var styles = Zotero.Styles.getVisible(); 79 var selectIndex = null; 80 for (let i=0; i < styles.length; i++) { 81 var itemNode = document.createElement("listitem"); 82 itemNode.setAttribute("value", styles[i].styleID); 83 itemNode.setAttribute("label", styles[i].title); 84 listbox.appendChild(itemNode); 85 86 if(styles[i].styleID == _io.style) { 87 selectIndex = i; 88 } 89 } 90 91 let requestedLocale; 92 if (selectIndex === null) { 93 // Requested style not found in list, pre-select first style 94 selectIndex = 0; 95 } else { 96 requestedLocale = _io.locale; 97 } 98 99 let style = styles[selectIndex]; 100 lastSelectedLocale = Zotero.Prefs.get("export.lastLocale"); 101 if (requestedLocale && style && !style.locale) { 102 // pre-select supplied locale 103 lastSelectedLocale = requestedLocale; 104 } 105 106 // add locales to list 107 Zotero.Styles.populateLocaleList(document.getElementById("locale-menu")); 108 109 // Has to be async to work properly 110 window.setTimeout(function () { 111 listbox.ensureIndexIsVisible(selectIndex); 112 listbox.selectedIndex = selectIndex; 113 if (listbox.selectedIndex == -1) { 114 // This can happen in tests if styles aren't loaded 115 Zotero.debug("No styles to select", 2); 116 return; 117 } 118 Zotero_File_Interface_Bibliography.styleChanged(); 119 }, 0); 120 121 // ONLY FOR bibliography.xul: export options 122 if(document.getElementById("save-as-rtf")) { 123 var settings = Zotero.Prefs.get("export.bibliographySettings"); 124 try { 125 settings = JSON.parse(settings); 126 var mode = settings.mode; 127 var method = settings.method; 128 } 129 // If not JSON, assume it's the previous format-as-a-string 130 catch (e) { 131 method = settings; 132 } 133 if (!mode) mode = "bibliography"; 134 if (!method) method = "save-as-rtf"; 135 136 // restore saved bibliographic settings 137 document.getElementById('output-mode-radio').selectedItem = 138 document.getElementById(mode); 139 document.getElementById('output-method-radio').selectedItem = 140 document.getElementById(method); 141 } 142 143 // ONLY FOR integrationDocPrefs.xul: set selected endnotes/footnotes 144 isDocPrefs = !!document.getElementById("displayAs"); 145 if (isDocPrefs) { 146 if(_io.useEndnotes && _io.useEndnotes == 1) document.getElementById("displayAs").selectedIndex = 1; 147 let dialog = document.getElementById("zotero-doc-prefs-dialog"); 148 dialog.setAttribute('title', `${Zotero.clientName} - ${dialog.getAttribute('title')}`); 149 150 if (document.getElementById("formatUsing-groupbox")) { 151 if (["Field", "ReferenceMark"].includes(_io.primaryFieldType)) { 152 if(_io.fieldType == "Bookmark") document.getElementById("formatUsing").selectedIndex = 1; 153 var formatOption = (_io.primaryFieldType == "ReferenceMark" ? "referenceMarks" : "fields"); 154 document.getElementById("fields").label = 155 Zotero.getString("integration."+formatOption+".label"); 156 document.getElementById("fields-caption").textContent = 157 Zotero.getString("integration."+formatOption+".caption"); 158 document.getElementById("fields-file-format-notice").textContent = 159 Zotero.getString("integration."+formatOption+".fileFormatNotice"); 160 document.getElementById("bookmarks-file-format-notice").textContent = 161 Zotero.getString("integration.fields.fileFormatNotice"); 162 } else { 163 document.getElementById("formatUsing-groupbox").style.display = "none"; 164 _io.fieldType = _io.primaryFieldType; 165 } 166 } 167 if(document.getElementById("automaticJournalAbbreviations-checkbox")) { 168 if(_io.automaticJournalAbbreviations === undefined) { 169 _io.automaticJournalAbbreviations = Zotero.Prefs.get("cite.automaticJournalAbbreviations"); 170 } 171 if(_io.automaticJournalAbbreviations) { 172 document.getElementById("automaticJournalAbbreviations-checkbox").checked = true; 173 } 174 175 document.getElementById("automaticCitationUpdates-checkbox").checked = !_io.delayCitationUpdates; 176 } 177 } 178 179 // set style to false, in case this is cancelled 180 _io.style = false; 181 }); 182 183 this.openHelpLink = function() { 184 var url = "https://www.zotero.org/support/word_processor_plugin_usage"; 185 var win = Components.classes["@mozilla.org/appshell/window-mediator;1"] 186 .getService(Components.interfaces.nsIWindowMediator) 187 .getMostRecentWindow("navigator:browser"); 188 Zotero.launchURL(url); 189 }; 190 191 /* 192 * Called when locale is changed 193 */ 194 this.localeChanged = function (selectedValue) { 195 lastSelectedLocale = selectedValue; 196 }; 197 198 /* 199 * Called when style is changed 200 */ 201 this.styleChanged = function () { 202 var selectedItem = document.getElementById("style-listbox").selectedItem; 203 lastSelectedStyle = selectedItem.getAttribute('value'); 204 var selectedStyleObj = Zotero.Styles.get(lastSelectedStyle); 205 206 updateLocaleMenu(selectedStyleObj); 207 208 // 209 // For integrationDocPrefs.xul 210 // 211 if (isDocPrefs) { 212 // update status of displayAs box based on style class 213 var isNote = selectedStyleObj.class == "note"; 214 var multipleNotesSupported = _io.supportedNotes.length > 1; 215 document.getElementById("displayAs-groupbox").hidden = !isNote || !multipleNotesSupported; 216 217 // update status of formatUsing box based on style class 218 if(isNote) document.getElementById("formatUsing").selectedIndex = 0; 219 document.getElementById("bookmarks").disabled = isNote; 220 document.getElementById("bookmarks-caption").disabled = isNote; 221 222 // update status of displayAs box based on style class 223 document.getElementById("automaticJournalAbbreviations-vbox").hidden = 224 !selectedStyleObj.usesAbbreviation; 225 } 226 227 // 228 // For bibliography.xul 229 // 230 231 // Change label to "Citation" or "Note" depending on style class 232 if(document.getElementById("citations")) { 233 let label = ""; 234 if(Zotero.Styles.get(lastSelectedStyle).class == "note") { 235 label = Zotero.getString('citation.notes'); 236 } else { 237 label = Zotero.getString('citation.citations'); 238 } 239 document.getElementById("citations").label = label; 240 } 241 242 window.sizeToContent(); 243 }; 244 245 /* 246 * Update locale menulist when style is changed 247 */ 248 function updateLocaleMenu(selectedStyle) { 249 Zotero.Styles.updateLocaleList( 250 document.getElementById("locale-menu"), 251 selectedStyle, 252 lastSelectedLocale 253 ); 254 } 255 256 this.acceptSelection = function () { 257 // collect code 258 _io.style = document.getElementById("style-listbox").value; 259 260 let localeMenu = document.getElementById("locale-menu"); 261 _io.locale = localeMenu.disabled ? undefined : localeMenu.value; 262 263 if(document.getElementById("output-method-radio")) { 264 // collect settings 265 _io.mode = document.getElementById("output-mode-radio").selectedItem.id; 266 _io.method = document.getElementById("output-method-radio").selectedItem.id; 267 // save settings 268 Zotero.Prefs.set("export.bibliographySettings", 269 JSON.stringify({ mode: _io.mode, method: _io.method })); 270 } 271 272 // ONLY FOR integrationDocPrefs.xul: 273 if(isDocPrefs) { 274 var automaticJournalAbbreviationsEl = document.getElementById("automaticJournalAbbreviations-checkbox"); 275 _io.automaticJournalAbbreviations = automaticJournalAbbreviationsEl.checked; 276 if(!automaticJournalAbbreviationsEl.hidden && lastSelectedStyle) { 277 Zotero.Prefs.set("cite.automaticJournalAbbreviations", _io.automaticJournalAbbreviations); 278 } 279 _io.useEndnotes = document.getElementById("displayAs").selectedIndex; 280 _io.fieldType = (document.getElementById("formatUsing").selectedIndex == 0 ? _io.primaryFieldType : _io.secondaryFieldType); 281 _io.delayCitationUpdates = !document.getElementById("automaticCitationUpdates-checkbox").checked; 282 } 283 284 // remember style and locale if user selected these explicitly 285 if(lastSelectedStyle) { 286 Zotero.Prefs.set("export.lastStyle", _io.style); 287 } 288 289 if (lastSelectedLocale) { 290 Zotero.Prefs.set("export.lastLocale", lastSelectedLocale); 291 } 292 }; 293 294 295 this.manageStyles = function () { 296 document.documentElement.getButton('cancel').click(); 297 var win = Zotero.Utilities.Internal.openPreferences('zotero-prefpane-cite', { tab: 'styles-tab' }); 298 if (isDocPrefs) { 299 Zotero.Utilities.Internal.activate(win); 300 } 301 }; 302 }