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

Last change on this file since 11 was 11, checked in by dungnv, 11 years ago
  • Property svn:mime-type set to text/plain
File size: 10.5 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                                sendCommand({
187                                        script: script,
188                                        postdata:{parentID:pid,name:dirName},
189                                        callbackSuccess: function (parsedData) {createNode({
190                                                                                id: parsedData.id,
191                                                                                name: parsedData.name,
192                                                                                curentNode: c,
193                                                                                hidden: false,
194                                                                                addToJSONData:true});
195                                                                        }
196                                });
197                        }
198
199                        this.deleteDir = function (c) {
200                                var childExisted = (countNodeChild(c) > 0) ? true:false;
201                                var confirmDel = false;
202                                var confirmChild = true;
203
204                                var cid = $(c).parent().attr('id');
205                                var pid = cid.substring(o.dirIDprefix.length, cid.length);
206
207                                if (isHome(c)) return;
208
209                                confirmDel = confirm('Are you sure you want to delete this Directory?');
210
211                                if (childExisted > 0 && confirmDel)
212                                        confirmChild = confirm('This node has childs, you still want to delete it?');
213
214                                if (confirmDel && confirmChild) {
215                                        var postdata = {id:pid,delallchild:true}
216                                        sendCommand({
217                                                script:'ajax/privatecontent/deletedir',
218                                                postdata:postdata,
219                                                callbackSuccess: function (parsedData) {deleteNode(parsedData)}
220                                        });
221                                }
222                        }
223
224                        this.copy = function () {
225                                alert ('copy');
226                        }
227
228                        this.paste = function () {
229                                alert ('paste');
230                        }
231
232                        this.initialize = function() {
233                        return this;
234                };
235
236                        return this.initialize();
237                },
238
239                violetGrid: function (o) {
240                        if( !o ) var o = {};
241                        if( o.container == undefined ) o.container = $(this);
242                        if( o.defaultViewMode == undefined ) o.defaultViewMode = 'thumbnail';//or 'list'
243                        if( o.directoryTreeData == undefined ) o.directoryTreeData = null;
244                        if( o.dirIDprefix == undefined ) o.dirIDprefix = null;
245
246                        var createNode = function (d) {
247                                if( !d ) var d = {};
248                                if( d.id == undefined ) d.id = null;
249                                if( d.name == undefined ) d.name = null;
250                                if( d.minetype == undefined ) d.minetype = 'directory';
251                                if( d.parentID == undefined ) d.parentID = null;
252                                if( d.clickEvent == undefined ) d.clickEvent = null;
253                                if( d.customEvent == undefined ) d.customEvent = null;
254
255                                var strHTML = '<div class="vscell" rel="id:' + d.id + '">';
256                                        strHTML += '<div class="selector unselected">';
257                                        strHTML +=      '<div class="icon-' + d.minetype + '"></div>';
258                                        strHTML += '</div>';
259                                        strHTML += '<div class="file-name unselected">' + d.name + '</div>';
260                                        strHTML += '</div>';
261
262                                $(o.container).append(strHTML);
263
264                                $('div[rel="id:'+ d.id +'"]').bind('click',function(e){itemClick(this)});
265                        }
266
267                        var itemClick = function (i) {
268                                $(i).parent().find('.selector').removeClass('selected');
269                                $(i).parent().find('.selector').addClass('unselected');
270                                $(i).parent().find('.file-name').removeClass('selected');
271                                $(i).parent().find('.file-name').addClass('unselected');
272
273                                $(i).find('.file-name').removeClass('unselected');
274                                $(i).find('.file-name').addClass('selected');
275                                $(i).find('.selector').removeClass('unselected');
276                                $(i).find('.selector').addClass('selected');
277                        }
278
279                        var renderGrid = function (o) {
280                                $(o.container).find ('.vscell').remove();
281                               
282                                var childDir = o.directoryTreeData.DIRECTORIES;
283                                var childFile = o.directoryTreeData.FILES;
284                                var curentDirID = o.curentParent.substring(o.dirIDprefix.length, o.curentParent.length);
285                               
286                                for (var i = 0 ; i < childDir.length; i++) {
287                                        if (childDir[i].parentID != curentDirID) continue;
288                                        createNode ({
289                                                id: childDir[i].id,
290                                                name: childDir[i].name,
291                                                parentID: curentDirID,
292                                        });
293                                }
294
295                                for (var i = 0 ; i < childFile.length; i++) {
296                                        if (childFile[i].parentID != curentDirID) continue;
297                                        createNode ({
298                                                id: childFile[i].id,
299                                                name: childFile[i].name,
300                                                parentID: curentDirID,
301                                                minetype: childFile[i].minetype,
302                                        });
303                                }
304                        }
305
306                        this.getData = function (data) {
307                                o.directoryTreeData = data.directoryTreeData;
308                                o.curentParent = data.curentParent;
309                                o.dirIDprefix = data.dirIDprefix
310                                renderGrid(o);
311                        }
312
313                        this.initialize = function() {
314                        return this;
315                };
316
317                        return this.initialize();
318                }
319        });
320
321})(jQuery);
Note: See TracBrowser for help on using the repository browser.