www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

exportOptions.js (7238B)


      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_Export
     29 //
     30 //////////////////////////////////////////////////////////////////////////////
     31 
     32 const OPTION_PREFIX = "export-option-";
     33 
     34 // Class to provide options for export
     35 
     36 var Zotero_File_Interface_Export = new function() {
     37 	this.init = init;
     38 	this.updateOptions = updateOptions;
     39 	this.accept = accept;
     40 	this.cancel = cancel;
     41 	
     42 	var _charsets = false;
     43 	
     44 	/*
     45 	 * add options to export
     46 	 */
     47 	function init() {
     48 		// Set font size from pref
     49 		var sbc = document.getElementById('zotero-export-options-container');
     50 		Zotero.setFontSize(sbc);
     51 		
     52 		var addedOptions = new Object();
     53 		
     54 		var translators = window.arguments[0].translators;
     55 		translators.sort(function(a, b) { return a.label.localeCompare(b.label) });
     56 		
     57 		// get format popup
     58 		var formatPopup = document.getElementById("format-popup");
     59 		var formatMenu = document.getElementById("format-menu");
     60 		var optionsBox = document.getElementById("translator-options");
     61 		var charsetBox = document.getElementById("charset-box");
     62 		
     63 		var selectedTranslator = Zotero.Prefs.get("export.lastTranslator");
     64 		
     65 		// add styles to format popup
     66 		for(var i in translators) {
     67 			var itemNode = document.createElement("menuitem");
     68 			itemNode.setAttribute("label", translators[i].label);
     69 			formatPopup.appendChild(itemNode);
     70 			
     71 			// add options
     72 			for(var option in translators[i].displayOptions) {
     73 				if(!addedOptions[option]) {		// if this option is not already
     74 												// presented to the user
     75 					// get readable name for option
     76 					try {
     77 						var optionLabel = Zotero.getString("exportOptions."+option);
     78 					} catch(e) {
     79 						var optionLabel = option;
     80 					}
     81 					
     82 					// right now, option interface supports only boolean values, which
     83 					// it interprets as checkboxes
     84 					if(typeof(translators[i].displayOptions[option]) == "boolean") {
     85 						var checkbox = document.createElement("checkbox");
     86 						checkbox.setAttribute("id", OPTION_PREFIX+option);
     87 						checkbox.setAttribute("label", optionLabel);
     88 						optionsBox.insertBefore(checkbox, charsetBox);
     89 					}
     90 					
     91 					addedOptions[option] = true;
     92 				}
     93 			}
     94 			
     95 			// select last selected translator
     96 			if(translators[i].translatorID == selectedTranslator) {
     97 				formatMenu.selectedIndex = i;
     98 			}
     99 		}
    100 		
    101 		// select first item by default
    102 		if(formatMenu.selectedIndex == -1) {
    103 			formatMenu.selectedIndex = 0;
    104 		}
    105 		
    106 		// from charsetMenu.js
    107 		if(Zotero.Prefs.get("export.displayCharsetOption")) {
    108 			_charsets = Zotero_Charset_Menu.populate(document.getElementById(OPTION_PREFIX+"exportCharset"), true);
    109 		}
    110 		
    111 		updateOptions(Zotero.Prefs.get("export.translatorSettings"));
    112 	}
    113 	
    114 	/*
    115 	 * update translator-specific options
    116 	 */
    117 	function updateOptions(optionString) {
    118 		// get selected translator
    119 		var index = document.getElementById("format-menu").selectedIndex;
    120 		var translatorOptions = window.arguments[0].translators[index].displayOptions;
    121 		
    122 		if(optionString) {
    123 			try {
    124 				var options = JSON.parse(optionString);
    125 			} catch(e) {}
    126 		}
    127 		
    128 		var optionsBox = document.getElementById("translator-options");
    129 		optionsBox.hidden = true;
    130 		var haveOption = false;
    131 		for(var i=0; i<optionsBox.childNodes.length; i++) {
    132 			// loop through options to see which should be enabled
    133 			var node = optionsBox.childNodes[i];
    134 			// skip non-options
    135 			if(node.id.length <= OPTION_PREFIX.length
    136 					|| node.id.substr(0, OPTION_PREFIX.length) != OPTION_PREFIX) {
    137 				continue;
    138 			}
    139 			
    140 			var optionName = node.id.substr(OPTION_PREFIX.length);
    141 			if (translatorOptions && translatorOptions[optionName] != undefined) {
    142 				// option should be enabled
    143 				optionsBox.hidden = undefined;
    144 				node.hidden = undefined;
    145 				
    146 				var defValue = translatorOptions[optionName];
    147 				if(typeof(defValue) == "boolean") {
    148 					if(options && options[optionName] !== undefined) {
    149 						// if there's a saved prefs string, use it
    150 						var isChecked = options[optionName];
    151 					} else {
    152 						// use defaults
    153 						var isChecked = (defValue ? "true" : "false");
    154 					}
    155 					node.setAttribute("checked", isChecked);
    156 				}
    157 			} else {
    158 				// option should be disabled and unchecked to prevent confusion
    159 				node.hidden = true;
    160 				node.checked = false;
    161 			}
    162 		}
    163 		
    164 		// handle charset popup
    165 		if(_charsets && translatorOptions && translatorOptions.exportCharset) {
    166 			optionsBox.hidden = undefined;
    167 			document.getElementById("charset-box").hidden = undefined;
    168 			var charsetMenu = document.getElementById(OPTION_PREFIX+"exportCharset");
    169 			var charset = "UTF-8";
    170 			if(options && options.exportCharset && _charsets[options.exportCharset]) {
    171 				charset = options.exportCharset;
    172 			} else if(translatorOptions.exportCharset && _charsets[translatorOptions.exportCharset]) {
    173 				charset = translatorOptions.exportCharset;
    174 			}
    175 			
    176 			charsetMenu.selectedItem = _charsets[charset];
    177 		} else {
    178 			document.getElementById("charset-box").hidden = true;
    179 		}
    180 		
    181 		window.sizeToContent();
    182 	}
    183 	
    184 	/*
    185 	 * make option array reflect status
    186 	 */
    187 	function accept() {
    188 		// set selected translator
    189 		var index = document.getElementById("format-menu").selectedIndex;
    190 		window.arguments[0].selectedTranslator = window.arguments[0].translators[index];
    191 		
    192 		// save selected translator
    193 		Zotero.Prefs.set("export.lastTranslator", window.arguments[0].translators[index].translatorID);
    194 		
    195 		// set options on selected translator and generate optionString
    196 		var optionsAvailable = window.arguments[0].selectedTranslator.displayOptions;
    197 		var displayOptions = window.arguments[0].displayOptions = {};
    198 		for(var option in optionsAvailable) {
    199 			var defValue = optionsAvailable[option];
    200 			var element = document.getElementById(OPTION_PREFIX+option);
    201 			
    202 			if(option == "exportCharset") {
    203 				if(_charsets) {
    204 					displayOptions[option] = element.selectedItem.value;
    205 				} else {
    206 					displayOptions[option] = optionsAvailable[option];
    207 				}
    208 			} else if(typeof(defValue) == "boolean") {
    209 				displayOptions[option] = !!element.checked;
    210 			}
    211 		}
    212 		
    213 		// save options
    214 		var optionString = JSON.stringify(displayOptions);
    215 		Zotero.Prefs.set("export.translatorSettings", optionString);
    216 	}
    217 	
    218 	/*
    219 	 * make option array reflect status
    220 	 */
    221 	function cancel() {
    222 		window.arguments[0].selectedTranslator = false;
    223 	}
    224 }