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

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