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 = $(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 |
|
---|
108 | $('#' + o.dirIDprefix + d.id).find('a').contextMenu({
|
---|
109 | menu: 'treeMenu'
|
---|
110 | }, function(action, el, pos) {
|
---|
111 | alert(
|
---|
112 | 'Action: ' + action + '\n\n' +
|
---|
113 | 'Element text: ' + $(el).text() + '\n\n' +
|
---|
114 | 'X: ' + pos.x + ' Y: ' + pos.y + ' (relative to element)\n\n' +
|
---|
115 | 'X: ' + pos.docX + ' Y: ' + pos.docY+ ' (relative to document)'
|
---|
116 | );
|
---|
117 | });
|
---|
118 |
|
---|
119 | if (d.customEvent != null)
|
---|
120 | $('#' + o.dirIDprefix + d.id).find('a').bind(d.customEvent.eventName, function(e){d.customEvent.eventTrigger(this)});
|
---|
121 |
|
---|
122 | return $('#' + o.dirIDprefix + d.id);
|
---|
123 | }
|
---|
124 | //END - Create a node of Tree
|
---|
125 |
|
---|
126 | var deleteNode = function (parsedData) {
|
---|
127 | if (parsedData.isSuccess == false) return false;
|
---|
128 | if (!$('#' + o.dirIDprefix + parsedData.id).hasClass('home'))
|
---|
129 | $('#' + o.dirIDprefix + parsedData.id).parent().remove();
|
---|
130 | }
|
---|
131 |
|
---|
132 | var isHome = function (n) {
|
---|
133 | return $(n).parent().hasClass('home');
|
---|
134 | }
|
---|
135 |
|
---|
136 | var countNodeChild = function (n) {
|
---|
137 | var parent = $(n).parent();
|
---|
138 | return $(parent).find('UL').size();
|
---|
139 | }
|
---|
140 |
|
---|
141 | var openDir = function (o) {
|
---|
142 | if( $(o).parent().hasClass('collapsed') ) {
|
---|
143 | $(o).parent().find('> UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
|
---|
144 | $(o).parent().removeClass('collapsed').addClass('expanded');
|
---|
145 | }
|
---|
146 | else if( $(o).parent().hasClass('expanded') ) {
|
---|
147 | $(o).parent().find('> UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
|
---|
148 | $(o).parent().removeClass('expanded').addClass('collapsed');
|
---|
149 | }
|
---|
150 | selectDir(o);
|
---|
151 | }
|
---|
152 |
|
---|
153 | var selectDir = function (c) {
|
---|
154 | $('.currentDir').removeClass('currentDir');
|
---|
155 | $(c).addClass('currentDir');
|
---|
156 | sendtoGrid();
|
---|
157 | }
|
---|
158 |
|
---|
159 | var sendtoGrid = function () {
|
---|
160 | o.grid.getData({
|
---|
161 | directoryTreeData: o.directoryTreeData,
|
---|
162 | curentParent:$(o.container).find('.currentDir').parent().attr('id'),
|
---|
163 | dirIDprefix: o.dirIDprefix
|
---|
164 | });
|
---|
165 | }
|
---|
166 |
|
---|
167 | loadTree();
|
---|
168 | // END - PRIVATE methods
|
---|
169 |
|
---|
170 |
|
---|
171 | this.expandAll = function() {
|
---|
172 | $(o.container).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
|
---|
173 | };
|
---|
174 |
|
---|
175 | this.getCurrentDirName = function() {
|
---|
176 | return $(o.container).find('.currentDir').attr('rel');
|
---|
177 | };
|
---|
178 |
|
---|
179 | this.getCurrentDir = function() {
|
---|
180 | return $(o.container).find('.currentDir');
|
---|
181 | };
|
---|
182 |
|
---|
183 | this.createDir = function(c) {
|
---|
184 | var cid = $(c).parent().attr('id');
|
---|
185 | var pid = cid.substring(o.dirIDprefix.length, cid.length);
|
---|
186 |
|
---|
187 | var dirName = prompt("Please enter new directory name", "");
|
---|
188 | var script = 'ajax/privatecontent/createdir';
|
---|
189 |
|
---|
190 | if (dirName != null && dirName.length != 0) {
|
---|
191 | sendCommand({
|
---|
192 | script: script,
|
---|
193 | postdata:{parentID:pid,name:dirName},
|
---|
194 | callbackSuccess: function (parsedData) {createNode({
|
---|
195 | id: parsedData.id,
|
---|
196 | name: parsedData.name,
|
---|
197 | curentNode: c,
|
---|
198 | hidden: false,
|
---|
199 | addToJSONData:true});
|
---|
200 | }
|
---|
201 | });
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | this.deleteDir = function (c) {
|
---|
206 | var childExisted = (countNodeChild(c) > 0) ? true:false;
|
---|
207 | var confirmDel = false;
|
---|
208 | var confirmChild = true;
|
---|
209 |
|
---|
210 | var cid = $(c).parent().attr('id');
|
---|
211 | var pid = cid.substring(o.dirIDprefix.length, cid.length);
|
---|
212 |
|
---|
213 | if (isHome(c)) return;
|
---|
214 |
|
---|
215 | confirmDel = confirm('Are you sure you want to delete this Directory?');
|
---|
216 |
|
---|
217 | if (childExisted > 0 && confirmDel)
|
---|
218 | confirmChild = confirm('This node has childs, you still want to delete it?');
|
---|
219 |
|
---|
220 | if (confirmDel && confirmChild) {
|
---|
221 | var postdata = {id:pid,delallchild:true}
|
---|
222 | sendCommand({
|
---|
223 | script:'ajax/privatecontent/deletedir',
|
---|
224 | postdata:postdata,
|
---|
225 | callbackSuccess: function (parsedData) {deleteNode(parsedData)}
|
---|
226 | });
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | this.copy = function () {
|
---|
231 | alert ('copy');
|
---|
232 | }
|
---|
233 |
|
---|
234 | this.paste = function () {
|
---|
235 | alert ('paste');
|
---|
236 | }
|
---|
237 |
|
---|
238 | this.initialize = function() {
|
---|
239 | return this;
|
---|
240 | };
|
---|
241 |
|
---|
242 | return this.initialize();
|
---|
243 | }
|
---|
244 | });
|
---|
245 |
|
---|
246 | })(jQuery); |
---|