if(jQuery) (function($){ $.extend($.fn, { 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; if( o.host == undefined ) o.host = 'http://localhost/'; if( o.sharemodal == undefined ) o.sharemodal = $("#box-shareto"); if( o.copymodal == undefined ) o.copymodal = $("#box-copyto"); if( o.movemodal == undefined ) o.movemodal = $("#box-moveto"); var currentObj = {}; 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 += '
'; var disabledItemsList = null; if (d.minetype == 'directory') { disabledItemsList = ['preview']; } $(o.container).append(strHTML); $('div[rel="id:'+ d.id +'"]').bind('click',function(e){itemClick(this)}); $('div[rel="id:'+ d.id +'"]').contextMenu({ menu: 'gridMenu', disabledItems: disabledItemsList }, function(action, el, pos) { itemClick(el); switch(action) { case 'rename': rename(el); break; case 'share': showShareModal(currentObj); break; case 'copy': showCopyModal(); break; case 'cut': showMoveModal(); break; default: break; } }); } var keyboardRename = function () { $(document).bind('keydown','keypress', function(e) { var selectedItem = $('.selector.selected').parent(); switch( e.keyCode ) { case 113: rename(selectedItem); break; case 27: cancelRename(selectedItem); break; default: break; } }); } var showShareModal = function (obj) { if (obj.type == undefined || obj.type == null) obj.type = 'directory'; $(o.sharemodal).modal('show'); $(o.sharemodal).find('INPUT#txtSelectedObj').val(obj.name); $(o.sharemodal).find('.modal-header').text('Chia sẻ ' + (obj.type == 'directory'?'thư mục':'file') + ' [' + obj.name + ']'); } var showCopyModal = function () { $(o.copymodal).modal('show'); /*$(o.copymodal).find('INPUT#txtSelectedObj').val(currentObj.name); $(o.copymodal).find('.modal-header').text('Chia sẻ ' + (currentObj.type == 'directory'?'thư mục':'file') + ' [' + currentObj.name + ']');*/ } var showMoveModal = function () { $(o.movemodal).modal('show'); } 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'); if ($(i).find('.file-name').text() != '') currentObj.name = $(i).find('.file-name').text(); currentObj.id = $(i).attr('rel').substring(3); currentObj.type = $(i).find('>div>div').hasClass('icon-directory') ? 'directory':'file'; console.log(currentObj.type); } 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, }); } keyboardRename(); } var rename = function (e) { var nameObj = $(e).find('.file-name'); var editor = document.createElement ('INPUT'); editor.type = 'text'; editor.className = 'rename'; $(nameObj).text(''); $(nameObj).append(editor); $(editor).attr('value', currentObj.name); editor.focus(); $(editor).select(); $(editor).bind('focusout',function (e) { cancelRename($(this).parent().parent()); }); $(editor).bind('keypress',function (e) { var keycode = (e.keyCode ? e.keyCode : e.which); if(keycode == '13'){ completeRename($(this).parent()); } else { } e.stopPropagation(); }); } var cancelRename = function (e) { $(e).find('INPUT.rename').remove(); $(e).find('.file-name').text(currentObj.name); } var completeRename = function (e) { var postdata = {id:currentObj.id, objtype:currentObj.type, newname:$(e).find('INPUT.rename').val()} sendCommand({ script:'ajax/privatecontent/rename', postdata:postdata, callbackSuccess: function (parsedData) { if (parsedData.RESULT == true) { currentObj.name = parsedData.UPDATED.name; $(e).find('INPUT.rename').remove(); $(e).text(currentObj.name); }else return false; } }); } 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);}); } } this.getData = function (data) { o.directoryTreeData = data.directoryTreeData; o.curentParent = data.curentParent; o.dirIDprefix = data.dirIDprefix renderGrid(o); } this.showModal = function (act, obj) { switch( act ) { case 'copy': break; case 'move': break; case 'share': showShareModal(obj); break; default: break; } } this.initialize = function() { return this; }; return this.initialize(); } }); })(jQuery);