if(jQuery) (function($){
$.extend($.fn, {
violetFileManager : function (o) {
if( !o ) var o = {};
if( o.tree == undefined ) o.tree = null;
if( o.grid == undefined ) o.grid = null;
if( o.maincontainer == undefined ) o.maincontainer = null;
if( o.titlebar == undefined ) o.titlebar = null;
if( o.toolsbar == undefined ) o.toolsbar = null;
if( o.statusbar == undefined ) o.statusbar = null;
if( o.oTree == undefined ) o.oTree = null;
if( o.oGrid == undefined ) o.oGrid = null;
if( o.host == undefined ) o.host = 'http://localhost/ajax/';
if( o.hostmodule == undefined ) o.hostmodule = 'privatecontent/';
if( o.script == undefined ) o.script = 'getcontent';
if( o.data == undefined ) o.data = null;
if( o.datasource == undefined ) o.datasource = 'ajax';
var isDev = false;
var contextmenu = null;
var oContainer = this;
var tree = [];
var totalItem = 0;
var countItem = 0;
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) {
if( p.postdata == undefined ) p.postdata = null;
if( p.script == undefined ) p.script = o.script;
if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
if( p.callbackDone == undefined ) p.callbackDone = null;
if( p.callbackFail == undefined ) p.callbackFail = null;
if( p.callbackAlways == undefined ) p.callbackAlways = null;
if( p.parseData == undefined ) p.parseData = null;
if( p.self == undefined ) p.self = this;
if (p.script != null && (o.datasource == 'ajax' || isDev)) {
$.post(o.host + o.hostmodule + p.script, p.postdata, function (data){
if (data) {
parseData = $.parseJSON(data);
p.parseData = parseData;
}
if (p.callbackSuccess != null) {
if (parseInt(parseData.ERROR.errCode) === 0)
p.callbackSuccess(parseData);
else {
p.callbackFail(parseData.ERROR);
}
}
}).done(function() {if (p.callbackDone != null)p.callbackDone(p.parseData);}).fail(function() {if (p.callbackFail != null)p.callbackFail(this);}).always(function() {if (p.callbackAlways != null)p.callbackAlways(this);});
}
else if (o.datasource == 'json'){
if (p.callbackSuccess != null) p.callbackSuccess(o.data);
if (p.callbackDone != null) p.callbackDone(this);
}
};
var getDirTreeMaxWidth = function () {
var scrWidth = $(o.maincontainer).width();
return parseInt(scrWidth / 2);
}
var layoutRender = function () {
var scrWidth = $('#' + o.maincontainer).width();
var scrHeght = $(window).height();
var dirTreeHeight = scrHeght - $('#' + o.titlebar).height() - $('#' + o.toolsbar).height() - $('#' + o.statusbar).height() - 2;
$('#' + o.tree).parent().height(dirTreeHeight);
$('#' + o.grid).parent().height(dirTreeHeight);
$('#' + o.grid).parent().width('calc(100% - ' + ($('#' + o.tree).parent().width() + 8) + 'px)');
var scollWidth = $('#' + o.grid).parent().width();
maxWidth = getDirTreeMaxWidth();
$(o.tree).height(dirTreeHeight - 5);
}
var createFileManager = function (parseData) {
o.data = parseData;
totalItem = o.data.DIRECTORIES.length;
o.oTree = $('#' + o.tree).violetTree({data:o.data.DIRECTORIES,manager:oContainer});
o.oGrid = $('#' + o.grid).violetGrid({data:o.data,manager:oContainer});
};
var getAllDirChild = function (parentID, aryChild) {
parentID = parentID == null ? 0:parentID;
var dirList = searchItemsByParent(parentID,'directory');
var index = aryChild.length;
aryChild[index] = parentID;
if (dirList.length > 0) {
for (var i = 0; i < dirList.length; i++) {
getAllDirChild(dirList[i].id, aryChild);
}
}
}
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');
node.id = dir.id;
node.name = dir.name;
node.type = 'directory';
$(aryChildDirs).each(function (index) {
var id = this.id;
var name = this.name;
var type = 'directory';
var cDir = {id:id, name:name, type:type, childs:null};
aryChildIDs[index] = cDir;
});
if ($(aryChildFiles).length > 0 ) {
if (node.files == undefined) node.files = [];
$(aryChildFiles).each(function (index) {
var id = this.id;
var name = this.name;
var type = 'file';
var cFile = {id:id, name:name, type:type};
node.files[index] = cFile;
});
}
if ($(aryChildDirs).length > 0) {
if (node.childs == undefined) node.childs = [];
$(aryChildIDs).each(function (index) {
node.childs[index] = new Object;
buildTreeFromParent(aryChildIDs[index].id, node.childs[index]);
});
}
}
var checkChildExisted = function (id) {
var dirList = searchItemsByParent(id,'directory');
var fileList = searchItemsByParent(id,'file');
return (dirList.length > 0) || (fileList.length > 0);
}
var doneInit = function () {
bindEventToToolbars();
documentEventsBinding();
};
var failInit = function (er) {
bootbox.alert(er.err);
}
var init = function () {
layoutRender ();
$('#' + o.tree).parent().resizable({
maxWidth: maxWidth,
minWidth: 220,
handles: "e",
resize: function (event, ui) {
layoutRender ();
}
});
$(window).resize (function() {layoutRender ();$('#' + o.tree).parent().resizable({maxWidth: maxWidth});});
sendCommand ({postdata:null,callbackSuccess:createFileManager,callbackDone:doneInit,callbackFail:failInit});
};
var searchItemByID = function (id, type) {
var data = {};
switch (type) {
case 'directory':
data = o.data.DIRECTORIES;
break;
case 'file':
data = o.data.FILES;
break;
default:
break;
}
for (var i = 0; i < data.length; i++) {
if (data[i].id == id) return i;
}
}
var searchItemsByParent = function (parentID, type) {
var data = {};
var aryItem = [];
var index = aryItem.length;
switch (type) {
case 'directory':
data = o.data.DIRECTORIES;
break;
case 'file':
data = o.data.FILES;
break;
default:
break;
}
for (var i = 0; i < data.length; i++) {
if (data[i].parentID == parentID) {
aryItem[index] = data[i];
index ++;
}
}
return aryItem;
}
/**************************
* 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 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){createFolderFinish(parseData);},
callbackFail: function(){}
});
}
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 *
*******************************/
/********************************
* 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;
if (this.type == 'directory')
buildTreeFromParent(this.id, node);
else {
node.id = this.id;
node.type = 'file';
}
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 (và files) đã chọn?';
}
else if (item.length == 1) {
if (item[0].id == 0) return false;
confirmText += (item[0].type == 'directory')?'thư mục':'file';
confirmText += ' ' + item[0].name + " không?";
}
confirmText += '