Fixes #908, Item saving with automatic tags is very slow with tag selector open

This commit is contained in:
Dan Stillman 2008-01-26 18:28:37 +00:00
parent 49689b6236
commit cabc061ed5
2 changed files with 60 additions and 8 deletions

View File

@ -1901,6 +1901,21 @@ Zotero.Item.prototype.addTag = function(tag, type){
}
Zotero.Item.prototype.addTags = function (tags, type) {
Zotero.DB.beginTransaction();
try {
for each(var tag in tags) {
this.addTag(tag, type);
}
Zotero.DB.commitTransaction();
}
catch (e) {
Zotero.DB.rollbackTransaction();
throw (e);
}
}
Zotero.Item.prototype.addTagByID = function(tagID) {
if (!this.getID()) {
throw ('Cannot add tag to unsaved item in Item.addTagByID()');

View File

@ -1022,18 +1022,36 @@ Zotero.Translate.prototype._itemTagsAndSeeAlso = function(item, newItem) {
}
}
if(item.tags) {
var tagsToAdd = {};
tagsToAdd[0] = []; // user tags
tagsToAdd[1] = []; // automatic tags
for each(var tag in item.tags) {
if(typeof(tag) == "string") {
// accept strings in tag array as automatic tags, or, if
// importing, as non-automatic tags
newItem.addTag(tag, (this.type == "import" ? 0 : 1));
if (this.type == 'import') {
tagsToAdd[0].push(tag);
}
else {
tagsToAdd[1].push(tag);
}
} else if(typeof(tag) == "object") {
// also accept objects
if(tag.tag) {
newItem.addTag(tag.tag, tag.type ? tag.type : 0);
if (!tagsToAdd[tag.type]) {
tagsToAdd[tag.type] = [];
}
tagsToAdd[tag.type].push(tag.tag);
}
}
}
for (var type in tagsToAdd) {
if (tagsToAdd[type].length) {
newItem.addTags(tagsToAdd[type], type);
}
}
}
}
@ -1364,23 +1382,42 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
// enabled in the preferences (as it is by default)
if(item.tags &&
(this.type == "import" || Zotero.Prefs.get("automaticTags"))) {
var tagsToAdd = {};
tagsToAdd[0] = []; // user tags
tagsToAdd[1] = []; // automatic tags
for each(var tag in item.tags) {
if(typeof(tag) == "string") {
// accept strings in tag array as automatic tags, or, if
// importing, as non-automatic tags
newItem.addTag(tag, (this.type == "import" ? 0 : 1));
if (this.type == 'import') {
tagsToAdd[0].push(tag);
}
else {
tagsToAdd[1].push(tag);
}
} else if(typeof(tag) == "object") {
// also accept objects
if(tag.tag) {
if(this.type == "import") {
newItem.addTag(tag.tag, tag.type ? tag.type : 0);
} else {
// force web imports to automatic
newItem.addTag(tag.tag, 1);
if (this.type == "import") {
if (!tagsToAdd[tag.type]) {
tagsToAdd[tag.type] = [];
}
tagsToAdd[tag.type].push(tag.tag);
}
// force web imports to automatic
else {
tagsToAdd[1].push(tag.tag);
}
}
}
}
for (var type in tagsToAdd) {
if (tagsToAdd[type].length) {
newItem.addTags(tagsToAdd[type], type);
}
}
}
if(!attachedTo) this.runHandler("itemDone", newItem);