[15] | 1 | if(jQuery) (function($){
|
---|
| 2 |
|
---|
| 3 | $.extend($.fn, {
|
---|
| 4 | violetGrid: function (o) {
|
---|
| 5 | if( !o ) var o = {};
|
---|
| 6 | if( o.container == undefined ) o.container = $(this);
|
---|
| 7 | if( o.defaultViewMode == undefined ) o.defaultViewMode = 'thumbnail';//or 'list'
|
---|
| 8 | if( o.directoryTreeData == undefined ) o.directoryTreeData = null;
|
---|
| 9 | if( o.dirIDprefix == undefined ) o.dirIDprefix = null;
|
---|
[16] | 10 | if( o.host == undefined ) o.host = 'http://localhost/';
|
---|
[15] | 11 |
|
---|
[16] | 12 | if( o.sharemodal == undefined ) o.sharemodal = $("#box-shareto");
|
---|
| 13 | if( o.copymodal == undefined ) o.copymodal = $("#box-copyto");
|
---|
| 14 | if( o.movemodal == undefined ) o.movemodal = $("#box-moveto");
|
---|
| 15 |
|
---|
| 16 | var currentObj = {};
|
---|
| 17 |
|
---|
[15] | 18 | var createNode = function (d) {
|
---|
| 19 | if( !d ) var d = {};
|
---|
| 20 | if( d.id == undefined ) d.id = null;
|
---|
| 21 | if( d.name == undefined ) d.name = null;
|
---|
| 22 | if( d.minetype == undefined ) d.minetype = 'directory';
|
---|
| 23 | if( d.parentID == undefined ) d.parentID = null;
|
---|
| 24 | if( d.clickEvent == undefined ) d.clickEvent = null;
|
---|
| 25 | if( d.customEvent == undefined ) d.customEvent = null;
|
---|
| 26 |
|
---|
| 27 | var strHTML = '<div class="vscell" rel="id:' + d.id + '">';
|
---|
| 28 | strHTML += '<div class="selector unselected">';
|
---|
| 29 | strHTML += '<div class="icon-' + d.minetype + '"></div>';
|
---|
| 30 | strHTML += '</div>';
|
---|
| 31 | strHTML += '<div class="file-name unselected">' + d.name + '</div>';
|
---|
| 32 | strHTML += '</div>';
|
---|
| 33 |
|
---|
| 34 | var disabledItemsList = null;
|
---|
| 35 |
|
---|
| 36 | if (d.minetype == 'directory') {
|
---|
| 37 | disabledItemsList = ['preview'];
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | $(o.container).append(strHTML);
|
---|
| 41 |
|
---|
[16] | 42 | $('div[rel="id:'+ d.id +'"]').bind('click',function(e){itemClick(this)});
|
---|
| 43 |
|
---|
[15] | 44 | $('div[rel="id:'+ d.id +'"]').contextMenu({
|
---|
| 45 | menu: 'gridMenu',
|
---|
| 46 | disabledItems: disabledItemsList
|
---|
| 47 | }, function(action, el, pos) {
|
---|
[16] | 48 | itemClick(el);
|
---|
[15] | 49 | switch(action) {
|
---|
| 50 | case 'rename':
|
---|
| 51 | rename(el);
|
---|
| 52 | break;
|
---|
[16] | 53 | case 'share':
|
---|
| 54 | showShareModal(currentObj);
|
---|
| 55 | break;
|
---|
| 56 | case 'copy':
|
---|
| 57 | case 'cut':
|
---|
[17] | 58 | showCopyModal(currentObj, action);
|
---|
[16] | 59 | break;
|
---|
[15] | 60 | default:
|
---|
| 61 | break;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | });
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | var keyboardRename = function () {
|
---|
[16] | 68 | $(document).bind('keydown','keypress', function(e) {
|
---|
[15] | 69 | var selectedItem = $('.selector.selected').parent();
|
---|
[16] | 70 |
|
---|
[15] | 71 | switch( e.keyCode ) {
|
---|
| 72 | case 113:
|
---|
| 73 | rename(selectedItem);
|
---|
| 74 | break;
|
---|
[16] | 75 | case 27:
|
---|
[15] | 76 | cancelRename(selectedItem);
|
---|
| 77 | break;
|
---|
| 78 | default:
|
---|
| 79 | break;
|
---|
| 80 | }
|
---|
| 81 | });
|
---|
| 82 | }
|
---|
[16] | 83 |
|
---|
| 84 | var showShareModal = function (obj) {
|
---|
| 85 | if (obj.type == undefined || obj.type == null) obj.type = 'directory';
|
---|
| 86 | $(o.sharemodal).modal('show');
|
---|
| 87 | $(o.sharemodal).find('INPUT#txtSelectedObj').val(obj.name);
|
---|
| 88 | $(o.sharemodal).find('.modal-header').text('Chia sẻ ' + (obj.type == 'directory'?'thư mục':'file') + ' [' + obj.name + ']');
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[17] | 91 | var showCopyModal = function (obj, action) {
|
---|
| 92 | var modalTitle = action == 'copy' ? 'Sao chép ' + (obj.type == 'directory'?'thư mục':'file') + ' [' + obj.name + ']':'Di chuyá»n ' + (obj.type == 'directory'?'thư mục':'file') + ' [' + obj.name + ']'
|
---|
| 93 | var submitTitle = action == 'copy' ? 'Sao chép' : 'Di chuyá»n';
|
---|
| 94 | var submitIcon = action == 'copy' ? '<i class="icon-copy"></i>' : '<i class="icon-cut"></i>';
|
---|
| 95 |
|
---|
| 96 | //btn-primary
|
---|
[16] | 97 | $(o.copymodal).modal('show');
|
---|
[17] | 98 | $(o.copymodal).find('INPUT#txtSelectedObj').val(obj.name);
|
---|
| 99 | $(o.copymodal).find('.modal-header').text(modalTitle);
|
---|
| 100 | $(o.copymodal).find('.btn-primary').empty();
|
---|
| 101 | $(o.copymodal).find('.btn-primary').append(submitIcon + "\n" + submitTitle);
|
---|
| 102 |
|
---|
| 103 | var selectTree = $(o.copymodal).find('#select-destination-tree').violetTree({
|
---|
| 104 | container: $('#select-destination-tree'),
|
---|
| 105 | expandEasing: 'easeOutBounce',
|
---|
| 106 | collapseEasing: 'easeOutBounce',
|
---|
| 107 | homeDirNameDisplay: "Thư mục gá»c",
|
---|
| 108 | contextmenuON: false
|
---|
| 109 | });
|
---|
| 110 |
|
---|
| 111 | $(o.copymodal).find('.btn-primary').button().click(function() {excuteCopy(obj, selectTree, action)})
|
---|
| 112 |
|
---|
| 113 | $(o.copymodal).on('hide.bs.modal', function () {
|
---|
| 114 | $(o.copymodal).find('.btn-primary').unbind('click');
|
---|
| 115 | });
|
---|
[16] | 116 | }
|
---|
| 117 |
|
---|
[17] | 118 | var excuteCopy = function (obj, tree, action) {
|
---|
| 119 | var destObj = tree.getSelectedObj();
|
---|
| 120 | //alert(action + ' ' + obj.type + ' ' + obj.name + ' to ' + destObj.type + ' ' + destObj.name);
|
---|
| 121 |
|
---|
| 122 | //sendCommand
|
---|
| 123 | var postdata = {sourceid:obj.id,
|
---|
| 124 | sourcetype:obj.type,
|
---|
| 125 | destid: destObj.id,
|
---|
| 126 | desttype: destObj.type,
|
---|
| 127 | flag:action}
|
---|
| 128 |
|
---|
| 129 | sendCommand({
|
---|
| 130 | script:'ajax/privatecontent/copy',
|
---|
| 131 | postdata:postdata,
|
---|
| 132 | callbackSuccess: function (parsedData) {
|
---|
| 133 | if (parsedData.RESULT == true) {
|
---|
| 134 | //please add code here
|
---|
| 135 | }else return false;
|
---|
| 136 | }
|
---|
| 137 | });
|
---|
[16] | 138 | }
|
---|
[15] | 139 |
|
---|
| 140 | var itemClick = function (i) {
|
---|
| 141 | $(i).parent().find('.selector').removeClass('selected');
|
---|
| 142 | $(i).parent().find('.selector').addClass('unselected');
|
---|
| 143 | $(i).parent().find('.file-name').removeClass('selected');
|
---|
| 144 | $(i).parent().find('.file-name').addClass('unselected');
|
---|
| 145 |
|
---|
| 146 | $(i).find('.file-name').removeClass('unselected');
|
---|
| 147 | $(i).find('.file-name').addClass('selected');
|
---|
| 148 | $(i).find('.selector').removeClass('unselected');
|
---|
| 149 | $(i).find('.selector').addClass('selected');
|
---|
[16] | 150 |
|
---|
| 151 | if ($(i).find('.file-name').text() != '')
|
---|
| 152 | currentObj.name = $(i).find('.file-name').text();
|
---|
| 153 |
|
---|
| 154 | currentObj.id = $(i).attr('rel').substring(3);
|
---|
| 155 | currentObj.type = $(i).find('>div>div').hasClass('icon-directory') ? 'directory':'file';
|
---|
[15] | 156 | }
|
---|
| 157 |
|
---|
| 158 | var renderGrid = function (o) {
|
---|
| 159 | $(o.container).find ('.vscell').remove();
|
---|
| 160 |
|
---|
| 161 | var childDir = o.directoryTreeData.DIRECTORIES;
|
---|
| 162 | var childFile = o.directoryTreeData.FILES;
|
---|
| 163 | var curentDirID = o.curentParent.substring(o.dirIDprefix.length, o.curentParent.length);
|
---|
| 164 |
|
---|
| 165 | for (var i = 0 ; i < childDir.length; i++) {
|
---|
| 166 | if (childDir[i].parentID != curentDirID) continue;
|
---|
| 167 | createNode ({
|
---|
| 168 | id: childDir[i].id,
|
---|
| 169 | name: childDir[i].name,
|
---|
| 170 | parentID: curentDirID,
|
---|
| 171 | });
|
---|
| 172 | }
|
---|
[16] | 173 |
|
---|
[15] | 174 | for (var i = 0 ; i < childFile.length; i++) {
|
---|
| 175 | if (childFile[i].parentID != curentDirID) continue;
|
---|
| 176 | createNode ({
|
---|
| 177 | id: childFile[i].id,
|
---|
| 178 | name: childFile[i].name,
|
---|
| 179 | parentID: curentDirID,
|
---|
| 180 | minetype: childFile[i].minetype,
|
---|
| 181 | });
|
---|
| 182 | }
|
---|
[16] | 183 |
|
---|
| 184 | keyboardRename();
|
---|
[15] | 185 | }
|
---|
| 186 |
|
---|
| 187 | var rename = function (e) {
|
---|
| 188 | var nameObj = $(e).find('.file-name');
|
---|
| 189 | var editor = document.createElement ('INPUT');
|
---|
| 190 | editor.type = 'text';
|
---|
| 191 | editor.className = 'rename';
|
---|
| 192 |
|
---|
| 193 | $(nameObj).text('');
|
---|
| 194 | $(nameObj).append(editor);
|
---|
[16] | 195 |
|
---|
| 196 | $(editor).attr('value', currentObj.name);
|
---|
[15] | 197 | editor.focus();
|
---|
| 198 | $(editor).select();
|
---|
[16] | 199 | $(editor).bind('focusout',function (e) {
|
---|
| 200 | cancelRename($(this).parent().parent());
|
---|
| 201 | });
|
---|
| 202 |
|
---|
| 203 | $(editor).bind('keypress',function (e) {
|
---|
| 204 | var keycode = (e.keyCode ? e.keyCode : e.which);
|
---|
| 205 | if(keycode == '13'){
|
---|
| 206 | completeRename($(this).parent());
|
---|
| 207 | }
|
---|
| 208 | else {
|
---|
| 209 |
|
---|
| 210 | }
|
---|
| 211 | e.stopPropagation();
|
---|
| 212 | });
|
---|
[15] | 213 | }
|
---|
| 214 |
|
---|
| 215 | var cancelRename = function (e) {
|
---|
| 216 | $(e).find('INPUT.rename').remove();
|
---|
[16] | 217 | $(e).find('.file-name').text(currentObj.name);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | var completeRename = function (e) {
|
---|
[15] | 221 |
|
---|
[16] | 222 | var postdata = {id:currentObj.id,
|
---|
| 223 | objtype:currentObj.type,
|
---|
| 224 | newname:$(e).find('INPUT.rename').val()}
|
---|
| 225 | sendCommand({
|
---|
| 226 | script:'ajax/privatecontent/rename',
|
---|
| 227 | postdata:postdata,
|
---|
| 228 | callbackSuccess: function (parsedData) {
|
---|
| 229 | if (parsedData.RESULT == true) {
|
---|
| 230 | currentObj.name = parsedData.UPDATED.name;
|
---|
| 231 | $(e).find('INPUT.rename').remove();
|
---|
| 232 | $(e).text(currentObj.name);
|
---|
| 233 | }else return false;
|
---|
| 234 | }
|
---|
| 235 | });
|
---|
[15] | 236 | }
|
---|
[16] | 237 |
|
---|
| 238 | var sendCommand = function (p) {
|
---|
| 239 | if( p.postdata == undefined ) p.postdata = null;
|
---|
| 240 | if( p.script == undefined ) p.script = o.script;
|
---|
| 241 | if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
|
---|
| 242 | if( p.callbackDone == undefined ) p.callbackDone = null;
|
---|
| 243 | if( p.callbackFail == undefined ) p.callbackFail = null;
|
---|
| 244 | if( p.callbackAlways == undefined ) p.callbackAlways = null;
|
---|
[15] | 245 |
|
---|
[16] | 246 | if (p.script != null) {
|
---|
| 247 | $.post(o.host + p.script, p.postdata, function (data){
|
---|
| 248 | if (data) {
|
---|
| 249 | parseData = $.parseJSON(data);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | if (p.callbackSuccess != null) {
|
---|
| 253 | p.callbackSuccess(parseData);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | }).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);});
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[15] | 260 | this.getData = function (data) {
|
---|
| 261 | o.directoryTreeData = data.directoryTreeData;
|
---|
| 262 | o.curentParent = data.curentParent;
|
---|
| 263 | o.dirIDprefix = data.dirIDprefix
|
---|
| 264 | renderGrid(o);
|
---|
| 265 | }
|
---|
[16] | 266 |
|
---|
[17] | 267 | this.showModal = function (obj, act) {
|
---|
[16] | 268 | switch( act ) {
|
---|
| 269 | case 'copy':
|
---|
[17] | 270 | case 'cut':
|
---|
| 271 | showCopyModal(obj, act);
|
---|
[16] | 272 | break;
|
---|
| 273 | case 'share':
|
---|
| 274 | showShareModal(obj);
|
---|
| 275 | break;
|
---|
| 276 | default:
|
---|
| 277 | break;
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
[15] | 280 |
|
---|
| 281 | this.initialize = function() {
|
---|
| 282 | return this;
|
---|
| 283 | };
|
---|
| 284 |
|
---|
| 285 | return this.initialize();
|
---|
| 286 | }
|
---|
| 287 | });
|
---|
| 288 |
|
---|
| 289 | })(jQuery); |
---|