source: pro-bachkim-filespace/sourcecode/assets/js/vsfilemanager.js @ 9

Last change on this file since 9 was 9, checked in by dungnv, 11 years ago
  • Property svn:mime-type set to text/plain
File size: 10.3 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.script == undefined ) o.script = null;
9                        if( o.container == undefined ) o.container = $(this);
10                        if( o.dirIDprefix == undefined ) o.dirIDprefix = 'vsdir_';
11
12                        if( o.expandSpeed == undefined ) o.expandSpeed= 500;
13                        if( o.collapseSpeed == undefined ) o.collapseSpeed= 500;
14                        if( o.expandEasing == undefined ) o.expandEasing = null;
15                        if( o.collapseEasing == undefined ) o.collapseEasing = null;
16
17                        if( o.directoryTreeData == undefined ) o.directoryTreeData = null;
18                        if( o.grid == undefined ) o.grid = null;
19
20                        // PRIVATE methods
21                        var sendCommand = function (p) {
22                                if( p.postdata == undefined ) p.postdata = null;
23                                if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
24                                if( p.callbackDone == undefined ) p.callbackDone = null;
25                                if( p.callbackFail == undefined ) p.callbackFail = null;
26                                if( p.callbackAlways == undefined ) p.callbackAlways = null;
27
28                                if (o.script != null) {
29                                        $.post(o.script, p.postdata, function (data){
30                                                if (data) {
31                                                        parseData = $.parseJSON(data);
32                                                }
33
34                                                if (p.callbackSuccess != null) {
35                                                        p.callbackSuccess(parseData);
36                                                }
37
38                                        }).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);});
39                                }
40                        }
41
42                        var loadTree = function () {
43                                sendCommand ({postdata:null,callbackSuccess:renderTree});
44                        };
45
46                        var renderTree = function  (parseData) {
47
48                                o.directoryTreeData = parseData;                               
49                                var directoryData = parseData.DIRECTORIES;
50
51                                var homeNode = createNode({
52                                        id:0,
53                                        name:o.homeDirNameDisplay,
54                                });
55
56                                selectDir($(homeNode).find('> A'));
57                                if (directoryData != null) {
58                                        for (var i = 0; i < directoryData.length ; i++) {
59                                                var node = createNode ({
60                                                        id: directoryData[i].id,
61                                                        name: directoryData[i].name,
62                                                        curentNode: $('#' + o.dirIDprefix + directoryData[i].parentID).find('> A'),
63                                                        hidden: (directoryData[i].parentID > 0) ? true : false
64                                                })
65                                        };
66                                }
67                        };
68
69                        //Create a node of Tree
70                        var createNode = function (d) {
71                                if( !d ) var d = {};
72                                if( d.id == undefined ) d.id = null;
73                                if( d.name == undefined ) d.name = null;
74                                if( d.curentNode == undefined ) d.curentNode = null;
75                                if( d.hidden == undefined ) d.hidden = true;
76                                if( d.clickEvent == undefined ) d.clickEvent = openDir;
77                                if( d.customEvent == undefined ) d.customEvent = null; //customEvent.eventName, customEvent.eventTrigger
78                                if( d.addToJSONData == undefined ) d.addToJSONData = false;
79                               
80                                if (d.curentNode != null) {
81                                        var strHTML = '<ul class="vstree"><li id="' + o.dirIDprefix + d.id + '" class="directory collapsed"><a href="#" rel="' + d.name + '">' + d.name + '</a></li></ul>';
82
83                                        $(d.curentNode).parent().append(strHTML);
84                                        if (d.hidden == true)
85                                                $('#' + o.dirIDprefix + d.id).parent().css('display','none');
86                                       
87                                }else if (d.id == 0){
88                                        var strHTML = '<ul class="vstree"><li id="' + o.dirIDprefix + d.id + '" class="home"><a href="#" rel="' + d.name + '">' + d.name + '</a></li></ul>';
89                                        $(o.container).append(strHTML);
90                                }
91
92                                //bind new node to data
93                                if (d.addToJSONData == true) {
94                                        //o.directoryTreeData.DIRECTORIES.length
95                                        var newdir = {};
96                                        newdir.id = d.id;
97                                        newdir.name = d.name;
98                                        newdir.parentID = $(d.curentNode).parent().attr('id').substring(o.dirIDprefix.length, $(d.curentNode).parent().attr('id').length);
99                                        o.directoryTreeData.DIRECTORIES.push(newdir);
100                                        sendtoGrid();
101                                }
102
103                                //bind event on new node
104                                $('#' + o.dirIDprefix + d.id).find('a').bind("click", function(e){d.clickEvent(this);return false;});
105                                $('#' + o.dirIDprefix + d.id).find('a').bind("contextmenu",function(e){
106                                e.preventDefault();
107                                showContextMenu (this);
108                                });
109
110                                if (d.customEvent != null)
111                                        $('#' + o.dirIDprefix + d.id).find('a').bind(d.customEvent.eventName, function(e){d.customEvent.eventTrigger(this)});
112
113                                return $('#' + o.dirIDprefix + d.id);
114                        }
115                        //END - Create a node of Tree
116
117                        var deleteNode = function (parsedData) {
118                                if (parsedData.isSuccess == false) return false;
119                                if (!$('#' + o.dirIDprefix + parsedData.id).hasClass('home'))
120                                        $('#' + o.dirIDprefix + parsedData.id).parent().remove();
121                        }
122
123                        var isHome = function (n) {
124                                return $(n).parent().hasClass('home');
125                        }
126
127                        var countNodeChild = function (n) {
128                                var parent = $(n).parent();
129                                return $(parent).find('UL').size();
130                        }
131
132                        var openDir = function (o) {
133                                if( $(o).parent().hasClass('collapsed') ) {
134                                        $(o).parent().find('> UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
135                                        $(o).parent().removeClass('collapsed').addClass('expanded');
136                                }
137                                else if( $(o).parent().hasClass('expanded') ) {
138                                        $(o).parent().find('> UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
139                                        $(o).parent().removeClass('expanded').addClass('collapsed');
140                                }
141                                selectDir(o);
142                        }
143
144                        var selectDir = function (c) {
145                                $('.currentDir').removeClass('currentDir');
146                                $(c).addClass('currentDir');
147                                sendtoGrid();
148                        }
149
150                        var showContextMenu = function  (n) {
151                                alert ('showContextMenu on ' + $(n).text());
152                        };
153
154                        var sendtoGrid = function () {
155                                o.grid.getData({
156                                                                directoryTreeData: o.directoryTreeData,
157                                                                curentParent:$(o.container).find('.currentDir').parent().attr('id'),
158                                                                dirIDprefix: o.dirIDprefix
159                                                        });
160                        }
161
162                        loadTree();
163                        // END - PRIVATE methods
164
165
166                        this.expandAll = function() {
167                                $(o.container).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
168                        };
169
170                        this.getCurrentDirName = function() {
171                                return $(o.container).find('.currentDir').attr('rel');
172                        };
173
174                        this.getCurrentDir = function() {
175                                return $(o.container).find('.currentDir');
176                        };
177
178                        this.createDir = function(c) {
179                                var cid = $(c).parent().attr('id');
180                                var pid = cid.substring(o.dirIDprefix.length, cid.length);
181
182                                var dirName = prompt("Please enter new directory name", "");
183
184                                sendCommand({
185                                        postdata:{action:"create_dir",parentID:pid,name:dirName},
186                                        callbackSuccess: function (parsedData) {createNode({
187                                                                                id: parsedData.id,
188                                                                                name: parsedData.name,
189                                                                                curentNode: c,
190                                                                                hidden: false,
191                                                                                addToJSONData:true});
192                                                                        }
193                                });
194                        }
195
196                        this.deleteDir = function (c) {
197                                var childExisted = (countNodeChild(c) > 0) ? true:false;
198                                var confirmDel = false;
199                                var confirmChild = true;
200
201                                var cid = $(c).parent().attr('id');
202                                var pid = cid.substring(o.dirIDprefix.length, cid.length);
203
204                                if (isHome(c)) return;
205
206                                confirmDel = confirm('Are you sure you want to delete this Directory?');
207
208                                if (childExisted > 0 && confirmDel)
209                                        confirmChild = confirm('This node has childs, you still want to delete it?');
210
211                                if (confirmDel && confirmChild) {
212                                        var postdata = {action:"delete_dir",id:pid}
213                                        sendCommand({
214                                                postdata:postdata,
215                                                callbackSuccess: function (parsedData) {deleteNode(parsedData)}
216                                        });
217                                }       
218                        }
219
220                        this.copy = function () {
221                                alert ('copy');
222                        }
223
224                        this.paste = function () {
225                                alert ('paste');
226                        }
227
228                        this.initialize = function() {
229                        return this;
230                };
231
232                        return this.initialize();
233                },
234
235                violetGrid: function (o) {
236                        if( !o ) var o = {};
237                        if( o.container == undefined ) o.container = $(this);
238                        if( o.defaultViewMode == undefined ) o.defaultViewMode = 'thumbnail';//or 'list'
239                        if( o.directoryTreeData == undefined ) o.directoryTreeData = null;
240                        if( o.dirIDprefix == undefined ) o.dirIDprefix = null;
241
242                        var createNode = function (d) {
243                                if( !d ) var d = {};
244                                if( d.id == undefined ) d.id = null;
245                                if( d.name == undefined ) d.name = null;
246                                if( d.minetype == undefined ) d.minetype = 'directory';
247                                if( d.parentID == undefined ) d.parentID = null;
248                                if( d.clickEvent == undefined ) d.clickEvent = null;
249                                if( d.customEvent == undefined ) d.customEvent = null;
250
251                                var strHTML = '<div class="vscell" rel="id:' + d.id + '">';
252                                        strHTML += '<div class="selector unselected">';
253                                        strHTML +=      '<div class="icon-' + d.minetype + '"></div>';
254                                        strHTML += '</div>';
255                                        strHTML += '<div class="file-name unselected">' + d.name + '</div>';
256                                        strHTML += '</div>';
257
258                                $(o.container).append(strHTML);
259
260                                $('div[rel="id:'+ d.id +'"]').bind('click',function(e){itemClick(this)});
261                        }
262
263                        var itemClick = function (i) {
264                                $(i).parent().find('.selector').removeClass('selected');
265                                $(i).parent().find('.selector').addClass('unselected');
266                                $(i).parent().find('.file-name').removeClass('selected');
267                                $(i).parent().find('.file-name').addClass('unselected');
268
269                                $(i).find('.file-name').removeClass('unselected');
270                                $(i).find('.file-name').addClass('selected');
271                                $(i).find('.selector').removeClass('unselected');
272                                $(i).find('.selector').addClass('selected');
273                        }
274
275                        var renderGrid = function (o) {
276                                $(o.container).find ('.vscell').remove();
277                               
278                                var childDir = o.directoryTreeData.DIRECTORIES;
279                                var childFile = o.directoryTreeData.FILES;
280                                var curentDirID = o.curentParent.substring(o.dirIDprefix.length, o.curentParent.length);
281                               
282                                for (var i = 0 ; i < childDir.length; i++) {
283                                        if (childDir[i].parentID != curentDirID) continue;
284                                        createNode ({
285                                                id: childDir[i].id,
286                                                name: childDir[i].name,
287                                                parentID: curentDirID,
288                                        });
289                                }
290
291                                for (var i = 0 ; i < childFile.length; i++) {
292                                        if (childFile[i].parentID != curentDirID) continue;
293                                        createNode ({
294                                                id: childFile[i].id,
295                                                name: childFile[i].name,
296                                                parentID: curentDirID,
297                                                minetype: childFile[i].minetype,
298                                        });
299                                }
300                        }
301
302                        this.getData = function (data) {
303                                o.directoryTreeData = data.directoryTreeData;
304                                o.curentParent = data.curentParent;
305                                o.dirIDprefix = data.dirIDprefix
306                                renderGrid(o);
307                        }
308
309                        this.initialize = function() {
310                        return this;
311                };
312
313                        return this.initialize();
314                }
315        });
316
317})(jQuery);
Note: See TracBrowser for help on using the repository browser.