Fixes #522, [Async DB] Quick Copy is broken

Export depends on #520 (but still with a synchronous interface, somehow)
This commit is contained in:
Dan Stillman 2014-08-12 21:36:45 -04:00
parent a67521e9dd
commit c729dc8a54
5 changed files with 165 additions and 135 deletions

View file

@ -146,7 +146,7 @@ Zotero_Preferences.Export = {
}, },
showQuickCopySiteEditor: function (index) { showQuickCopySiteEditor: Zotero.Promise.coroutine(function* (index) {
var treechildren = document.getElementById('quickCopy-siteSettings-rows'); var treechildren = document.getElementById('quickCopy-siteSettings-rows');
if (index != undefined && index > -1 && index < treechildren.childNodes.length) { if (index != undefined && index > -1 && index < treechildren.childNodes.length) {
@ -156,29 +156,28 @@ Zotero_Preferences.Export = {
var asHTML = treerow.childNodes[2].getAttribute('label') != ''; var asHTML = treerow.childNodes[2].getAttribute('label') != '';
} }
Zotero.QuickCopy.getSettingFromFormattedName(format) var format = yield Zotero.QuickCopy.getSettingFromFormattedName(format);
.then(function (format) { if (asHTML) {
if (asHTML) { format = format.replace('bibliography=', 'bibliography/html=');
format = format.replace('bibliography=', 'bibliography/html='); }
}
var io = {domain: domain, format: format, ok: false};
var io = {domain: domain, format: format, ok: false}; window.openDialog('chrome://zotero/content/preferences/quickCopySiteEditor.xul', "zotero-preferences-quickCopySiteEditor", "chrome, modal", io);
window.openDialog('chrome://zotero/content/preferences/quickCopySiteEditor.xul', "zotero-preferences-quickCopySiteEditor", "chrome, modal", io);
if (!io.ok) {
if (!io.ok) { return;
return; }
}
if (domain && domain != io.domain) {
if (domain && domain != io.domain) { yield Zotero.DB.queryAsync("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domain]);
Zotero.DB.query("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domain]); }
}
yield Zotero.DB.queryAsync("REPLACE INTO settings VALUES ('quickCopySite', ?, ?)", [io.domain, io.format]);
Zotero.DB.query("REPLACE INTO settings VALUES ('quickCopySite', ?, ?)", [io.domain, io.format]);
yield Zotero.QuickCopy.loadSiteSettings();
this.refreshQuickCopySiteList();
}.bind(this)) yield this.refreshQuickCopySiteList();
.done(); }),
},
refreshQuickCopySiteList: Zotero.Promise.coroutine(function* () { refreshQuickCopySiteList: Zotero.Promise.coroutine(function* () {
@ -200,29 +199,28 @@ Zotero_Preferences.Export = {
domainCell.setAttribute('label', siteData[i].domainPath); domainCell.setAttribute('label', siteData[i].domainPath);
yield Zotero.QuickCopy.getFormattedNameFromSetting(siteData[i].format) var formatted = yield Zotero.QuickCopy.getFormattedNameFromSetting(siteData[i].format);
.then(function (formatted) { formatCell.setAttribute('label', formatted);
formatCell.setAttribute('label', formatted); var copyAsHTML = Zotero.QuickCopy.getContentType(siteData[i].format) == 'html';
var copyAsHTML = Zotero.QuickCopy.getContentType(siteData[i].format) == 'html'; HTMLCell.setAttribute('label', copyAsHTML ? ' ✓ ' : '');
HTMLCell.setAttribute('label', copyAsHTML ? ' ✓ ' : '');
treerow.appendChild(domainCell);
treerow.appendChild(domainCell); treerow.appendChild(formatCell);
treerow.appendChild(formatCell); treerow.appendChild(HTMLCell);
treerow.appendChild(HTMLCell); treeitem.appendChild(treerow);
treeitem.appendChild(treerow); treechildren.appendChild(treeitem);
treechildren.appendChild(treeitem);
});
} }
}), }),
deleteSelectedQuickCopySite: function () { deleteSelectedQuickCopySite: Zotero.Promise.coroutine(function* () {
var tree = document.getElementById('quickCopy-siteSettings'); var tree = document.getElementById('quickCopy-siteSettings');
var treeitem = tree.lastChild.childNodes[tree.currentIndex]; var treeitem = tree.lastChild.childNodes[tree.currentIndex];
var domainPath = treeitem.firstChild.firstChild.getAttribute('label'); var domainPath = treeitem.firstChild.firstChild.getAttribute('label');
Zotero.DB.query("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domainPath]); yield Zotero.DB.queryAsync("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domainPath]);
yield Zotero.QuickCopy.loadSiteSettings();
this.refreshQuickCopySiteList(); this.refreshQuickCopySiteList();
}, }),
updateQuickCopyInstructions: function () { updateQuickCopyInstructions: function () {

View file

@ -167,14 +167,13 @@ Zotero.CreatorTypes = new function() {
this.getTypesForItemType = getTypesForItemType; this.getTypesForItemType = getTypesForItemType;
this.isValidForItemType = isValidForItemType; this.isValidForItemType = isValidForItemType;
this.getPrimaryIDForType = getPrimaryIDForType;
this._typeDesc = 'creator type'; this._typeDesc = 'creator type';
this._idCol = 'creatorTypeID'; this._idCol = 'creatorTypeID';
this._nameCol = 'creatorType'; this._nameCol = 'creatorType';
this._table = 'creatorTypes'; this._table = 'creatorTypes';
var _primaryIDCache = {}; var _primaryIDCache;
var _hasCreatorTypeCache = {}; var _hasCreatorTypeCache = {};
var _creatorTypesByItemType = {}; var _creatorTypesByItemType = {};
var _isValidForItemType = {}; var _isValidForItemType = {};
@ -200,6 +199,16 @@ Zotero.CreatorTypes = new function() {
name: row.name name: row.name
}); });
} }
// Load primary creator type ids
_primaryIDCache = {};
var sql = "SELECT itemTypeID, creatorTypeID FROM itemTypeCreatorTypes "
+ "WHERE primaryField=1";
var rows = yield Zotero.DB.queryAsync(sql);
for (let i=0; i<rows.length; i++) {
let row = rows[i];
_primaryIDCache[row.itemTypeID] = row.creatorTypeID;
}
}); });
@ -248,18 +257,18 @@ Zotero.CreatorTypes = new function() {
} }
function getPrimaryIDForType(itemTypeID) { this.getPrimaryIDForType = function (itemTypeID) {
if (_primaryIDCache[itemTypeID]) { if (!_primaryIDCache) {
return _primaryIDCache[itemTypeID]; throw new Zotero.Exception.UnloadedDataException(
"Primary creator types not yet loaded"
);
} }
var sql = "SELECT creatorTypeID FROM itemTypeCreatorTypes "
+ "WHERE itemTypeID=? AND primaryField=1"; if (_primaryIDCache[itemTypeID] === undefined) {
var creatorTypeID = Zotero.DB.valueQuery(sql, itemTypeID);
if (!creatorTypeID) {
return false; return false;
} }
_primaryIDCache[itemTypeID] = creatorTypeID;
return creatorTypeID; return _primaryIDCache[itemTypeID];
} }
} }

