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,8 +156,7 @@ 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=');
} }
@ -170,15 +169,15 @@ Zotero_Preferences.Export = {
} }
if (domain && domain != io.domain) { if (domain && domain != io.domain) {
Zotero.DB.query("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domain]); yield Zotero.DB.queryAsync("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domain]);
} }
Zotero.DB.query("REPLACE INTO settings VALUES ('quickCopySite', ?, ?)", [io.domain, io.format]); yield Zotero.DB.queryAsync("REPLACE INTO settings VALUES ('quickCopySite', ?, ?)", [io.domain, io.format]);
this.refreshQuickCopySiteList(); yield Zotero.QuickCopy.loadSiteSettings();
}.bind(this))
.done(); yield this.refreshQuickCopySiteList();
}, }),
refreshQuickCopySiteList: Zotero.Promise.coroutine(function* () { refreshQuickCopySiteList: Zotero.Promise.coroutine(function* () {
@ -200,8 +199,7 @@ 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 ? ' ✓ ' : '');
@ -211,18 +209,18 @@ Zotero_Preferences.Export = {
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,15 +2473,12 @@ Zotero.ItemTreeView.prototype.onDragStart = function (event) {
} }
} }
// Quick Copy is asynchronous
Zotero.spawn(function* () {
// Get Quick Copy format for current URL // Get Quick Copy format for current URL
var url = this._ownerDocument.defaultView.content && this._ownerDocument.defaultView.content.location ? var url = this._ownerDocument.defaultView.content && this._ownerDocument.defaultView.content.location ?
this._ownerDocument.defaultView.content.location.href : null; this._ownerDocument.defaultView.content.location.href : null;
//var format = yield Zotero.QuickCopy.getFormatFromURL(url); var format = Zotero.QuickCopy.getFormatFromURL(url);
var format = 'bibliography=http://www.zotero.org/styles/chicago-note-bibliography';
Zotero.debug("Dragging with format " + (yield Zotero.QuickCopy.getFormattedNameFromSetting(format))); Zotero.debug("Dragging with format " + format);
var exportCallback = function(obj, worked) { var exportCallback = function(obj, worked) {
if (!worked) { if (!worked) {
@ -2515,7 +2512,6 @@ Zotero.ItemTreeView.prototype.onDragStart = function (event) {
Zotero.debug(e); Zotero.debug(e);
Components.utils.reportError(e + " with format '" + format + "'"); Components.utils.reportError(e + " with format '" + format + "'");
} }
}.bind(this));
}; };

View file

@ -29,45 +29,35 @@ 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 rows = yield Zotero.DB.queryAsync(sql);
// Unproxify storage row
_siteSettings = [for (row of rows) { domainPath: row.domainPath, format: row.format }];
});
this.getFormattedNameFromSetting = Zotero.Promise.coroutine(function* (setting) {
if (!_formattedNames) {
yield _loadFormattedNames();
}
var name = _formattedNames[this.stripContentType(setting)]; var name = _formattedNames[this.stripContentType(setting)];
return name ? name : ''; return name ? name : '';
}.bind(this)); });
}
this.getSettingFromFormattedName = function (name) { this.getSettingFromFormattedName = Zotero.Promise.coroutine(function* (name) {
return _init() if (!_formattedNames) {
.then(function () { yield _loadFormattedNames();
}
for (var setting in _formattedNames) { for (var setting in _formattedNames) {
if (_formattedNames[setting] == name) { if (_formattedNames[setting] == name) {
return setting; return setting;
@ -75,7 +65,6 @@ Zotero.QuickCopy = new function() {
} }
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) {