1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/* jQuery treeTable Plugin 2.2 - http://ludo.cubicphuse.nl/jquery-plugins/treeTable/ */
(function($) {
// Helps to make options available to all functions
// TODO: This gives problems when there are both expandable and non-expandable
// trees on a page. The options shouldn't be global to all these instances!
var options;
$.fn.treeTable = function(opts) {
options = $.extend({}, $.fn.treeTable.defaults, opts);
return this.each(function() {
$(this).addClass("treeTable").find("tbody tr").each(function() {
// Initialize root nodes only whenever possible
if(!options.expandable || $(this)[0].className.search("child-of-") == -1) {
initialize($(this));
}
});
});
};
$.fn.treeTable.defaults = {
childPrefix: "child-of-",
expandable: true,
indent: 19,
initialState: "collapsed",
treeColumn: 0
};
// Recursively hide all node's children in a tree
$.fn.collapse = function() {
$(this).addClass("collapsed");
childrenOf($(this)).each(function() {
initialize($(this));
if(!$(this).hasClass("collapsed")) {
$(this).collapse();
}
$(this).hide();
});
return this;
};
// Recursively show all node's children in a tree
$.fn.expand = function() {
$(this).removeClass("collapsed").addClass("expanded");
childrenOf($(this)).each(function() {
initialize($(this));
if($(this).is(".expanded.parent")) {
$(this).expand();
}
$(this).show();
});
return this;
};
// Add an entire branch to +destination+
$.fn.appendBranchTo = function(destination) {
var node = $(this);
var parent = parentOf(node);
var ancestorNames = $.map(ancestorsOf(destination), function(a) { return a.id; });
// Conditions:
// 1: +node+ should not be inserted in a location in a branch if this would
// result in +node+ being an ancestor of itself.
// 2: +node+ should not have a parent OR the destination should not be the
// same as +node+'s current parent (this last condition prevents +node+
// from being moved to the same location where it already is).
// 3: +node+ should not be inserted as a child of +node+ itself.
if($.inArray(node[0].id, ancestorNames) == -1 && (!parent || (destination.attr("id") != parent[0].id)) && destination.attr("id") != node[0].id) {
indent(node, ancestorsOf(node).length * options.indent * -1); // Remove indentation
if(parent) { node.removeClass(options.childPrefix + parent[0].id); }
var dest_id = $(destination).attr("id");
while ($(".child-of-"+dest_id).length > 0) {
var move_to = $(".child-of-"+dest_id+":last");
dest_id = move_to.attr("id");
}
node.addClass(options.childPrefix + destination.attr("id"));
if (move_to)
moveChild(node, move_to); // Recursively move nodes to new location
else
moveChild(node, destination);
indent(node, ancestorsOf(node).length * options.indent);
}
return this;
};
$.fn.insertBranchBefore = function(destination) {
var node = $(this);
var parent = parentOf_jQuery(node);
var dest_parent = parentOf_jQuery(destination);
if ($(this).attr("id")==destination.attr("id"))
return false;
var ancestorNames = $.map(ancestorsOf_jQuery(destination), function(a) { return a.id; });
indent(node, ancestorsOf_jQuery(node).length * options.indent * -1); // Remove indentation
if(parent) { node.removeClass(options.childPrefix + parent[0].id); }
if (dest_parent)
node.addClass(options.childPrefix + dest_parent.attr("id"));
moveBefore(node, destination); // Recursively move nodes to new location
indent(node, (ancestorsOf_jQuery(node).length * options.indent));
return this;
};
// Add reverse() function from JS Arrays
$.fn.reverse = function() {
return this.pushStack(this.get().reverse(), arguments);
};
// Toggle an entire branch
$.fn.toggleBranch = function() {
if($(this).hasClass("collapsed")) {
$(this).expand();
} else {
$(this).removeClass("expanded").collapse();
}
return this;
};
// === Private functions
function ancestorsOf(node) {
var ancestors = [];
while(node = parentOf(node)) {
ancestors[ancestors.length] = node[0];
}
return ancestors;
};
function childrenOf(node) {
return $("table.treeTable tbody tr." + options.childPrefix + node[0].id);
};
function indent(node, value) {
var cell = $(node.children("td")[options.treeColumn]);
var padding = parseInt(cell.css("padding-left"), 10) + value;
cell.css("padding-left", + padding + "px");
childrenOf(node).each(function() {
indent($(this), value);
});
};
function initialize(node) {
if(!node.hasClass("initialized")) {
node.addClass("initialized");
var childNodes = childrenOf(node);
if(!node.hasClass("parent") && childNodes.length > 0) {
node.addClass("parent");
}
if(node.hasClass("parent")) {
var cell = $(node.children("td")[options.treeColumn]);
var padding = parseInt(cell.css("padding-left"), 10) + options.indent;
childNodes.each(function() {
$($(this).children("td")[options.treeColumn]).css("padding-left", padding + "px");
});
if(options.expandable) {
cell.children(":first").children("span").prepend('<span style="margin-left: -' + (options.indent-15) + 'px; padding-left: ' + (options.indent-5) + 'px;" class="expander"></span>');
//$(cell[0].firstChild).click(function() { node.toggleBranch(); });
// Check for a class set explicitly by the user, otherwise set the default class
if(!(node.hasClass("expanded") || node.hasClass("collapsed"))) {
node.addClass(options.initialState);
}
if(node.hasClass("collapsed")) {
node.collapse();
} else if (node.hasClass("expanded")) {
node.expand();
}
}
} else {
var cell = $(node.children("td")[options.treeColumn]);
cell.children(":first").children("span").prepend('<span style="margin-left: -' + (options.indent-15) + 'px; padding-left: ' + (options.indent-5) + 'px;"></span>');
}
node.children(":first").addClass("padded");
}
};
function move(node, destination) {
node.insertAfter(destination);
childrenOf(node).reverse().each(function() { move($(this), node[0]); });
};
function moveChild(node, destination) {
node.insertAfter(destination)
childrenOf(node).reverse().each(function() { move($(this), node[0]); });
};
function moveBefore(node, destination) {
node.insertBefore(destination)
childrenOf(node).reverse().each(function() { move($(this), node[0]); });
};
function parentOf(node) {
var classNames = node[0].className.split(' ');
for(key in classNames) {
if(classNames[key].match("child-of-")) {
return $("#" + classNames[key].substring(9));
}
}
};
})(jQuery);
// public functions
function handle_drop_event(source, dest, method){
var ancestorNames = $.map(ancestorsOf_jQuery(dest), function(a) { return a.attr("id"); });
if (method=="child")
dest.find(".wrap").removeClass("hover-as-child").addClass("nohover");
else // method == "sibling"
dest.find("div:first").remove();
// do not drop on itself or its own children, if method == "child"
if ( (method == "sibling") || (source.attr("id") != dest.attr("id") && $.inArray(source.attr("id"), ancestorNames) == -1) ) {
var source_child_of = null;
if (source.attr("class").match(/child-of-node-(\d+)/))
source_child_of = source.attr("class").match(/child-of-node-(\d+)/)[0];
var dest_child_of = "child-of-" + dest.attr("id");
if (source_child_of && $("."+source_child_of).length - 1 == 0) {
var parent_id = "#" + source_child_of.substring(9);
$(parent_id).removeClass("parent");
if ($(parent_id).hasClass("expanded"))
$(parent_id).removeClass("expanded").addClass("collapsed");
$(parent_id+" .title-col span").removeClass("expander");
}
if (method=="child") {
if ($("."+dest_child_of).length == 0) {
var parent_id = "#" + dest_child_of.substring(9);
$(parent_id).addClass("parent").find(".title-col span").addClass("expander");
}
if (!dest.hasClass("expanded"))
dest.expand();
// *** INSERT ***
source.appendBranchTo(dest);
} else // method == "sibling"
source.insertBranchBefore(dest);
}
source.find(".wrap").switchClass("nohover","flash",0).switchClass("flash","nohover",500);
}
function handle_page_delete(node) {
var page_id = node.attr("class").match(/page-id-(\d+)/)[1];
var parent_id = null;
if (node.attr("class").match(/child-of-node-(\d+)/))
parent_id = node.attr("class").match(/child-of-node-(\d+)/)[1];
var popup_bg = '<div class="popup_bg"></div>';
$("body").append(popup_bg);
if (node.hasClass("parent")){
jAlert('Cannot delete item, because it is parent of at least one other item.',
'Cannot delete item', function(){
$(".popup_bg").remove();
});
} else {
jConfirm('Really delete item?', 'Confirm to delete item', function(r) {
if (r==true) {
$.post('.', {'__cmd': 'delete_item', 'item_id': page_id}, function(data){
if (data == "OK") {
if (parent_id && $(".child-of-node-"+parent_id).length == 1) {
$("#node-"+parent_id).removeClass("parent")
.removeClass("expanded").addClass("collapsed")
.find(".expander").removeClass("expander");
}
node.remove();
$("body").append(popup_bg);
jAlert('Item deleted successfully.',
'Item deleted', function(){
$(".popup_bg").remove();
});
}
});
}
$(".popup_bg").remove();
});
}
}
function parentOf_jQuery(node) {
if (node.attr("class").match(/child-of-node-(\d+)/)) {
var parent_id = node.attr("class").match(/child-of-node-(\d+)/)[1];
return $("#node-"+parent_id);
}
return null;
};
function ancestorsOf_jQuery(node) {
var ancestors = [];
while(node = parentOf_jQuery(node)) {
ancestors[ancestors.length] = node;
}
return ancestors;
};
function save_page_tree() {
var send_tree = new Array();
// prepare tree
var i = 0;
var ancestor_tree_ids = [];
var ancestor_indeces = [];
var tree_id = 0;
$("#sitetree tbody tr").each(function(){
var tobj = new Array();
// 0 = tree_id, 1 = parent_id, 2 = left, 3 = right, 4 = level, 5 = page_id
var classNames = $(this).attr("class").split(' ');
var is_child = false; var is_parent = false;
var parent_id = "";
tobj[1] = null;
// gather information
for (key in classNames) {
if(classNames[key].match("page-id-"))
tobj[5] = parseInt(classNames[key].substring(8));
if(classNames[key].match("parent"))
is_parent = true;
if(classNames[key].match("child-of-")) {
is_child = true;
var node_parent_id = classNames[key].substring(9);
var parent_id = parseInt($("#"+node_parent_id).attr("class").match(/page-id-(\d+)/)[1])
tobj[1] = parent_id;
}
}
// save info
var inArray = ancestor_tree_ids.indexOf(parent_id);
while ( ( is_child && inArray < ancestor_tree_ids.length - 1 && inArray >= 0) || ( !is_child && ancestor_tree_ids.length > 0 ) ) {
send_tree[ancestor_indeces.pop()][3] = i++;
ancestor_tree_ids.pop();
}
if (!is_child) {
tree_id++;
i = 0;
}
tobj[0] = tree_id;
tobj[4] = ancestor_tree_ids.length;
tobj[2] = i++;
if (is_parent) {
ancestor_tree_ids.push(tobj[5]);
ancestor_indeces.push(send_tree.length);
} else {
tobj[3] = i++;
}
send_tree.push(tobj);
});
while (ancestor_tree_ids.length>0) {
send_tree[ancestor_indeces.pop()][3] = i++;
ancestor_tree_ids.pop();
}
// send tree to url
$.post('.', {'__cmd': 'save_tree', 'tree': $.toJSON(send_tree)}, function(data){
if (data == "OK") {
var popup_bg = '<div class="popup_bg"></div>';
$("body").append(popup_bg);
jAlert("Tree saved successfully.", "Tree saved", function(){
$(".popup_bg").remove();
});
}
});
}
|