var oldparent; var title_column='name'; $(document).ready(function() { if (!$('#changelist').length) return false; var allcats = $('#changelist tbody tr th').map(function(i, el) { return [[document.forms[0]['form-' + i + '-' + title_column].value, $(el).find('a')[0].innerHTML, document.forms[0]['form-' + i + '-parent'].value, i]] }); function build_tree(par) { return allcats.filter(function(i) { return this[2] == par; }).map(function(i, el) { var node = { attributes: { "id": 'n' + el[1], "pk": el[1], "alt": el[3] }, data: { title: el[0], attributes: { href: el[1] + '/' } } }; var chld = build_tree(el[1]); if (chld.length) node.children = chld; return node; }).get(); } var treeheight = $('#changelist').innerHeight(); $('#changelist').hide(); var treediv = document.createElement('div'); treediv.style.cssText = 'position:relative;width:100%;'; treediv.className = 'module'; treediv.id = 'treediv'; $(treediv).insertAfter('#changelist'); var savediv = document.createElement('div'); savediv.style.cssText = 'clear:both;width:100%;height:32px;'; var btn = document.createElement('input'); btn.className = 'default'; btn.type = 'submit'; btn.value = $('#changelist input[type=submit]:last')[0].value; btn.onclick = function() { document.forms[0].submit(); return false; } $(savediv).append(btn); $(savediv).append(btn); $(savediv).insertAfter('#changelist'); $('#changelist input[name=_save]')[0].type = 'text'; $(function() { $("#treediv").tree({ ui: { theme_name: 'apple' }, plugins: { contextmenu: { items: { remove: false, create: { label: "Create", icon: "create", visible: function(NODE, TREE_OBJ) { if (NODE.length != 1) return 0; return TREE_OBJ.check("creatable", NODE); }, action: function(NODE, TREE_OBJ) { location.href = 'add/?parent=' + $(NODE).attr('pk') }, separator_after: true }, rename: { label: "Rename", icon: "rename", visible: function(NODE, TREE_OBJ) { if (NODE.length != 1) return false; return TREE_OBJ.check("renameable", NODE); }, action: function(NODE, TREE_OBJ) { TREE_OBJ.rename(NODE); } }, edit: { label: "Modify", icon: "rename", visible: function(NODE, TREE_OBJ) { if (NODE.length != 1) return false; return TREE_OBJ.check("renameable", NODE); }, action: function(NODE, TREE_OBJ) { location.href = $(NODE).attr('pk') + '/'; } } } } }, callback: { onmove: function(NODE, REF_NODE, TYPE, TREE_OBJ, RB) { document.forms[0]['form-' + $(NODE).attr('alt') + '-parent'].value = ((TREE_OBJ.parent(NODE) != -1) ? TREE_OBJ.parent(NODE).attr('pk') : ''); TREE_OBJ.children(oldparent).map(function(i, el) { document.forms[0]['form-' + $(el).attr('alt') + '-position'].value = i; }); TREE_OBJ.children(TREE_OBJ.parent(NODE)).map(function(i, el) { document.forms[0]['form-' + $(el).attr('alt') + '-position'].value = i; }); }, beforemove: function(NODE, REF_NODE, TYPE, TREE_OBJ) { oldparent = TREE_OBJ.parent(NODE); return true; }, ondblclk: function(NODE, TREE_OBJ) { location.href = $(NODE).attr('pk'); }, onload: function(TREE_OBJ) { TREE_OBJ.open_all(); }, onrename: function(NODE, TREE_OBJ, RB) { document.forms[0]['form-' + $(NODE).attr('alt') + '-' + title_column].value = TREE_OBJ.get_text(NODE); }, onselect: function(NODE, TREE_OBJ) { } }, data: { type: "json", opts: { static: build_tree('') } } }); }); });