View file

@ -2473,49 +2473,45 @@ Zotero.ItemTreeView.prototype.onDragStart = function (event) {
} }
} }
// Quick Copy is asynchronous // Get Quick Copy format for current URL
Zotero.spawn(function* () { var url = this._ownerDocument.defaultView.content && this._ownerDocument.defaultView.content.location ?
// Get Quick Copy format for current URL this._ownerDocument.defaultView.content.location.href : null;
var url = this._ownerDocument.defaultView.content && this._ownerDocument.defaultView.content.location ? var format = Zotero.QuickCopy.getFormatFromURL(url);
this._ownerDocument.defaultView.content.location.href : null;
//var format = yield Zotero.QuickCopy.getFormatFromURL(url); Zotero.debug("Dragging with format " + format);
var format = 'bibliography=http://www.zotero.org/styles/chicago-note-bibliography';
var exportCallback = function(obj, worked) {
Zotero.debug("Dragging with format " + (yield Zotero.QuickCopy.getFormattedNameFromSetting(format))); if (!worked) {
Zotero.log(Zotero.getString("fileInterface.exportError"), 'warning');
var exportCallback = function(obj, worked) { return;
if (!worked) {
Zotero.log(Zotero.getString("fileInterface.exportError"), 'warning');
return;
}
var text = obj.string.replace(/\r\n/g, "\n");
event.dataTransfer.setData("text/plain", text);
} }
try { var text = obj.string.replace(/\r\n/g, "\n");
var [mode, ] = format.split('='); event.dataTransfer.setData("text/plain", text);
if (mode == 'export') { }
Zotero.QuickCopy.getContentFromItems(items, format, exportCallback);
} try {
else if (mode.indexOf('bibliography') == 0) { var [mode, ] = format.split('=');
var content = Zotero.QuickCopy.getContentFromItems(items, format, null, event.shiftKey); if (mode == 'export') {
if (content) { Zotero.QuickCopy.getContentFromItems(items, format, exportCallback);
if (content.html) { }
event.dataTransfer.setData("text/html", content.html); else if (mode.indexOf('bibliography') == 0) {
} var content = Zotero.QuickCopy.getContentFromItems(items, format, null, event.shiftKey);
event.dataTransfer.setData("text/plain", content.text); if (content) {
if (content.html) {
event.dataTransfer.setData("text/html", content.html);
} }
} event.dataTransfer.setData("text/plain", content.text);
else {
Components.utils.reportError("Invalid Quick Copy mode '" + mode + "'");
} }
} }
catch (e) { else {
Zotero.debug(e); Components.utils.reportError("Invalid Quick Copy mode '" + mode + "'");
Components.utils.reportError(e + " with format '" + format + "'");
} }
}.bind(this)); }
catch (e) {
Zotero.debug(e);
Components.utils.reportError(e + " with format '" + format + "'");
}
}; };

