Items.getTreeRows() now takes an optional second param, type, which can (for the moment) be 'folders' or 'items' to limit the results

New methods: Folder.hasChildFolders() and Folder.hasChildItems()
This commit is contained in:
Dan Stillman 2006-05-23 20:53:09 +00:00
parent edb653cad4
commit e5b25a966b

View File

@ -904,7 +904,7 @@ Scholar.Items = new function(){
* *
* Type can tested with instanceof (e.g. if (obj instanceof Scholar.Folder)) or isFolder() * Type can tested with instanceof (e.g. if (obj instanceof Scholar.Folder)) or isFolder()
*/ */
function getTreeRows(parent){ function getTreeRows(parent, type){
var toReturn = new Array(); var toReturn = new Array();
/* /*
@ -917,7 +917,18 @@ Scholar.Items = new function(){
} }
var sql = 'SELECT * FROM treeStructure TS ' var sql = 'SELECT * FROM treeStructure TS '
+ 'WHERE parentFolderID=' + parent + ' ORDER BY orderIndex'; + 'WHERE parentFolderID=' + parent;
switch (type){
case 'folder':
sql += ' AND isFolder=1';
break;
case 'item':
sql += ' AND isFolder=0';
break;
}
sql += ' ORDER BY orderIndex';
var tree = Scholar.DB.query(sql); var tree = Scholar.DB.query(sql);
@ -1074,8 +1085,11 @@ Scholar.Folder = function(){
Scholar.Folder.prototype.loadFromID = function(id){ Scholar.Folder.prototype.loadFromID = function(id){
// Should be same as query in Scholar.Folders, just with folderID // Should be same as query in Scholar.Folders, just with folderID
var sql = "SELECT folderID, folderName, parentFolderID, " var sql = "SELECT folderID, folderName, parentFolderID, "
+ "(SELECT COUNT(*) FROM treeStructure WHERE parentFolderID=" + + "(SELECT COUNT(*) FROM treeStructure WHERE "
id + ")=0 AS isEmpty FROM folders F " + "parentFolderID=TS.id AND isFolder=1)!=0 AS hasChildFolders, "
+ "(SELECT COUNT(*) FROM treeStructure WHERE "
+ "parentFolderID=TS.id AND isFolder=0)!=0 AS hasChildItems "
+ "FROM folders F "
+ "JOIN treeStructure TS ON (F.folderID=TS.id AND TS.isFolder=1) " + "JOIN treeStructure TS ON (F.folderID=TS.id AND TS.isFolder=1) "
+ "WHERE folderID=" + id; + "WHERE folderID=" + id;
@ -1091,7 +1105,8 @@ Scholar.Folder.prototype.loadFromRow = function(row){
this._id = row['folderID']; this._id = row['folderID'];
this._name = row['folderName']; this._name = row['folderName'];
this._parent = row['parentFolderID']; this._parent = row['parentFolderID'];
this._empty = row['isEmpty']; this._hasChildFolders = row['hasChildFolders'];
this._hasChildItems = row['hasChildItems'];
} }
Scholar.Folder.prototype.getID = function(){ Scholar.Folder.prototype.getID = function(){
@ -1111,9 +1126,18 @@ Scholar.Folder.prototype.getParent = function(){
} }
Scholar.Folder.prototype.isEmpty = function(){ Scholar.Folder.prototype.isEmpty = function(){
return !!parseInt(this._empty); return !(parseInt(this._hasChildFolders)) && !(parseInt(this._hasChildItems));
} }
Scholar.Folder.prototype.hasChildFolders = function(){
return !!(parseInt(this._hasChildFolders));
}
Scholar.Folder.prototype.hasChildItems = function(){
return !!(parseInt(this._hasChildItems));
}
/** /**
* Deletes a folder and all descendent folders and items * Deletes a folder and all descendent folders and items
**/ **/
@ -1232,7 +1256,10 @@ Scholar.Folders = new function(){
function _load(){ function _load(){
var sql = "SELECT folderID, folderName, parentFolderID, " var sql = "SELECT folderID, folderName, parentFolderID, "
+ "(SELECT COUNT(*) FROM treeStructure WHERE " + "(SELECT COUNT(*) FROM treeStructure WHERE "
+ "parentFolderID=TS.id)=0 AS isEmpty FROM folders F " + "parentFolderID=TS.id AND isFolder=1)!=0 AS hasChildFolders, "
+ "(SELECT COUNT(*) FROM treeStructure WHERE "
+ "parentFolderID=TS.id AND isFolder=0)!=0 AS hasChildItems "
+ "FROM folders F "
+ "JOIN treeStructure TS ON (F.folderID=TS.id AND TS.isFolder=1) " + "JOIN treeStructure TS ON (F.folderID=TS.id AND TS.isFolder=1) "
+ "WHERE folderID>0"; // skip 'root' folder + "WHERE folderID>0"; // skip 'root' folder
var result = Scholar.DB.query(sql); var result = Scholar.DB.query(sql);