Index: pro-bachkim-filespace/sourcecode/assets/js/tree.js
===================================================================
--- pro-bachkim-filespace/sourcecode/assets/js/tree.js	(revision 66)
+++ pro-bachkim-filespace/sourcecode/assets/js/tree.js	(revision 70)
@@ -20,4 +20,5 @@
 			
 			var createNode = function (node) {
+				
 				if( !node ) var node = {};
 				if( node.item == undefined ) node.item = null; //item.id, item.name, item.parentID
@@ -25,6 +26,6 @@
 				if( node.defaultStatus == undefined ) node.defaultStatus = 0; //0: collapsed; 1:expanded;
 				if( node.showContextMenu == undefined ) node.showContextMenu = true; //0: collapsed; 1:expanded;
-				
-				var parentNode = node.isRoot == true ? oContainer : $(oContainer).find('UL.vstree > LI[rel="folder"] > A#' + node.item.parentID);
+				if (node.callback == undefined ) node.callback = null;
+				
 				var nodeClass = node.isRoot == true ? 'home':'directory';
 				var hidden = (node.isRoot == true || node.item.parentID == 0 ) ? null:'display:none';
@@ -32,14 +33,52 @@
 				
 				var a = $('<a></a>',{id: node.item.id,href: '#',rel: node.item.name,text: node.item.name});
-				var li = $('<li></li>',{'class': nodeClass,rel: 'folder'}).append(a);			
-				var ul = $('<ul></ul>',{'class':'vstree',style:hidden}).append(li);
-				$(li).addClass(defaulState);
-				
+				var li = $('<li></li>',{'class': nodeClass,rel: 'folder'})
+																		.addClass(defaulState)
+																		.append(a);
+				var ul = null;
 				bindNodeEvents(a);
 				
 				treeNodes[node.item.id] = {id:node.item.id, name:node.item.name, parentID:node.item.parentID, html:ul, status:defaulState, isRoot:node.isRoot};
-
-				if (node.isRoot == false) $(parentNode).parent().append(ul); else $(parentNode).append(ul);
-				return (parentNode.length == 1);
+				if (node.isRoot == true) {
+					ul = $('<ul></ul>',{'class':'vstree',rel:'node-1',style:hidden}).append(li);
+					$(oContainer).append(ul);
+				}
+				else {
+					var preUL = $(oContainer).find('UL.vstree[rel^="node-1"]');
+					
+					if (preUL != null) {
+						if (node.item.parentID == 0) {
+							var groupUL = $(oContainer).find('UL.vstree[rel^="node0"]');
+							if ($(groupUL).length == 0) {
+								ul = $('<ul></ul>',{'class':'vstree',rel:'node0',style:hidden}).append(li);
+								$(preUL).find('> LI.home').append(ul);
+							}
+							else {
+								$(groupUL).append(li);
+							}
+						}
+						else {
+							var liArray = $(oContainer).find('UL.vstree LI[rel="folder"]');
+							for (var i = 0; i < $(liArray).length; i++) {
+								var id = $(liArray[i]).find('A').attr('id');
+								
+								if (id == node.item.parentID) {
+									var groupUL = $(oContainer).find('UL.vstree[rel^="node' + id + '"]');
+									if ($(groupUL).length == 0) {
+										ul = $('<ul></ul>',{'class':'vstree',rel:'node'+node.item.parentID,style:hidden}).append(li);
+										$(liArray[i]).append(ul);
+									}
+									else {
+										$(groupUL).append(li);
+									}
+								}
+							}
+						}
+					}
+				}
+				
+				if (node.callback != null) node.callback(this);
+				//return (parentNode.length == 1);
+				return true;
 			};
 			
@@ -49,64 +88,4 @@
 				//click event
 				$(node).bind("click", function (e){if(e.button == 0)nodeClick(this);return false;});
-								
-				//Press F2 to rename, ESC to cancel rename
-				if (documentBoundEvent == false) {
-					$(oContainer).bind('keydown','keypress', function(e) {
-						switch( e.keyCode ) {
-						case 113:
-							renameInit(selectedNode);
-							break;
-						case 27:
-							renameCancel(selectedNode);
-							break;
-						case 46:
-							var dirID = $(selectedNode).attr('id');
-							var dirObj = {};
-							dirObj.id = treeNodes[dirID].id;
-							dirObj.name = treeNodes[dirID].name;
-							dirObj.parentID = treeNodes[dirID].parentID;
-							dirObj.type = 'directory';
-							if (e.keyCode == 46) o.manager.deleteItem([dirObj]);
-							break;
-						case 13:
-						default:
-							break;
-					}
-					});
-					
-					documentBoundEvent = true;
-				}
-				
-				disabledItemsList = [];
-				
-				$(node).contextMenu({menu: 'treeMenu',disabledItems: disabledItemsList}, 
-					function(action, el, pos) {
-						var dirID = $(el).attr('id');
-						var item = $(treeNodes[dirID].html).find('> LI > A#' + dirID);
-						var dirObj = {};
-						dirObj.id = treeNodes[dirID].id;
-						dirObj.name = treeNodes[dirID].name;
-						dirObj.parentID = treeNodes[dirID].parentID;
-						dirObj.type = 'directory';
-						
-						switch(action) {
-							case 'rename':
-								renameInit(item);
-								break;
-							case 'newfolder':
-								o.manager.createNewFolder();
-								break;
-							case 'share':
-							case 'copy':
-							case 'cut':
-								break;
-							case 'delete':
-								o.manager.deleteItem([dirObj]);
-								break;
-							default:
-								break;
-						}
-				});
-				
 			};
 			
