source: pro-bachkim-filespace/sourcecode/assets/js/vsgrid.js @ 15

Last change on this file since 15 was 15, checked in by dungnv, 11 years ago
  • Property svn:mime-type set to text/plain
File size: 4.2 KB
Line 
1if(jQuery) (function($){
2       
3        $.extend($.fn, {
4                violetGrid: function (o) {
5                        if( !o ) var o = {};
6                        if( o.container == undefined ) o.container = $(this);
7                        if( o.defaultViewMode == undefined ) o.defaultViewMode = 'thumbnail';//or 'list'
8                        if( o.directoryTreeData == undefined ) o.directoryTreeData = null;
9                        if( o.dirIDprefix == undefined ) o.dirIDprefix = null;
10                       
11                        var createNode = function (d) {
12                                if( !d ) var d = {};
13                                if( d.id == undefined ) d.id = null;
14                                if( d.name == undefined ) d.name = null;
15                                if( d.minetype == undefined ) d.minetype = 'directory';
16                                if( d.parentID == undefined ) d.parentID = null;
17                                if( d.clickEvent == undefined ) d.clickEvent = null;
18                                if( d.customEvent == undefined ) d.customEvent = null;
19
20                                var strHTML = '<div class="vscell" rel="id:' + d.id + '">';
21                                        strHTML += '<div class="selector unselected">';
22                                        strHTML +=      '<div class="icon-' + d.minetype + '"></div>';
23                                        strHTML += '</div>';
24                                        strHTML += '<div class="file-name unselected">' + d.name + '</div>';
25                                        strHTML += '</div>';
26                               
27                                var disabledItemsList = null;
28                               
29                                if (d.minetype == 'directory') {
30                                        disabledItemsList = ['preview'];
31                                }
32                               
33                                $(o.container).append(strHTML);
34
35                                $('div[rel="id:'+ d.id +'"]').bind('click',function(e){itemClick(this)});                               
36                                $('div[rel="id:'+ d.id +'"]').contextMenu({
37                                        menu: 'gridMenu',
38                                        disabledItems: disabledItemsList
39                                }, function(action, el, pos) {
40                               
41                                        switch(action) {
42                                                case 'rename':
43                                                        itemClick(el);
44                                                        rename(el);
45                                                        break;
46                                                default:
47                                                        break;
48                                        }
49                                       
50                                });
51                        }
52                       
53                        var keyboardRename = function () {
54                                $(document).keypress( function(e) {
55                                        var selectedItem = $('.selector.selected').parent();
56                                        switch( e.keyCode ) {
57                                                case 113:
58                                                        rename(selectedItem);
59                                                        break;
60                                                case 32:
61                                                        cancelRename(selectedItem);
62                                                        break;
63                                                default:
64                                                        break;
65                                        }
66                                        return false;
67                                });
68                        }
69
70                        var itemClick = function (i) {
71                                $(i).parent().find('.selector').removeClass('selected');
72                                $(i).parent().find('.selector').addClass('unselected');
73                                $(i).parent().find('.file-name').removeClass('selected');
74                                $(i).parent().find('.file-name').addClass('unselected');
75
76                                $(i).find('.file-name').removeClass('unselected');
77                                $(i).find('.file-name').addClass('selected');
78                                $(i).find('.selector').removeClass('unselected');
79                                $(i).find('.selector').addClass('selected');
80                        }
81
82                        var renderGrid = function (o) {
83                                $(o.container).find ('.vscell').remove();
84                               
85                                var childDir = o.directoryTreeData.DIRECTORIES;
86                                var childFile = o.directoryTreeData.FILES;
87                                var curentDirID = o.curentParent.substring(o.dirIDprefix.length, o.curentParent.length);
88                               
89                                for (var i = 0 ; i < childDir.length; i++) {
90                                        if (childDir[i].parentID != curentDirID) continue;
91                                        createNode ({
92                                                id: childDir[i].id,
93                                                name: childDir[i].name,
94                                                parentID: curentDirID,
95                                        });
96                                }
97
98                                for (var i = 0 ; i < childFile.length; i++) {
99                                        if (childFile[i].parentID != curentDirID) continue;
100                                        createNode ({
101                                                id: childFile[i].id,
102                                                name: childFile[i].name,
103                                                parentID: curentDirID,
104                                                minetype: childFile[i].minetype,
105                                        });
106                                }
107                        }
108                       
109                        var rename = function (e) {
110                                var nameObj = $(e).find('.file-name');
111                                var oldName = $(nameObj).text();
112                                var editor = document.createElement ('INPUT');
113                                editor.type = 'text';
114                                editor.className = 'rename';
115                                editor.value = oldName;
116                               
117                                $(nameObj).text('');
118                                $(nameObj).append(editor);
119                                editor.focus();
120                                $(editor).select();
121                        }
122                       
123                        var cancelRename = function (e) {
124                                var oldName = $(e).find('INPUT.rename').attr('value');
125                                $(e).find('INPUT.rename').remove();
126                                $(e).find('.file-name').text(oldName);
127                               
128                        }
129
130                        this.getData = function (data) {
131                                o.directoryTreeData = data.directoryTreeData;
132                                o.curentParent = data.curentParent;
133                                o.dirIDprefix = data.dirIDprefix
134                                renderGrid(o);
135                                keyboardRename();
136                        }
137
138                        this.initialize = function() {
139                        return this;
140                };
141
142                        return this.initialize();
143                }
144        });
145
146})(jQuery);
Note: See TracBrowser for help on using the repository browser.