From 46a5e4b19568906bcac0569f14869981735ef50f Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 21 Apr 2007 21:18:05 +0000 Subject: [PATCH] Fixes #621, saved search displays child items in black even if only parent item matches search criteria Addresses #597, Don't iterate Array with for...in Also fixes child items displaying twice in saved search reports and adds proper collation support in reports --- chrome/content/zotero/xpcom/search.js | 48 +++++++++++++++------------ components/zotero-protocol-handler.js | 42 +++++++++++++++-------- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/chrome/content/zotero/xpcom/search.js b/chrome/content/zotero/xpcom/search.js index 473be0a292..9f485cdef7 100644 --- a/chrome/content/zotero/xpcom/search.js +++ b/chrome/content/zotero/xpcom/search.js @@ -22,6 +22,7 @@ Zotero.Search = function(savedSearchID){ this._scope = null; + this._scopeIncludeChildren = null; this._sql = null; this._sqlParams = null; this._maxSearchConditionID = 0; @@ -235,8 +236,9 @@ Zotero.Search.prototype.addCondition = function(condition, operator, value, requ /* * Sets scope of search to the results of the passed Search object */ -Zotero.Search.prototype.setScope = function (searchObj) { +Zotero.Search.prototype.setScope = function (searchObj, includeChildren) { this._scope = searchObj; + this._scopeIncludeChildren = includeChildren; } @@ -323,6 +325,21 @@ Zotero.Search.prototype.search = function(asTempTable){ this._buildQuery(); } + var joinMode = 'all'; + for each(var condition in this._conditions) { + switch (condition.condition) { + case 'joinMode': + if (condition.operator == 'any') { + joinMode = 'any'; + } + break; + + case 'blockStart': + var hasQuicksearch = true; + break; + } + } + if (this._scope) { // If subsearch has post-search filter, run and insert ids into temp table if (this._scope.hasPostSearchFilter()) { @@ -343,15 +360,18 @@ Zotero.Search.prototype.search = function(asTempTable){ Zotero.DB.query(sql); } - // Search ids in temp table and their child items + // Search ids in temp table var sql = "SELECT itemID FROM items WHERE itemID IN (" + this._sql + ") " + "AND (" - + "itemID IN (SELECT itemID FROM " + tmpTable + ") OR " - + "itemID IN (SELECT itemID FROM itemAttachments" + + "itemID IN (SELECT itemID FROM " + tmpTable + ")"; + + if (this._scopeIncludeChildren) { + sql += " OR itemID IN (SELECT itemID FROM itemAttachments" + " WHERE sourceItemID IN (SELECT itemID FROM " + tmpTable + ")) OR " + "itemID IN (SELECT itemID FROM itemNotes" - + " WHERE sourceItemID IN (SELECT itemID FROM " + tmpTable + "))" - + ")"; + + " WHERE sourceItemID IN (SELECT itemID FROM " + tmpTable + "))"; + } + sql += ")"; var ids = Zotero.DB.columnQuery(sql, this._sqlParams); Zotero.DB.query("DROP TABLE " + tmpTable); @@ -363,25 +383,9 @@ Zotero.Search.prototype.search = function(asTempTable){ //Zotero.debug('IDs from main search: '); //Zotero.debug(ids); - var joinMode = 'all'; - for each(var condition in this._conditions) { - if (condition.condition == 'joinMode') { - if (condition.operator == 'any') { - joinMode = 'any'; - } - break; - } - } //Zotero.debug('Join mode: ' + joinMode); - for each(var condition in this._conditions) { - if (condition.condition == 'blockStart') { - var hasQuicksearch = true; - break; - } - } - // Filter results with fulltext search // // If join mode ALL, return the (union of main and fulltext word search) diff --git a/components/zotero-protocol-handler.js b/components/zotero-protocol-handler.js index d39855ce8a..b27410850f 100644 --- a/components/zotero-protocol-handler.js +++ b/components/zotero-protocol-handler.js @@ -107,19 +107,28 @@ function ChromeExtensionHandler() { case 'search': var s = new Zotero.Search(ids); - var items = Zotero.Items.get(s.search()); + var ids = s.search(); break; case 'items': case 'item': - var items = Zotero.Items.get(ids.split('-')); + var ids = ids.split('-'); break; default: var type = 'library'; var s = new Zotero.Search(); s.addCondition('noChildren', 'true'); - var items = Zotero.Items.get(s.search()); + var ids = s.search(); + } + + var results = Zotero.Items.get(ids); + var items = []; + // Only include parent items + for (var i=0; i b[sorts[index].field] ? - sorts[index].order - : a[sorts[index].field] < b[sorts[index].field] ? - (sorts[index].order * -1) - : 0; + var cmp = collation.compareString(0, + a.getField(sorts[index].field), + b.getField(sorts[index].field) + ); + if (cmp == 0) { + continue; + } + + var result = cmp * sorts[index].order; index++; } while (result == 0 && sorts[index]); @@ -177,6 +187,10 @@ function ChromeExtensionHandler() { items.sort(compareFunction); + // Convert item objects to export arrays + for (var i=0; i