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

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