/* +-----------------------------------------------------------------------+ | Roundcube Treelist widget | | | | This file is part of the Roundcube Webmail client | | Copyright (C) 2013, The Roundcube Dev Team | | | | Licensed under the GNU General Public License version 3 or | | any later version with exceptions for skins & plugins. | | See the README file for a full license statement. | | | +-----------------------------------------------------------------------+ | Authors: Thomas Bruederli | +-----------------------------------------------------------------------+ | Requires: common.js | +-----------------------------------------------------------------------+ */ /** * Roundcube Treelist widget class * @contructor */ function rcube_treelist_widget(node, p) { // apply some defaults to p p = $.extend({ id_prefix: '', autoexpand: 1000, selectable: false, scroll_delay: 500, scroll_step: 5, scroll_speed: 20, check_droptarget: function(node){ return !node.virtual } }, p || {}); var container = $(node), data = p.data || [], indexbyid = {}, selection = null, drag_active = false, box_coords = {}, item_coords = [], autoexpand_timer, autoexpand_item, body_scroll_top = 0, list_scroll_top = 0, scroll_timer, me = this; /////// export public members and methods this.container = container; this.expand = expand; this.collapse = collapse; this.select = select; this.render = render; this.reset = reset; this.drag_start = drag_start; this.drag_end = drag_end; this.intersects = intersects; this.update = update_node; this.insert = insert; this.remove = remove; this.get_item = get_item; this.get_selection = get_selection; /////// startup code (constructor) // abort if node not found if (!container.length) return; if (p.data) index_data({ children:data }); // load data from DOM else update_data(); // register click handlers on list container.on('click', 'div.treetoggle', function(e){ toggle(dom2id($(this).parent())); e.stopPropagation(); }); container.on('click', 'li', function(e){ var node = p.selectable ? indexbyid[dom2id($(this))] : null; if (node && !node.virtual) { select(node.id); e.stopPropagation(); } }); /////// private methods /** * Collaps a the node with the given ID */ function collapse(id, recursive, set) { var node; if (node = indexbyid[id]) { node.collapsed = typeof set == 'undefined' || set; update_dom(node); // Work around a bug in IE7, see #1485309 if (window.bw && bw.ie7 && node.collapsed) { id2dom(node.id).next().children('ul:visible').hide().show(); } if (recursive && node.children) { for (var i=0; i < node.children.length; i++) { collapse(node.children[i].id, recursive, set); } } me.triggerEvent(node.collapsed ? 'collapse' : 'expand', node); } } /** * Expand a the node with the given ID */ function expand(id, recursive) { collapse(id, recursive, false); } /** * Toggle collapsed state of a list node */ function toggle(id, recursive) { var node; if (node = indexbyid[id]) { collapse(id, recursive, !node.collapsed); } } /** * Select a tree node by it's ID */ function select(id) { if (selection) { id2dom(selection).removeClass('selected'); selection = null; } var li = id2dom(id); if (li.length) { li.addClass('selected'); selection = id; // TODO: expand all parent nodes if collapsed scroll_to_node(li); } me.triggerEvent('select', indexbyid[id]); } /** * Getter for the currently selected node ID */ function get_selection() { return selection; } /** * Return the DOM element of the list item with the given ID */ function get_item(id) { return id2dom(id).get(0); } /** * Insert the given node */ function insert(node, parent_id, sort) { var li, parent_li, parent_node = parent_id ? indexbyid[parent_id] : null; // insert as child of an existing node if (parent_node) { if (!parent_node.children) parent_node.children = []; parent_node.children.push(node); parent_li = id2dom(parent_id); // re-render the entire subtree if (parent_node.children.length == 1) { render_node(parent_node, parent_li.parent(), parent_li); li = id2dom(node.id); } else { // append new node to parent's child list li = render_node(node, parent_li.children('ul').first()); } } // insert at top level else { data.push(node); li = render_node(node, container); } indexbyid[node.id] = node; if (sort) { resort_node(li, typeof sort == 'string' ? '[class~="' + sort + '"]' : ''); } } /** * Update properties of an existing node */ function update_node(id, updates, sort) { var li, node = indexbyid[id]; if (node) { li = id2dom(id); if (updates.id || updates.html || updates.children || updates.classes) { $.extend(node, updates); render_node(node, li.parent(), li); } if (node.id != id) { delete indexbyid[id]; indexbyid[node.id] = node; } if (sort) { resort_node(li, typeof sort == 'string' ? '[class~="' + sort + '"]' : ''); } } } /** * Helper method to sort the list of the given item */ function resort_node(li, filter) { var first, sibling, myid = li.get(0).id, sortname = li.children().first().text().toUpperCase(); li.parent().children('li' + filter).each(function(i, elem) { if (i == 0) first = elem; if (elem.id == myid) { // skip } else if (elem.id != myid && sortname >= $(elem).children().first().text().toUpperCase()) { sibling = elem; } else { return false; } }); if (sibling) { li.insertAfter(sibling); } else if (first.id != myid) { li.insertBefore(first); } // reload data from dom update_data(); } /** * Remove the item with the given ID */ function remove(id) { var node, li; if (node = indexbyid[id]) { li = id2dom(id); li.remove(); node.deleted = true; delete indexbyid[id]; return true; } return false; } /** * (Re-)read tree data from DOM */ function update_data() { data = walk_list(container); } /** * Apply the 'collapsed' status of the data node to the corresponding DOM element(s) */ function update_dom(node) { var li = id2dom(node.id); li.children('ul').first()[(node.collapsed ? 'hide' : 'show')](); li.children('div.treetoggle').removeClass('collapsed expanded').addClass(node.collapsed ? 'collapsed' : 'expanded'); me.triggerEvent('toggle', node); } /** * */ function reset() { select(''); data = []; indexbyid = {}; drag_active = false; container.html(''); } /** * Render the tree list from the internal data structure */ function render() { if (me.triggerEvent('renderBefore', data) === false) return; // remove all child nodes container.html(''); // render child nodes for (var i=0; i < data.length; i++) { render_node(data[i], container); } me.triggerEvent('renderAfter', container); } /** * Render a specific node into the DOM list */ function render_node(node, parent, replace) { if (node.deleted) return; var li = $('
  • ') .attr('id', p.id_prefix + (p.id_encode ? p.id_encode(node.id) : node.id)) .addClass((node.classes || []).join(' ')); if (replace) replace.replaceWith(li); else li.appendTo(parent); if (typeof node.html == 'string') li.html(node.html); else if (typeof node.html == 'object') li.append(node.html); if (node.virtual) li.addClass('virtual'); if (node.id == selection) li.addClass('selected'); // add child list and toggle icon if (node.children && node.children.length) { $('
     
    ').appendTo(li); var ul = $('