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

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