diff options
author | Thomas Bruederli <thomas@roundcube.net> | 2014-06-24 10:51:09 +0200 |
---|---|---|
committer | Thomas Bruederli <thomas@roundcube.net> | 2014-06-24 10:51:09 +0200 |
commit | 9cc0c12bcd9f610d78e6e2346a283603b105a348 (patch) | |
tree | 4fb15104e14170ccd81ee04ddee6d7d11cb2422d /program/js/treelist.js | |
parent | 47788a7c10ce225e5c133350dfc2ef2030a68090 (diff) |
Allow to move a treelist node to a new parent with .update()
Diffstat (limited to 'program/js/treelist.js')
-rw-r--r-- | program/js/treelist.js | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/program/js/treelist.js b/program/js/treelist.js index 3b9454391..631a81a6a 100644 --- a/program/js/treelist.js +++ b/program/js/treelist.js @@ -329,14 +329,28 @@ function rcube_treelist_widget(node, p) */ function update_node(id, updates, sort) { - var li, node = indexbyid[id]; + var li, parent_ul, parent_node, old_parent, + node = indexbyid[id]; if (node) { li = id2dom(id); + parent_ul = li.parent(); + + if (updates.id || updates.html || updates.children || updates.classes || updates.parent) { + if (updates.parent && (parent_node = indexbyid[updates.parent])) { + // remove reference from old parent's child list + if (old_parent = indexbyid[dom2id(parent_ul.closest('li'))]) { + old_parent.children = $.grep(old_parent.children, function(elem, i){ return elem.id != node.id; }); + } + // append to new parent node + parent_ul = id2dom(updates.parent).children('ul').first(); + if (!parent_node.children) + parent_node.children = []; + parent_node.children.push(node); + } - if (updates.id || updates.html || updates.children || updates.classes) { $.extend(node, updates); - render_node(node, li.parent(), li); + render_node(node, parent_ul, li); } if (node.id != id) { @@ -546,8 +560,11 @@ function rcube_treelist_widget(node, p) .addClass((node.classes || []).join(' ')) .data('id', node.id); - if (replace) + if (replace) { replace.replaceWith(li); + if (parent) + li.appendTo(parent); + } else li.appendTo(parent); |