Make Collection.getDescendents() a[n officially] public method and add second param to limit results to 'collection' or 'item'

This commit is contained in:
Dan Stillman 2006-08-05 06:39:15 +00:00
parent 701762a11f
commit 8dd972dea1

View file

@ -2349,7 +2349,7 @@ Scholar.Collection.prototype.hasItem = function(itemID){
Scholar.Collection.prototype.hasDescendent = function(type, id){ Scholar.Collection.prototype.hasDescendent = function(type, id){
var descendents = this._getDescendents(); var descendents = this.getDescendents();
for (var i=0, len=descendents.length; i<len; i++){ for (var i=0, len=descendents.length; i<len; i++){
if (descendents[i]['type']==type && descendents[i]['id']==id){ if (descendents[i]['type']==type && descendents[i]['id']==id){
return true; return true;
@ -2365,7 +2365,7 @@ Scholar.Collection.prototype.hasDescendent = function(type, id){
Scholar.Collection.prototype.erase = function(deleteItems){ Scholar.Collection.prototype.erase = function(deleteItems){
Scholar.DB.beginTransaction(); Scholar.DB.beginTransaction();
var descendents = this._getDescendents(); var descendents = this.getDescendents();
var collections = [this.getID()], items = []; var collections = [this.getID()], items = [];
for(var i=0, len=descendents.length; i<len; i++){ for(var i=0, len=descendents.length; i<len; i++){
@ -2409,7 +2409,7 @@ Scholar.Collection.prototype.isCollection = function(){
Scholar.Collection.prototype.toArray = function(){ Scholar.Collection.prototype.toArray = function(){
return this._getDescendents(true); return this.getDescendents(true);
} }
@ -2436,7 +2436,7 @@ Scholar.Collection.prototype._loadChildItems = function(){
* *
* nested: Return multidimensional array with 'children' nodes instead of flat array * nested: Return multidimensional array with 'children' nodes instead of flat array
**/ **/
Scholar.Collection.prototype._getDescendents = function(nested){ Scholar.Collection.prototype.getDescendents = function(nested, type){
var toReturn = new Array(); var toReturn = new Array();
// 0 == collection // 0 == collection
@ -2450,14 +2450,16 @@ Scholar.Collection.prototype._getDescendents = function(nested){
for(var i=0, len=children.length; i<len; i++){ for(var i=0, len=children.length; i<len; i++){
switch (children[i]['type']){ switch (children[i]['type']){
case 0: case 0:
toReturn.push({ if (!type || type=='collection'){
id: children[i]['id'], toReturn.push({
name: children[i]['collectionName'], id: children[i]['id'],
type: 'collection' name: children[i]['collectionName'],
}); type: 'collection'
});
}
var descendents = var descendents =
Scholar.Collections.get(children[i]['id'])._getDescendents(nested); Scholar.Collections.get(children[i]['id']).getDescendents(nested, type);
if (nested){ if (nested){
toReturn[toReturn.length-1]['children'] = descendents; toReturn[toReturn.length-1]['children'] = descendents;
@ -2470,10 +2472,12 @@ Scholar.Collection.prototype._getDescendents = function(nested){
break; break;
case 1: case 1:
toReturn.push({ if (!type || type=='item'){
id: children[i]['id'], toReturn.push({
type: 'item' id: children[i]['id'],
}); type: 'item'
});
}
break; break;
} }
} }