source: pro-violet-viettel/www/deploy/20150304/assets/js/manager.js @ 836

Last change on this file since 836 was 836, checked in by dungnv, 10 years ago
File size: 43.3 KB
Line 
1if (jQuery)
2    (function ($) {
3        $.extend($.fn, {
4            violetFileManager: function (o) {
5                if (!o)
6                    var o = {};
7                if (o.tree == undefined)
8                    o.tree = null;
9                if (o.grid == undefined)
10                    o.grid = null;
11
12                if (o.maincontainer == undefined)
13                    o.maincontainer = null;
14                if (o.titlebar == undefined)
15                    o.titlebar = null;
16                if (o.toolsbar == undefined)
17                    o.toolsbar = null;
18                if (o.statusbar == undefined)
19                    o.statusbar = null;
20
21                if (o.oTree == undefined)
22                    o.oTree = null;
23                if (o.oGrid == undefined)
24                    o.oGrid = null;
25                if (o.host == undefined)
26                    o.host = 'http://localhost/';
27                if (o.hostmodule == undefined)
28                    o.hostmodule = 'privatecontent/';
29                if (o.script == undefined)
30                    o.script = 'getcontent';
31                if (o.data == undefined)
32                    o.data = null;
33                if (o.datasource == undefined)
34                    o.datasource = 'ajax';
35               
36                if (o.filehosting == undefined)
37                        o.filehosting = 'http://sbgapi.violet.vn/';
38               
39                if (o.invisibledButtons == undefined)
40                        o.invisibledButtons = null;
41
42                o.host = o.host + 'ajax/';
43
44                var isDev = false;
45                var contextmenu = null;
46                var oContainer = this;
47                var tree = [];
48                var totalItem = 0;
49                var countItem = 0;
50                var maxWidth = 0;
51                var treeCurrentNode = null;
52                var self = this;
53                var oClipBoard = {items: null, act: null};
54
55                /**
56                 * Toolbar defined
57                 * */
58                var btnNewFolder = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnNewFolder');
59                var btnDel = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnDel');
60                var btnCopy = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnCopy');
61                var btnCut = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnCut');
62                var btnPaste = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnPaste');
63                var btnShare = $('#' + o.toolsbar + ' > DIV.btn-group.social > #btnShare');
64                var btnPreview = $('#' + o.toolsbar + ' > DIV.btn-group.social > #btnPreview');
65                var btnDownload = $('#' + o.toolsbar + ' > DIV.btn-group.creation > #btnDownload');
66                var btnUpload = $('#' + o.toolsbar + ' > DIV.btn-group.creation > #btnUpload');
67                var btnRefresh = $('#' + o.toolsbar + ' > DIV.btn-group.control > #btnRefresh');
68
69                var toolbarButtons = [btnNewFolder, btnDel, btnCopy, btnCut, btnPaste, btnShare, btnPreview, btnDownload, btnUpload, btnRefresh];
70                var statusbar = $('DIV#' + o.statusbar);
71               
72                var sendCommand = function (p) {
73                    if (p.postdata == undefined)
74                        p.postdata = null;
75                    if (p.script == undefined)
76                        p.script = o.script;
77                    if (p.callbackSuccess == undefined)
78                        p.callbackSuccess = null;
79                    if (p.callbackDone == undefined)
80                        p.callbackDone = null;
81                    if (p.callbackFail == undefined)
82                        p.callbackFail = null;
83                    if (p.callbackAlways == undefined)
84                        p.callbackAlways = null;
85                    if (p.parseData == undefined)
86                        p.parseData = null;
87                    if (p.self == undefined)
88                        p.self = this;
89
90                    if (p.script != null && (o.datasource == 'ajax' || isDev)) {
91                        $.post(o.host + o.hostmodule + p.script, p.postdata, function (data) {
92                            if (data) {
93                                parseData = $.parseJSON(data);
94                                p.parseData = parseData;
95                            }
96
97                            if (p.callbackSuccess != null) {
98                                if (parseInt(parseData.ERROR.errCode) === 0)
99                                    p.callbackSuccess(parseData);
100                                else {
101                                    p.callbackFail(parseData.ERROR);
102                                }
103                            }
104
105                        }).done(function () {
106                            if (p.callbackDone != null)
107                                p.callbackDone(p.parseData);
108                        }).fail(function () {
109                            if (p.callbackFail != null)
110                                p.callbackFail(this);
111                        }).always(function () {
112                            if (p.callbackAlways != null)
113                                p.callbackAlways(this);
114                        });
115                    }
116                    else if (o.datasource == 'json') {
117                        if (p.callbackSuccess != null)
118                            p.callbackSuccess(o.data);
119                        if (p.callbackDone != null)
120                            p.callbackDone(this);
121                    }
122
123                };
124
125                var getDirTreeMaxWidth = function () {
126                    var scrWidth = $(o.maincontainer).width();
127                    return parseInt(scrWidth / 2);
128                }
129
130                var layoutRender = function () {
131                    var scrWidth = $('#' + o.maincontainer).width();
132                    var scrHeght = $(window).height();
133                    var dirTreeHeight = scrHeght - $('#' + o.titlebar).height() - $('#' + o.toolsbar).height() - $('#' + o.statusbar).height() - 2;
134                    $('#' + o.tree).parent().height(dirTreeHeight);
135                    $('#' + o.grid).parent().height(dirTreeHeight);
136                    $('#' + o.grid).parent().width('calc(100% - ' + ($('#' + o.tree).parent().width() + 8) + 'px)');
137                    var scollWidth = $('#' + o.grid).parent().width();
138                    maxWidth = getDirTreeMaxWidth();
139                    $(o.tree).height(dirTreeHeight - 5);
140                   
141                    if (o.invisibledButtons != null) {
142                        for (var i = 0; i < o.invisibledButtons.length; i++) {
143                                $('#' + o.invisibledButtons[i]).hide();
144                        }
145                    }
146                }
147
148                var createFileManager = function (parseData) {
149                    o.data = parseData;
150                    totalItem = o.data.DIRECTORIES.length;
151                    o.oTree = $('#' + o.tree).violetTree({data: o.data.DIRECTORIES, manager: oContainer});
152                    o.oGrid = $('#' + o.grid).violetGrid({data: o.data, manager: oContainer});
153                };
154
155                var getAllDirChild = function (parentID, aryChild) {
156                    parentID = parentID == null ? 0 : parentID;
157                    var dirList = searchItemsByParent(parentID, 'directory');
158                    var index = aryChild.length;
159                    aryChild[index] = parentID;
160                    if (dirList.length > 0) {
161                        for (var i = 0; i < dirList.length; i++) {
162                            getAllDirChild(dirList[i].id, aryChild);
163                        }
164                    }
165                }
166
167                var buildTreeFromParent = function (dirID, node) {
168                    var aryChildFiles = [];
169                    var aryChildDirs = [];
170                    var aryChildIDs = [];
171                    var aryTmp = [];
172                    var dir = o.data.DIRECTORIES[searchItemByID(dirID, 'directory')];
173                    aryChildDirs = searchItemsByParent(dirID, 'directory');
174                    aryChildFiles = searchItemsByParent(dirID, 'file');
175
176                    node.id = dir.id;
177                    node.name = dir.name;
178                    node.type = 'directory';
179
180                    $(aryChildDirs).each(function (index) {
181                        var id = this.id;
182                        var name = this.name;
183                        var type = 'directory';
184                        var cDir = {id: id, name: name, type: type, childs: null};
185                        aryChildIDs[index] = cDir;
186                    });
187
188                    if ($(aryChildFiles).length > 0) {
189                        if (node.files == undefined)
190                            node.files = [];
191                        $(aryChildFiles).each(function (index) {
192                            var id = this.id;
193                            var name = this.name;
194                            var type = 'file';
195                            var cFile = {id: id, name: name, type: type};
196                            node.files[index] = cFile;
197                        });
198                    }
199
200                    if ($(aryChildDirs).length > 0) {
201                        if (node.childs == undefined)
202                            node.childs = [];
203                        $(aryChildIDs).each(function (index) {
204                            node.childs[index] = new Object;
205                            buildTreeFromParent(aryChildIDs[index].id, node.childs[index]);
206                        });
207                    }
208                }
209
210                var checkChildExisted = function (id) {
211                    var dirList = searchItemsByParent(id, 'directory');
212                    var fileList = searchItemsByParent(id, 'file');
213                    return (dirList.length > 0) || (fileList.length > 0);
214                }
215
216                var doneInit = function () {
217                    bindEventToToolbars();
218                    documentEventsBinding();
219                };
220
221                var failInit = function (er) {
222                    bootbox.alert(er.err);
223                }
224
225                var init = function () {
226                    layoutRender();
227                    $('#' + o.tree).parent().resizable({
228                        maxWidth: maxWidth,
229                        minWidth: 220,
230                        handles: "e",
231                        resize: function (event, ui) {
232                            layoutRender();
233                        }
234                    });
235                    $(window).resize(function () {
236                        layoutRender();
237                        $('#' + o.tree).parent().resizable({maxWidth: maxWidth});
238                    });
239                    sendCommand({postdata: null, callbackSuccess: createFileManager, callbackDone: doneInit, callbackFail: failInit});
240                };
241
242                var searchItemByID = function (id, type) {
243                    var data = {};
244                    switch (type) {
245                        case 'directory':
246                            data = o.data.DIRECTORIES;
247                            break;
248                        case 'file':
249                            data = o.data.FILES;
250                            break;
251                        default:
252                            break;
253                    }
254
255                    //for (var i = 0; i < data.length; i++) {
256                    for (var i in data) {
257                        if (data[i].id == id) {
258                            return i;
259                        }
260                    }
261                }
262
263                var searchItemsByParent = function (parentID, type) {
264                    var data = {};
265                    var aryItem = [];
266                    var index = aryItem.length;
267
268                    switch (type) {
269                        case 'directory':
270                            data = o.data.DIRECTORIES;
271                            break;
272                        case 'file':
273                            data = o.data.FILES;
274                            break;
275                        default:
276                            break;
277                    }
278
279                    for (i in data) {
280                        if (data[i].parentID == parentID) {
281                            aryItem[index] = data[i];
282                            index++;
283                        }
284                    }
285
286                    return aryItem;
287                }
288
289                /**************************
290                 * TOOLBAR EVENTS - START *
291                 **************************/
292                var btnRefreshClick = function (obj) {
293                    $(o).find('i').addClass('icon-spin');
294                    sendCommand({postdata: null,
295                        callbackSuccess: function (parseData) {
296                            o.data = parseData;
297                            self.updateData({updateAll: true});
298                            o.oTree.refeshTree();
299                        },
300                        callbackDone: function () {
301                            $(o).find('i').removeClass('icon-spin');
302                        },
303                        callbackFail: failInit
304                    });
305                }
306
307                var btnNewFolderClick = function () {
308                    createFolderStart();
309                }
310
311                var btnUploadClick = function () {
312                    uploadStart();
313                    uploadInit();
314
315                }
316                var btnDownloadClick = function () {
317                    var items = o.oGrid.getHightLightItem();
318                   
319                    window.location.href = o.host+"download/getFile/"+items[0].id;
320                }
321
322                var btnDelClick = function () {
323                    var items = o.oGrid.getHightLightItem();
324                    if ($(items).length == 0) {
325                        var dirID = $(o.oTree.getSelectedNode()).attr('id');
326                        var item = o.data.DIRECTORIES[searchItemByID(dirID, 'directory')];
327                        item.type = 'directory';
328                        items = [item];
329                    }
330                    self.deleteItem(items);
331                }
332
333                var btnCopyClick = function () {
334                    copy('copy');
335                }
336
337                var btnPasteClick = function () {
338                    paste();
339                }
340
341                var btnCutClick = function () {
342                    copy('move');
343                }
344
345                var bindEventToToolbars = function () {
346                    $(btnRefresh).click(function (e) {
347                        btnRefreshClick(this)
348                    });
349
350                    $(btnNewFolder).click(function (e) {
351                        btnNewFolderClick()
352                    });
353                    $(btnUpload).click(function (e) {
354                        btnUploadClick()
355                    });
356
357                    $(btnDel).click(function (e) {
358                        btnDelClick()
359                    });
360                    $(btnCopy).click(function (e) {
361                        btnCopyClick()
362                    });
363                    $(btnCut).click(function (e) {
364                        btnCutClick()
365                    });
366                    $(btnPaste).click(function (e) {
367                        btnPasteClick()
368                    })
369                    $(btnPreview).click(function (e) {
370                        btnPreviewClick()
371                    })
372                    $(btnDownload).click(function (e) {
373                        btnDownloadClick()
374                    })
375
376                    /*btnShare
377                     btnPreview
378                     btnDownload
379                     btnUpload*/
380                }
381                /************************
382                 * TOOLBAR EVENTS - END *
383                 ************************/
384
385                /***********************************
386                 * DOCUMENT EVENTS BINDING - START *
387                 ***********************************/
388                var documentEventsBinding = function () {
389                    $(document).bind('keydown', function (e) {
390                        switch (e.which) {
391                            case 113:
392                            case 27:
393                                var gridSelectedItems = o.oGrid.getHightLightItem();
394                                if ($(gridSelectedItems).length > 0) {
395                                    o.oGrid.rename(e.which);
396                                } else {
397                                    o.oTree.rename(e.which);
398                                }
399                                break;
400                            case 46:
401                                //delete
402                                btnDelClick();
403                                break;
404                            case 65:
405                                if (e.ctrlKey) {
406                                    o.oGrid.selectAllNode();
407                                }
408                                break;
409                            default:
410                                break;
411                        }
412                    });
413                }
414                /***********************************
415                 * DOCUMENT EVENTS BINDING - END *
416                 ***********************************/
417
418                /*******************************
419                 * CREATE FOLDER - START *
420                 *******************************/
421                var createFolderStart = function () {
422                    var promptOptions = {
423                        title: "Tạo thư mục mới",
424                        buttons: {
425                            confirm: {
426                                label: "Lưu"
427                            },
428                            cancel: {
429                                label: "Há»§y"
430                            }
431                        },
432                        callback: function (result) {
433                            if (result === null) {
434                            } else {
435                                createFolder(treeCurrentNode, result);
436                            }
437                        }
438                    };
439
440                    return bootbox.prompt(promptOptions);
441                }
442
443                var uploadStart = function () {
444                    var userid = o.data.userinfo.us_id;
445                    var promptOptions = {
446                        title: "Tải lên",
447                        message: "<form id='upload' method='post'  action='"+api_url+"space/upload' enctype='multipart/form-data'><div id='drop'>Kéo thả tệp vào đây <a> Chọn tệp </a><input type='hidden' name='response' value='1'/><input type='hidden' name='dir' value='" + self.getTreeCurrentNode() + "'/><input type='hidden' name='userid' value='"+userid+"'/><input type='file' name='upload_file' multiple /></div><ul></ul></form>",
448                        buttons: {
449                            success: {
450                                label: "Xong",
451                                className: "btn btn-primary",
452                                callback: function (result) {
453                                   
454                                }
455                            },
456                        },
457                        onEscape: function() {uploadDone(self.getTreeCurrentNode());}
458                    };
459
460                    return bootbox.dialog(promptOptions);
461                }
462                var uploadDone = function (result) {
463                   
464                }
465                var btnPreviewClick = function(){
466                    var items = o.oGrid.getHightLightItem();
467                    if ($(items).length == 0) {
468                        var dirID = $(o.oTree.getSelectedNode()).attr('id');
469                        var item = o.data.DIRECTORIES[searchItemByID(dirID, 'directory')];
470                        item.type = 'directory';
471                        items = [item];
472                    }
473                    previewFile(items[0]);
474                }
475               
476                var previewFile = function(node) {
477                    var content="";
478                    $ext=node.fileurl.split('.').pop();
479                    $ext = $ext.toLowerCase();
480                    if($.inArray( $ext, [ "jpg", "jpeg","png","gif"])>=0)
481                   {
482                        content="<img style='width:100%' src='"+node.fileurl+"' /><br />"+node.name;
483                        bootbox.alert(content);
484                    }
485                   
486                    if($.inArray( $ext, [ "flv", "mp4","avi","m4v"])>=0)
487                   {
488                        $.ajax({
489                            url: o.host+"preview/getVideoPreview/",
490                            type: "POST",
491                            data: {fileurl:node.fileurl,name:node.name},
492                            success: function(data, textStatus, jqXHR)
493                            {
494                                bootbox.alert(data);
495                            }
496                            ,
497                            error: function(jqXHR, textStatus, errorThrown)
498                            {
499
500                            }
501                        });
502                    }
503                   
504                    if($.inArray( $ext, [ "ogg"])>=0)
505                   {
506                        content='<audio controls>\
507                        <source src="'+node.fileurl+'" type="audio/ogg">\
508                      Your browser does not support the audio element.\
509                      </audio> <br />'+node.name;
510                        bootbox.alert(content);
511                    }
512                    if($.inArray( $ext, [ "mp3"])>=0)
513                   {
514                        content='<audio controls>\
515                        <source src="'+node.fileurl+'" type="audio/mpeg">\
516                      Your browser does not support the audio element.\
517                      </audio> <br />'+node.name;
518                        bootbox.alert(content); 
519                    }
520                   
521                    if($.inArray( $ext, ["ppt","xls","doc","pdf","docx","pptx","xlsx"])>=0)
522                   {
523                         
524                            $.ajax({
525                            url: o.host+"preview/getFilePreview/"+node.id,
526                            type: "POST",
527                            data: {},
528                            success: function(data, textStatus, jqXHR)
529                            {
530                                bootbox.alert(data);
531                            }
532                            ,
533                            error: function(jqXHR, textStatus, errorThrown)
534                            {
535
536                            }
537                        });
538                    }
539                    if($.inArray( $ext, [ "xvl"])>=0)
540                   {
541                       var redirect = o.host + 'frontend/lecture/';
542                       self.redirectPost(redirect, {fileid: node.id});
543                    }
544                   
545                     
546                }
547               
548                var uploadInit = function () {
549                    var ul = $('#upload ul');
550
551                    $('#drop a').click(function () {
552                        $(this).parent().find('input').click();
553                    });
554                   
555                    $('#upload').fileupload({
556                        dropZone: $('#drop'),
557                        add: function (e, data) {
558
559                            var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"' +
560                                    ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');
561                            tpl.find('p').text(data.files[0].name)
562                                    .append('<i>' + formatFileSize(data.files[0].size) + '</i>');
563                            data.context = tpl.appendTo(ul);
564                            tpl.find('input').knob();
565                            tpl.find('span').click(function () {
566
567                                if (tpl.hasClass('working')) {
568                                    jqXHR.abort();
569                                }
570
571                                tpl.fadeOut(function () {
572                                    tpl.remove();
573                                });
574
575                            });
576                           
577                            var jqXHR = data.submit();
578                        },
579                        progress: function (e, data) {
580
581                            // Calculate the completion percentage of the upload
582                            var progress = parseInt(data.loaded / data.total * 100, 10);
583
584                            // Update the hidden input field and trigger a change
585                            // so that the jQuery knob plugin knows to update the dial
586                            data.context.find('input').val(progress).change();
587
588                            if (progress == 100) {
589                                data.context.removeClass('working');
590                               
591                               
592                                                       
593                               
594                            }
595                        },
596                        fail: function (e, data) {
597                            // Something has gone wrong!
598                            data.context.addClass('error');
599                        },
600                        done: function (e, data) {
601                            //var newFileData = {"DIRECTORIES": [{"id": "5000", "name": "Dir1", "parentID": 85}], "FILES": [{"id": "2000", "name": "File in root 1", "parentID": 85, "minetype": "text"},{"id": "2001", "name": "File in root 2", "parentID": 85, "minetype": "text"}], "ERROR": {"err": "", "errCode": 0}};
602                              var newFileData=data.result;
603                              newFileData=$.parseJSON(data.result);
604                                if (newFileData.ERROR.errCode == 0) {
605                                    for (var i = 0; i < $(newFileData.FILES).length; i++) {
606                                        var file = newFileData.FILES[i];
607                                        var node = {thumbnail: file.thumbnail, id: file.id, name: file.name,fileurl: file.fileurl, parentID: file.parentID, minetype:file.minetype};
608                                        o.oGrid.createNode(node);
609                                        o.data.FILES[$(o.data.FILES).length] = node;
610                                    }
611                                   
612                                    for (var i = 0; i < $(newFileData.DIRECTORIES).length; i++) {
613                                        var file = newFileData.DIRECTORIES[i];
614                                        var node = {id: file.id, name: file.name, parentID: file.parentID, minetype:file.minetype};
615                                        o.oTree.createNode(node);
616                                        o.oGrid.createNode(node);
617                                        o.data.DIRECTORIES[$(o.data.DIRECTORIES).length] = node;
618                                    }
619                                   
620                                    /*var node = {id: parseData.id, name: parseData.name, parentID: parseData.parentID};
621                                    o.oTree.createNode(node);
622                                    o.data.DIRECTORIES[$(o.data.DIRECTORIES).length] = node;
623                                    if (o.oGrid) o.oGrid.reloadGrid();*/
624                                }       
625                        }
626
627                    });
628                   
629                    $(document).on('drop dragover', function (e) {
630                        e.preventDefault();
631                    });
632                   
633                    function formatFileSize(bytes) {
634                        if (typeof bytes !== 'number') {
635                            return '';
636                        }
637
638                        if (bytes >= 1000000000) {
639                            return (bytes / 1000000000).toFixed(2) + ' GB';
640                        }
641
642                        if (bytes >= 1000000) {
643                            return (bytes / 1000000).toFixed(2) + ' MB';
644                        }
645
646                        return (bytes / 1000).toFixed(2) + ' KB';
647                    }
648
649                };
650
651                var createFolder = function (parent, name) {
652                    var postdata = {fname: name, fparentid: parent};
653                    var script = 'createdir';
654                    /*isDev = true;*/
655                    sendCommand({
656                        postdata: postdata,
657                        script: script,
658                        callbackSuccess: function (parseData) {
659                            createFolderFinish(parseData);
660                        },
661                        callbackFail: function () {
662                        }
663                    });
664                }
665
666                var createFolderFinish = function (parseData) {
667                    /*isDev = false;*/
668                    if (parseData.ERROR.errCode == 0) {
669                        var node = {id: parseData.id, name: parseData.name, parentID: parseData.parentID};
670                        o.oTree.createNode(node);
671                        o.data.DIRECTORIES[$(o.data.DIRECTORIES).length] = node;
672                        if (o.oGrid)
673                            o.oGrid.reloadGrid();
674                    }
675                }
676                /*******************************
677                 * CREATE FOLDER - END         *
678                 *******************************/
679                /********************************
680                 * COPY & PASTE & MOVE - START  *
681                 ************=*******************/
682                var copy = function (act) {
683                    //detect selected items
684                    //push to clipboard
685                    var items = o.oGrid.getHightLightItem();
686
687                    if ($(items).length == 0) {
688                        var node = o.oTree.getSelectedNode();
689                        var itemID = $(node).attr('id');
690
691                        if (itemID == 0)
692                            return false;
693
694                        items[0] = o.data.DIRECTORIES[searchItemByID(itemID, 'directory')];
695                        items[0].type = 'directory';
696                    }
697
698                    if ($(items).length > 0) {
699                        oClipBoard.items = items;
700                        oClipBoard.act = act;
701                    }
702                    return true;
703                }
704
705                var paste = function () {
706                    if ((oClipBoard.act != 'copy'
707                            && oClipBoard.act != 'move')
708                            || oClipBoard.items == null)
709                        return;
710
711                    var items = [];
712                    var destination = self.getTreeCurrentNode();
713                    if (oClipBoard.act != 'copy') {
714                        $(oClipBoard.items).each(function (index) {
715                            var node = new Object;
716                            if (this.type == 'directory')
717                                buildTreeFromParent(this.id, node);
718                            else {
719                                node.id = this.id;
720                                node.type = 'file';
721                            }
722
723                            items[index] = node;
724                        });
725                    }
726                    else {
727                        items = oClipBoard.items;
728                    }
729
730                                        for (var i = 0; i < items.length; i++) {
731                                                if (items[i].type == 'directory') {
732                                                        items[i] = self.getAllDirChilds(items[i]);                                                     
733                                                }
734                                        }
735                                       
736                    var postdata = {act: oClipBoard.act, destination: destination, data: JSON.stringify(items)};
737                    var script = oClipBoard.act;
738
739                    sendCommand({
740                        postdata: postdata,
741                        script: script,
742                        callbackSuccess: function (parseData) {
743                            if (oClipBoard.act == 'copy') {
744                                $(parseData.DIRECTORIES).each(function (index) {
745                                    o.data.DIRECTORIES[$(o.data.DIRECTORIES).length] = this;
746                                });
747
748                                $(parseData.FILES).each(function (index) {
749                                    o.data.FILES[$(o.data.FILES).length] = this;
750                                });
751
752                                o.data.DIRECTORIES.sort(function (a, b) {
753                                    return a.parentID - b.parentID;
754                                });
755
756                                o.oTree.setData(o.data.DIRECTORIES);
757                                o.oGrid.setData(o.data);
758                                o.oTree.createCopyNode(parseData.DIRECTORIES);
759                                o.oGrid.reloadGrid();
760                            }
761                            else if (oClipBoard.act == 'move') {
762
763                            }
764                        }
765                    });
766                }
767
768                var move = function () {
769
770                }
771
772                var copyTo = function () {
773
774                }
775
776                var moveTo = function () {
777
778                }
779
780                /*****************************
781                 * COPY & PASTE & MOVE - END *
782                 *****************************/
783
784                this.deleteItem = function (item) {
785
786                    var confirmText = 'Bạn có muốn xóa ';
787
788                    if ($.isArray(item) && item.length > 1) {
789                        confirmText += 'các thư mục (và files) đã chọn?';
790                    }
791                    else if (item.length == 1) {
792                        if (item[0].id == 0)
793                            return false;
794                        confirmText += (item[0].type == 'directory') ? 'thư mục' : 'file';
795                        confirmText += ' <span style="font-weight:bold">' + item[0].name + "</span> khÃŽng?";
796                    }
797
798                    confirmText += '<br /><div style="color:red">(hành động này sẜ xóa tất cả thư mục con và các file trong các thư mục đã chọn)</div>';
799
800                    var parentID = item[0].parentID;
801
802                    for (var i = 0; i < item.length; i++) {
803                        if (item[i].type == 'directory') {
804                                item[i] = self.getAllDirChilds(item[i]);                               
805                        }
806                    }
807
808                    var confirmOptions = {
809                        message: confirmText,
810                        buttons: {
811                            confirm: {
812                                label: "Xóa"
813                            },
814                            cancel: {
815                                label: "KhÃŽng xóa"
816                            }
817                        },
818                        callback: function (result) {
819                            if (result) {
820                                var delobj = JSON.stringify(item);
821                                var postdata = {delobj: delobj};
822                                var script = 'delete';
823                                sendCommand({
824                                    postdata: postdata,
825                                    script: script,
826                                    callbackSuccess: function (parseData) {
827                                        if ($(parseData.DIRECTORIES).length > 0) {
828                                            $(parseData.DIRECTORIES).each(function (index) {
829                                                o.oTree.deletion(this);
830                                                o.oGrid.deletion(this, 'directory');
831                                            });
832                                        }
833
834                                        if ($(parseData.FILES).length > 0) {
835                                            $(parseData.FILES).each(function (index) {
836                                                var file = o.data.FILES[searchItemByID(this, 'file')];
837                                                o.oGrid.deletion(this, file.minetype);
838                                            });
839                                        }
840                                    },
841                                    callbackDone: function (obj) {
842                                        if ($(parseData.DIRECTORIES).length > 0) {
843                                            $(parseData.DIRECTORIES).each(function (index) {
844                                                delete o.data.DIRECTORIES[searchItemByID(this, 'directory')];
845                                            });
846                                        }
847
848                                        if ($(parseData.FILES).length > 0) {
849                                            $(parseData.FILES).each(function (index) {
850                                                delete o.data.FILES[searchItemByID(this, 'file')];
851                                            });
852                                        }
853
854                                        o.oTree.setData(o.data.DIRECTORIES);
855                                        o.oGrid.setData(o.data);
856                                        self.setTreeCurrentNode(parentID);
857                                        o.oGrid.reloadGrid();
858                                    },
859                                    callbackFail: function () {
860                                    }
861                                });
862                            }
863                        }
864                    };
865
866                    bootbox.confirm(confirmOptions);
867                }
868               
869                this.getAllDirChilds = function (oDirItem) {
870                        var aryChildDirTmp = [];
871                    var aryChildDirID = [];
872                    var aryChildFiles = searchItemsByParent(oDirItem.id, 'file');
873                    var aryChildDirs = [];
874
875                    getAllDirChild(oDirItem.id, aryChildDirTmp);
876                    for (var d = 1; d < aryChildDirTmp.length; d++) {
877                        aryChildDirID[d - 1] = aryChildDirTmp[d];
878                    }
879
880                    for (var j = 0; j < aryChildDirID.length; j++) {
881                        if (o.data.DIRECTORIES[searchItemByID(aryChildDirID[j], 'directory')] != undefined)
882                            aryChildDirs[aryChildDirs.length] = o.data.DIRECTORIES[searchItemByID(aryChildDirID[j], 'directory')];
883
884                        var aryTmp = searchItemsByParent(aryChildDirID[j], 'file');
885                        if (aryTmp.length > 0)
886                            for (var f in aryTmp) {
887                                aryChildFiles[aryChildFiles.length] = aryTmp[f];
888                            }
889                    }
890
891                    oDirItem.childDirs = aryChildDirs;
892                    oDirItem.childFiles = aryChildFiles;
893                    return oDirItem;
894                }
895
896                this.setTreeCurrentNode = function (treeNode) {
897                    //fire when click a node on Tree
898                    //then fire action of Grid
899                    treeCurrentNode = treeNode;
900                    if (o.oGrid)
901                        o.oGrid.reloadGrid();
902                };
903
904                this.getTreeCurrentNode = function () {
905                    return treeCurrentNode;
906                }
907
908                this.gridNodeDblClick = function (node) {
909                    if (node.minetype == 'directory') {
910                        var treeNode = $('#' + o.tree).find('UL.vstree[rel^="node' + node.parentID + '"] > LI[rel^="folder"] > A#' + node.id);
911                        o.oTree.activeNode(treeNode);
912                    }
913                    else {
914                        //execute or preview file
915                         previewFile(node);
916                    }
917                };
918
919                this.createNewFolder = function () {
920
921                }
922
923                this.updateData = function (p) {
924                    if (p.item == undefined)
925                        p.item = null;
926                    if (p.updateAll == undefined)
927                        p.updateAll = false;
928                    if (p.from == undefined)
929                        p.from = null;
930                    if (p.type == undefined)
931                        p.type = null;
932                    if (p.callback == undefined)
933                        p.callback = null;
934
935                    var obj = p.from == 'tree' ? o.oGrid : o.oTree;
936                    if (!p.updateAll) {
937                        var index = searchItemByID(p.item.id, p.type);
938                        switch (p.type) {
939                            case 'directory':
940                                o.data.DIRECTORIES[index].name = p.item.name;
941                                o.data.DIRECTORIES[index].parentID = p.item.parentID;
942                                break;
943                            case 'file':
944                                o.data.FILES[index].name = p.item.name;
945                                o.data.FILES[index].parentID = p.item.parentID;
946                                o.data.FILES[index].minetype = p.item.minetype;
947                                o.data.FILES[index].fileurl = p.item.fileurl;
948                                break;
949                            default:
950                                break;
951                        }
952                    }
953
954                    o.oTree.setData(o.data.DIRECTORIES);
955                    o.oGrid.setData(o.data);
956
957                    if (p.callback != null) {
958                        eval('obj.' + p.callback + '(p.item);')
959                    }
960
961                    //call sendCommand
962                }
963
964                this.searchItemsByParent = function (parentID, type) {
965                    return searchItemsByParent(parentID, type);
966                }
967
968                this.searchItemByID = function (parentID, type) {
969                    return searchItemByID(parentID, type);
970                }
971               
972                this.refreshStatusBar = function (message) {
973                        $(statusbar).text(message);
974                }
975               
976                this.redirectPost = function(location, args){
977                    var form = '';
978                    $.each( args, function( key, value ) {
979                        form += '<input type="hidden" name="'+key+'" value="'+value+'">';
980                    });
981                    $('<form action="'+location+'" method="POST">'+form+'</form>').appendTo('body').submit();
982                }
983               
984                this.initialize = function () {
985                    init();
986                    return this;
987                };
988
989                return this.initialize();
990            }
991        });
992    })(jQuery);
Note: See TracBrowser for help on using the repository browser.