preferences_export.js (11310B)
1 /* 2 ***** BEGIN LICENSE BLOCK ***** 3 4 Copyright © 2006–2013 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 "use strict"; 27 28 Zotero_Preferences.Export = { 29 init: Zotero.Promise.coroutine(function* () { 30 this.updateQuickCopyInstructions(); 31 yield this.populateQuickCopyList(); 32 33 var charsetMenu = document.getElementById("zotero-import-charsetMenu"); 34 var charsetMap = Zotero_Charset_Menu.populate(charsetMenu, false); 35 charsetMenu.selectedItem = 36 charsetMap[Zotero.Prefs.get("import.charset")] ? 37 charsetMap[Zotero.Prefs.get("import.charset")] : charsetMap["auto"]; 38 }), 39 40 41 getQuickCopyTranslators: async function () { 42 var translation = new Zotero.Translate("export"); 43 var translators = await translation.getTranslators(); 44 translators.sort((a, b) => { 45 var collation = Zotero.getLocaleCollation(); 46 return collation.compareString(1, a.label, b.label); 47 }); 48 return translators; 49 }, 50 51 52 /* 53 * Builds the main Quick Copy drop-down from the current global pref 54 */ 55 populateQuickCopyList: Zotero.Promise.coroutine(function* () { 56 // Initialize default format drop-down 57 var format = Zotero.Prefs.get("export.quickCopy.setting"); 58 format = Zotero.QuickCopy.unserializeSetting(format); 59 var menulist = document.getElementById("zotero-quickCopy-menu"); 60 yield Zotero.Styles.init(); 61 var translators = yield this.getQuickCopyTranslators(); 62 this.buildQuickCopyFormatDropDown( 63 menulist, format.contentType, format, translators 64 ); 65 menulist.setAttribute('preference', "pref-quickCopy-setting"); 66 67 // Initialize locale drop-down 68 var localeMenulist = document.getElementById("zotero-quickCopy-locale-menu"); 69 Zotero.Styles.populateLocaleList(localeMenulist); 70 localeMenulist.setAttribute('preference', "pref-quickCopy-locale"); 71 72 this._lastSelectedLocale = Zotero.Prefs.get("export.quickCopy.locale"); 73 this.updateQuickCopyUI(); 74 75 yield this.refreshQuickCopySiteList(); 76 }), 77 78 79 /* 80 * Builds a Quick Copy drop-down 81 */ 82 buildQuickCopyFormatDropDown: function (menulist, contentType, format, translators) { 83 if (!format) { 84 format = menulist.value; 85 } 86 87 format = Zotero.QuickCopy.unserializeSetting(format); 88 89 menulist.selectedItem = null; 90 menulist.removeAllItems(); 91 92 // Prevent Cmd-w from setting "Wikipedia" 93 menulist.onkeydown = function (event) { 94 if ((Zotero.isMac && event.metaKey) || event.ctrlKey) { 95 event.preventDefault(); 96 } 97 } 98 99 var popup = document.createElement('menupopup'); 100 menulist.appendChild(popup); 101 102 var itemNode = document.createElement("menuitem"); 103 itemNode.setAttribute("label", Zotero.getString('zotero.preferences.export.quickCopy.citationStyles')); 104 itemNode.setAttribute("disabled", true); 105 popup.appendChild(itemNode); 106 107 // add styles to list 108 var styles = Zotero.Styles.getVisible(); 109 styles.forEach(function (style) { 110 var val = 'bibliography' + (contentType == 'html' ? '/html' : '') + '=' + style.styleID; 111 var itemNode = document.createElement("menuitem"); 112 itemNode.setAttribute("value", val); 113 itemNode.setAttribute("label", style.title); 114 itemNode.setAttribute("oncommand", 'Zotero_Preferences.Export.updateQuickCopyUI()'); 115 popup.appendChild(itemNode); 116 117 if (format.mode == 'bibliography' && format.id == style.styleID) { 118 menulist.selectedItem = itemNode; 119 } 120 }); 121 122 var itemNode = document.createElement("menuitem"); 123 itemNode.setAttribute("label", Zotero.getString('zotero.preferences.export.quickCopy.exportFormats')); 124 itemNode.setAttribute("disabled", true); 125 popup.appendChild(itemNode); 126 127 // add export formats to list 128 translators.sort((a, b) => a.label.localeCompare(b.label)) 129 translators.forEach(function (translator) { 130 // Skip RDF formats 131 switch (translator.translatorID) { 132 case '6e372642-ed9d-4934-b5d1-c11ac758ebb7': 133 case '14763d24-8ba0-45df-8f52-b8d1108e7ac9': 134 return; 135 } 136 var val = 'export=' + translator.translatorID; 137 var itemNode = document.createElement("menuitem"); 138 itemNode.setAttribute("value", val); 139 itemNode.setAttribute("label", translator.label); 140 itemNode.setAttribute("oncommand", 'Zotero_Preferences.Export.updateQuickCopyUI()'); 141 popup.appendChild(itemNode); 142 143 if (format.mode == 'export' && format.id == translator.translatorID) { 144 menulist.selectedItem = itemNode; 145 } 146 }); 147 148 menulist.click(); 149 }, 150 151 152 onCopyAsHTMLChange: async function (checked) { 153 var menulist = document.getElementById('zotero-quickCopy-menu'); 154 var translators = await this.getQuickCopyTranslators(); 155 this.buildQuickCopyFormatDropDown(menulist, checked ? 'html' : '', null, translators); 156 }, 157 158 159 updateQuickCopyUI: function () { 160 var format = document.getElementById('zotero-quickCopy-menu').value; 161 162 var mode, contentType; 163 164 [mode, format] = format.split('='); 165 [mode, contentType] = mode.split('/'); 166 167 var checkbox = document.getElementById('zotero-quickCopy-copyAsHTML'); 168 checkbox.checked = contentType == 'html'; 169 checkbox.disabled = mode != 'bibliography'; 170 171 Zotero.Styles.updateLocaleList( 172 document.getElementById('zotero-quickCopy-locale-menu'), 173 mode == 'bibliography' ? Zotero.Styles.get(format) : null, 174 this._lastSelectedLocale 175 ); 176 }, 177 178 /** 179 * Disables UI buttons when no site-specific quick copy entries are selected 180 */ 181 disableQuickCopySiteButtons: function () { 182 document.getElementById('quickCopy-edit').disabled = true; 183 document.getElementById('quickCopy-delete').disabled = true; 184 }, 185 186 /** 187 * Enables UI buttons when a site-specific quick copy entry is selected 188 */ 189 enableQuickCopySiteButtons: function () { 190 document.getElementById('quickCopy-edit').disabled = false; 191 document.getElementById('quickCopy-delete').disabled = false; 192 }, 193 194 showQuickCopySiteEditor: Zotero.Promise.coroutine(function* (index) { 195 var treechildren = document.getElementById('quickCopy-siteSettings-rows'); 196 197 var formattedName = document.getElementById('zotero-quickCopy-menu').label; 198 var locale = this._lastSelectedLocale; 199 var asHTML = document.getElementById('zotero-quickCopy-copyAsHTML').checked; 200 201 if (index !== undefined && index > -1 && index < treechildren.childNodes.length) { 202 var treerow = treechildren.childNodes[index].firstChild; 203 var domain = treerow.childNodes[0].getAttribute('label'); 204 formattedName = treerow.childNodes[1].getAttribute('label'); 205 locale = treerow.childNodes[2].getAttribute('label'); 206 asHTML = treerow.childNodes[3].getAttribute('label') !== ''; 207 } 208 209 var format = yield Zotero.QuickCopy.getSettingFromFormattedName(formattedName); 210 if (asHTML) { 211 format = format.replace('bibliography=', 'bibliography/html='); 212 } 213 214 var styles = Zotero.Styles.getVisible(); 215 var translation = new Zotero.Translate("export"); 216 var translators = yield translation.getTranslators(); 217 218 var io = { domain, format, locale, asHTML, ok: false, styles, translators }; 219 window.openDialog('chrome://zotero/content/preferences/quickCopySiteEditor.xul', 220 "zotero-preferences-quickCopySiteEditor", "chrome,modal,centerscreen", io); 221 222 if (!io.ok) { 223 return; 224 } 225 226 if (domain && domain != io.domain) { 227 yield Zotero.DB.queryAsync("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domain]); 228 } 229 230 var quickCopysetting = Zotero.QuickCopy.unserializeSetting(io.format); 231 quickCopysetting.locale = io.locale; 232 233 yield Zotero.DB.queryAsync("REPLACE INTO settings VALUES ('quickCopySite', ?, ?)", [io.domain, JSON.stringify(quickCopysetting)]); 234 235 yield Zotero.QuickCopy.loadSiteSettings(); 236 237 yield this.refreshQuickCopySiteList(); 238 }), 239 240 241 refreshQuickCopySiteList: Zotero.Promise.coroutine(function* () { 242 var treechildren = document.getElementById('quickCopy-siteSettings-rows'); 243 while (treechildren.hasChildNodes()) { 244 treechildren.removeChild(treechildren.firstChild); 245 } 246 247 var sql = "SELECT key AS domainPath, value AS format FROM settings " 248 + "WHERE setting='quickCopySite' ORDER BY domainPath COLLATE NOCASE"; 249 var siteData = yield Zotero.DB.queryAsync(sql); 250 251 for (var i=0; i<siteData.length; i++) { 252 var treeitem = document.createElement('treeitem'); 253 var treerow = document.createElement('treerow'); 254 var domainCell = document.createElement('treecell'); 255 var formatCell = document.createElement('treecell'); 256 var localeCell = document.createElement('treecell'); 257 var htmlCell = document.createElement('treecell'); 258 259 domainCell.setAttribute('label', siteData[i].domainPath); 260 261 var formattedName = yield Zotero.QuickCopy.getFormattedNameFromSetting(siteData[i].format); 262 formatCell.setAttribute('label', formattedName); 263 264 var format = Zotero.QuickCopy.unserializeSetting(siteData[i].format); 265 localeCell.setAttribute('label', format.locale); 266 htmlCell.setAttribute('label', format.contentType == 'html' ? ' ✓ ' : ''); 267 268 treerow.appendChild(domainCell); 269 treerow.appendChild(formatCell); 270 treerow.appendChild(localeCell); 271 treerow.appendChild(htmlCell); 272 treeitem.appendChild(treerow); 273 treechildren.appendChild(treeitem); 274 } 275 276 this.disableQuickCopySiteButtons(); 277 }), 278 279 280 deleteSelectedQuickCopySite: Zotero.Promise.coroutine(function* () { 281 var tree = document.getElementById('quickCopy-siteSettings'); 282 var treeitem = tree.lastChild.childNodes[tree.currentIndex]; 283 var domainPath = treeitem.firstChild.firstChild.getAttribute('label'); 284 yield Zotero.DB.queryAsync("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domainPath]); 285 yield Zotero.QuickCopy.loadSiteSettings(); 286 yield this.refreshQuickCopySiteList(); 287 }), 288 289 290 updateQuickCopyInstructions: function () { 291 var prefix = Zotero.isMac ? Zotero.getString('general.keys.cmdShift') : Zotero.getString('general.keys.ctrlShift'); 292 293 var key = Zotero.Prefs.get('keys.copySelectedItemsToClipboard'); 294 var str = Zotero.getString('zotero.preferences.export.quickCopy.instructions', prefix + key); 295 var instr = document.getElementById('quickCopy-instructions'); 296 while (instr.hasChildNodes()) { 297 instr.removeChild(instr.firstChild); 298 } 299 instr.appendChild(document.createTextNode(str)); 300 301 key = Zotero.Prefs.get('keys.copySelectedItemCitationsToClipboard'); 302 str = Zotero.getString('zotero.preferences.export.quickCopy.citationInstructions', prefix + key); 303 instr = document.getElementById('quickCopy-citationInstructions'); 304 while (instr.hasChildNodes()) { 305 instr.removeChild(instr.firstChild); 306 } 307 instr.appendChild(document.createTextNode(str)); 308 } 309 };