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

Last change on this file since 16 was 16, checked in by dungnv, 11 years ago
  • Property svn:mime-type set to text/plain
File size: 7.6 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                        if( o.host == undefined ) o.host = 'http://localhost/';
11                       
12                        if( o.sharemodal == undefined ) o.sharemodal = $("#box-shareto");
13                        if( o.copymodal == undefined ) o.copymodal = $("#box-copyto");
14                        if( o.movemodal == undefined ) o.movemodal = $("#box-moveto");
15                       
16                        var currentObj = {};
17                       
18                        var createNode = function (d) {
19                                if( !d ) var d = {};
20                                if( d.id == undefined ) d.id = null;
21                                if( d.name == undefined ) d.name = null;
22                                if( d.minetype == undefined ) d.minetype = 'directory';
23                                if( d.parentID == undefined ) d.parentID = null;
24                                if( d.clickEvent == undefined ) d.clickEvent = null;
25                                if( d.customEvent == undefined ) d.customEvent = null;
26
27                                var strHTML = '<div class="vscell" rel="id:' + d.id + '">';
28                                        strHTML += '<div class="selector unselected">';
29                                        strHTML +=      '<div class="icon-' + d.minetype + '"></div>';
30                                        strHTML += '</div>';
31                                        strHTML += '<div class="file-name unselected">' + d.name + '</div>';
32                                        strHTML += '</div>';
33                               
34                                var disabledItemsList = null;
35                               
36                                if (d.minetype == 'directory') {
37                                        disabledItemsList = ['preview'];
38                                }
39                               
40                                $(o.container).append(strHTML);
41
42                                $('div[rel="id:'+ d.id +'"]').bind('click',function(e){itemClick(this)});
43                               
44                                $('div[rel="id:'+ d.id +'"]').contextMenu({
45                                        menu: 'gridMenu',
46                                        disabledItems: disabledItemsList
47                                }, function(action, el, pos) {
48                                        itemClick(el);
49                                        switch(action) {
50                                                case 'rename':
51                                                        rename(el);
52                                                        break;
53                                                case 'share':
54                                                        showShareModal(currentObj);
55                                                        break;
56                                                case 'copy':
57                                                        showCopyModal();
58                                                        break;
59                                                case 'cut':
60                                                        showMoveModal();
61                                                        break;
62                                                default:
63                                                        break;
64                                        }
65                                       
66                                });
67                        }
68                       
69                        var keyboardRename = function () {
70                                $(document).bind('keydown','keypress', function(e) {
71                                        var selectedItem = $('.selector.selected').parent();
72                                       
73                                        switch( e.keyCode ) {
74                                                case 113:
75                                                        rename(selectedItem);
76                                                        break;
77                                                case 27:
78                                                        cancelRename(selectedItem);
79                                                        break;
80                                                default:
81                                                        break;
82                                        }
83                                });
84                        }
85                       
86                        var showShareModal = function (obj) {
87                                if (obj.type == undefined || obj.type == null) obj.type = 'directory';
88                                $(o.sharemodal).modal('show');
89                                $(o.sharemodal).find('INPUT#txtSelectedObj').val(obj.name);
90                                $(o.sharemodal).find('.modal-header').text('Chia sẻ ' + (obj.type == 'directory'?'thư mục':'file') + ' [' + obj.name + ']');
91                        }
92                       
93                        var showCopyModal = function () {
94                                $(o.copymodal).modal('show');
95                                /*$(o.copymodal).find('INPUT#txtSelectedObj').val(currentObj.name);
96                                $(o.copymodal).find('.modal-header').text('Chia sẻ ' + (currentObj.type == 'directory'?'thư mục':'file') + ' [' + currentObj.name + ']');*/
97                        }
98                       
99                        var showMoveModal = function () {
100                                $(o.movemodal).modal('show');
101                        }
102
103                        var itemClick = function (i) {
104                                $(i).parent().find('.selector').removeClass('selected');
105                                $(i).parent().find('.selector').addClass('unselected');
106                                $(i).parent().find('.file-name').removeClass('selected');
107                                $(i).parent().find('.file-name').addClass('unselected');
108
109                                $(i).find('.file-name').removeClass('unselected');
110                                $(i).find('.file-name').addClass('selected');
111                                $(i).find('.selector').removeClass('unselected');
112                                $(i).find('.selector').addClass('selected');
113                               
114                                if ($(i).find('.file-name').text() != '')
115                                        currentObj.name = $(i).find('.file-name').text();
116                               
117                                currentObj.id = $(i).attr('rel').substring(3);
118                                currentObj.type = $(i).find('>div>div').hasClass('icon-directory') ? 'directory':'file';
119                                console.log(currentObj.type);
120                        }
121
122                        var renderGrid = function (o) {
123                                $(o.container).find ('.vscell').remove();
124                               
125                                var childDir = o.directoryTreeData.DIRECTORIES;
126                                var childFile = o.directoryTreeData.FILES;
127                                var curentDirID = o.curentParent.substring(o.dirIDprefix.length, o.curentParent.length);
128                               
129                                for (var i = 0 ; i < childDir.length; i++) {
130                                        if (childDir[i].parentID != curentDirID) continue;
131                                        createNode ({
132                                                id: childDir[i].id,
133                                                name: childDir[i].name,
134                                                parentID: curentDirID,
135                                        });
136                                }
137                               
138                                for (var i = 0 ; i < childFile.length; i++) {
139                                        if (childFile[i].parentID != curentDirID) continue;
140                                        createNode ({
141                                                id: childFile[i].id,
142                                                name: childFile[i].name,
143                                                parentID: curentDirID,
144                                                minetype: childFile[i].minetype,
145                                        });
146                                }
147                               
148                                keyboardRename();
149                        }
150                       
151                        var rename = function (e) {
152                                var nameObj = $(e).find('.file-name');
153                                var editor = document.createElement ('INPUT');
154                                editor.type = 'text';
155                                editor.className = 'rename';
156                               
157                                $(nameObj).text('');
158                                $(nameObj).append(editor);
159                               
160                                $(editor).attr('value', currentObj.name); 
161                                editor.focus();
162                                $(editor).select();
163                                $(editor).bind('focusout',function (e) {
164                                        cancelRename($(this).parent().parent());
165                                });
166                               
167                                $(editor).bind('keypress',function (e) {
168                                var keycode = (e.keyCode ? e.keyCode : e.which);
169                                if(keycode == '13'){
170                                        completeRename($(this).parent());
171                                }
172                                else {
173                                       
174                                }
175                                e.stopPropagation();
176                                });
177                        }
178                       
179                        var cancelRename = function (e) {
180                                $(e).find('INPUT.rename').remove();
181                                $(e).find('.file-name').text(currentObj.name);
182                        }
183                       
184                        var completeRename = function (e) {
185                               
186                                var postdata = {id:currentObj.id,
187                                                                objtype:currentObj.type,
188                                                                newname:$(e).find('INPUT.rename').val()}
189                                sendCommand({
190                                        script:'ajax/privatecontent/rename',
191                                        postdata:postdata,
192                                        callbackSuccess: function (parsedData) {
193                                                if (parsedData.RESULT == true) {
194                                                        currentObj.name = parsedData.UPDATED.name;
195                                                        $(e).find('INPUT.rename').remove();
196                                                        $(e).text(currentObj.name);
197                                                }else return false;
198                                        }
199                                });
200                        }
201                       
202                        var sendCommand = function (p) {
203                                if( p.postdata == undefined ) p.postdata = null;
204                                if( p.script == undefined ) p.script = o.script;
205                                if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
206                                if( p.callbackDone == undefined ) p.callbackDone = null;
207                                if( p.callbackFail == undefined ) p.callbackFail = null;
208                                if( p.callbackAlways == undefined ) p.callbackAlways = null;
209
210                                if (p.script != null) {
211                                        $.post(o.host + p.script, p.postdata, function (data){
212                                                if (data) {
213                                                        parseData = $.parseJSON(data);
214                                                }
215
216                                                if (p.callbackSuccess != null) {
217                                                        p.callbackSuccess(parseData);
218                                                }
219
220                                        }).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);});
221                                }
222                        }
223
224                        this.getData = function (data) {
225                                o.directoryTreeData = data.directoryTreeData;
226                                o.curentParent = data.curentParent;
227                                o.dirIDprefix = data.dirIDprefix
228                                renderGrid(o);
229                        }
230                       
231                        this.showModal = function (act, obj) {
232                                switch( act ) {
233                                        case 'copy':
234                                                break;
235                                        case 'move':
236                                                break;
237                                        case 'share':
238                                                showShareModal(obj);
239                                                break;
240                                        default:
241                                                break;
242                                }
243                        }
244
245                        this.initialize = function() {
246                        return this;
247                };
248
249                        return this.initialize();
250                }
251        });
252
253})(jQuery);
Note: See TracBrowser for help on using the repository browser.