Add Trash to group libraries

Also:

- Update trash icon properly when adding items to trash or emptying trash
- (dev) Zotero.Items.getDeleted() and Zotero.Items.emptyTrash() parameter order changed
- (dev) Zotero.Items.getDeleted() now returns an empty array rather than FALSE on no results

Group trash does not yet auto-empty
This commit is contained in:
Dan Stillman 2011-08-30 23:35:12 +00:00
parent 3b1cc39f21
commit 09e934128f
7 changed files with 84 additions and 59 deletions

View File

@ -41,10 +41,11 @@ Zotero.CollectionTreeView = function()
this._treebox = null; this._treebox = null;
this._highlightedRows = {}; this._highlightedRows = {};
this._unregisterID = Zotero.Notifier.registerObserver(this, ['collection', 'search', 'share', 'group', 'bucket']); this._unregisterID = Zotero.Notifier.registerObserver(this, ['collection', 'search', 'share', 'group', 'trash', 'bucket']);
this._containerState = {}; this._containerState = {};
this._duplicateLibraries = []; this._duplicateLibraries = [];
this._unfiledLibraries = []; this._unfiledLibraries = [];
this._trashNotEmpty = {};
} }
/* /*
@ -148,7 +149,6 @@ Zotero.CollectionTreeView.prototype.refresh = function()
var self = this; var self = this;
var library = { var library = {
id: null,
libraryID: null libraryID: null
}; };
@ -256,11 +256,14 @@ Zotero.CollectionTreeView.prototype.notify = function(action, type, ids)
return; return;
} }
if (action == 'refresh' && type == 'trash') {
this._trashNotEmpty[ids[0]] = !!Zotero.Items.getDeleted(ids[0]).length;
return;
}
this.selection.selectEventsSuppressed = true; this.selection.selectEventsSuppressed = true;
var savedSelection = this.saveSelection(); var savedSelection = this.saveSelection();
var madeChanges = false;
if (action == 'delete') { if (action == 'delete') {
var selectedIndex = this.selection.count ? this.selection.selectedIndex : 0; var selectedIndex = this.selection.count ? this.selection.selectedIndex : 0;
@ -432,20 +435,20 @@ Zotero.CollectionTreeView.prototype.getCellText = function(row, column)
Zotero.CollectionTreeView.prototype.getImageSrc = function(row, col) Zotero.CollectionTreeView.prototype.getImageSrc = function(row, col)
{ {
var source = this._getItemAtRow(row); var itemGroup = this._getItemAtRow(row);
var collectionType = source.type; var collectionType = itemGroup.type;
switch (collectionType) { switch (collectionType) {
case 'trash': case 'trash':
if (this.trashNotEmpty) { if (this._trashNotEmpty[itemGroup.ref.libraryID ? itemGroup.ref.libraryID : 0]) {
collectionType += '-full'; collectionType += '-full';
} }
break; break;
case 'header': case 'header':
if (source.ref.id == 'group-libraries-header') { if (itemGroup.ref.id == 'group-libraries-header') {
collectionType = 'groups'; collectionType = 'groups';
} }
else if (source.ref.id == 'commons-header') { else if (itemGroup.ref.id == 'commons-header') {
collectionType = 'commons'; collectionType = 'commons';
} }
break; break;
@ -490,11 +493,13 @@ Zotero.CollectionTreeView.prototype.isContainerEmpty = function(row)
} }
if (itemGroup.isGroup()) { if (itemGroup.isGroup()) {
var libraryID = itemGroup.ref.libraryID; var libraryID = itemGroup.ref.libraryID;
libraryID = (libraryID ? libraryID : 0) + '';
return !itemGroup.ref.hasCollections() return !itemGroup.ref.hasCollections()
&& !itemGroup.ref.hasSearches() && !itemGroup.ref.hasSearches()
&& this._duplicateLibraries.indexOf(libraryID + '') == -1 && this._duplicateLibraries.indexOf(libraryID) == -1
&& this._unfiledLibraries.indexOf(libraryID + '') == -1; && this._unfiledLibraries.indexOf(libraryID) == -1
&& this.hideSources.indexOf('trash') != -1;
} }
if (itemGroup.isCollection()) { if (itemGroup.isCollection()) {
return !itemGroup.ref.hasChildCollections(); return !itemGroup.ref.hasChildCollections();
@ -819,17 +824,16 @@ Zotero.CollectionTreeView.prototype._expandRow = function (row, forceOpen) {
if (isGroup) { if (isGroup) {
var group = Zotero.Groups.getByLibraryID(libraryID); var group = Zotero.Groups.getByLibraryID(libraryID);
var collections = group.getCollections(); var collections = group.getCollections();
var showTrash = false;
} }
else { else {
var collections = Zotero.getCollections(itemGroup.ref.id); var collections = Zotero.getCollections(itemGroup.ref.id);
var showTrash = this.hideSources.indexOf('trash') == -1;
} }
var savedSearches = Zotero.Searches.getAll(libraryID); var savedSearches = Zotero.Searches.getAll(libraryID);
var showDuplicates = this.hideSources.indexOf('duplicates') == -1 var showDuplicates = this.hideSources.indexOf('duplicates') == -1
&& this._duplicateLibraries.indexOf(intLibraryID + '') != -1; && this._duplicateLibraries.indexOf(intLibraryID + '') != -1;
var showUnfiled = this._unfiledLibraries.indexOf(intLibraryID + '') != -1; var showUnfiled = this._unfiledLibraries.indexOf(intLibraryID + '') != -1;
var showTrash = this.hideSources.indexOf('trash') == -1;
// If not a manual open and either the library is set to be hidden // If not a manual open and either the library is set to be hidden
// or this is a collection that isn't explicitly opened, // or this is a collection that isn't explicitly opened,
@ -841,7 +845,7 @@ Zotero.CollectionTreeView.prototype._expandRow = function (row, forceOpen) {
return 0; return 0;
} }
var startOpen = !!(collections.length || savedSearches.length || showDuplicates || showUnfiled); var startOpen = !!(collections.length || savedSearches.length || showDuplicates || showUnfiled || showTrash);
// If this isn't a manual open, set the initial state depending on whether // If this isn't a manual open, set the initial state depending on whether
// there are child nodes // there are child nodes
@ -881,7 +885,7 @@ Zotero.CollectionTreeView.prototype._expandRow = function (row, forceOpen) {
// Duplicate items // Duplicate items
if (showDuplicates) { if (showDuplicates) {
var d = new Zotero.Duplicates(isGroup ? group.libraryID : 0); var d = new Zotero.Duplicates(intLibraryID);
this._showRow(new Zotero.ItemGroup('duplicates', d), level, row + 1 + newRows); this._showRow(new Zotero.ItemGroup('duplicates', d), level, row + 1 + newRows);
newRows++; newRows++;
} }
@ -890,22 +894,25 @@ Zotero.CollectionTreeView.prototype._expandRow = function (row, forceOpen) {
if (showUnfiled) { if (showUnfiled) {
var s = new Zotero.Search; var s = new Zotero.Search;
if (isGroup) { if (isGroup) {
s.libraryID = group.libraryID; s.libraryID = libraryID;
} }
s.name = Zotero.getString('pane.collections.unfiled'); s.name = Zotero.getString('pane.collections.unfiled');
s.addCondition('libraryID', 'is', isGroup ? group.libraryID : null); s.addCondition('libraryID', 'is', libraryID);
s.addCondition('unfiled', 'true'); s.addCondition('unfiled', 'true');
this._showRow(new Zotero.ItemGroup('unfiled', s), level, row + 1 + newRows); this._showRow(new Zotero.ItemGroup('unfiled', s), level, row + 1 + newRows);
newRows++; newRows++;
} }
if (showTrash) { if (showTrash) {
var deletedItems = Zotero.Items.getDeleted(); var deletedItems = Zotero.Items.getDeleted(libraryID);
if (deletedItems || Zotero.Prefs.get("showTrashWhenEmpty")) { if (deletedItems.length || Zotero.Prefs.get("showTrashWhenEmpty")) {
this._showRow(new Zotero.ItemGroup('trash', false), level, row + 1 + newRows); var ref = {
libraryID: libraryID
};
this._showRow(new Zotero.ItemGroup('trash', ref), level, row + 1 + newRows);
newRows++; newRows++;
} }
this.trashNotEmpty = !!deletedItems; this._trashNotEmpty[intLibraryID] = !!deletedItems.length;
} }
return newRows; return newRows;
@ -1785,7 +1792,7 @@ Zotero.ItemGroup.prototype.__defineGetter__('id', function () {
return 'U' + (this.ref.libraryID ? this.ref.libraryID : 0); return 'U' + (this.ref.libraryID ? this.ref.libraryID : 0);
case 'trash': case 'trash':
return 'T'; return 'T' + (this.ref.libraryID ? this.ref.libraryID : 0);
case 'header': case 'header':
if (this.ref.id == 'group-libraries-header') { if (this.ref.id == 'group-libraries-header') {
@ -2005,6 +2012,7 @@ Zotero.ItemGroup.prototype.getSearchObject = function() {
includeScopeChildren = true; includeScopeChildren = true;
} }
else if (this.isTrash()) { else if (this.isTrash()) {
s.addCondition('libraryID', 'is', this.ref.libraryID);
s.addCondition('deleted', 'true'); s.addCondition('deleted', 'true');
} }
else { else {

View File

@ -2053,7 +2053,7 @@ Zotero.Item.prototype.save = function() {
} }
} }
// Refresh trash // Refresh trash
Zotero.Notifier.trigger('refresh', 'collection', 0); Zotero.Notifier.trigger('refresh', 'trash', this.libraryID ? this.libraryID : 0);
if (this._deleted) { if (this._deleted) {
Zotero.Notifier.trigger('trash', 'item', this.id); Zotero.Notifier.trigger('trash', 'item', this.id);
} }

View File

@ -125,16 +125,34 @@ Zotero.Items = new function() {
/** /**
* Return items marked as deleted * Return items marked as deleted
* *
* @param {Boolean} asIDs Return itemIDs instead of * @param {Number|NULL} libraryID
* @param {Boolean} asIDs Return itemIDs instead of
* Zotero.Item objects * Zotero.Item objects
* @return {Zotero.Item[]|Integer[]} * @return {Zotero.Item[]|Integer[]}
*/ */
this.getDeleted = function (asIDs, days) { this.getDeleted = function (libraryID, asIDs, days) {
var sql = "SELECT itemID FROM deletedItems"; // Throw warning for pre-3.0b3 arguments
if (days) { if (typeof libraryID == 'boolean') {
sql += " WHERE dateDeleted<=DATE('NOW', '-" + parseInt(days) + " DAYS')"; throw new Error("libraryID must be a number or null");
}
var sql = "SELECT itemID FROM items JOIN deletedItems USING (itemID) "
+ "WHERE libraryID" + (libraryID ? "=?" : " IS NULL");
if (days) {
sql += " AND dateDeleted<=DATE('NOW', '-" + parseInt(days) + " DAYS')";
}
if (libraryID) {
var ids = Zotero.DB.columnQuery(sql, [libraryID]);
}
else {
var ids = Zotero.DB.columnQuery(sql);
}
if (!ids) {
return [];
} }
var ids = Zotero.DB.columnQuery(sql);
if (asIDs) { if (asIDs) {
return ids; return ids;
} }
@ -467,15 +485,15 @@ Zotero.Items = new function() {
/** /**
* @param {Integer} days Only delete items deleted more than this many days ago * @param {Integer} days Only delete items deleted more than this many days ago
*/ */
this.emptyTrash = function (days) { this.emptyTrash = function (libraryID, days) {
Zotero.DB.beginTransaction(); Zotero.DB.beginTransaction();
var deletedIDs = this.getDeleted(true, days); var deletedIDs = this.getDeleted(libraryID, true, days);
if (deletedIDs) { if (deletedIDs.length) {
this.erase(deletedIDs); this.erase(deletedIDs);
Zotero.Notifier.trigger('refresh', 'collection', 0); Zotero.Notifier.trigger('refresh', 'collection', libraryID ? libraryID : 0);
} }
Zotero.DB.commitTransaction(); Zotero.DB.commitTransaction();
return deletedIDs ? deletedIDs.length : 0; return deletedIDs.length;
} }

View File

@ -1546,10 +1546,7 @@ Zotero.ItemTreeView.prototype.deleteSelection = function (force)
else if (itemGroup.isTrash()) { else if (itemGroup.isTrash()) {
Zotero.Items.erase(ids); Zotero.Items.erase(ids);
} }
else if (itemGroup.isGroup() || (force && itemGroup.isWithinGroup())) { else if (itemGroup.isLibrary(true) || force) {
Zotero.Items.erase(ids);
}
else if (itemGroup.isLibrary() || force) {
Zotero.Items.trash(ids); Zotero.Items.trash(ids);
} }
else if (itemGroup.isCollection()) { else if (itemGroup.isCollection()) {

View File

@ -28,7 +28,7 @@ Zotero.Notifier = new function(){
var _disabled = false; var _disabled = false;
var _types = [ var _types = [
'collection', 'creator', 'search', 'share', 'share-items', 'item', 'collection', 'creator', 'search', 'share', 'share-items', 'item',
'collection-item', 'item-tag', 'tag', 'group', 'bucket', 'relation' 'collection-item', 'item-tag', 'tag', 'group', 'trash', 'bucket', 'relation'
]; ];
var _inTransaction; var _inTransaction;
var _locked = false; var _locked = false;
@ -277,7 +277,7 @@ Zotero.Notifier = new function(){
} }
} }
if (runQueue[type][event].ids.length) { if (runQueue[type][event].ids.length || event == 'refresh') {
totals += ' [' + event + '-' + type + ': ' + runQueue[type][event].ids.length + ']'; totals += ' [' + event + '-' + type + ': ' + runQueue[type][event].ids.length + ']';
} }
} }
@ -290,7 +290,7 @@ Zotero.Notifier = new function(){
for (var type in runQueue) { for (var type in runQueue) {
for (var event in runQueue[type]) { for (var event in runQueue[type]) {
if (runQueue[type][event].ids.length) { if (runQueue[type][event].ids.length || event == 'refresh') {
trigger(event, type, runQueue[type][event].ids, trigger(event, type, runQueue[type][event].ids,
runQueue[type][event].data, true); runQueue[type][event].data, true);
} }

View File

@ -1383,7 +1383,7 @@ if(appInfo.platformVersion[0] >= 2) {
var returns = []; var returns = [];
for (var i=0; i<args.length; i++){ for (var i=0; i<args.length; i++){
if (!args[i]) { if (!args[i] && args[i] !== 0) {
continue; continue;
} }
if (args[i].constructor.name == 'Array') { if (args[i].constructor.name == 'Array') {

View File

@ -404,7 +404,8 @@ var ZoteroPane = new function()
var days = Zotero.Prefs.get('trashAutoEmptyDays'); var days = Zotero.Prefs.get('trashAutoEmptyDays');
if (days) { if (days) {
var d = new Date(); var d = new Date();
var deleted = Zotero.Items.emptyTrash(days); // TODO: empty group trashes if permissions
var deleted = Zotero.Items.emptyTrash(null, days);
var d2 = new Date(); var d2 = new Date();
Zotero.debug("Emptied old items from trash in " + (d2 - d) + " ms"); Zotero.debug("Emptied old items from trash in " + (d2 - d) + " ms");
} }
@ -1430,13 +1431,13 @@ var ZoteroPane = new function()
) )
}; };
if (itemGroup.isLibrary()) { if (itemGroup.isLibrary(true)) {
// In library, don't prompt if meta key was pressed // In library, don't prompt if meta key was pressed
var prompt = force ? false : toTrash; var prompt = force ? false : toTrash;
} }
else if (itemGroup.isCollection()) { else if (itemGroup.isCollection()) {
// In collection, only prompt if trashing // In collection, only prompt if trashing
var prompt = force ? (itemGroup.isWithinGroup() ? toDelete : toTrash) : false; var prompt = force ? toTrash : false;
} }
else if (itemGroup.isSearch() || itemGroup.isUnfiled() || itemGroup.isDuplicates()) { else if (itemGroup.isSearch() || itemGroup.isUnfiled() || itemGroup.isDuplicates()) {
if (!force) { if (!force) {
@ -1458,10 +1459,6 @@ var ZoteroPane = new function()
} }
var prompt = toDelete; var prompt = toDelete;
} }
// This should be changed if/when groups get trash
else if (itemGroup.isGroup()) {
var prompt = toDelete;
}
else if (itemGroup.isBucket()) { else if (itemGroup.isBucket()) {
var prompt = toDelete; var prompt = toDelete;
} }
@ -1570,6 +1567,8 @@ var ZoteroPane = new function()
this.emptyTrash = function () { this.emptyTrash = function () {
var libraryID = this.getSelectedLibraryID();
var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService); .getService(Components.interfaces.nsIPromptService);
@ -1580,7 +1579,7 @@ var ZoteroPane = new function()
+ Zotero.getString('general.actionCannotBeUndone') + Zotero.getString('general.actionCannotBeUndone')
); );
if (result) { if (result) {
Zotero.Items.emptyTrash(); Zotero.Items.emptyTrash(libraryID);
Zotero.purgeDataObjects(true); Zotero.purgeDataObjects(true);
} }
} }
@ -1909,15 +1908,17 @@ var ZoteroPane = new function()
this.getSelectedLibraryID = function () { this.getSelectedLibraryID = function () {
var group = this.getSelectedGroup(); var itemGroup = this.getItemGroup();
if (group) { var groupID = this.getSelectedGroup(true);
return group.libraryID; if (groupID) {
return groupID;
} }
var collection = this.getSelectedCollection(); else if (itemGroup.isWithinGroup()) {
if (collection) { return itemGroup.ref.libraryID;
return collection.libraryID; }
else {
return null;
} }
return null;
} }
@ -1960,7 +1961,8 @@ var ZoteroPane = new function()
if (this.collectionsView.selection if (this.collectionsView.selection
&& this.collectionsView.selection.count > 0 && this.collectionsView.selection.count > 0
&& this.collectionsView.selection.currentIndex != -1) { && this.collectionsView.selection.currentIndex != -1) {
var itemGroup = this.collectionsView._getItemAtRow(this.collectionsView.selection.currentIndex);
var itemGroup = this.getItemGroup();
if (itemGroup && itemGroup.isGroup()) { if (itemGroup && itemGroup.isGroup()) {
return asID ? itemGroup.ref.id : itemGroup.ref; return asID ? itemGroup.ref.id : itemGroup.ref;
} }