charsetMenu.js (3919B)
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 var Zotero_Charset_Menu = new function() { 27 this.populate = populate; 28 29 /** 30 * Populate a character set menu, placing more commonly used character sets 31 * closer to the top 32 * 33 * @param {DOMElement} charsetMenu The menu to populate 34 * @param {Boolean} exportMenu Whether the menu is to be used for export 35 **/ 36 function populate(charsetMenu, exportMenu) { 37 var charsetMap = {}; 38 39 // get charset popup and charset RDF 40 var charsetPopup = document.createElement("menupopup"); 41 charsetMenu.appendChild(charsetPopup); 42 43 var charsets = []; 44 45 // Only list UTF-8 and Western for export 46 if (exportMenu) { 47 charsets.push( 48 { label: "Unicode (UTF-8)", value: "UTF-8" }, 49 { label: Zotero.getString("charset.UTF8withoutBOM"), value: "UTF-8xBOM" }, 50 { label: "Western", value: "windows-1252" } 51 ); 52 53 for (let charset of charsets) { 54 let { label, value } = charset; 55 56 let itemNode = document.createElement("menuitem"); 57 itemNode.setAttribute("label", label); 58 itemNode.setAttribute("value", value); 59 60 charsetMap[value] = itemNode; 61 charsetPopup.appendChild(itemNode); 62 } 63 } 64 else { 65 var charsetSeparator = document.createElement("menuseparator"); 66 charsetPopup.appendChild(charsetSeparator); 67 68 Components.utils.import("resource://gre/modules/CharsetMenu.jsm"); 69 var cmData = CharsetMenu.getData(); 70 for (let charsetList of [cmData.pinnedCharsets, cmData.otherCharsets]) { 71 for (let charsetInfo of charsetList) { 72 if(charsetInfo.value == "UTF-8") { 73 charsets.push({ 74 "label":"Unicode (UTF-8)", 75 "value":"UTF-8" 76 }); 77 } else { 78 charsets.push({ 79 "label":charsetInfo.label, 80 "value":charsetInfo.value 81 }); 82 } 83 } 84 } 85 charsets = charsets.concat([ 86 {"label":"UTF-16LE", "value":"UTF-16LE"}, 87 {"label":"UTF-16BE", "value":"UTF-16BE"}, 88 {"label":"Western (IBM-850)", "value":"IBM850"}, 89 {"label":"Western (MacRoman)", "value":"macintosh"} 90 ]); 91 92 for(var i=0; i<charsets.length; i++) { 93 var charset = charsets[i].value, 94 label = charsets[i].label; 95 96 // add element 97 var itemNode = document.createElement("menuitem"); 98 itemNode.setAttribute("label", label); 99 itemNode.setAttribute("value", charset); 100 101 charsetMap[charset] = itemNode; 102 if (label.length >= 7 && label.substr(0, 7) == "Western") { 103 charsetPopup.insertBefore(itemNode, charsetSeparator); 104 } else if(charset == "UTF-8") { 105 var oldFirst = (charsetPopup.firstChild ? charsetPopup.firstChild : null); 106 charsetPopup.insertBefore(itemNode, oldFirst); 107 } else { 108 charsetPopup.appendChild(itemNode); 109 } 110 } 111 112 var itemNode = document.createElement("menuitem"); 113 itemNode.setAttribute("label", Zotero.getString("charset.autoDetect")); 114 itemNode.setAttribute("value", "auto"); 115 charsetMap["auto"] = itemNode; 116 charsetPopup.insertBefore(itemNode, charsetPopup.firstChild); 117 } 118 119 return charsetMap; 120 } 121 }