1 | if(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 = ['rename','copy','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 | case 'delete':
|
---|
133 | break;
|
---|
134 | default:
|
---|
135 | break;
|
---|
136 | }
|
---|
137 | });
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (d.customEvent != null)
|
---|
141 | $(o.container).find('#' + o.dirIDprefix + d.id).find('a').bind(d.customEvent.eventName, function(e){d.customEvent.eventTrigger(this)});
|
---|
142 |
|
---|
143 | return $(o.container).find('#' + o.dirIDprefix + d.id);
|
---|
144 | }
|
---|
145 | //END - Create a node of Tree
|
---|
146 |
|
---|
147 | var deleteNode = function (parsedData) {
|
---|
148 | if (parsedData.isSuccess == false) return false;
|
---|
149 | if (!$('#' + o.dirIDprefix + parsedData.id).hasClass('home'))
|
---|
150 | $('#' + o.dirIDprefix + parsedData.id).parent().remove();
|
---|
151 | }
|
---|
152 |
|
---|
153 | var isHome = function (n) {
|
---|
154 | return $(n).parent().hasClass('home');
|
---|
155 | }
|
---|
156 |
|
---|
157 | var countNodeChild = function (n) {
|
---|
158 | var parent = $(n).parent();
|
---|
159 | return $(parent).find('UL').size();
|
---|
160 | }
|
---|
161 |
|
---|
162 | var openDir = function (o) {
|
---|
163 | if( $(o).parent().hasClass('collapsed') ) {
|
---|
164 | $(o).parent().find('> UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
|
---|
165 | $(o).parent().removeClass('collapsed').addClass('expanded');
|
---|
166 | }
|
---|
167 | else if( $(o).parent().hasClass('expanded') ) {
|
---|
168 | $(o).parent().find('> UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
|
---|
169 | $(o).parent().removeClass('expanded').addClass('collapsed');
|
---|
170 | }
|
---|
171 | selectDir(o);
|
---|
172 | }
|
---|
173 |
|
---|
174 | var selectDir = function (c) {
|
---|
175 | $('.currentDir').removeClass('currentDir');
|
---|
176 | $(c).addClass('currentDir');
|
---|
177 |
|
---|
178 | var cid = $(c).parent().attr('id');
|
---|
179 | var pid = cid.substring(o.dirIDprefix.length, cid.length);
|
---|
180 |
|
---|
181 | currentObj.id = pid;
|
---|
182 | currentObj.name = $(c).attr('rel');
|
---|
183 |
|
---|
184 | keyboardRename ();
|
---|
185 |
|
---|
186 | sendtoGrid();
|
---|
187 | }
|
---|
188 |
|
---|
189 | var sendtoGrid = function () {
|
---|
190 | if (o.grid == null) return false;
|
---|
191 |
|
---|
192 | o.grid.getData({
|
---|
193 | directoryTreeData: o.directoryTreeData,
|
---|
194 | curentParent:$(o.container).find('.currentDir').parent().attr('id'),
|
---|
195 | dirIDprefix: o.dirIDprefix
|
---|
196 | });
|
---|
197 | }
|
---|
198 |
|
---|
199 | var rename = function (o) {
|
---|
200 | var editor = document.createElement ('INPUT');
|
---|
201 | editor.type = 'text';
|
---|
202 | editor.className = 'rename';
|
---|
203 |
|
---|
204 | $(o).text('');
|
---|
205 | $(o).append(editor);
|
---|
206 |
|
---|
207 | $(editor).attr('value', currentObj.name);
|
---|
208 | editor.focus();
|
---|
209 | $(editor).select();
|
---|
210 |
|
---|
211 | $(editor).bind('focusout',function (e) {
|
---|
212 | cancelRename($(o));
|
---|
213 | });
|
---|
214 |
|
---|
215 | $(editor).bind('keypress',function (e) {
|
---|
216 | var keycode = (e.keyCode ? e.keyCode : e.which);
|
---|
217 | if(keycode == '13'){
|
---|
218 | completeRename($(o));
|
---|
219 | }
|
---|
220 | else {
|
---|
221 |
|
---|
222 | }
|
---|
223 | e.stopPropagation();
|
---|
224 | });
|
---|
225 | }
|
---|
226 |
|
---|
227 | var cancelRename = function (o) {
|
---|
228 | $(o).find('>INPUT').remove();
|
---|
229 | $(o).text(currentObj.name);
|
---|
230 | }
|
---|
231 |
|
---|
232 | var completeRename = function (o) {
|
---|
233 | var postdata = {id:currentObj.id,
|
---|
234 | objtype:'directory',
|
---|
235 | newname:$(o).find('INPUT.rename').val()}
|
---|
236 |
|
---|
237 | sendCommand({
|
---|
238 | script:'ajax/privatecontent/rename',
|
---|
239 | postdata:postdata,
|
---|
240 | callbackSuccess: function (parsedData) {
|
---|
241 | if (parsedData.RESULT == true) {
|
---|
242 | currentObj.name = parsedData.UPDATED.name;
|
---|
243 | $(o).find('INPUT.rename').remove();
|
---|
244 | $(o).text(currentObj.name);
|
---|
245 | }else return false;
|
---|
246 | }
|
---|
247 | });
|
---|
248 | }
|
---|
249 |
|
---|
250 | var keyboardRename = function () {
|
---|
251 | $(document).bind('keydown','keypress', function(e) {
|
---|
252 | var selectedItem = $('.currentDir');
|
---|
253 |
|
---|
254 | switch( e.keyCode ) {
|
---|
255 | case 113:
|
---|
256 | rename(selectedItem);
|
---|
257 | break;
|
---|
258 | case 27:
|
---|
259 | cancelRename(selectedItem);
|
---|
260 | break;
|
---|
261 | default:
|
---|
262 | break;
|
---|
263 | }
|
---|
264 | });
|
---|
265 | }
|
---|
266 |
|
---|
267 | loadTree();
|
---|
268 | // END - PRIVATE methods
|
---|
269 |
|
---|
270 |
|
---|
271 | this.expandAll = function() {
|
---|
272 | $(o.container).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
|
---|
273 | };
|
---|
274 |
|
---|
275 | this.getCurrentDirName = function() {
|
---|
276 | return $(o.container).find('.currentDir').attr('rel');
|
---|
277 | };
|
---|
278 |
|
---|
279 | this.getCurrentDir = function() {
|
---|
280 | return $(o.container).find('.currentDir');
|
---|
281 | };
|
---|
282 |
|
---|
283 | this.createDir = function(c) {
|
---|
284 | var cid = $(c).parent().attr('id');
|
---|
285 | var pid = cid.substring(o.dirIDprefix.length, cid.length);
|
---|
286 |
|
---|
287 | var dirName = prompt("Please enter new directory name", "");
|
---|
288 | var script = 'ajax/privatecontent/createdir';
|
---|
289 |
|
---|
290 | if (dirName != null && dirName.length != 0) {
|
---|
291 | sendCommand({
|
---|
292 | script: script,
|
---|
293 | postdata:{parentID:pid,name:dirName},
|
---|
294 | callbackSuccess: function (parsedData) {createNode({
|
---|
295 | id: parsedData.id,
|
---|
296 | name: parsedData.name,
|
---|
297 | curentNode: c,
|
---|
298 | hidden: false,
|
---|
299 | addToJSONData:true});
|
---|
300 | }
|
---|
301 | });
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | this.deleteDir = function (c) {
|
---|
306 | if (isHome(c)) return;
|
---|
307 | var childExisted = (countNodeChild(c) > 0) ? true:false;
|
---|
308 |
|
---|
309 | $.confirm ({
|
---|
310 | text: 'Bạn có muá»n xóa thư mục <span style="font-weight:bold">' + currentObj.name + "</span> khÃŽng?",
|
---|
311 | title: "Xác nháºn xóa!",
|
---|
312 | confirm: function(button) {
|
---|
313 | var cid = $(c).parent().attr('id');
|
---|
314 | var pid = cid.substring(o.dirIDprefix.length, cid.length);
|
---|
315 | var postdata = {id:pid,delallchild:true}
|
---|
316 | sendCommand({
|
---|
317 | script:'ajax/privatecontent/deletedir',
|
---|
318 | postdata:postdata,
|
---|
319 | callbackSuccess: function (parsedData) {deleteNode(parsedData)}
|
---|
320 | });
|
---|
321 | },
|
---|
322 | cancel: function(button) {
|
---|
323 | // do something
|
---|
324 | },
|
---|
325 | confirmButton: "Äá»ng Ü xóa",
|
---|
326 | cancelButton: "KhÃŽng",
|
---|
327 | post: false
|
---|
328 | });
|
---|
329 | }
|
---|
330 |
|
---|
331 | this.copy = function () {
|
---|
332 | alert ('copy');
|
---|
333 | }
|
---|
334 |
|
---|
335 | this.paste = function () {
|
---|
336 | alert ('paste');
|
---|
337 | }
|
---|
338 |
|
---|
339 | this.initialize = function() {
|
---|
340 | return this;
|
---|
341 | };
|
---|
342 |
|
---|
343 | this.getSelectedObj = function () {
|
---|
344 | return currentObj;
|
---|
345 | }
|
---|
346 |
|
---|
347 | return this.initialize();
|
---|
348 | }
|
---|
349 | });
|
---|
350 |
|
---|
351 | })(jQuery); |
---|