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