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');
if (index != undefined && index > -1 && index < treechildren.childNodes.length) {
@ -156,8 +156,7 @@ Zotero_Preferences.Export = {
var asHTML = treerow.childNodes[2].getAttribute('label') != '';
}
Zotero.QuickCopy.getSettingFromFormattedName(format)
.then(function (format) {
var format = yield Zotero.QuickCopy.getSettingFromFormattedName(format);
if (asHTML) {
format = format.replace('bibliography=', 'bibliography/html=');
}
@ -170,15 +169,15 @@ Zotero_Preferences.Export = {
}
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();
}.bind(this))
.done();
},
yield Zotero.QuickCopy.loadSiteSettings();
yield this.refreshQuickCopySiteList();
}),
refreshQuickCopySiteList: Zotero.Promise.coroutine(function* () {
@ -200,8 +199,7 @@ Zotero_Preferences.Export = {
domainCell.setAttribute('label', siteData[i].domainPath);
yield Zotero.QuickCopy.getFormattedNameFromSetting(siteData[i].format)
.then(function (formatted) {
var formatted = yield Zotero.QuickCopy.getFormattedNameFromSetting(siteData[i].format);
formatCell.setAttribute('label', formatted);
var copyAsHTML = Zotero.QuickCopy.getContentType(siteData[i].format) == 'html';
HTMLCell.setAttribute('label', copyAsHTML ? ' ✓ ' : '');
@ -211,18 +209,18 @@ Zotero_Preferences.Export = {
treerow.appendChild(HTMLCell);
treeitem.appendChild(treerow);
treechildren.appendChild(treeitem);
});
}
}),
deleteSelectedQuickCopySite: function () {
deleteSelectedQuickCopySite: Zotero.Promise.coroutine(function* () {
var tree = document.getElementById('quickCopy-siteSettings');
var treeitem = tree.lastChild.childNodes[tree.currentIndex];
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();
},
}),
updateQuickCopyInstructions: function () {

View file

@ -167,14 +167,13 @@ Zotero.CreatorTypes = new function() {
this.getTypesForItemType = getTypesForItemType;
this.isValidForItemType = isValidForItemType;
this.getPrimaryIDForType = getPrimaryIDForType;
this._typeDesc = 'creator type';
this._idCol = 'creatorTypeID';
this._nameCol = 'creatorType';
this._table = 'creatorTypes';
var _primaryIDCache = {};
var _primaryIDCache;
var _hasCreatorTypeCache = {};
var _creatorTypesByItemType = {};
var _isValidForItemType = {};
@ -200,6 +199,16 @@ Zotero.CreatorTypes = new function() {
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) {
if (_primaryIDCache[itemTypeID]) {
return _primaryIDCache[itemTypeID];
this.getPrimaryIDForType = function (itemTypeID) {
if (!_primaryIDCache) {
throw new Zotero.Exception.UnloadedDataException(
"Primary creator types not yet loaded"
);
}
var sql = "SELECT creatorTypeID FROM itemTypeCreatorTypes "
+ "WHERE itemTypeID=? AND primaryField=1";
var creatorTypeID = Zotero.DB.valueQuery(sql, itemTypeID);
if (!creatorTypeID) {
if (_primaryIDCache[itemTypeID] === undefined) {
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
var url = this._ownerDocument.defaultView.content && this._ownerDocument.defaultView.content.location ?
this._ownerDocument.defaultView.content.location.href : null;
//var format = yield Zotero.QuickCopy.getFormatFromURL(url);
var format = 'bibliography=http://www.zotero.org/styles/chicago-note-bibliography';
var format = Zotero.QuickCopy.getFormatFromURL(url);
Zotero.debug("Dragging with format " + (yield Zotero.QuickCopy.getFormattedNameFromSetting(format)));
Zotero.debug("Dragging with format " + format);
var exportCallback = function(obj, worked) {
if (!worked) {
@ -2515,7 +2512,6 @@ Zotero.ItemTreeView.prototype.onDragStart = function (event) {
Zotero.debug(e);
Components.utils.reportError(e + " with format '" + format + "'");
}
}.bind(this));
};

View file

@ -29,45 +29,35 @@ Zotero.QuickCopy = new function() {
this.stripContentType = stripContentType;
this.getContentFromItems = getContentFromItems;
var _formattedNames = {};
var _siteSettings;
var _formattedNames;
var _init = Zotero.lazy(function () {
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.init = Zotero.Promise.coroutine(function* () {
yield this.loadSiteSettings();
});
this.getFormattedNameFromSetting = function (setting) {
return _init()
.then(function () {
this.loadSiteSettings = Zotero.Promise.coroutine(function* () {
var sql = "SELECT key AS domainPath, value AS format FROM settings "
+ "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)];
return name ? name : '';
}.bind(this));
}
});
this.getSettingFromFormattedName = function (name) {
return _init()
.then(function () {
this.getSettingFromFormattedName = Zotero.Promise.coroutine(function* (name) {
if (!_formattedNames) {
yield _loadFormattedNames();
}
for (var setting in _formattedNames) {
if (_formattedNames[setting] == name) {
return setting;
@ -75,7 +65,6 @@ Zotero.QuickCopy = new function() {
}
return '';
});
}
/*
@ -95,7 +84,7 @@ Zotero.QuickCopy = new function() {
}
this.getFormatFromURL = Zotero.Promise.coroutine(function* (url) {
this.getFormatFromURL = function (url) {
if (!url) {
return Zotero.Prefs.get("export.quickCopy.setting");
}
@ -112,13 +101,21 @@ Zotero.QuickCopy = new function() {
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 rows = yield Zotero.DB.queryAsync(sql, ['%' + urlDomain + '%', '/%']);
for each(var row in rows) {
var matches = [];
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(/\//);
path = '/' + (path ? path : '');
var re = new RegExp(domain + '$');
@ -156,7 +153,7 @@ Zotero.QuickCopy = new function() {
}
return Zotero.Prefs.get("export.quickCopy.setting");
});
};
/*
@ -364,4 +361,32 @@ Zotero.QuickCopy = new function() {
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.Groups.init();
// TODO: Delay until after UI is shown
yield Zotero.QuickCopy.init();
Zotero.Items.startEmptyTrashTimer();
}
catch (e) {