1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2010, 2011, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 ZmShareTreeView = function(params) {
 25     if (arguments.length == 0) return;
 26     params.className = params.className || "ZmShareTreeView OverviewTree";
 27 	params.id = "ZmShareTreeView";
 28     DwtTree.call(this, params);
 29     // TODO: Why is this tree being set to overflow: visible?!?!
 30     this.getHtmlElement().style.overflow = "auto";
 31 };
 32 ZmShareTreeView.prototype = new DwtTree;
 33 ZmShareTreeView.prototype.constructor = ZmShareTreeView;
 34 
 35 ZmShareTreeView.TREE_INDEX = 0;
 36 
 37 ZmShareTreeView.prototype.toString = function() {
 38     return "ZmShareTreeView";
 39 };
 40 
 41 //
 42 // Public methods
 43 //
 44 
 45 ZmShareTreeView.prototype.set = function(params) {
 46     if (this.root) {
 47         this.removeNode(this.root);
 48     }
 49     this._itemHash = {};
 50 
 51     var root = this.root = params.dataTree.root;
 52     var rootItem = this._createTreeItem(this, root);
 53     var children = root.children.getArray();
 54     for (var i = 0; i < children.length; i++) {
 55         this._createTreeItem(rootItem, children[i]);
 56     }
 57 };
 58 
 59 ZmShareTreeView.prototype.getTreeItemById = function(id) {
 60     return this._itemHash && this._itemHash[id];
 61 };
 62 
 63 // DwtTree methods
 64 
 65 ZmShareTreeView.prototype.removeChild = function(child) {
 66     delete this._itemHash[child.getData(Dwt.KEY_ID)];
 67     DwtTree.prototype.removeChild.apply(this, arguments);
 68 };
 69 
 70 // node manipulation
 71 
 72 ZmShareTreeView.prototype.appendChild = function(newNode, parentNode, index, tooltip) {
 73     // add node
 74     var children = parentNode.children;
 75     index = index || children.size();
 76     children.add(newNode, index);
 77 
 78     // add tree item
 79     var parentTreeItem = this.getTreeItemById(parentNode.id);
 80     return this._createTreeItem(parentTreeItem, newNode, index, tooltip);
 81 };
 82 
 83 ZmShareTreeView.prototype.insertBefore = function(newNode, refNode) {
 84     var parent = refNode.parent;
 85     var index = parent.children.indexOf(refNode);
 86     this.appendChild(newNode, parent, index);
 87 };
 88 
 89 ZmShareTreeView.prototype.replaceNode = function(newNode, oldNode) {
 90     var parent = oldNode.parent;
 91     var children = parent.children;
 92     var index = children.indexOf(oldNode);
 93     var nextNode = children.get(index + 1);
 94     this.removeNode(oldNode);
 95     return nextNode ? this.insertBefore(newNode, nextNode) : this.appendChild(newNode, parent);
 96 };
 97 
 98 ZmShareTreeView.prototype.removeNode = function(oldNode) {
 99     // remove node
100     if (oldNode.parent) {
101         oldNode.parent.children.remove(oldNode);
102     }
103 
104     // remove tree item
105     var treeItem = this.getTreeItemById(oldNode.id);
106     if (treeItem) {
107         treeItem.parent.removeChild(treeItem);
108     }
109 };
110 
111 //
112 // Protected methods
113 //
114 
115 ZmShareTreeView.prototype._createTreeItem = function(parent, organizer, index, tooltip) {
116 	var treeItemId = "ZmShareTreeItem_" + ZmShareTreeView.TREE_INDEX++;
117     var treeItem = new DwtTreeItem({parent:parent, id: treeItemId, arrowDisabled: true, dynamicWidth: true});
118     treeItem.setText(AjxStringUtil.htmlEncode(organizer.name));
119     treeItem.setImage(organizer.getIcon());
120     treeItem.setToolTipContent(tooltip);
121     treeItem.setData(Dwt.KEY_ID, organizer.id);
122     treeItem.setData(Dwt.KEY_OBJECT, organizer);
123     this._itemHash[organizer.id] = treeItem;
124     return treeItem;
125 };
126