View file

@ -29,53 +29,42 @@ Zotero.QuickCopy = new function() {
this.stripContentType = stripContentType; this.stripContentType = stripContentType;
this.getContentFromItems = getContentFromItems; this.getContentFromItems = getContentFromItems;
var _formattedNames = {}; var _siteSettings;
var _formattedNames;
this.init = Zotero.Promise.coroutine(function* () {
var _init = Zotero.lazy(function () { yield this.loadSiteSettings();
Zotero.debug("Initializing Quick Copy");
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) { this.loadSiteSettings = Zotero.Promise.coroutine(function* () {
return _init() var sql = "SELECT key AS domainPath, value AS format FROM settings "
.then(function () { + "WHERE setting='quickCopySite'";
var name = _formattedNames[this.stripContentType(setting)]; var rows = yield Zotero.DB.queryAsync(sql);
return name ? name : ''; // Unproxify storage row
}.bind(this)); _siteSettings = [for (row of rows) { domainPath: row.domainPath, format: row.format }];
} });
this.getSettingFromFormattedName = function (name) {
return _init() this.getFormattedNameFromSetting = Zotero.Promise.coroutine(function* (setting) {
.then(function () { if (!_formattedNames) {
for (var setting in _formattedNames) { yield _loadFormattedNames();
if (_formattedNames[setting] == name) { }
return setting; var name = _formattedNames[this.stripContentType(setting)];
} return name ? name : '';
});
this.getSettingFromFormattedName = Zotero.Promise.coroutine(function* (name) {
if (!_formattedNames) {
yield _loadFormattedNames();
}
for (var setting in _formattedNames) {
if (_formattedNames[setting] == name) {
return setting;
} }
return ''; }
}); return '';
} });
/* /*
@ -95,7 +84,7 @@ Zotero.QuickCopy = new function() {
} }
this.getFormatFromURL = Zotero.Promise.coroutine(function* (url) { this.getFormatFromURL = function (url) {
if (!url) { if (!url) {
return Zotero.Prefs.get("export.quickCopy.setting"); return Zotero.Prefs.get("export.quickCopy.setting");
} }
@ -112,13 +101,21 @@ Zotero.QuickCopy = new function() {
return Zotero.Prefs.get("export.quickCopy.setting"); return Zotero.Prefs.get("export.quickCopy.setting");
} }
var matches = []; if (!_siteSettings) {
throw new Zotero.Exception.UnloadedDataException("Quick Copy site settings not loaded");
}
var sql = "SELECT key AS domainPath, value AS format FROM settings "
+ "WHERE setting='quickCopySite' AND (key LIKE ? OR key LIKE ?)";
var urlDomain = urlHostPort.match(/[^\.]+\.[^\.]+$/); var urlDomain = urlHostPort.match(/[^\.]+\.[^\.]+$/);
var rows = yield Zotero.DB.queryAsync(sql, ['%' + urlDomain + '%', '/%']); var matches = [];
for each(var row in rows) { for (let i=0; i<_siteSettings.length; i++) {
let row = _siteSettings[i];
// Only concern ourselves with entries containing the current domain
// or paths that apply to all domains
if (!row.domainPath.contains(urlDomain) && !row.domainPath.startsWith('/')) {
continue;
}
var [domain, path] = row.domainPath.split(/\//); var [domain, path] = row.domainPath.split(/\//);
path = '/' + (path ? path : ''); path = '/' + (path ? path : '');
var re = new RegExp(domain + '$'); var re = new RegExp(domain + '$');
@ -156,7 +153,7 @@ Zotero.QuickCopy = new function() {
} }
return Zotero.Prefs.get("export.quickCopy.setting"); return Zotero.Prefs.get("export.quickCopy.setting");
}); };
/* /*
@ -364,4 +361,32 @@ Zotero.QuickCopy = new function() {
throw ("Invalid mode '" + mode + "' in Zotero.QuickCopy.getContentFromItems()"); throw ("Invalid mode '" + mode + "' in Zotero.QuickCopy.getContentFromItems()");
} }
var _loadFormattedNames = Zotero.Promise.coroutine(function* () {
var t = new Date;
Zotero.debug("Loading formatted names for Quick Copy");
var translation = new Zotero.Translate.Export;
var translators = yield translation.getTranslators();
// add styles to list
_formattedNames = {};
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;
}
Zotero.debug("Loaded formatted names for Quick Copy in " + (new Date - t) + " ms");
});
} }

View file

@ -621,6 +621,8 @@ Components.utils.import("resource://gre/modules/osfile.jsm");
Zotero.Searches.init(); Zotero.Searches.init();
Zotero.Groups.init(); Zotero.Groups.init();
// TODO: Delay until after UI is shown
yield Zotero.QuickCopy.init();
Zotero.Items.startEmptyTrashTimer(); Zotero.Items.startEmptyTrashTimer();
} }
catch (e) { catch (e) {