if(jQuery) (function($){ $.extend($.fn, { violetTree: function(o) { if( !o ) var o = {}; if( o.user == undefined ) o.user = null; if( o.homeDirNameDisplay == undefined ) o.homeDirNameDisplay = 'Home'; if( o.host == undefined ) o.host = 'http://localhost/'; if( o.script == undefined ) o.script = 'ajax/privatecontent/getcontent'; if( o.container == undefined ) o.container = $(this); if( o.dirIDprefix == undefined ) o.dirIDprefix = 'vsdir_'; if( o.expandSpeed == undefined ) o.expandSpeed= 500; if( o.collapseSpeed == undefined ) o.collapseSpeed= 500; if( o.expandEasing == undefined ) o.expandEasing = null; if( o.collapseEasing == undefined ) o.collapseEasing = null; if( o.directoryTreeData == undefined ) o.directoryTreeData = null; if( o.grid == undefined ) o.grid = null; // PRIVATE methods 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.script != null) { $.post(o.host + p.script, p.postdata, function (data){ if (data) { parseData = $.parseJSON(data); } if (p.callbackSuccess != null) { p.callbackSuccess(parseData); } }).done(function() {if (p.callbackDone != null)p.callbackDone(this);}).fail(function() {if (p.callbackFail != null)p.callbackFail(this);}).always(function() {if (p.callbackAlways != null)p.callbackAlways(this);}); } } var loadTree = function () { sendCommand ({postdata:null,callbackSuccess:renderTree}); }; var renderTree = function (parseData) { o.directoryTreeData = parseData; var directoryData = parseData.DIRECTORIES; var homeNode = createNode({ id:0, name:o.homeDirNameDisplay, }); selectDir($(homeNode).find('> A')); if (directoryData != null) { for (var i = 0; i < directoryData.length ; i++) { var node = createNode ({ id: directoryData[i].id, name: directoryData[i].name, curentNode: $('#' + o.dirIDprefix + directoryData[i].parentID).find('> A'), hidden: (directoryData[i].parentID > 0) ? true : false }) }; } }; //Create a node of Tree var createNode = function (d) { if( !d ) var d = {}; if( d.id == undefined ) d.id = null; if( d.name == undefined ) d.name = null; if( d.curentNode == undefined ) d.curentNode = null; if( d.hidden == undefined ) d.hidden = true; if( d.clickEvent == undefined ) d.clickEvent = openDir; if( d.customEvent == undefined ) d.customEvent = null; //customEvent.eventName, customEvent.eventTrigger if( d.addToJSONData == undefined ) d.addToJSONData = false; if (d.curentNode != null) { var strHTML = ''; $(d.curentNode).parent().append(strHTML); if (d.hidden == true) $('#' + o.dirIDprefix + d.id).parent().css('display','none'); }else if (d.id == 0){ var strHTML = ''; $(o.container).append(strHTML); } //bind new node to data if (d.addToJSONData == true) { //o.directoryTreeData.DIRECTORIES.length var newdir = {}; newdir.id = d.id; newdir.name = d.name; newdir.parentID = $(d.curentNode).parent().attr('id').substring(o.dirIDprefix.length, $(d.curentNode).parent().attr('id').length); o.directoryTreeData.DIRECTORIES.push(newdir); sendtoGrid(); } //bind event on new node $('#' + o.dirIDprefix + d.id).find('a').bind("click", function(e){d.clickEvent(this);return false;}); $('#' + o.dirIDprefix + d.id).find('a').bind("contextmenu",function(e){ e.preventDefault(); showContextMenu (this); }); if (d.customEvent != null) $('#' + o.dirIDprefix + d.id).find('a').bind(d.customEvent.eventName, function(e){d.customEvent.eventTrigger(this)}); return $('#' + o.dirIDprefix + d.id); } //END - Create a node of Tree var deleteNode = function (parsedData) { if (parsedData.isSuccess == false) return false; if (!$('#' + o.dirIDprefix + parsedData.id).hasClass('home')) $('#' + o.dirIDprefix + parsedData.id).parent().remove(); } var isHome = function (n) { return $(n).parent().hasClass('home'); } var countNodeChild = function (n) { var parent = $(n).parent(); return $(parent).find('UL').size(); } var openDir = function (o) { if( $(o).parent().hasClass('collapsed') ) { $(o).parent().find('> UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing }); $(o).parent().removeClass('collapsed').addClass('expanded'); } else if( $(o).parent().hasClass('expanded') ) { $(o).parent().find('> UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing }); $(o).parent().removeClass('expanded').addClass('collapsed'); } selectDir(o); } var selectDir = function (c) { $('.currentDir').removeClass('currentDir'); $(c).addClass('currentDir'); sendtoGrid(); } var showContextMenu = function (n) { alert ('showContextMenu on ' + $(n).text()); }; var sendtoGrid = function () { o.grid.getData({ directoryTreeData: o.directoryTreeData, curentParent:$(o.container).find('.currentDir').parent().attr('id'), dirIDprefix: o.dirIDprefix }); } loadTree(); // END - PRIVATE methods this.expandAll = function() { $(o.container).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing }); }; this.getCurrentDirName = function() { return $(o.container).find('.currentDir').attr('rel'); }; this.getCurrentDir = function() { return $(o.container).find('.currentDir'); }; this.createDir = function(c) { var cid = $(c).parent().attr('id'); var pid = cid.substring(o.dirIDprefix.length, cid.length); var dirName = prompt("Please enter new directory name", ""); var script = 'ajax/privatecontent/createdir'; if (dirName != null && dirName.length != 0) { sendCommand({ script: script, postdata:{parentID:pid,name:dirName}, callbackSuccess: function (parsedData) {createNode({ id: parsedData.id, name: parsedData.name, curentNode: c, hidden: false, addToJSONData:true}); } }); } } this.deleteDir = function (c) { var childExisted = (countNodeChild(c) > 0) ? true:false; var confirmDel = false; var confirmChild = true; var cid = $(c).parent().attr('id'); var pid = cid.substring(o.dirIDprefix.length, cid.length); if (isHome(c)) return; confirmDel = confirm('Are you sure you want to delete this Directory?'); if (childExisted > 0 && confirmDel) confirmChild = confirm('This node has childs, you still want to delete it?'); if (confirmDel && confirmChild) { var postdata = {id:pid,delallchild:true} sendCommand({ script:'ajax/privatecontent/deletedir', postdata:postdata, callbackSuccess: function (parsedData) {deleteNode(parsedData)} }); } } this.copy = function () { alert ('copy'); } this.paste = function () { alert ('paste'); } this.initialize = function() { return this; }; return this.initialize(); }, violetGrid: function (o) { if( !o ) var o = {}; if( o.container == undefined ) o.container = $(this); if( o.defaultViewMode == undefined ) o.defaultViewMode = 'thumbnail';//or 'list' if( o.directoryTreeData == undefined ) o.directoryTreeData = null; if( o.dirIDprefix == undefined ) o.dirIDprefix = null; var createNode = function (d) { if( !d ) var d = {}; if( d.id == undefined ) d.id = null; if( d.name == undefined ) d.name = null; if( d.minetype == undefined ) d.minetype = 'directory'; if( d.parentID == undefined ) d.parentID = null; if( d.clickEvent == undefined ) d.clickEvent = null; if( d.customEvent == undefined ) d.customEvent = null; var strHTML = '
'; strHTML += '
'; strHTML += '
'; strHTML += '
'; strHTML += '
' + d.name + '
'; strHTML += '
'; $(o.container).append(strHTML); $('div[rel="id:'+ d.id +'"]').bind('click',function(e){itemClick(this)}); } var itemClick = function (i) { $(i).parent().find('.selector').removeClass('selected'); $(i).parent().find('.selector').addClass('unselected'); $(i).parent().find('.file-name').removeClass('selected'); $(i).parent().find('.file-name').addClass('unselected'); $(i).find('.file-name').removeClass('unselected'); $(i).find('.file-name').addClass('selected'); $(i).find('.selector').removeClass('unselected'); $(i).find('.selector').addClass('selected'); } var renderGrid = function (o) { $(o.container).find ('.vscell').remove(); var childDir = o.directoryTreeData.DIRECTORIES; var childFile = o.directoryTreeData.FILES; var curentDirID = o.curentParent.substring(o.dirIDprefix.length, o.curentParent.length); for (var i = 0 ; i < childDir.length; i++) { if (childDir[i].parentID != curentDirID) continue; createNode ({ id: childDir[i].id, name: childDir[i].name, parentID: curentDirID, }); } for (var i = 0 ; i < childFile.length; i++) { if (childFile[i].parentID != curentDirID) continue; createNode ({ id: childFile[i].id, name: childFile[i].name, parentID: curentDirID, minetype: childFile[i].minetype, }); } } this.getData = function (data) { o.directoryTreeData = data.directoryTreeData; o.curentParent = data.curentParent; o.dirIDprefix = data.dirIDprefix renderGrid(o); } this.initialize = function() { return this; }; return this.initialize(); }, violetcontextmenu: function (o) { if( !o ) var o = {}; if( o.groups = undefined ) o.groups = {{id:0, disabled:false}}; /*{{id:0,disabled:false}}*/ if( o.items = undefined ) o.items = null;/*{{id:0,groupID:0,name:'', trigger:e ,disabled:false}}*/ if( o.width = undefined ) o.width = null; if( o.itemHeight = undefined ) o.itemHeight = null; /*var buildGroup = function () { if (o.groups == null) return false; var strHTML = ''; } var buildItem = function (g) { var strHTML = ''; for(var i = 0; i < o.items.length; i++) { if (o.items[i].groupID == g.id) strHTML = '
' + o.items[i].name + '
'; } return strHTML; }*/ var buildMenu = function () { if (o.groups == null) return false; } } }); })(jQuery);