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

Last change on this file since 17 was 17, checked in by dungnv, 11 years ago
  • Property svn:mime-type set to text/plain
File size: 9.1 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                                                case 'cut':
58                                                        showCopyModal(currentObj, action);
59                                                        break;
60                                                default:
61                                                        break;
62                                        }
63                                       
64                                });
65                        }
66                       
67                        var keyboardRename = function () {
68                                $(document).bind('keydown','keypress', function(e) {
69                                        var selectedItem = $('.selector.selected').parent();
70                                       
71                                        switch( e.keyCode ) {
72                                                case 113:
73                                                        rename(selectedItem);
74                                                        break;
75                                                case 27:
76                                                        cancelRename(selectedItem);
77                                                        break;
78                                                default:
79                                                        break;
80                                        }
81                                });
82                        }
83                       
84                        var showShareModal = function (obj) {
85                                if (obj.type == undefined || obj.type == null) obj.type = 'directory';
86                                $(o.sharemodal).modal('show');
87                                $(o.sharemodal).find('INPUT#txtSelectedObj').val(obj.name);
88                                $(o.sharemodal).find('.modal-header').text('Chia sẻ ' + (obj.type == 'directory'?'thư mục':'file') + ' [' + obj.name + ']');
89                        }
90                       
91                        var showCopyModal = function (obj, action) {
92                                var modalTitle = action == 'copy' ? 'Sao chép ' + (obj.type == 'directory'?'thư mục':'file') + ' [' + obj.name + ']':'Di chuyển ' + (obj.type == 'directory'?'thư mục':'file') + ' [' + obj.name + ']'
93                                var submitTitle = action == 'copy' ? 'Sao chép' : 'Di chuyển';
94                                var submitIcon = action == 'copy' ? '<i class="icon-copy"></i>' : '<i class="icon-cut"></i>';
95                                               
96                                //btn-primary
97                                $(o.copymodal).modal('show');
98                                $(o.copymodal).find('INPUT#txtSelectedObj').val(obj.name);
99                                $(o.copymodal).find('.modal-header').text(modalTitle);
100                                $(o.copymodal).find('.btn-primary').empty();
101                                $(o.copymodal).find('.btn-primary').append(submitIcon + "\n" + submitTitle);
102                               
103                                var selectTree = $(o.copymodal).find('#select-destination-tree').violetTree({
104                                        container: $('#select-destination-tree'),
105                                        expandEasing: 'easeOutBounce',
106                                        collapseEasing: 'easeOutBounce',
107                                        homeDirNameDisplay: "Thư mục gốc",
108                                        contextmenuON: false
109                                });
110                               
111                                $(o.copymodal).find('.btn-primary').button().click(function() {excuteCopy(obj, selectTree, action)})
112                               
113                                $(o.copymodal).on('hide.bs.modal', function () {
114                                        $(o.copymodal).find('.btn-primary').unbind('click');
115                                });
116                        }
117                       
118                        var excuteCopy = function (obj, tree, action) {
119                                var destObj = tree.getSelectedObj();
120                                //alert(action + ' ' + obj.type + ' ' + obj.name + ' to ' + destObj.type + ' ' + destObj.name);
121                               
122                                //sendCommand
123                                var postdata = {sourceid:obj.id,
124                                                sourcetype:obj.type,
125                                                destid: destObj.id,
126                                                desttype: destObj.type,
127                                                flag:action}
128                               
129                                sendCommand({
130                                        script:'ajax/privatecontent/copy',
131                                        postdata:postdata,
132                                        callbackSuccess: function (parsedData) {
133                                                if (parsedData.RESULT == true) {
134                                                        //please add code here
135                                                }else return false;
136                                        }
137                                });
138                        }
139
140                        var itemClick = function (i) {
141                                $(i).parent().find('.selector').removeClass('selected');
142                                $(i).parent().find('.selector').addClass('unselected');
143                                $(i).parent().find('.file-name').removeClass('selected');
144                                $(i).parent().find('.file-name').addClass('unselected');
145
146                                $(i).find('.file-name').removeClass('unselected');
147                                $(i).find('.file-name').addClass('selected');
148                                $(i).find('.selector').removeClass('unselected');
149                                $(i).find('.selector').addClass('selected');
150                               
151                                if ($(i).find('.file-name').text() != '')
152                                        currentObj.name = $(i).find('.file-name').text();
153                               
154                                currentObj.id = $(i).attr('rel').substring(3);
155                                currentObj.type = $(i).find('>div>div').hasClass('icon-directory') ? 'directory':'file';
156                        }
157
158                        var renderGrid = function (o) {
159                                $(o.container).find ('.vscell').remove();
160                               
161                                var childDir = o.directoryTreeData.DIRECTORIES;
162                                var childFile = o.directoryTreeData.FILES;
163                                var curentDirID = o.curentParent.substring(o.dirIDprefix.length, o.curentParent.length);
164                               
165                                for (var i = 0 ; i < childDir.length; i++) {
166                                        if (childDir[i].parentID != curentDirID) continue;
167                                        createNode ({
168                                                id: childDir[i].id,
169                                                name: childDir[i].name,
170                                                parentID: curentDirID,
171                                        });
172                                }
173                               
174                                for (var i = 0 ; i < childFile.length; i++) {
175                                        if (childFile[i].parentID != curentDirID) continue;
176                                        createNode ({
177                                                id: childFile[i].id,
178                                                name: childFile[i].name,
179                                                parentID: curentDirID,
180                                                minetype: childFile[i].minetype,
181                                        });
182                                }
183                               
184                                keyboardRename();
185                        }
186                       
187                        var rename = function (e) {
188                                var nameObj = $(e).find('.file-name');
189                                var editor = document.createElement ('INPUT');
190                                editor.type = 'text';
191                                editor.className = 'rename';
192                               
193                                $(nameObj).text('');
194                                $(nameObj).append(editor);
195                               
196                                $(editor).attr('value', currentObj.name); 
197                                editor.focus();
198                                $(editor).select();
199                                $(editor).bind('focusout',function (e) {
200                                        cancelRename($(this).parent().parent());
201                                });
202                               
203                                $(editor).bind('keypress',function (e) {
204                                var keycode = (e.keyCode ? e.keyCode : e.which);
205                                if(keycode == '13'){
206                                        completeRename($(this).parent());
207                                }
208                                else {
209                                       
210                                }
211                                e.stopPropagation();
212                                });
213                        }
214                       
215                        var cancelRename = function (e) {
216                                $(e).find('INPUT.rename').remove();
217                                $(e).find('.file-name').text(currentObj.name);
218                        }
219                       
220                        var completeRename = function (e) {
221                               
222                                var postdata = {id:currentObj.id,
223                                                                objtype:currentObj.type,
224                                                                newname:$(e).find('INPUT.rename').val()}
225                                sendCommand({
226                                        script:'ajax/privatecontent/rename',
227                                        postdata:postdata,
228                                        callbackSuccess: function (parsedData) {
229                                                if (parsedData.RESULT == true) {
230                                                        currentObj.name = parsedData.UPDATED.name;
231                                                        $(e).find('INPUT.rename').remove();
232                                                        $(e).text(currentObj.name);
233                                                }else return false;
234                                        }
235                                });
236                        }
237                       
238                        var sendCommand = function (p) {
239                                if( p.postdata == undefined ) p.postdata = null;
240                                if( p.script == undefined ) p.script = o.script;
241                                if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
242                                if( p.callbackDone == undefined ) p.callbackDone = null;
243                                if( p.callbackFail == undefined ) p.callbackFail = null;
244                                if( p.callbackAlways == undefined ) p.callbackAlways = null;
245
246                                if (p.script != null) {
247                                        $.post(o.host + p.script, p.postdata, function (data){
248                                                if (data) {
249                                                        parseData = $.parseJSON(data);
250                                                }
251
252                                                if (p.callbackSuccess != null) {
253                                                        p.callbackSuccess(parseData);
254                                                }
255
256                                        }).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);});
257                                }
258                        }
259
260                        this.getData = function (data) {
261                                o.directoryTreeData = data.directoryTreeData;
262                                o.curentParent = data.curentParent;
263                                o.dirIDprefix = data.dirIDprefix
264                                renderGrid(o);
265                        }
266                       
267                        this.showModal = function (obj, act) {
268                                switch( act ) {
269                                        case 'copy':
270                                        case 'cut':
271                                                showCopyModal(obj, act);
272                                                break;
273                                        case 'share':
274                                                showShareModal(obj);
275                                                break;
276                                        default:
277                                                break;
278                                }
279                        }
280
281                        this.initialize = function() {
282                        return this;
283                };
284
285                        return this.initialize();
286                }
287        });
288
289})(jQuery);
Note: See TracBrowser for help on using the repository browser.