forked from feincms/feincms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfein_tree.js
More file actions
executable file
·383 lines (334 loc) · 14.3 KB
/
fein_tree.js
File metadata and controls
executable file
·383 lines (334 loc) · 14.3 KB
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
/* Suppress initial rendering of result list, but only if we can show it with JS later on */
document.write('<style type="text/css">#result_list { display: none }</style>');
feincms.jQuery(function($){
// recolor tree after expand/collapse
$.extend($.fn.recolorRows = function() {
$('tr:visible:even', this).removeClass('row2').addClass('row1');
$('tr:visible:odd', this).removeClass('row1').addClass('row2');
/* Mark inactive rows */
$('tr.item_inactive').removeClass('item_inactive');
$('div[id^=wrap_active_] input:checkbox:not(:checked)').parents('tr').addClass('item_inactive');
$('div[id^=wrap_active_] img').parents('tr').addClass('item_inactive');
});
function isExpandedNode(id) {
return feincms.collapsed_nodes.indexOf(id) == -1;
}
function markNodeAsExpanded(id) {
// remove itemId from array of collapsed nodes
var idx = feincms.collapsed_nodes.indexOf(id);
if(idx >= 0)
feincms.collapsed_nodes.splice(idx, 1);
}
function markNodeAsCollapsed(id) {
if(isExpandedNode(id))
feincms.collapsed_nodes.push(id);
}
// toggle children
function doToggle(id, show) {
var children = feincms.tree_structure[id];
for (var i=0; i<children.length; ++i) {
var childId = children[i];
if(show) {
$('#item-' + childId).show();
// only reveal children if current node is not collapsed
if(isExpandedNode(childId)) {
doToggle(childId, show);
}
} else {
$('#item-' + childId).hide();
// always recursively hide children
doToggle(childId, show);
}
}
}
function rowLevel($row) {
return parseInt($row.attr('rel').replace(/[^\d]/ig, ''));
}
/*
* FeinCMS Drag-n-drop tree reordering.
* Based upon code by bright4 for Radiant CMS, rewritten for
* FeinCMS by Bjorn Post.
*
* September 2010
*
*/
$.extend($.fn.feinTree = function() {
$('tr', this).each(function(i, el) {
// adds 'children' class to all parents
var pageId = extract_item_id($('.page_marker', el).attr('id'));
$(el).attr('id', 'item-' + pageId);
if (feincms.tree_structure[pageId].length) {
$('.page_marker', el).addClass('children');
}
// set 'level' on rel attribute
var pixels = $('.page_marker', el).css('width').replace(/[^\d]/ig,"");
var rel = Math.round(pixels/18);
$(el).attr('rel', rel);
});
$('div.drag_handle').bind('mousedown', function(event) {
BEFORE = 0;
AFTER = 1;
CHILD = 2;
CHILD_PAD = 20;
var originalRow = $(event.target).closest('tr');
var rowHeight = originalRow.height();
var childEdge = $(event.target).offset().left + $(event.target).width();
var moveTo = new Object();
var expandObj = new Object();
$("body").addClass('dragging').disableSelection().bind('mousemove', function(event) {
// attach dragged item to mouse
var cloned = originalRow.html();
if($('#ghost').length == 0) {
$('<div id="ghost"></div>').appendTo('body');
}
$('#ghost').html(cloned).css({
'opacity': .8,
'position': 'absolute',
'top': event.pageY,
'left': event.pageX-30,
'width': 600
});
// check on edge of screen
if(event.pageY+100 > $(window).height()+$(window).scrollTop()) {
$('html,body').stop().animate({scrollTop: $(window).scrollTop()+250 }, 500);
} else if(event.pageY-50 < $(window).scrollTop()) {
$('html,body').stop().animate({scrollTop: $(window).scrollTop()-250 }, 500);
}
// check if drag_line element already exists, else append
if($("#drag_line").length < 1) {
$("body").append('<div id="drag_line" style="position:absolute">line<div></div></div>');
}
// loop trough all rows
$("tr", originalRow.parent()).each(function(index, element) {
var element = $(element),
top = element.offset().top;
// check if mouse is over a row
if (event.pageY >= top && event.pageY < top + rowHeight) {
var targetRow = null,
targetLoc = null,
elementLevel = rowLevel(element);
if (event.pageY >= top && event.pageY < top + rowHeight / 3) {
targetRow = element;
targetLoc = BEFORE;
} else if (event.pageY >= top + rowHeight / 3 && event.pageY < top + rowHeight * 2 / 3) {
var next = element.next();
// there's no point in allowing adding children when there are some already
// better move the items to the correct place right away
if (!next.length || rowLevel(next) <= elementLevel) {
targetRow = element;
targetLoc = CHILD;
}
} else if (event.pageY >= top + rowHeight * 2 / 3 && event.pageY < top + rowHeight) {
var next = element.next();
if (!next.length || rowLevel(next) <= elementLevel) {
targetRow = element;
targetLoc = AFTER;
}
}
if(targetRow) {
var padding = 37 + element.attr('rel') * CHILD_PAD + (targetLoc == CHILD ? CHILD_PAD : 0 );
$("#drag_line").css({
'width': targetRow.width() - padding,
'left': targetRow.offset().left + padding,
'top': targetRow.offset().top + (targetLoc == AFTER || targetLoc == CHILD ? rowHeight: 0) -1
});
// Store the found row and options
moveTo.hovering = element;
moveTo.relativeTo = targetRow;
moveTo.side = targetLoc;
return true;
}
}
});
});
$('body').keydown(function(event) {
if (event.which == '27') {
$("#drag_line").remove();
$("#ghost").remove();
$("body").removeClass('dragging').enableSelection().unbind('mousemove').unbind('mouseup');
event.preventDefault();
}
});
$("body").bind('mouseup', function(event) {
if(moveTo.relativeTo) {
var cutItem = extract_item_id(originalRow.find('.page_marker').attr('id'));
var pastedOn = extract_item_id(moveTo.relativeTo.find('.page_marker').attr('id'));
// get out early if items are the same
if(cutItem != pastedOn) {
var isParent = (moveTo.relativeTo.next().attr('rel') > moveTo.relativeTo.attr('rel'));
var position = '';
// determine position
if(moveTo.side == CHILD && !isParent) {
position = 'last-child';
} else if (moveTo.side == BEFORE) {
position = 'left';
} else {
position = 'right';
}
// save
$.post('.', {
'__cmd': 'move_node',
'position': position,
'cut_item': cutItem,
'pasted_on': pastedOn
}, function(data) {
window.location.reload();
});
} else {
$("#drag_line").remove();
$("#ghost").remove();
}
$("body").removeClass('dragging').enableSelection().unbind('mousemove').unbind('mouseup');
}
});
});
return this;
});
/* Every time the user expands or collapses a part of the tree, we remember
the current state of the tree so we can restore it on a reload.
Note: We might use html5's session storage? */
function storeCollapsedNodes(nodes) {
$.cookie('feincms_collapsed_nodes', "[" + nodes.join(",") + "]", { expires: 7 });
}
function retrieveCollapsedNodes() {
var n = $.cookie('feincms_collapsed_nodes');
if(n != null) {
try {
n = $.parseJSON(n);
} catch(e) {
n = null;
}
}
return n;
}
function expandOrCollapseNode(item) {
var show = true;
if(!item.hasClass('children'))
return;
var itemId = extract_item_id(item.attr('id'));
if(!isExpandedNode(itemId)) {
item.removeClass('closed');
markNodeAsExpanded(itemId);
} else {
item.addClass('closed');
show = false;
markNodeAsCollapsed(itemId);
}
storeCollapsedNodes(feincms.collapsed_nodes);
doToggle(itemId, show);
$('#result_list tbody').recolorRows();
}
$.extend($.fn.feinTreeToggleItem = function() {
$(this).click(function(event){
expandOrCollapseNode($(this));
if(event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
return false;
});
return this;
});
// bind the collapse all children event
$.extend($.fn.bindCollapseTreeEvent = function() {
$(this).click(function() {
rlist = $("#result_list");
rlist.hide();
$('tbody tr', rlist).each(function(i, el) {
var marker = $('.page_marker', el);
if(marker.hasClass('children')) {
var itemId = extract_item_id(marker.attr('id'));
doToggle(itemId, false);
marker.addClass('closed');
markNodeAsCollapsed(itemId);
}
});
storeCollapsedNodes(feincms.collapsed_nodes);
rlist.show();
$('tbody', rlist).recolorRows();
});
return this;
});
// bind the open all children event
$.extend($.fn.bindOpenTreeEvent = function() {
$(this).click(function() {
rlist = $("#result_list");
rlist.hide();
$('tbody tr', rlist).each(function(i, el) {
var marker = $('span.page_marker', el);
if(marker.hasClass('children')) {
var itemId = extract_item_id($('span.page_marker', el).attr('id'));
doToggle(itemId, true);
marker.removeClass('closed');
markNodeAsExpanded(itemId);
}
});
storeCollapsedNodes([]);
rlist.show();
$('tbody', rlist).recolorRows();
});
return this;
});
var changelist_tab = function(elem, event, direction) {
event.preventDefault();
elem = $(elem);
var ne = (direction > 0) ? elem.nextAll(':visible:first') : elem.prevAll(':visible:first');
if(ne) {
elem.attr('tabindex', -1);
ne.attr('tabindex', '0');
ne.focus();
}
};
function keyboardNavigationHandler(event) {
// console.log('keydown', this, event.keyCode);
switch(event.keyCode) {
case 40: // down
changelist_tab(this, event, 1);
break;
case 38: // up
changelist_tab(this, event, -1);
break;
case 37: // left
case 39: // right
expandOrCollapseNode($(this).find('.page_marker'));
break;
case 13: // return
where_to = extract_item_id($('span', this).attr('id'));
document.location = document.location.pathname + where_to + '/'
break;
default:
break;
};
}
// fire!
rlist = $("#result_list");
if($('tbody tr', rlist).length > 1) {
rlist.hide();
$('tbody', rlist).feinTree();
$('span.page_marker', rlist).feinTreeToggleItem();
$('#collapse_entire_tree').bindCollapseTreeEvent();
$('#open_entire_tree').bindOpenTreeEvent();
// Disable things user cannot do anyway (object level permissions)
non_editable_fields = $('.tree-item-not-editable', rlist).parents('tr');
non_editable_fields.addClass('non-editable');
$('input:checkbox', non_editable_fields).attr('disabled', 'disabled');
$('a:first', non_editable_fields).click(function(e){e.preventDefault()});
$('.drag_handle', non_editable_fields).removeClass('drag_handle');
/* Enable focussing, put focus on first result, add handler for keyboard navigation */
$('tr', rlist).attr('tabindex', -1);
$('tbody tr:first', rlist).attr('tabindex', 0).focus();
$('tr', rlist).keydown(keyboardNavigationHandler);
feincms.collapsed_nodes = [];
var storedNodes = retrieveCollapsedNodes();
if(storedNodes == null) {
$('#collapse_entire_tree').click();
} else {
for(var i=0; i<storedNodes.length; i++) {
$('#page_marker-' + storedNodes[i]).click();
}
}
}
rlist.show();
$('tbody', rlist).recolorRows();
});