Added level column in folders table to track hierarchical level needed by treeView
Added getLevel() functions to Scholar.Object and Scholar.Folder -- save() and setPosition() not yet updated Updated sample data to include levels Added orderIndex (from treeOrder table, now) back into Scholar.Object data
This commit is contained in:
parent
d21638c465
commit
e14470db5a
3 changed files with 46 additions and 17 deletions
|
@ -37,6 +37,8 @@ Scholar.Object.prototype.isPrimaryField = function(field){
|
|||
if (!Scholar.Object.primaryFields){
|
||||
Scholar.Object.primaryFields = Scholar.DB.getColumnHash('objects');
|
||||
Scholar.Object.primaryFields['firstCreator'] = true;
|
||||
Scholar.Object.primaryFields['level'] = true;
|
||||
Scholar.Object.primaryFields['orderIndex'] = true;
|
||||
}
|
||||
|
||||
return !!Scholar.Object.primaryFields[field];
|
||||
|
@ -60,9 +62,12 @@ Scholar.Object.prototype.isEditableField = function(field){
|
|||
* Build object from database
|
||||
*/
|
||||
Scholar.Object.prototype.loadFromID = function(id){
|
||||
var sql = 'SELECT O.*, lastName AS firstCreator FROM objects O '
|
||||
+ 'LEFT JOIN objectCreators OC USING (objectID) '
|
||||
+ 'LEFT JOIN creators USING (creatorID) '
|
||||
var sql = 'SELECT O.*, lastName AS firstCreator, F.level, ORD.orderIndex '
|
||||
+ 'FROM objects O '
|
||||
+ 'LEFT JOIN folders F USING (folderID) '
|
||||
+ 'LEFT JOIN treeOrder ORD ON (O.objectID=ORD.id AND isFolder=0) '
|
||||
+ 'LEFT JOIN objectCreators OC ON (O.objectID=OC.objectID) '
|
||||
+ 'LEFT JOIN creators C ON (OC.creatorID=C.creatorID) '
|
||||
+ 'WHERE objectID=' + id + ' AND OC.orderIndex=0';
|
||||
var row = Scholar.DB.rowQuery(sql);
|
||||
this.loadFromRow(row);
|
||||
|
@ -290,6 +295,17 @@ Scholar.Object.prototype.setField = function(field, value, loadIn){
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
* Get the nesting level of the object (0 for root items)
|
||||
*/
|
||||
Scholar.Object.prototype.getLevel = function(){
|
||||
if (!this.getID()){
|
||||
throw ('Cannot get level of unsaved object'); // DEBUG: may change
|
||||
}
|
||||
return this._data['level'];
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Move object to new position and shift surrounding objects
|
||||
*
|
||||
|
@ -696,6 +712,7 @@ Scholar.Object.prototype._loadObjectData = function(){
|
|||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Primary interface for accessing Scholar objects
|
||||
*/
|
||||
|
@ -858,9 +875,13 @@ Scholar.Objects = new function(){
|
|||
return false;
|
||||
}
|
||||
|
||||
var sql = 'SELECT O.*, lastName AS firstCreator FROM objects O '
|
||||
+ 'LEFT JOIN objectCreators OC USING (objectID) '
|
||||
+ 'LEFT JOIN creators USING (creatorID) '
|
||||
// Should be the same as query in Scholar.Object.loadFromID, just without objectID clause
|
||||
var sql = 'SELECT O.*, lastName AS firstCreator, F.level, ORD.orderIndex '
|
||||
+ 'FROM objects O '
|
||||
+ 'LEFT JOIN folders F USING (folderID) '
|
||||
+ 'LEFT JOIN treeOrder ORD ON (O.objectID=ORD.id AND isFolder=0) '
|
||||
+ 'LEFT JOIN objectCreators OC ON (O.objectID=OC.objectID) '
|
||||
+ 'LEFT JOIN creators C ON (OC.creatorID=C.creatorID) '
|
||||
+ 'WHERE OC.orderIndex=0';
|
||||
|
||||
if (arguments[0]!='all'){
|
||||
|
@ -911,6 +932,7 @@ Scholar.Folder.prototype.loadFromRow = function(row){
|
|||
this._id = row['folderID'];
|
||||
this._name = row['folderName'];
|
||||
this._parent = row['parentFolderID'];
|
||||
this._level = row['level'];
|
||||
}
|
||||
|
||||
Scholar.Folder.prototype.getID = function(){
|
||||
|
@ -921,6 +943,10 @@ Scholar.Folder.prototype.getName = function(){
|
|||
return this._name;
|
||||
}
|
||||
|
||||
Scholar.Folder.prototype.getLevel = function(){
|
||||
return this._level;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
|
|
@ -411,11 +411,11 @@ Scholar.DB = new function(){
|
|||
for (var i=parseInt(fromVersion) + 1; i<=toVersion; i++){
|
||||
|
||||
// For now, just wipe and recreate
|
||||
if (i==3){
|
||||
if (i==4){
|
||||
_initializeSchema();
|
||||
}
|
||||
|
||||
if (i==4){
|
||||
if (i==5){
|
||||
// do stuff
|
||||
// _updateDBVersion(i);
|
||||
}
|
||||
|
|
21
schema.sql
21
schema.sql
|
@ -107,11 +107,12 @@
|
|||
folderID INT,
|
||||
folderName TEXT,
|
||||
parentFolderID INT DEFAULT 0,
|
||||
level INT DEFAULT 0,
|
||||
PRIMARY KEY (folderID),
|
||||
FOREIGN KEY (parentFolderID) REFERENCES folders(folderID)
|
||||
);
|
||||
CREATE INDEX parentFolderID ON folders(parentFolderID);
|
||||
INSERT INTO folders VALUES (0, 'root', 0);
|
||||
INSERT INTO folders VALUES (0, 'root', 0, 0);
|
||||
|
||||
DROP TABLE IF EXISTS treeOrder;
|
||||
CREATE TABLE treeOrder (
|
||||
|
@ -215,10 +216,11 @@
|
|||
INSERT INTO "objectCreators" VALUES(7, 8, 2);
|
||||
INSERT INTO "objectCreators" VALUES(9, 11, 1);
|
||||
|
||||
INSERT INTO folders VALUES (1241, 'Test Folder', 0);
|
||||
INSERT INTO folders VALUES (3262, 'Another Test Folder', 0);
|
||||
INSERT INTO folders VALUES (6856, 'A Subfolder', 0);
|
||||
INSERT INTO folders VALUES (7373, 'A Sub-Subfolder!', 3262);
|
||||
INSERT INTO folders VALUES (1241, 'Test Folder', 0, 0);
|
||||
INSERT INTO folders VALUES (3262, 'Another Test Folder', 0, 0);
|
||||
INSERT INTO folders VALUES (6856, 'Yet Another Folder', 0, 0);
|
||||
INSERT INTO folders VALUES (7373, 'A Subfolder!', 6856, 1);
|
||||
INSERT INTO folders VALUES (9233, 'A Sub-subfolder!', 7373, 2);
|
||||
|
||||
INSERT INTO treeOrder VALUES (1, 0, 1);
|
||||
INSERT INTO treeOrder VALUES (3262, 1, 2);
|
||||
|
@ -235,7 +237,8 @@
|
|||
INSERT INTO treeOrder VALUES (13, 0, 13);
|
||||
INSERT INTO treeOrder VALUES (7373, 1, 14);
|
||||
INSERT INTO treeOrder VALUES (15, 0, 15);
|
||||
INSERT INTO treeOrder VALUES (11, 0, 16);
|
||||
INSERT INTO treeOrder VALUES (10, 0, 17);
|
||||
INSERT INTO treeOrder VALUES (1241, 1, 18);
|
||||
INSERT INTO treeOrder VALUES (12, 0, 19);
|
||||
INSERT INTO treeOrder VALUES (9233, 1, 16);
|
||||
INSERT INTO treeOrder VALUES (11, 0, 17);
|
||||
INSERT INTO treeOrder VALUES (10, 0, 18);
|
||||
INSERT INTO treeOrder VALUES (1241, 1, 19);
|
||||
INSERT INTO treeOrder VALUES (12, 0, 20);
|
||||
|
|
Loading…
Reference in a new issue