1 /*********************************************************************************
2 * SugarCRM is a customer relationship management program developed by
3 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Affero General Public License version 3 as published by the
7 * Free Software Foundation with the addition of the following permission added
8 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
17 * You should have received a copy of the GNU Affero General Public License along with
18 * this program; if not, see http://www.gnu.org/licenses or write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25 * The interactive user interfaces in modified source and object code versions
26 * of this program must display Appropriate Legal Notices, as required under
27 * Section 5 of the GNU Affero General Public License version 3.
29 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30 * these Appropriate Legal Notices must retain the display of the "Powered by
31 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32 * technical reasons, the Appropriate Legal Notices must display the words
33 * "Powered by SugarCRM".
34 ********************************************************************************/
37 function treeinit(tree,treedata,treediv,params) {
38 tree = new YAHOO.widget.TreeView(treediv);
40 //create a name space for the tree.
41 //and store the module name;
42 YAHOO.namespace(treediv).param=params;
44 var root= tree.getRoot();
48 //loop thru all root level nodes.
49 for (nodedata in data) {
50 for (node in data[nodedata]) {
51 addNode(root, data[nodedata][node]);
54 tree.subscribe("clickEvent", function(o){
55 set_selected_node(this.id, null, o.node);
61 //set clicked node's id in tree's name space.
62 function set_selected_node(treeid, nodeid, node) {
63 if (typeof(node) == 'undefined')
64 node=YAHOO.widget.TreeView.getNode(treeid,nodeid);
66 if (typeof(node) != 'undefined') {
67 YAHOO.namespace(treeid).selectednode=node;
70 YAHOO.namespace(treeid).selectednode=null;
74 //add a node to the tree, function adds nodes recursively.
75 //change this function
76 function addNode(parentnode,nodedef) {
77 var dynamicload=false;
78 var dynamicloadfunction;
82 if (nodedef['data'] != 'undefined') {
83 //get custom property values;
84 if (typeof(nodedef['custom']) != 'undefined') {
85 dynamicload=nodedef['custom']['dynamicload'];
86 dynamicloadfunction=nodedef['custom']['dynamicloadfunction'];
87 expanded=nodedef['custom']['expanded'];
90 var tmpNode=new YAHOO.widget.TextNode(nodedef['data'], parentnode, expanded);
93 tmpNode.setDynamicLoad(eval(dynamicloadfunction));
96 if (typeof(nodedef['nodes']) != 'undefined') {
97 for (childnodes in nodedef['nodes']) {
98 addNode(tmpNode, nodedef['nodes'][childnodes]);
104 //use this function to respond to node click.
105 //the clicked node is set by set_selected_node, in the tree's name space.
106 //function will make an ajax call and populate the results in the target.
107 //target type should be of type div, the function does not support other
109 function node_click(treeid,target,targettype,functioname) {
110 node=YAHOO.namespace(treeid).selectednode;
113 var url=site_url.site_url + "/index.php?entryPoint=TreeData&function="+functioname+ construct_url_from_tree_param(node);
116 success: function(o) {
117 var targetdiv=document.getElementById(o.argument[0]);
118 targetdiv.innerHTML=o.responseText;
119 SUGAR.util.evalScript(o.responseText);
121 failure: function(o) {/*failure handler code*/},
122 argument: [target, targettype]
125 var trobj = YAHOO.util.Connect.asyncRequest('GET',url, callback, null);
129 //construs url parameters from node and tree parameters.
130 function construct_url_from_tree_param(node) {
131 var treespace=YAHOO.namespace(node.tree.id);
133 url="&PARAMT_depth="+node.depth;
135 //add request parameters from the trees name space.
136 if (treespace != 'undefined') {
137 for (tparam in treespace.param) {
138 url=url+"&PARAMT_"+tparam+'='+treespace.param[tparam];
142 //add parent nodes id to the url.
143 for (i=node.depth;i>=0;i--) {
148 currentnode=node.getAncestor(i);
151 url=url+"&PARAMN_id"+'_'+currentnode.depth+'='+currentnode.data.id;
153 //add other requested parameters.
154 if (currentnode.data.param != 'undefined') {
155 for (nparam in currentnode.data.param) {
156 url=url+"&PARAMN_"+nparam+'_'+currentnode.depth+'='+currentnode.data.param[nparam];
163 //following function uses an AJAX call to load the children nodes dynamically.
164 //this methods assumes that you have TreeData.php in the root of you application.
165 //the file must have get_node_data function.
166 //return value must be an array of nodes.
167 function loadDataForNode(node, onCompleteCallback) {
168 var id= node.data.id;
170 var params="entryPoint=TreeData&function=get_node_data" + construct_url_from_tree_param(node);
173 success: function(o) {
175 var nodes=JSON.parse(o.responseText);
177 for (nodedata in nodes) {
178 for (node1 in nodes[nodedata]) {
179 addNode(node, nodes[nodedata][node1]);
184 failure: function(o) {/*failure handler code*/},
185 argument: [node, onCompleteCallback]
187 var trobj = YAHOO.util.Connect.asyncRequest('POST','index.php', callback, params);