diff --git a/chrome/chromeFiles/content/scholar/xpcom/data_access.js b/chrome/chromeFiles/content/scholar/xpcom/data_access.js index 99d625ae9..65f8ba46b 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/data_access.js +++ b/chrome/chromeFiles/content/scholar/xpcom/data_access.js @@ -687,6 +687,11 @@ Scholar.Item.prototype.erase = function(){ } +Scholar.Item.prototype.isCollection = function(){ + return false; +} + + Scholar.Item.prototype.toString = function(){ return this.getTitle(); } @@ -1002,7 +1007,7 @@ Scholar.Collection.prototype.getName = function(){ } /** -* Returns the parent collection +* Returns collectionID of the parent collection **/ Scholar.Collection.prototype.getParent = function(){ return this._parent; @@ -1021,6 +1026,28 @@ Scholar.Collection.prototype.hasChildItems = function(){ return !!(parseInt(this._hasChildItems)); } +/** +* Rename the collection +* +* Returns true on success, or false on error +**/ +Scholar.Collection.prototype.rename = function(name){ + if (!name){ + return false; + } + + var sql = "UPDATE collections SET collectionName=? " + + "WHERE collectionID=?"; + Scholar.DB.query(sql, [{'string':name},{'int':this.getID()}]); + this._name = name; + + Scholar.Notifier.trigger('modify', 'collection', this.getID()); + return true; +} + +/** +* Add an item to the collection +**/ Scholar.Collection.prototype.addItem = function(itemID){ Scholar.DB.beginTransaction(); @@ -1045,6 +1072,9 @@ Scholar.Collection.prototype.addItem = function(itemID){ } +/** +* Remove an item from the collection (does not delete item from library) +**/ Scholar.Collection.prototype.removeItem = function(itemID){ Scholar.DB.beginTransaction(); @@ -1081,6 +1111,9 @@ Scholar.Collection.prototype.removeItem = function(itemID){ } +/** +* Check if an item belongs to the collection +**/ Scholar.Collection.prototype.hasItem = function(itemID){ if (!this._childItemsLoaded){ this._loadChildItems(); @@ -1089,10 +1122,8 @@ Scholar.Collection.prototype.hasItem = function(itemID){ } - - /** -* Deletes a collection and all descendent collections and items +* Deletes collection and all descendent collections and items **/ Scholar.Collection.prototype.erase = function(deleteItems){ Scholar.DB.beginTransaction(); @@ -1134,6 +1165,11 @@ Scholar.Collection.prototype.erase = function(deleteItems){ } +Scholar.Collection.prototype.isCollection = function(){ + return true; +} + + Scholar.Collection.prototype._loadChildItems = function(){ this._childItems = new Scholar.Hash(); @@ -1197,6 +1233,7 @@ Scholar.Collections = new function(){ var _collectionsLoaded = false; this.get = get; + this.add = add; this.reloadAll = reloadAll; this.unload = unload; @@ -1211,6 +1248,35 @@ Scholar.Collections = new function(){ } + function add(name, parent){ + if (!name){ + return false; + } + + Scholar.DB.beginTransaction(); + + if (parent && !this.get(parent)){ + Scholar.DB.rollbackTransaction(); + throw('Cannot add collection to invalid parent ' + parent); + } + + var parentParam = parent ? {'int':parent} : {'null':true}; + + var rnd = Scholar.getRandomID('collections', 'collectionID'); + + var sql = "INSERT INTO collections VALUES (?,?,?)"; + var sqlValues = [ {'int':rnd}, {'string':name}, parentParam ]; + Scholar.DB.query(sql, sqlValues); + + Scholar.DB.commitTransaction(); + + _load(rnd); + Scholar.Notifier.trigger('add', 'collection', rnd); + + return this.get(rnd); + } + + /** * Clears internal cache and reloads collection data from DB **/ @@ -1247,10 +1313,15 @@ Scholar.Collections = new function(){ + "collectionID=C.collectionID)!=0 AS hasChildItems " + "FROM collections C"; + var ids = Scholar.flattenArguments(arguments) + if (ids.length){ + sql += " WHERE collectionID IN (" + ids.join() + ")"; + } + var result = Scholar.DB.query(sql); if (!result){ - throw ('No collections exist'); + throw ('No collections found'); } for (var i=0; i