source: pro-bachkim-filespace/sourcecode/assets/js/vstree.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                violetTree: function(o) {
5                        if( !o ) var o = {};
6                        if( o.user == undefined ) o.user = null;
7                        if( o.homeDirNameDisplay == undefined ) o.homeDirNameDisplay = 'Home';
8                        if( o.host == undefined ) o.host = 'http://localhost/';
9                        if( o.script == undefined ) o.script = 'ajax/privatecontent/getcontent';
10                        if( o.container == undefined ) o.container = null;
11                        if( o.dirIDprefix == undefined ) o.dirIDprefix = 'vsdir_';
12
13                        if( o.expandSpeed == undefined ) o.expandSpeed= 500;
14                        if( o.collapseSpeed == undefined ) o.collapseSpeed= 500;
15                        if( o.expandEasing == undefined ) o.expandEasing = null;
16                        if( o.collapseEasing == undefined ) o.collapseEasing = null;
17                        if( o.me == undefined ) o.me = this;
18
19                        if( o.directoryTreeData == undefined ) o.directoryTreeData = null;
20                        if( o.grid == undefined ) o.grid = null;
21                        if( o.contextmenuON == undefined ) o.contextmenuON = true;
22                       
23                        var currentObj = {};
24                        if ( currentObj.type == undefined ) currentObj.type = 'directory';
25                       
26                        // PRIVATE methods
27                        var sendCommand = function (p) {
28                                if( p.postdata == undefined ) p.postdata = null;
29                                if( p.script == undefined ) p.script = o.script;
30                                if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
31                                if( p.callbackDone == undefined ) p.callbackDone = null;
32                                if( p.callbackFail == undefined ) p.callbackFail = null;
33                                if( p.callbackAlways == undefined ) p.callbackAlways = null;
34
35                                if (p.script != null) {
36                                        $.post(o.host + p.script, p.postdata, function (data){
37                                                if (data) {
38                                                        parseData = $.parseJSON(data);
39                                                }
40
41                                                if (p.callbackSuccess != null) {
42                                                        p.callbackSuccess(parseData);
43                                                }
44
45                                        }).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);});
46                                }
47                        }
48
49                        var loadTree = function () {
50                                sendCommand ({postdata:null,callbackSuccess:renderTree});
51                        };
52
53                        var renderTree = function  (parseData) {
54                                $(o.container).find ('.vstree').remove();
55                               
56                                o.directoryTreeData = parseData;                               
57                                var directoryData = parseData.DIRECTORIES;
58
59                                var homeNode = createNode({
60                                        id:0,
61                                        name:o.homeDirNameDisplay,
62                                });
63                               
64                                selectDir($(homeNode).find('> A'));
65                                if (directoryData != null) {
66                                        for (var i = 0; i < directoryData.length ; i++) {
67                                                var node = createNode ({
68                                                        id: directoryData[i].id,
69                                                        name: directoryData[i].name,
70                                                        currentNode: $(o.container).find('#' + o.dirIDprefix + directoryData[i].parentID).find('> A'),
71                                                        hidden: (directoryData[i].parentID > 0) ? true : false
72                                                })
73                                        };
74                                }
75                        };
76
77                        //Create a node of Tree
78                        var createNode = function (d) {
79                                if( !d ) var d = {};
80                                if( d.id == undefined ) d.id = null;
81                                if( d.name == undefined ) d.name = null;
82                                if( d.currentNode == undefined ) d.currentNode = null;
83                                if( d.hidden == undefined ) d.hidden = true;
84                                if( d.clickEvent == undefined ) d.clickEvent = openDir;
85                                if( d.customEvent == undefined ) d.customEvent = null; //customEvent.eventName, customEvent.eventTrigger
86                                if( d.addToJSONData == undefined ) d.addToJSONData = false;
87                               
88                                var disabledItemsList = null;
89                                if (d.name == o.homeDirNameDisplay)
90                                        disabledItemsList = ['rename','copy','cut','delete'];
91                               
92                                if (d.currentNode != null) {
93                                        var strHTML = '<ul class="vstree"><li id="' + o.dirIDprefix + d.id + '" class="directory collapsed"><a href="#" rel="' + d.name + '">' + d.name + '</a></li></ul>';
94
95                                        $(d.currentNode).parent().append(strHTML);
96                                        if (d.hidden == true)
97                                                $(o.container).find('#' + o.dirIDprefix + d.id).parent().css('display','none');
98                                       
99                                }else if (d.id == 0){
100                                        var strHTML = '<ul class="vstree"><li id="' + o.dirIDprefix + d.id + '" class="home"><a href="#" rel="' + d.name + '">' + d.name + '</a></li></ul>';
101                                        $(o.container).append(strHTML);
102                                }
103
104                                //bind new node to data
105                                if (d.addToJSONData == true) {
106                                        //o.directoryTreeData.DIRECTORIES.length
107                                        var newdir = {};
108                                        newdir.id = d.id;
109                                        newdir.name = d.name;
110                                       
111                                        console.log(d.currentNode);
112                                       
113                                        newdir.parentID = $(o.container).find(d.currentNode).parent().attr('id').substring(o.dirIDprefix.length, $(d.currentNode).parent().attr('id').length);
114                                        o.directoryTreeData.DIRECTORIES.push(newdir);
115                                        sendtoGrid();
116                                }
117                               
118                                //bind event on new node
119                                $(o.container).find('#' + o.dirIDprefix + d.id).find('a').bind("click", function(e){d.clickEvent(this);return false;});
120                               
121                                if (o.contextmenuON) {
122                                        $(o.container).find('#' + o.dirIDprefix + d.id).find('a').contextMenu({
123                                                menu: 'treeMenu',
124                                                disabledItems: disabledItemsList
125                                        }, function(action, el, pos) {
126                                                selectDir(el);
127                                                switch(action) {
128                                                        case 'rename':
129                                                                rename(el);
130                                                                break;
131                                                        case 'newfolder':
132                                                        case 'share':
133                                                        case 'copy':
134                                                        case 'cut':
135                                                                o.grid.showModal(currentObj, action);
136                                                                break;
137                                                        case 'delete':
138                                                                break;
139                                                        default:
140                                                                break;
141                                                }
142                                        });
143                                }
144                               
145                                if (d.customEvent != null)
146                                        $(o.container).find('#' + o.dirIDprefix + d.id).find('a').bind(d.customEvent.eventName, function(e){d.customEvent.eventTrigger(this)});
147
148                                return $(o.container).find('#' + o.dirIDprefix + d.id);
149                        }
150                        //END - Create a node of Tree
151                       
152                        var deleteNode = function (parsedData) {
153                                if (parsedData.isSuccess == false) return false;
154                                if (!$('#' + o.dirIDprefix + parsedData.id).hasClass('home'))
155                                        $('#' + o.dirIDprefix + parsedData.id).parent().remove();
156                        }
157
158                        var isHome = function (n) {
159                                return $(n).parent().hasClass('home');
160                        }
161
162                        var countNodeChild = function (n) {
163                                var parent = $(n).parent();
164                                return $(parent).find('UL').size();
165                        }
166
167                        var openDir = function (o) {
168                                if( $(o).parent().hasClass('collapsed') ) {
169                                        $(o).parent().find('> UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
170                                        $(o).parent().removeClass('collapsed').addClass('expanded');
171                                }
172                                else if( $(o).parent().hasClass('expanded') ) {
173                                        $(o).parent().find('> UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
174                                        $(o).parent().removeClass('expanded').addClass('collapsed');
175                                }
176                                selectDir(o);
177                        }
178
179                        var selectDir = function (c) {
180                                $('.currentDir').removeClass('currentDir');
181                                $(c).addClass('currentDir');
182                               
183                                var cid = $(c).parent().attr('id');
184                                var pid = cid.substring(o.dirIDprefix.length, cid.length);
185                               
186                                currentObj.id = pid;
187                                currentObj.name = $(c).attr('rel');
188                               
189                                keyboardRename ();
190                               
191                                sendtoGrid();
192                        }
193
194                        var sendtoGrid = function () {
195                                if (o.grid == null) return false;
196                               
197                                o.grid.setTree(o.me);
198                                o.grid.setData({
199                                                                directoryTreeData: o.directoryTreeData,
200                                                                curentParent:$(o.container).find('.currentDir').parent().attr('id'),
201                                                                dirIDprefix: o.dirIDprefix
202                                                        });
203                        }
204
205                        var rename = function (o) {
206                                var editor = document.createElement ('INPUT');
207                                editor.type = 'text';
208                                editor.className = 'rename';
209                               
210                                $(o).text('');
211                                $(o).append(editor);
212                               
213                                $(editor).attr('value', currentObj.name); 
214                                editor.focus();
215                                $(editor).select();
216                               
217                                $(editor).bind('focusout',function (e) {
218                                        cancelRename($(o));
219                                });
220                               
221                                $(editor).bind('keypress',function (e) {
222                                var keycode = (e.keyCode ? e.keyCode : e.which);
223                                if(keycode == '13'){
224                                        completeRename($(o));
225                                }
226                                else {
227                                       
228                                }
229                                e.stopPropagation();
230                                });
231                        }
232                       
233                        var cancelRename = function (o) {
234                                $(o).find('>INPUT').remove();
235                                $(o).text(currentObj.name);
236                        }
237                       
238                        var completeRename = function (o) {
239                                var postdata = {id:currentObj.id,
240                                                objtype:'directory',
241                                                newname:$(o).find('INPUT.rename').val()}
242                               
243                                sendCommand({
244                                        script:'ajax/privatecontent/rename',
245                                        postdata:postdata,
246                                        callbackSuccess: function (parsedData) {
247                                                if (parsedData.RESULT == true) {
248                                                        currentObj.name = parsedData.UPDATED.name;
249                                                        $(o).find('INPUT.rename').remove();
250                                                        $(o).text(currentObj.name);
251                                                }else return false;
252                                        }
253                                });
254                        }
255                       
256                        var keyboardRename = function () {
257                                $(document).bind('keydown','keypress', function(e) {
258                                        var selectedItem = $('.currentDir');
259                                       
260                                        switch( e.keyCode ) {
261                                                case 113:
262                                                        rename(selectedItem);
263                                                        break;
264                                                case 27:
265                                                        cancelRename(selectedItem);
266                                                        break;
267                                                default:
268                                                        break;
269                                        }
270                                });
271                        }
272                       
273                        loadTree();
274                        // END - PRIVATE methods
275
276
277                        this.expandAll = function() {
278                                $(o.container).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
279                        };
280
281                        this.getCurrentDirName = function() {
282                                return $(o.container).find('.currentDir').attr('rel');
283                        };
284
285                        this.getCurrentDir = function() {
286                                return $(o.container).find('.currentDir');
287                        };
288
289                        this.createDir = function(data) {
290                                var cNode = $('#' + o.dirIDprefix + data.parentID).find('> A');
291                               
292                                createNode({
293                                        id: data.id,
294                                        name: data.name,
295                                        currentNode: cNode,
296                                        hidden: false,
297                                        addToJSONData:true});
298                               
299                                //console.log($(o.container).find(cNode).parent().attr('id').substring(o.dirIDprefix.length, $(cNode).parent().attr('id').length));
300                        }
301
302                        this.deleteDir = function (c) {
303                                if (isHome(c)) return;
304                                var childExisted = (countNodeChild(c) > 0) ? true:false;
305                               
306                                $.confirm ({
307                                    text: 'Bạn có muốn xóa thư mục <span style="font-weight:bold">' + currentObj.name + "</span> khÃŽng?",
308                                    title: "Xác nhận xóa!",
309                                    confirm: function(button) {
310                                                var cid = $(c).parent().attr('id');
311                                                var pid = cid.substring(o.dirIDprefix.length, cid.length);
312                                                var postdata = {id:pid,delallchild:true}
313                                                sendCommand({
314                                                        script:'ajax/privatecontent/deletedir',
315                                                        postdata:postdata,
316                                                        callbackSuccess: function (parsedData) {deleteNode(parsedData)}
317                                                });
318                                    },
319                                    cancel: function(button) {
320                                        // do something
321                                    },
322                                    confirmButton: "Đồng Ü xóa",
323                                    cancelButton: "KhÃŽng",
324                                    post: false
325                                });
326                        }
327                       
328                        this.upload = function () {
329                                $('#box-upload').modal('show');
330                               
331                                $("#fileuploader").uploadFile({
332                                        url: o.host + 'ajax/privatecontent/upload',
333                                        fileName:"myfile",
334                                        multiple:true,
335                                        maxFileCount: 5,
336                                        dragDropStr: "<span><b>Kéo thả các file vào đây!</b></span>",
337                                        uploadErrorStr:"Tải lên có lỗi!"
338                                });
339                               
340                                $('#box-upload').on('hide.bs.modal', function () {
341                                        $('#box-upload').find('.modal-body>.row>.col-xs-12>').remove();
342                                        $('#box-upload').find('.modal-body>.row>.col-xs-12').append('<div id="fileuploader">Chọn file:</div>');
343                                });
344                        }
345
346                        this.copy = function () {
347                               
348                        }
349
350                        this.paste = function () {
351                               
352                        }
353
354                        this.initialize = function() {
355                        return this;
356                };
357               
358                this.getSelectedObj = function () {
359                        return currentObj;
360                }
361
362                        return this.initialize();
363                }
364        });
365
366})(jQuery);
Note: See TracBrowser for help on using the repository browser.