source: pro-bachkim-filespace/sourcecode/assets/js/vstree.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: 10.0 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 = $(this);
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
18                        if( o.directoryTreeData == undefined ) o.directoryTreeData = null;
19                        if( o.grid == undefined ) o.grid = null;
20                       
21                        var currentObj = {};
22                       
23                        // PRIVATE methods
24                        var sendCommand = function (p) {
25                                if( p.postdata == undefined ) p.postdata = null;
26                                if( p.script == undefined ) p.script = o.script;
27                                if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
28                                if( p.callbackDone == undefined ) p.callbackDone = null;
29                                if( p.callbackFail == undefined ) p.callbackFail = null;
30                                if( p.callbackAlways == undefined ) p.callbackAlways = null;
31
32                                if (p.script != null) {
33                                        $.post(o.host + p.script, p.postdata, function (data){
34                                                if (data) {
35                                                        parseData = $.parseJSON(data);
36                                                }
37
38                                                if (p.callbackSuccess != null) {
39                                                        p.callbackSuccess(parseData);
40                                                }
41
42                                        }).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);});
43                                }
44                        }
45
46                        var loadTree = function () {
47                                sendCommand ({postdata:null,callbackSuccess:renderTree});
48                        };
49
50                        var renderTree = function  (parseData) {
51                                o.directoryTreeData = parseData;                               
52                                var directoryData = parseData.DIRECTORIES;
53
54                                var homeNode = createNode({
55                                        id:0,
56                                        name:o.homeDirNameDisplay,
57                                });
58
59                                selectDir($(homeNode).find('> A'));
60                                if (directoryData != null) {
61                                        for (var i = 0; i < directoryData.length ; i++) {
62                                                var node = createNode ({
63                                                        id: directoryData[i].id,
64                                                        name: directoryData[i].name,
65                                                        curentNode: $('#' + o.dirIDprefix + directoryData[i].parentID).find('> A'),
66                                                        hidden: (directoryData[i].parentID > 0) ? true : false
67                                                })
68                                        };
69                                }
70                        };
71
72                        //Create a node of Tree
73                        var createNode = function (d) {
74                                if( !d ) var d = {};
75                                if( d.id == undefined ) d.id = null;
76                                if( d.name == undefined ) d.name = null;
77                                if( d.curentNode == undefined ) d.curentNode = null;
78                                if( d.hidden == undefined ) d.hidden = true;
79                                if( d.clickEvent == undefined ) d.clickEvent = openDir;
80                                if( d.customEvent == undefined ) d.customEvent = null; //customEvent.eventName, customEvent.eventTrigger
81                                if( d.addToJSONData == undefined ) d.addToJSONData = false;
82                               
83                                var disabledItemsList = null;
84                                if (d.name == o.homeDirNameDisplay)
85                                        disabledItemsList = ['cut','delete'];
86                               
87                                if (d.curentNode != null) {
88                                        var strHTML = '<ul class="vstree"><li id="' + o.dirIDprefix + d.id + '" class="directory collapsed"><a href="#" rel="' + d.name + '">' + d.name + '</a></li></ul>';
89
90                                        $(d.curentNode).parent().append(strHTML);
91                                        if (d.hidden == true)
92                                                $('#' + o.dirIDprefix + d.id).parent().css('display','none');
93                                       
94                                }else if (d.id == 0){
95                                        var strHTML = '<ul class="vstree"><li id="' + o.dirIDprefix + d.id + '" class="home"><a href="#" rel="' + d.name + '">' + d.name + '</a></li></ul>';
96                                        $(o.container).append(strHTML);
97                                }
98
99                                //bind new node to data
100                                if (d.addToJSONData == true) {
101                                        //o.directoryTreeData.DIRECTORIES.length
102                                        var newdir = {};
103                                        newdir.id = d.id;
104                                        newdir.name = d.name;
105                                        newdir.parentID = $(d.curentNode).parent().attr('id').substring(o.dirIDprefix.length, $(d.curentNode).parent().attr('id').length);
106                                        o.directoryTreeData.DIRECTORIES.push(newdir);
107                                        sendtoGrid();
108                                }
109                               
110                                //bind event on new node
111                                $('#' + o.dirIDprefix + d.id).find('a').bind("click", function(e){d.clickEvent(this);return false;});
112                               
113                                $('#' + o.dirIDprefix + d.id).find('a').contextMenu({
114                                        menu: 'treeMenu',
115                                        disabledItems: disabledItemsList
116                                }, function(action, el, pos) {
117                                        selectDir(el);
118                                        switch(action) {
119                                                case 'rename':
120                                                        rename(el);
121                                                        break;
122                                                case 'share':
123                                                        o.grid.showModal(action, currentObj);
124                                                        break;
125                                                default:
126                                                        break;
127                                        }
128                                });
129                               
130                                if (d.customEvent != null)
131                                        $('#' + o.dirIDprefix + d.id).find('a').bind(d.customEvent.eventName, function(e){d.customEvent.eventTrigger(this)});
132
133                                return $('#' + o.dirIDprefix + d.id);
134                        }
135                        //END - Create a node of Tree
136
137                        var deleteNode = function (parsedData) {
138                                if (parsedData.isSuccess == false) return false;
139                                if (!$('#' + o.dirIDprefix + parsedData.id).hasClass('home'))
140                                        $('#' + o.dirIDprefix + parsedData.id).parent().remove();
141                        }
142
143                        var isHome = function (n) {
144                                return $(n).parent().hasClass('home');
145                        }
146
147                        var countNodeChild = function (n) {
148                                var parent = $(n).parent();
149                                return $(parent).find('UL').size();
150                        }
151
152                        var openDir = function (o) {
153                                if( $(o).parent().hasClass('collapsed') ) {
154                                        $(o).parent().find('> UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
155                                        $(o).parent().removeClass('collapsed').addClass('expanded');
156                                }
157                                else if( $(o).parent().hasClass('expanded') ) {
158                                        $(o).parent().find('> UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
159                                        $(o).parent().removeClass('expanded').addClass('collapsed');
160                                }
161                                selectDir(o);
162                        }
163
164                        var selectDir = function (c) {
165                                $('.currentDir').removeClass('currentDir');
166                                $(c).addClass('currentDir');
167                               
168                                var cid = $(c).parent().attr('id');
169                                var pid = cid.substring(o.dirIDprefix.length, cid.length);
170                               
171                                currentObj.id = pid;
172                                currentObj.name = $(c).attr('rel');
173                               
174                                keyboardRename ();
175                               
176                                sendtoGrid();
177                        }
178
179                        var sendtoGrid = function () {
180                                o.grid.getData({
181                                                                directoryTreeData: o.directoryTreeData,
182                                                                curentParent:$(o.container).find('.currentDir').parent().attr('id'),
183                                                                dirIDprefix: o.dirIDprefix
184                                                        });
185                        }
186
187                        var rename = function (o) {
188                                var editor = document.createElement ('INPUT');
189                                editor.type = 'text';
190                                editor.className = 'rename';
191                               
192                                $(o).text('');
193                                $(o).append(editor);
194                               
195                                $(editor).attr('value', currentObj.name); 
196                                editor.focus();
197                                $(editor).select();
198                               
199                                $(editor).bind('focusout',function (e) {
200                                        cancelRename($(o));
201                                });
202                               
203                                $(editor).bind('keypress',function (e) {
204                                var keycode = (e.keyCode ? e.keyCode : e.which);
205                                if(keycode == '13'){
206                                        completeRename($(o));
207                                }
208                                else {
209                                       
210                                }
211                                e.stopPropagation();
212                                });
213                        }
214                       
215                        var cancelRename = function (o) {
216                                $(o).find('>INPUT').remove();
217                                $(o).text(currentObj.name);
218                        }
219                       
220                        var completeRename = function (o) {
221                                var postdata = {id:currentObj.id,
222                                                objtype:'directory',
223                                                newname:$(o).find('INPUT.rename').val()}
224                               
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                                                        $(o).find('INPUT.rename').remove();
232                                                        $(o).text(currentObj.name);
233                                                }else return false;
234                                        }
235                                });
236                        }
237                       
238                        var keyboardRename = function () {
239                                $(document).bind('keydown','keypress', function(e) {
240                                        var selectedItem = $('.currentDir');
241                                       
242                                        switch( e.keyCode ) {
243                                                case 113:
244                                                        rename(selectedItem);
245                                                        break;
246                                                case 27:
247                                                        cancelRename(selectedItem);
248                                                        break;
249                                                default:
250                                                        break;
251                                        }
252                                });
253                        }
254                       
255                        loadTree();
256                        // END - PRIVATE methods
257
258
259                        this.expandAll = function() {
260                                $(o.container).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
261                        };
262
263                        this.getCurrentDirName = function() {
264                                return $(o.container).find('.currentDir').attr('rel');
265                        };
266
267                        this.getCurrentDir = function() {
268                                return $(o.container).find('.currentDir');
269                        };
270
271                        this.createDir = function(c) {
272                                var cid = $(c).parent().attr('id');
273                                var pid = cid.substring(o.dirIDprefix.length, cid.length);
274
275                                var dirName = prompt("Please enter new directory name", "");
276                                var script = 'ajax/privatecontent/createdir';
277
278                                if (dirName != null && dirName.length != 0) {
279                                        sendCommand({
280                                                script: script,
281                                                postdata:{parentID:pid,name:dirName},
282                                                callbackSuccess: function (parsedData) {createNode({
283                                                                                        id: parsedData.id,
284                                                                                        name: parsedData.name,
285                                                                                        curentNode: c,
286                                                                                        hidden: false,
287                                                                                        addToJSONData:true});
288                                                                                }
289                                        });
290                                }
291                        }
292
293                        this.deleteDir = function (c) {
294                                var childExisted = (countNodeChild(c) > 0) ? true:false;
295                                var confirmDel = false;
296                                var confirmChild = true;
297
298                                var cid = $(c).parent().attr('id');
299                                var pid = cid.substring(o.dirIDprefix.length, cid.length);
300
301                                if (isHome(c)) return;
302
303                                confirmDel = confirm('Are you sure you want to delete this Directory?');
304
305                                if (childExisted > 0 && confirmDel)
306                                        confirmChild = confirm('This node has childs, you still want to delete it?');
307
308                                if (confirmDel && confirmChild) {
309                                        var postdata = {id:pid,delallchild:true}
310                                        sendCommand({
311                                                script:'ajax/privatecontent/deletedir',
312                                                postdata:postdata,
313                                                callbackSuccess: function (parsedData) {deleteNode(parsedData)}
314                                        });
315                                }
316                        }
317
318                        this.copy = function () {
319                                alert ('copy');
320                        }
321
322                        this.paste = function () {
323                                alert ('paste');
324                        }
325
326                        this.initialize = function() {
327                        return this;
328                };
329
330                        return this.initialize();
331                }
332        });
333
334})(jQuery);
Note: See TracBrowser for help on using the repository browser.