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/'; 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'; o.host = o.host + '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++) { for (var i in data) { 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 (i in data) { 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(); }, callbackDone: function () { $(o).find('i').removeClass('icon-spin'); }, callbackFail: failInit }); } var btnNewFolderClick = function () { createFolderStart(); } var btnUploadClick = function () { uploadStart(); uploadInit(); } 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('copy'); } var btnPasteClick = function () { paste(); } var btnCutClick = function () { copy('move'); } var bindEventToToolbars = function () { $(btnRefresh).click(function (e) { btnRefreshClick(this) }); $(btnNewFolder).click(function (e) { btnNewFolderClick() }); $(btnUpload).click(function (e) { btnUploadClick() }); $(btnDel).click(function (e) { btnDelClick() }); $(btnCopy).click(function (e) { btnCopyClick() }); $(btnCut).click(function (e) { btnCutClick() }); $(btnPaste).click(function (e) { btnPasteClick() }) $(btnPreview).click(function (e) { btnPreviewClick() }) /*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 uploadStart = function () { var userid = o.data.userinfo.us_id; var promptOptions = { title: "Tải lên", message: "
", buttons: { success: { label: "Xong", className: "btn btn-primary", callback: function (result) { } }, }, onEscape: function() {uploadDone(self.getTreeCurrentNode());} }; return bootbox.dialog(promptOptions); } var uploadDone = function (result) { } var btnPreviewClick = 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]; } previewFile(items[0]); } var previewFile = function(node) { var content=""; $ext=node.fileurl.split('.').pop(); if($.inArray( $ext, [ "jpg", "jpeg","png","gif"])>=0) { content="