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

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