Simplify cloning of collections and searches

This commit is contained in:
Dan Stillman 2018-12-16 02:17:57 -05:00
parent 5f07f36ae5
commit 13d55910ed
3 changed files with 21 additions and 38 deletions

View file

@ -2032,10 +2032,7 @@ Zotero.CollectionTreeView.prototype.drop = Zotero.Promise.coroutine(function* (r
// Collections
if (desc.type == 'collection') {
var c = yield Zotero.Collections.getAsync(desc.id);
var newCollection = new Zotero.Collection;
newCollection.libraryID = targetLibraryID;
c.clone(false, newCollection);
let newCollection = c.clone(targetLibraryID);
if (parentID) {
newCollection.parentID = parentID;
}

View file

@ -533,34 +533,31 @@ Zotero.Collection.prototype.diff = function (collection, includeMatches) {
/**
* Returns an unsaved copy of the collection
* Returns an unsaved copy of the collection without id and key
*
* Does not copy parent collection or child items
*
* @param {Boolean} [includePrimary=false]
* @param {Zotero.Collection} [newCollection=null]
* Doesn't duplicate subcollections or items, because the collection isn't saved
*/
Zotero.Collection.prototype.clone = function (includePrimary, newCollection) {
Zotero.Collection.prototype.clone = function (libraryID) {
Zotero.debug('Cloning collection ' + this.id);
if (newCollection) {
var sameLibrary = newCollection.libraryID == this.libraryID;
}
else {
var newCollection = new this.constructor;
var sameLibrary = true;
if (includePrimary) {
newCollection.id = this.id;
newCollection.libraryID = this.libraryID;
newCollection.key = this.key;
// TODO: This isn't used, but if it were, it should probably include
// parent collection and child items
}
if (libraryID !== undefined && libraryID !== null && typeof libraryID !== 'number') {
throw new Error("libraryID must be null or an integer");
}
newCollection.name = this.name;
if (libraryID === undefined || libraryID === null) {
libraryID = this.libraryID;
}
var sameLibrary = libraryID == this.libraryID;
var newCollection = new Zotero.Collection;
newCollection.libraryID = libraryID;
var json = this.toJSON();
if (!sameLibrary) {
delete json.parentCollection;
delete json.relations;
}
newCollection.fromJSON(json);
return newCollection;
}

View file

@ -254,18 +254,7 @@ Zotero.Search.prototype._finalizeSave = Zotero.Promise.coroutine(function* (env)
Zotero.Search.prototype.clone = function (libraryID) {
var s = new Zotero.Search();
s.libraryID = libraryID === undefined ? this.libraryID : libraryID;
var conditions = this.getConditions();
for (let condition of Object.values(conditions)) {
var name = condition.mode ?
condition.condition + '/' + condition.mode :
condition.condition
s.addCondition(name, condition.operator, condition.value,
condition.required);
}
s.fromJSON(this.toJSON());
return s;
};