Index: pro-bachkim-filespace/sourcecode/assets/js/manager.js
===================================================================
--- pro-bachkim-filespace/sourcecode/assets/js/manager.js	(revision 66)
+++ pro-bachkim-filespace/sourcecode/assets/js/manager.js	(revision 70)
@@ -19,4 +19,6 @@
 			if( o.datasource == undefined ) o.datasource = 'ajax';
 			
+			var isDev = false;
+			var contextmenu = null;
 			var oContainer = this;
 			var tree = [];
@@ -25,4 +27,20 @@
 			var maxWidth = 0;
 			var treeCurrentNode = null;
+			var self = this;
+			var oClipBoard = {items:null, act:null};
+			
+			/**
+			 * Toolbar defined
+			 * */
+			var btnNewFolder = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnNewFolder');
+			var btnDel = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnDel');
+			var btnCopy = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnCopy');
+			var btnCut = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnCut');
+			var btnPaste = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnPaste');
+			var btnShare = $('#' + o.toolsbar + ' > DIV.btn-group.social > #btnShare');
+			var btnPreview = $('#' + o.toolsbar + ' > DIV.btn-group.social > #btnPreview');
+			var btnDownload = $('#' + o.toolsbar + ' > DIV.btn-group.creation > #btnDownload');
+			var btnUpload = $('#' + o.toolsbar + ' > DIV.btn-group.creation > #btnUpload');
+			var btnRefresh = $('#' + o.toolsbar + ' > DIV.btn-group.control > #btnRefresh');
 			
 			var sendCommand = function (p) {
@@ -34,5 +52,5 @@
 				if( p.callbackAlways == undefined ) p.callbackAlways = null;
 
-				if (p.script != null && o.datasource == 'ajax') {
+				if (p.script != null && (o.datasource == 'ajax' || isDev)) {
 					$.post(o.host + o.hostmodule + p.script, p.postdata, function (data){
 						if (data) {
@@ -92,4 +110,35 @@
 				}
 			}
+
+			var buildTreeFromParent = function (dirID, node) {				
+				var aryChildFiles = [];
+				var aryChildDirs = [];
+				var aryChildIDs = [];
+				var aryTmp = [];
+				var dir = o.data.DIRECTORIES[searchItemByID(dirID, 'directory')];
+				aryChildDirs = searchItemsByParent(dirID, 'directory');
+				aryChildFiles = searchItemsByParent(dirID, 'file');
+
+				$(aryChildDirs).each(function (index) {
+					aryChildIDs[index] = this.id;
+				});
+
+				if (node.id == undefined) node.id = dirID;				
+
+				if ($(aryChildFiles).length > 0 ) {
+					if (node.files == undefined) node.files = [];
+					$(aryChildFiles).each(function (index) {
+						node.files[index] = this.id;
+					});
+				}				
+
+				if ($(aryChildDirs).length > 0) {
+					if (node.childs == undefined) node.childs = [];
+					$(aryChildIDs).each(function (index) {
+						node.childs[index] = new Object;
+						buildTreeFromParent(aryChildIDs[index], node.childs[index]);
+					});
+				}
+			}
 			
 			var checkChildExisted = function (id) {
@@ -100,4 +149,6 @@
 			
 			var doneInit = function () {
+				bindEventToToolbars();
+				documentEventsBinding();
 			};
 			
@@ -108,5 +159,4 @@
 			var init = function () {
 				layoutRender ();
-
 				$('#' + o.tree).parent().resizable({
 					maxWidth: maxWidth,
@@ -117,7 +167,5 @@
 					}
 				});
-
 				$(window).resize (function() {layoutRender ();$('#' + o.tree).parent().resizable({maxWidth: maxWidth});});
-				
 				sendCommand ({postdata:null,callbackSuccess:createFileManager,callbackDone:doneInit,callbackFail:failInit});
 			};
@@ -167,46 +215,215 @@
 			}
 			
+			/**************************
+			 * TOOLBAR EVENTS - START *
+			 **************************/
+			var btnRefreshClick = function (obj) {
+				$(o).find('i').addClass('icon-spin');				
+				sendCommand ({	postdata:null,
+								callbackSuccess:function (parseData) {
+									o.data = parseData;
+									self.updateData({updateAll:true});
+									o.oTree.refeshTree();
+									console.log(o);
+								},
+								callbackDone:function () {$(o).find('i').removeClass('icon-spin');},
+								callbackFail:failInit
+					});
+			}
+			
+			var btnNewFolderClick = function () {
+				createFolderStart();
+			}
+			
+			var btnDelClick = function () {
+				var items = o.oGrid.getHightLightItem();
+				if ($(items).length == 0){
+					var dirID = $(o.oTree.getSelectedNode()).attr('id');
+					var item = o.data.DIRECTORIES[searchItemByID(dirID,'directory')];
+					item.type = 'directory';
+					items = [item];
+				}
+				self.deleteItem(items);
+			}
+			
+			var btnCopyClick = function () {				
+				copy();
+			}
+
+			var btnPasteClick = function () {
+				paste();
+			}
+			
+			
+			var bindEventToToolbars = function () {
+				$(btnRefresh).click(function(e){btnRefreshClick(this)});
+				
+				$(btnNewFolder).click(function(e){btnNewFolderClick()});
+				$(btnDel).click(function(e){btnDelClick()});
+				$(btnCopy).click(function(e){btnCopyClick()});
+				$(btnPaste).click(function(e){btnPasteClick()})
+				
+				/*btnCut
+				btnShare
+				btnPreview
+				btnDownload
+				btnUpload*/
+			}
+			/************************
+			 * TOOLBAR EVENTS - END *
+			 ************************/
+
+			/***********************************
+			 * DOCUMENT EVENTS BINDING - START *
+			 ***********************************/
+			 var documentEventsBinding = function () {
+			 	$(document).bind('keydown', function (e){
+			 		switch( e.which ) {
+			 			case 113:			 				
+			 			case 27:
+			 				var gridSelectedItems = o.oGrid.getHightLightItem();
+			 				if ($(gridSelectedItems).length > 0) {
+			 					o.oGrid.rename(e.which);
+			 				}else {			 					
+			 					o.oTree.rename(e.which);
+			 				}
+			 				break;
+			 			case 46:
+			 				//delete
+			 				btnDelClick();
+			 				break;
+			 			case 65:
+			 				if (e.ctrlKey) {
+			 					o.oGrid.selectAllNode();
+			 				}
+			 				break;
+			 			default:
+			 				break;
+			 		}
+			 	});
+			 }
+			/***********************************
+			 * DOCUMENT EVENTS BINDING - END *
+			 ***********************************/			 
+			
 			/*******************************
 			 * CREATE FOLDER - START *
 			 *******************************/
-			var createFolder = function (parent) {
-				var postdata = {delobj:delobj};
+			var createFolderStart = function () {
+				var promptOptions = {
+						  title: "Táº¡o thÆ° má»¥c má»i",
+						  buttons: {
+						    confirm: {
+						      label: "LÆ°u"
+						    },
+						    cancel: {
+						      label: "Há»§y"	
+						    }
+						  },
+						  callback: function(result) {                
+						      if (result === null) {                                             
+						                                      
+						      } else {
+						    	  createFolder(treeCurrentNode, result);
+						      }
+						    }
+						};
+
+				return bootbox.prompt(promptOptions);
+			}
+			
+			var createFolder = function (parent, name) {
+				var postdata = {fname:name,fparentid:parent};
 				var script = 'createdir';
+				/*isDev = true;*/
 				sendCommand ({
 					postdata:postdata,
 					script:script,
-					callbackSuccess:function(parseData){console.log(parseData);},
+					callbackSuccess:function(parseData){createFolderFinish(parseData);},
 					callbackFail: function(){}
 				});
 			}
 			
-			var createFolderCallBack = function (parent) {
-				
+			var createFolderFinish = function (parseData) {
+				/*isDev = false;*/
+				if (parseData.ERROR.errCode == 0) {
+					var node = {id:parseData.id, name:parseData.name,parentID:parseData.parentID};
+					o.oTree.createNode(node);
+					o.data.DIRECTORIES[$(o.data.DIRECTORIES).length] = node;
+					if (o.oGrid) o.oGrid.reloadGrid(parseData.parentID);
+				}
 			}
 			/*******************************
-			 * CREATE FOLDER - END *
+			 * CREATE FOLDER - END         *
 			 *******************************/
-			
-			/*******************************
-			 * CLIPBOARD - START *
-			 *******************************/
-			var oClipBoard = {item:null, act:null};
-			
-			var addToClipboard = function (item, act) {
-				oClipboard = {item:item,act:act};
-			}
-			
-			var clearClipboard = function () {
-				oClipBoard = {item:null, act:null};
-			}
-			
-			/*******************************
-			 * CLIPBOARD - START *
-			 *******************************/
+			/********************************
+			 * COPY & PASTE & MOVE - START  *
+			 ************=*******************/
+			var copy = function (){
+				//detect selected items
+				//push to clipboard
+				var items = o.oGrid.getHightLightItem();
+				
+				if ($(items).length == 0) {
+					var node = o.oTree.getSelectedNode();
+					var itemID = $(node).attr('id');
+					items[0] = o.data.DIRECTORIES[searchItemByID(itemID, 'directory')];
+					items[0].type = 'directory';
+				}
+
+				if ($(items).length > 0) {
+					oClipBoard.items = items;
+					oClipBoard.act = 'copy';
+				}
+			}
+			
+			var paste = function () {
+				if ((oClipBoard.act != 'copy' 
+					&& oClipBoard.act != 'move') 
+					|| oClipBoard.items == null) return;
+				
+				var items = [];
+				var destination = self.getTreeCurrentNode();
+
+				$(oClipBoard.items).each(function (index) {
+					var node = new Object;
+					buildTreeFromParent(this.id, node);
+					items[index] = node;
+				});
+
+				var postdata = {destination:destination,data:JSON.stringify(items)};
+				var script = 'copy';
+				sendCommand ({
+					postdata:postdata,
+					script:script,
+					callbackSuccess:function(parseData){
+						console.log(parseData);
+					}
+				});
+				
+			}
+
+			var move = function () {
+
+			}
+
+			var copyTo = function () {
+
+			}
+
+			var moveTo = function () {
+
+			}
+			
+			/*****************************
+			 * COPY & PASTE & MOVE - END *
+			 *****************************/
+
 			this.deleteItem = function (item) {
+				
 				var confirmText = 'Báº¡n cÃ³ muá»n xÃ³a ';
 				
 				if ($.isArray(item) && item.length > 1) {
-					confirmText += 'cÃ¡c thÆ° má»¥c (file) vÃ  cÃ¡c thÆ° má»¥c (file) con ÄÃ£ chá»n ?';
+					confirmText += 'cÃ¡c thÆ° má»¥c (vÃ  files) ÄÃ£ chá»n?';
 				}
 				else if (item.length == 1) {
@@ -215,4 +432,6 @@
 					confirmText += ' <span style="font-weight:bold">' + item[0].name + "</span> khÃŽng?";
 				}
+				
+				confirmText += '<br /><div style="color:red">(hÃ nh Äá»ng nÃ y sáºœ xÃ³a táº¥t cáº£ thÆ° má»¥c con vÃ  cÃ¡c file trong cÃ¡c thÆ° má»¥c ÄÃ£ chá»n)</div>';
 				
 				for (var i = 0; i < item.length; i++) {
@@ -262,5 +481,21 @@
 										postdata:postdata,
 										script:script,
-										callbackSuccess:function(parseData){console.log(parseData);},
+										callbackSuccess:function(parseData){
+											if($(parseData.DIRECTORIES).length > 0) {
+												for(var i = 0; i < $(parseData.DIRECTORIES).length; i++) {
+													o.oTree.deletion($(parseData.DIRECTORIES).get(i));
+													o.oGrid.deletion($(parseData.DIRECTORIES).get(i), 'directory');
+												}
+											}
+
+											if($(parseData.FILES).length > 0) {
+												for(var i = 0; i < $(parseData.FILES).length; i++) {	
+													var id = $(parseData.FILES).get(i);
+													var file = o.data.FILES[searchItemByID(id)];											
+													o.oGrid.deletion(file.id, file.minetype);
+												}												
+											}
+
+										},
 										callbackFail: function(){}
 									});
@@ -270,5 +505,4 @@
 				
 				bootbox.confirm(confirmOptions);
-				
 			}
 			
@@ -284,53 +518,41 @@
 			}
 			
-			this.gridNodeClick = function (gridNode) {
-				//fire when click a node on Grid
-				//then fire action of Tree
+			this.gridNodeDblClick = function (node) {
+				if (node.minetype == 'directory') {
+					var treeNode = $('#' + o.tree).find('UL.vstree[rel^="node' + node.parentID + '"] > LI[rel^="folder"] > A#' + node.id);
+					o.oTree.activeNode(treeNode);
+				}
+				else {
+					//execute or preview file
+				}
 			};
 			
 			this.createNewFolder = function () {
-				var promptOptions = {
-						  title: "Táº¡o thÆ° má»¥c má»i",
-						  buttons: {
-						    confirm: {
-						      label: "LÆ°u"
-						    },
-						    cancel: {
-						      label: "Há»§y"	
-						    }
-						  },
-						  callback: function(result) {                
-						      if (result === null) {                                             
-						        console.log("Prompt dismissed");                              
-						      } else {
-						        console.log("Hi "+result);                          
-						      }
-						    }
-						};
-
-				bootbox.prompt(promptOptions);
+				
 			}
 			
 			this.updateData = function (p) {
 				if( p.item == undefined ) p.item = null;
+				if( p.updateAll == undefined ) p.updateAll = false;
 				if( p.from == undefined ) p.from = null;
 				if( p.type == undefined ) p.type = null;
 				if( p.callback == undefined ) p.callback = null;
-				
+
 				var obj = p.from == 'tree' ? o.oGrid : o.oTree;
-				
-				var index = searchItemByID(p.item.id, p.type);
-				switch (p.type) {
-					case 'directory':
-						o.data.DIRECTORIES[index].name = p.item.name;
-						o.data.DIRECTORIES[index].parentID = p.item.parentID;
-						break;
-					case 'file':
-						o.data.FILES[index].name = p.item.name;
-						o.data.FILES[index].parentID = p.item.parentID;
-						o.data.FILES[index].minetype = p.item.minetype;
-						break;
-					default:
-						break;
+				if (!p.updateAll) {
+					var index = searchItemByID(p.item.id, p.type);
+					switch (p.type) {
+						case 'directory':
+							o.data.DIRECTORIES[index].name = p.item.name;
+							o.data.DIRECTORIES[index].parentID = p.item.parentID;
+							break;
+						case 'file':
+							o.data.FILES[index].name = p.item.name;
+							o.data.FILES[index].parentID = p.item.parentID;
+							o.data.FILES[index].minetype = p.item.minetype;
+							break;
+						default:
+							break;
+					}
 				}
 				
@@ -343,4 +565,12 @@
 				
 				//call sendCommand
+			}
+			
+			this.searchItemsByParent = function (parentID, type) {
+				return searchItemsByParent(parentID, type);
+			}
+			
+			this.searchItemByID = function (parentID, type) {
+				return searchItemByID(parentID, type);
 			}
 			
