Update quickCopy.js and preferences_export.js for async getTranslators()

This commit is contained in:
Dan Stillman 2013-08-16 16:27:17 -04:00
parent 540a54515e
commit e85eaf218a
2 changed files with 108 additions and 104 deletions

View file

@ -45,8 +45,8 @@ Zotero_Preferences.Export = {
// Initialize default format drop-down
var format = Zotero.Prefs.get("export.quickCopy.setting");
var menulist = document.getElementById("zotero-quickCopy-menu");
this.buildQuickCopyFormatDropDown(menulist, Zotero.QuickCopy.getContentType(format), format);
menulist.setAttribute('preference', "pref-quickCopy-setting");
this.buildQuickCopyFormatDropDown(menulist, Zotero.QuickCopy.getContentType(format), format);
this.updateQuickCopyHTMLCheckbox();
if (!Zotero.isStandalone) {
@ -106,30 +106,30 @@ Zotero_Preferences.Export = {
// add export formats to list
var translation = new Zotero.Translate("export");
var translators = translation.getTranslators();
for (var i=0; i<translators.length; i++) {
// Skip RDF formats
switch (translators[i].translatorID) {
case '6e372642-ed9d-4934-b5d1-c11ac758ebb7':
case '14763d24-8ba0-45df-8f52-b8d1108e7ac9':
continue;
translation.getTranslators()
.then(function (translators) {
for (var i=0; i<translators.length; i++) {
// Skip RDF formats
switch (translators[i].translatorID) {
case '6e372642-ed9d-4934-b5d1-c11ac758ebb7':
case '14763d24-8ba0-45df-8f52-b8d1108e7ac9':
continue;
}
var val = 'export=' + translators[i].translatorID;
var itemNode = document.createElement("menuitem");
itemNode.setAttribute("value", val);
itemNode.setAttribute("label", translators[i].label);
itemNode.setAttribute("oncommand", 'Zotero_Preferences.Export.updateQuickCopyHTMLCheckbox()');
popup.appendChild(itemNode);
if (val == currentFormat) {
menulist.selectedItem = itemNode;
}
}
var val = 'export=' + translators[i].translatorID;
var itemNode = document.createElement("menuitem");
itemNode.setAttribute("value", val);
itemNode.setAttribute("label", translators[i].label);
itemNode.setAttribute("oncommand", 'Zotero_Preferences.Export.updateQuickCopyHTMLCheckbox()');
popup.appendChild(itemNode);
if (val == currentFormat) {
menulist.selectedItem = itemNode;
}
}
menulist.click();
return popup;
menulist.click();
})
.done();
},
@ -156,25 +156,28 @@ Zotero_Preferences.Export = {
var asHTML = treerow.childNodes[2].getAttribute('label') != '';
}
var format = Zotero.QuickCopy.getSettingFromFormattedName(format);
if (asHTML) {
format = format.replace('bibliography=', 'bibliography/html=');
}
var io = {domain: domain, format: format, ok: false};
window.openDialog('chrome://zotero/content/preferences/quickCopySiteEditor.xul', "zotero-preferences-quickCopySiteEditor", "chrome, modal", io);
if (!io.ok) {
return;
}
if (domain && domain != io.domain) {
Zotero.DB.query("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domain]);
}
Zotero.DB.query("REPLACE INTO settings VALUES ('quickCopySite', ?, ?)", [io.domain, io.format]);
this.refreshQuickCopySiteList();
Zotero.QuickCopy.getSettingFromFormattedName(format)
.then(function (format) {
if (asHTML) {
format = format.replace('bibliography=', 'bibliography/html=');
}
var io = {domain: domain, format: format, ok: false};
window.openDialog('chrome://zotero/content/preferences/quickCopySiteEditor.xul', "zotero-preferences-quickCopySiteEditor", "chrome, modal", io);
if (!io.ok) {
return;
}
if (domain && domain != io.domain) {
Zotero.DB.query("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domain]);
}
Zotero.DB.query("REPLACE INTO settings VALUES ('quickCopySite', ?, ?)", [io.domain, io.format]);
this.refreshQuickCopySiteList();
}.bind(this))
.done();
},
@ -192,26 +195,30 @@ Zotero_Preferences.Export = {
return;
}
for (var i=0; i<siteData.length; i++) {
var treeitem = document.createElement('treeitem');
var treerow = document.createElement('treerow');
var domainCell = document.createElement('treecell');
var formatCell = document.createElement('treecell');
var HTMLCell = document.createElement('treecell');
domainCell.setAttribute('label', siteData[i].domainPath);
var formatted = Zotero.QuickCopy.getFormattedNameFromSetting(siteData[i].format);
formatCell.setAttribute('label', formatted);
var copyAsHTML = Zotero.QuickCopy.getContentType(siteData[i].format) == 'html';
HTMLCell.setAttribute('label', copyAsHTML ? ' ✓ ' : '');
treerow.appendChild(domainCell);
treerow.appendChild(formatCell);
treerow.appendChild(HTMLCell);
treeitem.appendChild(treerow);
treechildren.appendChild(treeitem);
}
Q.async(function () {
for (var i=0; i<siteData.length; i++) {
let treeitem = document.createElement('treeitem');
let treerow = document.createElement('treerow');
let domainCell = document.createElement('treecell');
let formatCell = document.createElement('treecell');
let HTMLCell = document.createElement('treecell');
domainCell.setAttribute('label', siteData[i].domainPath);
yield Zotero.QuickCopy.getFormattedNameFromSetting(siteData[i].format)
.then(function (formatted) {
formatCell.setAttribute('label', formatted);
var copyAsHTML = Zotero.QuickCopy.getContentType(siteData[i].format) == 'html';
HTMLCell.setAttribute('label', copyAsHTML ? ' ✓ ' : '');
treerow.appendChild(domainCell);
treerow.appendChild(formatCell);
treerow.appendChild(HTMLCell);
treeitem.appendChild(treerow);
treechildren.appendChild(treeitem);
});
}
})().done();
},

View file

@ -25,38 +25,57 @@
Zotero.QuickCopy = new function() {
this.getFormattedNameFromSetting = getFormattedNameFromSetting;
this.getSettingFromFormattedName = getSettingFromFormattedName;
this.getContentType = getContentType;
this.stripContentType = stripContentType;
this.getFormatFromURL = getFormatFromURL;
this.getContentFromItems = getContentFromItems;
var _initialized = false;
var _formattedNames = {};
function getFormattedNameFromSetting(setting) {
if (!_initialized) {
_init();
}
var _init = Zotero.lazy(function () {
Zotero.debug("Initializing Quick Copy");
var name = _formattedNames[this.stripContentType(setting)];
return name ? name : '';
var translation = new Zotero.Translate.Export;
return translation.getTranslators()
.then(function (translators) {
// add styles to list
var styles = Zotero.Styles.getVisible();
for each(var style in styles) {
_formattedNames['bibliography=' + style.styleID] = style.title;
}
for (var i=0; i<translators.length; i++) {
// Skip RDF formats
switch (translators[i].translatorID) {
case '6e372642-ed9d-4934-b5d1-c11ac758ebb7':
case '14763d24-8ba0-45df-8f52-b8d1108e7ac9':
continue;
}
_formattedNames['export=' + translators[i].translatorID] = translators[i].label;
}
});
});
this.getFormattedNameFromSetting = function (setting) {
return _init()
.then(function () {
var name = _formattedNames[this.stripContentType(setting)];
return name ? name : '';
}.bind(this));
}
function getSettingFromFormattedName(name) {
if (!_initialized) {
_init();
}
for (var setting in _formattedNames) {
if (_formattedNames[setting] == name) {
return setting;
this.getSettingFromFormattedName = function (name) {
return _init()
.then(function () {
for (var setting in _formattedNames) {
if (_formattedNames[setting] == name) {
return setting;
}
}
}
return '';
return '';
});
}
@ -346,26 +365,4 @@ Zotero.QuickCopy = new function() {
throw ("Invalid mode '" + mode + "' in Zotero.QuickCopy.getContentFromItems()");
}
function _init() {
var translation = new Zotero.Translate.Export;
var translators = translation.getTranslators();
// add styles to list
var styles = Zotero.Styles.getVisible();
for each(var style in styles) {
_formattedNames['bibliography=' + style.styleID] = style.title;
}
for (var i=0; i<translators.length; i++) {
// Skip RDF formats
switch (translators[i].translatorID) {
case '6e372642-ed9d-4934-b5d1-c11ac758ebb7':
case '14763d24-8ba0-45df-8f52-b8d1108e7ac9':
continue;
}
_formattedNames['export=' + translators[i].translatorID] = translators[i].label;
}
}
}