]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/ytree/treeutil.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / ytree / treeutil.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
4  * 
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.
11  * 
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
15  * details.
16  * 
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
20  * 02110-1301 USA.
21  * 
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.
24  * 
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.
28  * 
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  ********************************************************************************/
35
36
37 function treeinit(tree,treedata,treediv,params) {
38         tree = new YAHOO.widget.TreeView(treediv);
39         
40         //create a name space for the tree.
41         //and store the module name;
42         YAHOO.namespace(treediv).param=params;
43         
44         var root= tree.getRoot();
45         var tmpNode;
46
47         var data=treedata;      
48         //loop thru all root level nodes.       
49         for (nodedata in data) {
50                 for (node in data[nodedata]) {
51                         addNode(root, data[nodedata][node]);
52                 }
53         }
54         tree.subscribe("clickEvent", function(o){
55                 set_selected_node(this.id, null, o.node);
56         });
57         
58         tree.draw();
59 }
60
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);
65         
66         if (typeof(node) != 'undefined') {
67                 YAHOO.namespace(treeid).selectednode=node;
68         } else {
69                 //clear selection.
70                 YAHOO.namespace(treeid).selectednode=null;
71         }
72 }
73
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;
79         var childnodes;
80         var expanded=false;
81         
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'];
88                 }
89                 
90                 var tmpNode=new YAHOO.widget.TextNode(nodedef['data'], parentnode, expanded);   
91                 
92                 if (dynamicload) {
93                         tmpNode.setDynamicLoad(eval(dynamicloadfunction));
94                 }
95                 //add child nodes.              
96                 if (typeof(nodedef['nodes']) != 'undefined') {
97                         for (childnodes in nodedef['nodes']) {
98                                         addNode(tmpNode, nodedef['nodes'][childnodes]);
99                         }
100                 }
101         }
102 }
103
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
108 //target types.
109 function node_click(treeid,target,targettype,functioname) {
110         node=YAHOO.namespace(treeid).selectednode;
111
112         //request url.
113         var url=site_url.site_url + "/index.php?entryPoint=TreeData&function="+functioname+ construct_url_from_tree_param(node);
114
115         var callback =  {
116                   success: function(o) {    
117                                 var targetdiv=document.getElementById(o.argument[0]);
118                                 targetdiv.innerHTML=o.responseText;
119                                 SUGAR.util.evalScript(o.responseText);
120                   },
121                   failure: function(o) {/*failure handler code*/},
122                   argument: [target, targettype]
123         }
124         
125         var trobj = YAHOO.util.Connect.asyncRequest('GET',url, callback, null);
126         
127 }
128
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);
132
133         url="&PARAMT_depth="+node.depth;
134
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];
139                 }
140         }       
141
142         //add parent nodes id to the url.
143         for (i=node.depth;i>=0;i--) {
144                 var currentnode;
145                 if (i==node.depth) {    
146                         currentnode=node;
147                 } else {
148                         currentnode=node.getAncestor(i);
149                 }
150                 //add id to url.
151                 url=url+"&PARAMN_id"+'_'+currentnode.depth+'='+currentnode.data.id;                     
152
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];                     
157                         }
158                 }
159         }
160         return url;
161 }
162
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;
169
170         var params="entryPoint=TreeData&function=get_node_data" + construct_url_from_tree_param(node);
171
172         var callback =  {
173                   success: function(o) {    
174                                 node=o.argument[0];
175                         var nodes=YAHOO.lang.JSON.parse(o.responseText);
176                                 var tmpNode;
177                                 for (nodedata in nodes) {
178                                         for (node1 in nodes[nodedata]) {
179                                                 addNode(node, nodes[nodedata][node1]);
180                                         }
181                                 }       
182                         o.argument[1]();
183                   },
184                   failure: function(o) {/*failure handler code*/},
185                   argument: [node, onCompleteCallback]
186         }
187         var trobj = YAHOO.util.Connect.asyncRequest('POST','index.php', callback, params);
188 }