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

Last change on this file since 42 was 42, checked in by dungnv, 11 years ago
  • Property svn:mime-type set to text/plain
File size: 11.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                        if( o.host == undefined ) o.host = 'http://localhost/';
11                        if( o.tree == undefined ) o.tree = null;
12                       
13                        if( o.sharemodal == undefined ) o.sharemodal = $("#box-shareto");
14                        if( o.copymodal == undefined ) o.copymodal = $("#box-copyto");
15                        if( o.movemodal == undefined ) o.movemodal = $("#box-moveto");
16                       
17                        var currentObj = {};
18                       
19                        var createNode = function (d) {
20                                if( !d ) var d = {};
21                                if( d.id == undefined ) d.id = null;
22                                if( d.name == undefined ) d.name = null;
23                                if( d.minetype == undefined ) d.minetype = 'directory';
24                                if( d.parentID == undefined ) d.parentID = null;
25                                if( d.clickEvent == undefined ) d.clickEvent = null;
26                                if( d.customEvent == undefined ) d.customEvent = null;
27
28                                var strHTML = '<div class="vscell" rel="id:' + d.id + '">';
29                                        strHTML += '<div class="selector unselected">';
30                                        strHTML +=      '<div class="icon-' + d.minetype + '"></div>';
31                                        strHTML += '</div>';
32                                        strHTML += '<div class="file-name unselected">' + d.name + '</div>';
33                                        strHTML += '</div>';
34                               
35                                var disabledItemsList = null;
36                               
37                                if (d.minetype == 'directory') {
38                                        disabledItemsList = ['preview'];
39                                }
40                               
41                                $(o.container).append(strHTML);
42
43                                $('div[rel="id:'+ d.id +'"]').bind('click',function(e){itemClick(this)});
44                               
45                                $('div[rel="id:'+ d.id +'"]').contextMenu({
46                                        menu: 'gridMenu',
47                                        disabledItems: disabledItemsList
48                                }, function(action, el, pos) {
49                                        itemClick(el);
50                                        switch(action) {
51                                                case 'newfolder':
52                                                        openNewFolderModal(currentObj);
53                                                        break;
54                                                case 'rename':
55                                                        rename(el);
56                                                        break;
57                                                case 'share':
58                                                        showShareModal(currentObj);
59                                                        break;
60                                                case 'copy':
61                                                case 'cut':
62                                                        showCopyModal(currentObj, action);
63                                                        break;
64                                                default:
65                                                        break;
66                                        }
67                                       
68                                });
69                        }
70                       
71                        var openNewFolderModal = function (c) {
72                                $('#box-newfolder').modal('show');
73                                $('#frm-newfolder').find('#f-parentid').val(c.id);
74                        }
75                       
76                        var bindCreateFolder = function () {
77                                $('#box-newfolder').find('#frm-newfolder').unbind('submit');
78                                $('#box-newfolder').find('#btn-submit-newfolder').unbind('click');
79                               
80                                $('#box-newfolder').find('#frm-newfolder').bind('submit',
81                                        function(e){
82                                                if ($('#frm-newfolder').find('#f-newfoldername').val() == '') {
83                                                        alert('Chưa nhập tên thư mục mới!');
84                                                }else {
85                                                        var postData = $('#box-newfolder').find('#frm-newfolder').serializeArray();
86                                                        var script = 'ajax/privatecontent/createdir';
87                                                        sendCommand({
88                                                                script: script,
89                                                                postdata:postData,
90                                                                callbackSuccess: function (parsedData) {
91                                                                                                        createNode({
92                                                                                                                id: parsedData.id,
93                                                                                                                name: parsedData.name,
94                                                                                                                curentNode: currentObj,
95                                                                                                                hidden: false,
96                                                                                                                addToJSONData:true});
97                                                                                                        o.tree.createDir(parsedData);
98                                                                                                }
99                                                        });
100                                                       
101                                                        $('#box-newfolder').modal('hide');
102                                                }
103                                               
104                                                e.preventDefault();
105                                });
106                               
107                                $('#box-newfolder').on('show.bs.modal', function () {
108                                        $('#box-newfolder').find('#frm-newfolder').get(0).reset();
109                                });
110                               
111                                $('#box-newfolder').find('#btn-submit-newfolder').bind('click',
112                                        function (){
113                                                $('#box-newfolder').find('#frm-newfolder').submit();
114                                });
115                        }
116                       
117                        var keyboardRename = function () {
118                                $(document).bind('keydown','keypress', function(e) {
119                                        var selectedItem = $('.selector.selected').parent();
120                                       
121                                        switch( e.keyCode ) {
122                                                case 113:
123                                                        rename(selectedItem);
124                                                        break;
125                                                case 27:
126                                                        cancelRename(selectedItem);
127                                                        break;
128                                                default:
129                                                        break;
130                                        }
131                                });
132                        }
133                       
134                        var showShareModal = function (obj) {
135                                if (obj.type == undefined || obj.type == null) obj.type = 'directory';
136                                $(o.sharemodal).modal('show');
137                                $(o.sharemodal).find('INPUT#txtSelectedObj').val(obj.name);
138                                $(o.sharemodal).find('.modal-header').text('Chia sẻ ' + (obj.type == 'directory'?'thư mục ':'file ') + obj.name);
139                        }
140                       
141                        var showCopyModal = function (obj, action) {
142                                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
143                                var submitTitle = action == 'copy' ? 'Sao chép' : 'Di chuyển';
144                                var submitIcon = action == 'copy' ? '<i class="icon-copy"></i>' : '<i class="icon-cut"></i>';
145                                               
146                                //btn-primary
147                                $(o.copymodal).modal('show');
148                                $(o.copymodal).find('INPUT#txtSelectedObj').val(obj.name);
149                                $(o.copymodal).find('.modal-header').text(modalTitle);
150                                $(o.copymodal).find('.btn-primary').empty();
151                                $(o.copymodal).find('.btn-primary').append(submitIcon + "\n" + submitTitle);
152                               
153                                var selectTree = $(o.copymodal).find('#select-destination-tree').violetTree({
154                                        container: $('#select-destination-tree'),
155                                        expandEasing: 'easeOutBounce',
156                                        collapseEasing: 'easeOutBounce',
157                                        homeDirNameDisplay: "Thư mục gốc",
158                                        contextmenuON: false
159                                });
160                               
161                                $(o.copymodal).find('.btn-primary').button().click(function() {excuteCopy(obj, selectTree, action)})
162                               
163                                $(o.copymodal).on('hide.bs.modal', function () {
164                                        $(o.copymodal).find('.btn-primary').unbind('click');
165                                });
166                        }
167                       
168                        var excuteCopy = function (obj, tree, action) {
169                                var destObj = tree.getSelectedObj();
170                               
171                                //sendCommand
172                                var postdata = {sourceid:obj.id,
173                                                sourcetype:obj.type,
174                                                destid: destObj.id,
175                                                desttype: destObj.type,
176                                                flag:action}
177                               
178                                sendCommand({
179                                        script:'ajax/privatecontent/copy',
180                                        postdata:postdata,
181                                        callbackSuccess: function (parsedData) {
182                                                if (parsedData.RESULT == true) {
183                                                        //please add code here
184                                                }else return false;
185                                        }
186                                });
187                        }
188
189                        var itemClick = function (i) {
190                                $(i).parent().find('.selector').removeClass('selected');
191                                $(i).parent().find('.selector').addClass('unselected');
192                                $(i).parent().find('.file-name').removeClass('selected');
193                                $(i).parent().find('.file-name').addClass('unselected');
194
195                                $(i).find('.file-name').removeClass('unselected');
196                                $(i).find('.file-name').addClass('selected');
197                                $(i).find('.selector').removeClass('unselected');
198                                $(i).find('.selector').addClass('selected');
199                               
200                                if ($(i).find('.file-name').text() != '')
201                                        currentObj.name = $(i).find('.file-name').text();
202                               
203                                currentObj.id = $(i).attr('rel').substring(3);
204                                currentObj.type = $(i).find('>div>div').hasClass('icon-directory') ? 'directory':'file';
205                                var item = searchItemByID(currentObj.id, currentObj.type);
206                                currentObj.parentID = item.parentID;
207                        }
208                       
209                        var searchItemByID = function (itemID, type) {
210                                var source = (type == 'directory') ? o.directoryTreeData.DIRECTORIES : (type == 'file') ? o.directoryTreeData.FILES : null;
211                                var item = null;
212                                for (var i = 0 ; i < source.length; i++) {
213                                        if (source[i].id == itemID) {
214                                                item = source[i];
215                                                break;
216                                        }
217                                }
218                                return item;
219                        }
220
221                        var renderGrid = function (o) {
222                                $(o.container).find ('.vscell').remove();
223                               
224                                var childDir = o.directoryTreeData.DIRECTORIES;
225                                var childFile = o.directoryTreeData.FILES;
226                                var curentDirID = o.curentParent.substring(o.dirIDprefix.length, o.curentParent.length);
227                               
228                                for (var i = 0 ; i < childDir.length; i++) {
229                                        if (childDir[i].parentID != curentDirID) continue;
230                                        createNode ({
231                                                id: childDir[i].id,
232                                                name: childDir[i].name,
233                                                parentID: curentDirID,
234                                        });
235                                }
236                               
237                                for (var i = 0 ; i < childFile.length; i++) {
238                                        if (childFile[i].parentID != curentDirID) continue;
239                                        createNode ({
240                                                id: childFile[i].id,
241                                                name: childFile[i].name,
242                                                parentID: curentDirID,
243                                                minetype: childFile[i].minetype,
244                                        });
245                                }
246                               
247                                keyboardRename();
248                                bindCreateFolder();
249                        }
250                       
251                        var rename = function (e) {
252                                var nameObj = $(e).find('.file-name');
253                                var editor = document.createElement ('INPUT');
254                                editor.type = 'text';
255                                editor.className = 'rename';
256                               
257                                $(nameObj).text('');
258                                $(nameObj).append(editor);
259                               
260                                $(editor).attr('value', currentObj.name); 
261                                editor.focus();
262                                $(editor).select();
263                                $(editor).bind('focusout',function (e) {
264                                        cancelRename($(this).parent().parent());
265                                });
266                               
267                                $(editor).bind('keypress',function (e) {
268                                var keycode = (e.keyCode ? e.keyCode : e.which);
269                                if(keycode == '13'){
270                                        completeRename($(this).parent());
271                                }
272                                else {
273                                       
274                                }
275                                e.stopPropagation();
276                                });
277                        }
278                       
279                        var cancelRename = function (e) {
280                                $(e).find('INPUT.rename').remove();
281                                $(e).find('.file-name').text(currentObj.name);
282                        }
283                       
284                        var completeRename = function (e) {
285                               
286                                var postdata = {id:currentObj.id,
287                                                                objtype:currentObj.type,
288                                                                newname:$(e).find('INPUT.rename').val()}
289                                sendCommand({
290                                        script:'ajax/privatecontent/rename',
291                                        postdata:postdata,
292                                        callbackSuccess: function (parsedData) {
293                                                if (parsedData.RESULT == true) {
294                                                        currentObj.name = parsedData.UPDATED.name;
295                                                        $(e).find('INPUT.rename').remove();
296                                                        $(e).text(currentObj.name);
297                                                }else return false;
298                                        }
299                                });
300                        }
301                       
302                        var sendCommand = function (p) {
303                                if( p.postdata == undefined ) p.postdata = null;
304                                if( p.script == undefined ) p.script = o.script;
305                                if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
306                                if( p.callbackDone == undefined ) p.callbackDone = null;
307                                if( p.callbackFail == undefined ) p.callbackFail = null;
308                                if( p.callbackAlways == undefined ) p.callbackAlways = null;
309
310                                if (p.script != null) {
311                                        $.post(o.host + p.script, p.postdata, function (data){
312                                                if (data) {
313                                                        parseData = $.parseJSON(data);
314                                                }
315
316                                                if (p.callbackSuccess != null) {
317                                                        p.callbackSuccess(parseData);
318                                                }
319                                        }).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);});
320                                }
321                        }
322
323                        this.setData = function (data) {
324                                o.directoryTreeData = data.directoryTreeData;
325                                o.curentParent = data.curentParent;
326                                o.dirIDprefix = data.dirIDprefix;
327                                renderGrid(o);
328                        }
329                       
330                        this.setTree = function (treeObj) {
331                                o.tree = treeObj;
332                        }
333                       
334                        this.showModal = function (obj, act) {
335                                switch( act ) {
336                                        case 'newfolder':
337                                                openNewFolderModal(obj);
338                                                break;
339                                        case 'copy':
340                                        case 'cut':
341                                                showCopyModal(obj, act);
342                                                break;
343                                        case 'share':
344                                                showShareModal(obj);
345                                                break;
346                                        default:
347                                                break;
348                                }
349                        }
350
351                        this.initialize = function() {
352                        return this;
353                };
354
355                        return this.initialize();
356                }
357        });
358
359})(jQuery);
Note: See TracBrowser for help on using the repository browser.