source: pro-bachkim-filespace/sourcecode/assets/js/manager.js @ 137

Last change on this file since 137 was 121, checked in by dungnv, 11 years ago
  • Property svn:mime-type set to text/plain
File size: 18.9 KB
Line 
1if(jQuery) (function($){
2        $.extend($.fn, {
3                violetFileManager : function (o) {
4                        if( !o ) var o = {};
5                        if( o.tree == undefined ) o.tree = null;
6                        if( o.grid == undefined ) o.grid = null;
7                       
8                        if( o.maincontainer == undefined ) o.maincontainer = null;
9                        if( o.titlebar == undefined ) o.titlebar = null;
10                        if( o.toolsbar == undefined ) o.toolsbar = null;
11                        if( o.statusbar == undefined ) o.statusbar = null;
12                       
13                        if( o.oTree == undefined ) o.oTree = null;
14                        if( o.oGrid == undefined ) o.oGrid = null;
15                        if( o.host == undefined ) o.host = 'http://localhost/ajax/';
16                        if( o.hostmodule == undefined ) o.hostmodule = 'privatecontent/';
17                        if( o.script == undefined ) o.script = 'getcontent';
18                        if( o.data == undefined ) o.data = null;
19                        if( o.datasource == undefined ) o.datasource = 'ajax';
20                       
21                        var isDev = false;
22                        var contextmenu = null;
23                        var oContainer = this;
24                        var tree = [];
25                        var totalItem = 0;
26                        var countItem = 0;
27                        var maxWidth = 0;
28                        var treeCurrentNode = null;
29                        var self = this;
30                        var oClipBoard = {items:null, act:null};
31                       
32                        /**
33                         * Toolbar defined
34                         * */
35                        var btnNewFolder = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnNewFolder');
36                        var btnDel = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnDel');
37                        var btnCopy = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnCopy');
38                        var btnCut = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnCut');
39                        var btnPaste = $('#' + o.toolsbar + ' > DIV.btn-group.basic > #btnPaste');
40                        var btnShare = $('#' + o.toolsbar + ' > DIV.btn-group.social > #btnShare');
41                        var btnPreview = $('#' + o.toolsbar + ' > DIV.btn-group.social > #btnPreview');
42                        var btnDownload = $('#' + o.toolsbar + ' > DIV.btn-group.creation > #btnDownload');
43                        var btnUpload = $('#' + o.toolsbar + ' > DIV.btn-group.creation > #btnUpload');
44                        var btnRefresh = $('#' + o.toolsbar + ' > DIV.btn-group.control > #btnRefresh');
45                       
46                        var sendCommand = function (p) {
47                                if( p.postdata == undefined ) p.postdata = null;
48                                if( p.script == undefined ) p.script = o.script;
49                                if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
50                                if( p.callbackDone == undefined ) p.callbackDone = null;
51                                if( p.callbackFail == undefined ) p.callbackFail = null;
52                                if( p.callbackAlways == undefined ) p.callbackAlways = null;
53                                if( p.parseData == undefined ) p.parseData = null;
54                                if( p.self == undefined ) p.self = this;
55
56                                if (p.script != null && (o.datasource == 'ajax' || isDev)) {
57                                        $.post(o.host + o.hostmodule + p.script, p.postdata, function (data){
58                                                if (data) {
59                                                        parseData = $.parseJSON(data);
60                                                        p.parseData = parseData;
61                                                }
62
63                                                if (p.callbackSuccess != null) {
64                                                        if (parseInt(parseData.ERROR.errCode) === 0)
65                                                                p.callbackSuccess(parseData);
66                                                        else {
67                                                                p.callbackFail(parseData.ERROR);
68                                                        }
69                                                }
70
71                                        }).done(function() {if (p.callbackDone != null)p.callbackDone(p.parseData);}).fail(function() {if (p.callbackFail != null)p.callbackFail(this);}).always(function() {if (p.callbackAlways != null)p.callbackAlways(this);});
72                                }
73                                else if (o.datasource == 'json'){
74                                        if (p.callbackSuccess != null) p.callbackSuccess(o.data);
75                                        if (p.callbackDone != null)     p.callbackDone(this);
76                                }
77                               
78                        };
79                       
80                        var getDirTreeMaxWidth = function  () {
81                                var scrWidth = $(o.maincontainer).width();
82                                return parseInt(scrWidth / 2);
83                        }
84                       
85                        var layoutRender = function () {
86                                var scrWidth = $('#' + o.maincontainer).width();
87                                var scrHeght = $(window).height();
88                                var dirTreeHeight = scrHeght - $('#' + o.titlebar).height() - $('#' + o.toolsbar).height() - $('#' + o.statusbar).height() - 2;
89                                $('#' + o.tree).parent().height(dirTreeHeight);
90                                $('#' + o.grid).parent().height(dirTreeHeight);
91                                $('#' + o.grid).parent().width('calc(100% - ' + ($('#' + o.tree).parent().width() + 8) + 'px)');
92                                var scollWidth = $('#' + o.grid).parent().width();
93                                maxWidth = getDirTreeMaxWidth();
94                                $(o.tree).height(dirTreeHeight - 5);
95                        }
96                       
97                        var createFileManager = function (parseData) {
98                                o.data = parseData;
99                                totalItem = o.data.DIRECTORIES.length;
100                                o.oTree = $('#' + o.tree).violetTree({data:o.data.DIRECTORIES,manager:oContainer});
101                                o.oGrid = $('#' + o.grid).violetGrid({data:o.data,manager:oContainer});
102                        };
103
104                        var getAllDirChild = function (parentID, aryChild) {
105                                parentID = parentID == null ? 0:parentID;
106                                var dirList = searchItemsByParent(parentID,'directory');
107                                var index = aryChild.length;
108                                aryChild[index] = parentID;
109                                if (dirList.length > 0) {
110                                        for (var i = 0; i < dirList.length; i++) {
111                                                getAllDirChild(dirList[i].id, aryChild);
112                                        }
113                                }
114                        }
115
116                        var buildTreeFromParent = function (dirID, node) {                             
117                                var aryChildFiles = [];
118                                var aryChildDirs = [];
119                                var aryChildIDs = [];
120                                var aryTmp = [];
121                                var dir = o.data.DIRECTORIES[searchItemByID(dirID, 'directory')];
122                                aryChildDirs = searchItemsByParent(dirID, 'directory');
123                                aryChildFiles = searchItemsByParent(dirID, 'file');
124
125                                node.id = dir.id;
126                                node.name = dir.name;
127                                node.type = 'directory';
128
129                                $(aryChildDirs).each(function (index) {
130                                        var id = this.id;
131                                        var name = this.name;
132                                        var type = 'directory';
133                                        var cDir = {id:id, name:name, type:type, childs:null};
134                                        aryChildIDs[index] = cDir;
135                                });
136
137                                if ($(aryChildFiles).length > 0 ) {
138                                        if (node.files == undefined) node.files = [];
139                                        $(aryChildFiles).each(function (index) {
140                                                var id = this.id;
141                                                var name = this.name;
142                                                var type = 'file';
143                                                var cFile = {id:id, name:name, type:type};
144                                                node.files[index] = cFile;
145                                        });
146                                }                               
147
148                                if ($(aryChildDirs).length > 0) {
149                                        if (node.childs == undefined) node.childs = [];
150                                        $(aryChildIDs).each(function (index) {
151                                                node.childs[index] = new Object;
152                                                buildTreeFromParent(aryChildIDs[index].id, node.childs[index]);
153                                        });
154                                }
155                        }
156                       
157                        var checkChildExisted = function (id) {
158                                var dirList = searchItemsByParent(id,'directory');
159                                var fileList = searchItemsByParent(id,'file');
160                                return (dirList.length > 0) || (fileList.length > 0);
161                        }
162                       
163                        var doneInit = function () {
164                                bindEventToToolbars();
165                                documentEventsBinding();
166                        };
167                       
168                        var failInit = function (er) {
169                                bootbox.alert(er.err);
170                        }
171                       
172                        var init = function () {
173                                layoutRender ();
174                                $('#' + o.tree).parent().resizable({
175                                        maxWidth: maxWidth,
176                                        minWidth: 220,
177                                        handles: "e",
178                                        resize: function (event, ui) {
179                                                layoutRender ();
180                                        }
181                                });
182                                $(window).resize (function() {layoutRender ();$('#' + o.tree).parent().resizable({maxWidth: maxWidth});});
183                                sendCommand ({postdata:null,callbackSuccess:createFileManager,callbackDone:doneInit,callbackFail:failInit});
184                        };
185                       
186                        var searchItemByID = function (id, type) {
187                                var data = {};
188                                switch (type) {
189                                        case 'directory':
190                                                data = o.data.DIRECTORIES;
191                                                break;
192                                        case 'file':
193                                                data = o.data.FILES;
194                                                break;
195                                        default:
196                                                break;
197                                }
198                               
199                                for (var i = 0; i < data.length; i++) {
200                                        if (data[i].id == id) return i;
201                                }
202                        }
203                       
204                        var searchItemsByParent = function (parentID, type) {
205                                var data = {};
206                                var aryItem = [];
207                                var index = aryItem.length;
208                               
209                                switch (type) {
210                                        case 'directory':
211                                                data = o.data.DIRECTORIES;
212                                                break;
213                                        case 'file':
214                                                data = o.data.FILES;
215                                                break;
216                                        default:
217                                                break;
218                                }
219                               
220                                for (var i = 0; i < data.length; i++) {
221                                        if (data[i].parentID == parentID) {
222                                                aryItem[index] = data[i];
223                                                index ++;
224                                        }
225                                }
226                               
227                                return aryItem;
228                        }
229                       
230                        /**************************
231                         * TOOLBAR EVENTS - START *
232                         **************************/
233                        var btnRefreshClick = function (obj) {
234                                $(o).find('i').addClass('icon-spin');                           
235                                sendCommand ({  postdata:null,
236                                                                callbackSuccess:function (parseData) {
237                                                                        o.data = parseData;
238                                                                        self.updateData({updateAll:true});
239                                                                        o.oTree.refeshTree();
240                                                                        console.log(o);
241                                                                },
242                                                                callbackDone:function () {$(o).find('i').removeClass('icon-spin');},
243                                                                callbackFail:failInit
244                                        });
245                        }
246                       
247                        var btnNewFolderClick = function () {
248                                createFolderStart();
249                        }
250                       
251                        var btnDelClick = function () {
252                                var items = o.oGrid.getHightLightItem();
253                                if ($(items).length == 0){
254                                        var dirID = $(o.oTree.getSelectedNode()).attr('id');
255                                        var item = o.data.DIRECTORIES[searchItemByID(dirID,'directory')];
256                                        item.type = 'directory';
257                                        items = [item];
258                                }
259                                self.deleteItem(items);
260                        }
261                       
262                        var btnCopyClick = function () {                               
263                                copy();
264                        }
265
266                        var btnPasteClick = function () {
267                                paste();
268                        }
269                       
270                       
271                        var bindEventToToolbars = function () {
272                                $(btnRefresh).click(function(e){btnRefreshClick(this)});
273                               
274                                $(btnNewFolder).click(function(e){btnNewFolderClick()});
275                                $(btnDel).click(function(e){btnDelClick()});
276                                $(btnCopy).click(function(e){btnCopyClick()});
277                                $(btnPaste).click(function(e){btnPasteClick()})
278                               
279                                /*btnCut
280                                btnShare
281                                btnPreview
282                                btnDownload
283                                btnUpload*/
284                        }
285                        /************************
286                         * TOOLBAR EVENTS - END *
287                         ************************/
288
289                        /***********************************
290                         * DOCUMENT EVENTS BINDING - START *
291                         ***********************************/
292                         var documentEventsBinding = function () {
293                                $(document).bind('keydown', function (e){
294                                        switch( e.which ) {
295                                                case 113:                                                       
296                                                case 27:
297                                                        var gridSelectedItems = o.oGrid.getHightLightItem();
298                                                        if ($(gridSelectedItems).length > 0) {
299                                                                o.oGrid.rename(e.which);
300                                                        }else {                                                         
301                                                                o.oTree.rename(e.which);
302                                                        }
303                                                        break;
304                                                case 46:
305                                                        //delete
306                                                        btnDelClick();
307                                                        break;
308                                                case 65:
309                                                        if (e.ctrlKey) {
310                                                                o.oGrid.selectAllNode();
311                                                        }
312                                                        break;
313                                                default:
314                                                        break;
315                                        }
316                                });
317                         }
318                        /***********************************
319                         * DOCUMENT EVENTS BINDING - END *
320                         ***********************************/                   
321                       
322                        /*******************************
323                         * CREATE FOLDER - START *
324                         *******************************/
325                        var createFolderStart = function () {
326                                var promptOptions = {
327                                                  title: "Tạo thư mục mới",
328                                                  buttons: {
329                                                    confirm: {
330                                                      label: "Lưu"
331                                                    },
332                                                    cancel: {
333                                                      label: "Há»§y"   
334                                                    }
335                                                  },
336                                                  callback: function(result) {               
337                                                      if (result === null) {                                             
338                                                                                     
339                                                      } else {
340                                                          createFolder(treeCurrentNode, result);
341                                                      }
342                                                    }
343                                                };
344
345                                return bootbox.prompt(promptOptions);
346                        }
347                       
348                        var createFolder = function (parent, name) {
349                                var postdata = {fname:name,fparentid:parent};
350                                var script = 'createdir';
351                                /*isDev = true;*/
352                                sendCommand ({
353                                        postdata:postdata,
354                                        script:script,
355                                        callbackSuccess:function(parseData){createFolderFinish(parseData);},
356                                        callbackFail: function(){}
357                                });
358                        }
359                       
360                        var createFolderFinish = function (parseData) {
361                                /*isDev = false;*/
362                                if (parseData.ERROR.errCode == 0) {
363                                        var node = {id:parseData.id, name:parseData.name,parentID:parseData.parentID};
364                                        o.oTree.createNode(node);
365                                        o.data.DIRECTORIES[$(o.data.DIRECTORIES).length] = node;
366                                        if (o.oGrid) o.oGrid.reloadGrid(parseData.parentID);
367                                }
368                        }
369                        /*******************************
370                         * CREATE FOLDER - END         *
371                         *******************************/
372                        /********************************
373                         * COPY & PASTE & MOVE - START  *
374                         ************=*******************/
375                        var copy = function (){
376                                //detect selected items
377                                //push to clipboard
378                                var items = o.oGrid.getHightLightItem();
379                               
380                                if ($(items).length == 0) {
381                                        var node = o.oTree.getSelectedNode();
382                                        var itemID = $(node).attr('id');
383                                        items[0] = o.data.DIRECTORIES[searchItemByID(itemID, 'directory')];
384                                        items[0].type = 'directory';
385                                }
386
387                                if ($(items).length > 0) {
388                                        oClipBoard.items = items;
389                                        oClipBoard.act = 'copy';
390                                }
391                        }
392                       
393                        var paste = function () {
394                                if ((oClipBoard.act != 'copy'
395                                        && oClipBoard.act != 'move')
396                                        || oClipBoard.items == null) return;
397                               
398                                var items = [];
399                                var destination = self.getTreeCurrentNode();
400
401                                $(oClipBoard.items).each(function (index) {
402                                        var node = new Object;
403                                        if (this.type == 'directory')
404                                                buildTreeFromParent(this.id, node);
405                                        else {
406                                                node.id = this.id;
407                                                node.type = 'file';
408                                        }
409
410                                        items[index] = node;
411                                });
412
413                                var postdata = {destination:destination,data:JSON.stringify(items)};
414                                var script = 'copy';
415                                sendCommand ({
416                                        postdata:postdata,
417                                        script:script,
418                                        callbackSuccess:function(parseData){
419                                                console.log(parseData);
420                                        }
421                                });
422                               
423                        }
424
425                        var move = function () {
426
427                        }
428
429                        var copyTo = function () {
430
431                        }
432
433                        var moveTo = function () {
434
435                        }
436                       
437                        /*****************************
438                         * COPY & PASTE & MOVE - END *
439                         *****************************/
440
441                        this.deleteItem = function (item) {
442                               
443                                var confirmText = 'Bạn có muốn xóa ';
444                               
445                                if ($.isArray(item) && item.length > 1) {
446                                        confirmText += 'các thư mục (và files) đã chọn?';
447                                }
448                                else if (item.length == 1) {
449                                        if (item[0].id == 0) return false;
450                                        confirmText += (item[0].type == 'directory')?'thư mục':'file';
451                                        confirmText += ' <span style="font-weight:bold">' + item[0].name + "</span> khÃŽng?";
452                                }
453                               
454                                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>';
455                               
456                                for (var i = 0; i < item.length; i++) {
457                                        if (item[i].type == 'directory') {
458                                                var aryChildDirTmp = [];
459                                                var aryChildDirID = [];
460                                                var aryChildFiles = searchItemsByParent(item[i].id, 'file');
461                                                var aryChildDirs = [];
462                                               
463                                                getAllDirChild (item[i].id, aryChildDirTmp);
464                                                for(var d = 1; d < aryChildDirTmp.length; d++) {
465                                                        aryChildDirID[d-1] = aryChildDirTmp[d];
466                                                }
467                                               
468                                                for (var j = 0; j < aryChildDirID.length; j++) {
469                                                        if (o.data.DIRECTORIES[searchItemByID(aryChildDirID[j],'directory')] != undefined)
470                                                                aryChildDirs[aryChildDirs.length] = o.data.DIRECTORIES[searchItemByID(aryChildDirID[j],'directory')];
471                                                       
472                                                        var aryTmp = searchItemsByParent(aryChildDirID[j], 'file');
473                                                        if (aryTmp.length > 0)
474                                                                for(var f in aryTmp) {
475                                                                        aryChildFiles[aryChildFiles.length] = aryTmp[f];
476                                                                }
477                                                }
478                                               
479                                                item[i].childDirs = aryChildDirs;
480                                                item[i].childFiles = aryChildFiles;
481                                        }
482                                }
483                               
484                                var confirmOptions = {
485                                                  message:confirmText,
486                                                  buttons: {
487                                                    confirm: {
488                                                      label: "Xóa"
489                                                    },
490                                                    cancel: {
491                                                      label: "KhÃŽng xóa"     
492                                                    }
493                                                  },
494                                                  callback: function(result) {               
495                                                          if (result) {
496                                                                        var delobj = JSON.stringify(item);
497                                                                        var postdata = {delobj:delobj};
498                                                                        var script = 'deletedir';
499                                                                        sendCommand ({
500                                                                                postdata:postdata,
501                                                                                script:script,
502                                                                                callbackSuccess:function(parseData){
503                                                                                        if($(parseData.DIRECTORIES).length > 0) {
504                                                                                                $(parseData.DIRECTORIES).each (function (index) {
505                                                                                                        o.oTree.deletion(this);
506                                                                                                        o.oGrid.deletion(this, 'directory');   
507                                                                                                });
508                                                                                        }
509
510                                                                                        if($(parseData.FILES).length > 0) {
511                                                                                                $(parseData.FILES).each (function (index) {
512                                                                                                        var file = o.data.FILES[searchItemByID(this, 'file')];                                                                                 
513                                                                                                        o.oGrid.deletion(this, file.minetype);
514                                                                                                });     
515                                                                                        }
516                                                                                },
517                                                                                callbackDone: function (obj) {
518                                                                                        var dirs = [];
519                                                                                        var files = [];
520                                                                                        if($(parseData.DIRECTORIES).length > 0) {
521                                                                                                $(o.data.DIRECTORIES).each(function (index){
522                                                                                                        var found = $.inArray(this.id, parseData.DIRECTORIES);
523                                                                                                        if(found == -1) {
524                                                                                                                dirs[$(dirs).length] = this;
525                                                                                                        }
526                                                                                                });
527                                                                                               
528                                                                                                o.data.DIRECTORIES = dirs;
529                                                                                        }
530                                                                                       
531                                                                                        if($(parseData.FILES).length > 0) {
532                                                                                                $(o.data.FILES).each(function (index){
533                                                                                                        var found = $.inArray(this.id, parseData.FILES);
534                                                                                                        if(found == -1) {
535                                                                                                                files[$(dirs).length] = this;
536                                                                                                        }
537                                                                                                })
538                                                                                               
539                                                                                                o.data.FILES = files;
540                                                                                        }
541                                                                                       
542                                                                                        o.oTree.setData (o.data.DIRECTORIES);
543                                                                                        o.oGrid.setData (o.data);
544                                                                                },
545                                                                                callbackFail: function(){}
546                                                                        });
547                                                                }
548                                                    }
549                                                };
550                               
551                                bootbox.confirm(confirmOptions);
552                        }
553                       
554                        this.setTreeCurrentNode = function (treeNode) {
555                                //fire when click a node on Tree
556                                //then fire action of Grid
557                                treeCurrentNode = treeNode;
558                                if (o.oGrid) o.oGrid.reloadGrid(treeNode);
559                        };
560                       
561                        this.getTreeCurrentNode = function () {
562                                return treeCurrentNode;
563                        }
564                       
565                        this.gridNodeDblClick = function (node) {
566                                if (node.minetype == 'directory') {
567                                        var treeNode = $('#' + o.tree).find('UL.vstree[rel^="node' + node.parentID + '"] > LI[rel^="folder"] > A#' + node.id);
568                                        o.oTree.activeNode(treeNode);
569                                }
570                                else {
571                                        //execute or preview file
572                                }
573                        };
574                       
575                        this.createNewFolder = function () {
576                               
577                        }
578                       
579                        this.updateData = function (p) {
580                                if( p.item == undefined ) p.item = null;
581                                if( p.updateAll == undefined ) p.updateAll = false;
582                                if( p.from == undefined ) p.from = null;
583                                if( p.type == undefined ) p.type = null;
584                                if( p.callback == undefined ) p.callback = null;
585
586                                var obj = p.from == 'tree' ? o.oGrid : o.oTree;
587                                if (!p.updateAll) {
588                                        var index = searchItemByID(p.item.id, p.type);
589                                        switch (p.type) {
590                                                case 'directory':
591                                                        o.data.DIRECTORIES[index].name = p.item.name;
592                                                        o.data.DIRECTORIES[index].parentID = p.item.parentID;
593                                                        break;
594                                                case 'file':
595                                                        o.data.FILES[index].name = p.item.name;
596                                                        o.data.FILES[index].parentID = p.item.parentID;
597                                                        o.data.FILES[index].minetype = p.item.minetype;
598                                                        break;
599                                                default:
600                                                        break;
601                                        }
602                                }
603                               
604                                o.oTree.setData (o.data.DIRECTORIES);
605                                o.oGrid.setData (o.data);
606                               
607                                if (p.callback != null) {
608                                        eval('obj.' + p.callback + '(p.item);')
609                                }
610                               
611                                //call sendCommand
612                        }
613                       
614                        this.searchItemsByParent = function (parentID, type) {
615                                return searchItemsByParent(parentID, type);
616                        }
617                       
618                        this.searchItemByID = function (parentID, type) {
619                                return searchItemByID(parentID, type);
620                        }
621                       
622                        this.initialize = function () {
623                                init();
624                                return this;
625                        };
626                       
627                        return this.initialize();
628                }
629        });
630})(jQuery);
Note: See TracBrowser for help on using the repository browser.