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

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