@@ -124,5 +103,5 @@
 				}
 				else {
-					collapNode(node);
+					collapseNode(node);
 					treeNodes[folderID].status = 'collapsed';
 				}
@@ -142,5 +121,5 @@
 			};
 			
-			var collapNode = function (node) {
+			var collapseNode = function (node) {
 				var li = $(node).parent();
 				$(li).find('> UL').slideUp({ duration: o.expandSpeed, easing: o.expandEasing });
@@ -148,9 +127,33 @@
 			};
 			
+			var isCollapsedNode = function (node) {
+				return $(node).parent().hasClass('collapsed');
+			}
+			
+			var nodeIsExisted = function (nodeID) {
+				var node = $(oContainer).find('UL.vstree LI.directory > A#' + nodeID);
+				return ($(node).length > 0) ? node : false;
+			}
+			
 			var initTree = function () {
+				$(oContainer).attr('unselectable','on')
+				     .css({'-moz-user-select':'-moz-none',
+				           '-moz-user-select':'none',
+				           '-o-user-select':'none',
+				           '-khtml-user-select':'none',
+				           '-webkit-user-select':'none',
+				           '-ms-user-select':'none',
+				           'user-select':'none'
+				     }).bind('selectstart', function(){ return false;});
+
 				if (o.data == null) {
 					console.error(o.data, 'No directory to display!');
 					return;
 				}
+				
+				o.data.sort(function (a,b) {
+					return a.parentID - b.parentID;
+				});
+				
 				$(oContainer).find ('.vstree').remove();
 				var homeNode = createNode({
@@ -164,14 +167,4 @@
 				}
 				o.manager.setTreeCurrentNode(0);
-				
-				$(oContainer).attr('unselectable','on')
-			     .css({'-moz-user-select':'-moz-none',
-			           '-moz-user-select':'none',
-			           '-o-user-select':'none',
-			           '-khtml-user-select':'none',
-			           '-webkit-user-select':'none',
-			           '-ms-user-select':'none',
-			           'user-select':'none'
-			     }).bind('selectstart', function(){ return false;}); 
 			};
 			
@@ -179,11 +172,12 @@
 			 * RENAME - START *
 			 ******************/
-			
 			var renameInit = function (node) {
 				var editor = $('<input>',{class:'rename', type: 'text', value:$(node).text()});
 				$(editor).bind('focusout', function(e){renameCancel(node)});
-				$(editor).bind('keypress', function(e){
+				$(editor).bind('keydown', function(e){
 						var keycode = (e.keyCode ? e.keyCode : e.which);
-						if(keycode == '13') renameComplete(node);
+						if (keycode == '27') {
+							renameCancel(node);
+						}else if(keycode == '13') renameComplete(node);
 						e.stopPropagation();
 					});
@@ -199,5 +193,5 @@
 				$(node).find('>INPUT').remove();
 				$(node).text(treeNodes[dirID].name);
-				$(node).select();
+				$(node).focus();
 			}
 			
@@ -212,5 +206,4 @@
 				updateData(dirID, 'rename');
 			}
-			
 			/******************
 			 * RENAME - END *
@@ -222,28 +215,10 @@
 			var destroyNode = function (node) {
 				var dirID = $(node).attr('id');
-				$(node).parent().parent().remove();
+				$(node).parent().remove();
 				delete treeNodes[dirID];
 			}
 			/******************
-			 * DELTE - END *
+			 * DELTE - END    *
 			 ******************/
-			/*******************************
-			 * COPY & PASTE & MOVE - START *
-			 *******************************/
-			var copyNode = function (source){
-
-			}
-			
-			var pasteNode = function (destination) {
-				
-			}
-			
-			var moveNode = function (source, destination) {
-				
-			}
-			/*****************************
-			 * COPY & PASTE & MOVE - END *
-			 *****************************/
-			
 			var updateData = function (id, act) {
 				for (var i = 0; i < o.data.length; i++) {
@@ -263,6 +238,44 @@
 			}
 			
+			this.createNode = function (node) {
+				createNode ({item: node});
+				o.data.sort(function (a,b) {
+					return a.parentID - b.parentID;
+				});
+				if (node.parentID != 0) {
+					var parentNode = $(oContainer).find('UL LI > A#' + node.parentID);				
+					expandNode(parentNode);
+				}
+			}
+			
+			this.activeNode = function (node) {
+				var dirID = $(node).attr('id');
+				selectedNode = hightlightNode(node);
+				o.manager.setTreeCurrentNode(dirID);
+				expandNode(node);
+				$(node).select();
+			}
+
+			this.refeshTree = function () {
+				for (var i = 0; i < o.data.length; i++) {
+					var node = nodeIsExisted(o.data[i].id);
+					var defaulState = isCollapsedNode(node) ? 0:1;
+					if (node == false) 
+						createNode ({item: o.data[i],defaulState:defaulState});
+				}
+
+				o.manager.setTreeCurrentNode($(selectedNode).attr('id'));
+			}
+			
+			this.getSelectedNode = function () {
+				return selectedNode;
+			}
+			
 			this.deletion = function (id) {
 				destroyNode($(oContainer).find('A#'+id));
+			}
+			
+			this.findNodebyID = function (id) {
+				
 			}
 			
@@ -279,4 +292,16 @@
 				return this;
 			};
+
+			this.rename = function (key) {
+				if (selectedNode == null) return false;
+				if (key == 113) {
+					renameInit(selectedNode);
+				}
+				else if (key == 27) {
+					renameCancel(selectedNode);
+				}
+
+				return true;
+			}
 			
 			return this.initialize();
