/
exportOptions.js
224 lines (186 loc) · 7.03 KB
/
exportOptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2009 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
//////////////////////////////////////////////////////////////////////////////
//
// Zotero_File_Interface_Export
//
//////////////////////////////////////////////////////////////////////////////
const OPTION_PREFIX = "export-option-";
// Class to provide options for export
var Zotero_File_Interface_Export = new function() {
this.init = init;
this.updateOptions = updateOptions;
this.accept = accept;
this.cancel = cancel;
var _charsets = false;
/*
* add options to export
*/
function init() {
// Set font size from pref
var sbc = document.getElementById('zotero-export-options-container');
Zotero.setFontSize(sbc);
var addedOptions = new Object();
var translators = window.arguments[0].translators;
translators.sort(function(a, b) { return a.label.localeCompare(b.label) });
// get format popup
var formatPopup = document.getElementById("format-popup");
var formatMenu = document.getElementById("format-menu");
var optionsBox = document.getElementById("translator-options");
var charsetBox = document.getElementById("charset-box");
var selectedTranslator = Zotero.Prefs.get("export.lastTranslator");
// add styles to format popup
for(var i in translators) {
var itemNode = document.createElement("menuitem");
itemNode.setAttribute("label", translators[i].label);
formatPopup.appendChild(itemNode);
// add options
for(var option in translators[i].displayOptions) {
if(!addedOptions[option]) { // if this option is not already
// presented to the user
// get readable name for option
try {
var optionLabel = Zotero.getString("exportOptions."+option);
} catch(e) {
var optionLabel = option;
}
// right now, option interface supports only boolean values, which
// it interprets as checkboxes
if(typeof(translators[i].displayOptions[option]) == "boolean") {
var checkbox = document.createElement("checkbox");
checkbox.setAttribute("id", OPTION_PREFIX+option);
checkbox.setAttribute("label", optionLabel);
optionsBox.insertBefore(checkbox, charsetBox);
}
addedOptions[option] = true;
}
}
// select last selected translator
if(translators[i].translatorID == selectedTranslator) {
formatMenu.selectedIndex = i;
}
}
// select first item by default
if(formatMenu.selectedIndex == -1) {
formatMenu.selectedIndex = 0;
}
// from charsetMenu.js
if(Zotero.Prefs.get("export.displayCharsetOption")) {
_charsets = Zotero_Charset_Menu.populate(document.getElementById(OPTION_PREFIX+"exportCharset"), true);
}
updateOptions(Zotero.Prefs.get("export.translatorSettings"));
}
/*
* update translator-specific options
*/
function updateOptions(optionString) {
// get selected translator
var index = document.getElementById("format-menu").selectedIndex;
var translatorOptions = window.arguments[0].translators[index].displayOptions;
if(optionString) {
try {
var options = JSON.parse(optionString);
} catch(e) {}
}
var optionsBox = document.getElementById("translator-options");
optionsBox.hidden = true;
var haveOption = false;
for(var i=0; i<optionsBox.childNodes.length; i++) {
// loop through options to see which should be enabled
var node = optionsBox.childNodes[i];
// skip non-options
if(node.id.length <= OPTION_PREFIX.length
|| node.id.substr(0, OPTION_PREFIX.length) != OPTION_PREFIX) {
continue;
}
var optionName = node.id.substr(OPTION_PREFIX.length);
if(translatorOptions[optionName] != undefined) {
// option should be enabled
optionsBox.hidden = undefined;
node.hidden = undefined;
var defValue = translatorOptions[optionName];
if(typeof(defValue) == "boolean") {
if(options && options[optionName] !== undefined) {
// if there's a saved prefs string, use it
var isChecked = options[optionName];
} else {
// use defaults
var isChecked = (defValue ? "true" : "false");
}
node.setAttribute("checked", isChecked);
}
} else {
// option should be disabled and unchecked to prevent confusion
node.hidden = true;
node.checked = false;
}
}
// handle charset popup
if(_charsets && translatorOptions.exportCharset) {
optionsBox.hidden = undefined;
document.getElementById("charset-box").hidden = undefined;
var charsetMenu = document.getElementById(OPTION_PREFIX+"exportCharset");
var charset = "UTF-8";
if(options && options.exportCharset && _charsets[options.exportCharset]) {
charset = options.exportCharset;
} else if(translatorOptions.exportCharset && _charsets[translatorOptions.exportCharset]) {
charset = translatorOptions.exportCharset;
}
charsetMenu.selectedItem = _charsets[charset];
} else {
document.getElementById("charset-box").hidden = true;
}
window.sizeToContent();
}
/*
* make option array reflect status
*/
function accept() {
// set selected translator
var index = document.getElementById("format-menu").selectedIndex;
window.arguments[0].selectedTranslator = window.arguments[0].translators[index];
// save selected translator
Zotero.Prefs.set("export.lastTranslator", window.arguments[0].translators[index].translatorID);
// set options on selected translator and generate optionString
var optionsAvailable = window.arguments[0].selectedTranslator.displayOptions;
var displayOptions = window.arguments[0].displayOptions = {};
for(var option in optionsAvailable) {
var defValue = optionsAvailable[option];
var element = document.getElementById(OPTION_PREFIX+option);
if(option == "exportCharset") {
if(_charsets) {
displayOptions[option] = element.selectedItem.value;
} else {
displayOptions[option] = optionsAvailable[option];
}
} else if(typeof(defValue) == "boolean") {
displayOptions[option] = !!element.checked;
}
}
// save options
var optionString = JSON.stringify(displayOptions);
Zotero.Prefs.set("export.translatorSettings", optionString);
}
/*
* make option array reflect status
*/
function cancel() {
window.arguments[0].selectedTranslator = false;
}
}