]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/SugarDependentDropdown/javascript/SugarDependentDropdown.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / SugarDependentDropdown / javascript / SugarDependentDropdown.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 SUGAR.dependentDropdown = {
37         /*
38          * Container for "action" metadata - allows DD to parse saved choices and apply them at display time
39          */
40         currentAction : null,
41         /*
42          * Flag to turn on debug mode.
43          * Current debug output:
44          * SUGAR.dependentDropdown._stack - simple list of this class' called methods
45          */
46         debugMode : false
47 }
48
49 /**
50  * Handle drop-down dependencies
51  * @param object HTML form element object
52  */
53 SUGAR.dependentDropdown.handleDependentDropdown = function(el) {
54         /**
55          * 
56          * 
57          * PROTOTYPE THIS METHOD TO CUSTOMIZE RESPONSES FOR YOUR DEPENDENT DROPDOWNS
58          * 
59          * 
60          * 
61          */
62         /**
63         if(SUGAR.dependentDropdown.debugMode) SUGAR.dependentDropdown.utils.debugStack('handleDependentDropdown');
64         
65         /*
66          * el.id example:
67          * "criteriaGroup::0:::0:-:crit0id"
68          * [grouping from metadata]::[index]:::[elementIndex]:-:[assignedID from metadata]
69          * index is row-number
70          * elementIndex is the index of the current element in this row
71         var index = el.id.slice(el.id.indexOf("::") + 2, el.id.indexOf(":::"));
72         var elementRow = el.boxObject.parentBox;
73         var elementIndex = el.id.slice(el.id.indexOf(":::") + 3, el.id.indexOf(":-:"));
74
75         elementIndex++;
76         var elementKey = "element" + elementIndex;
77         var focusElement = SUGAR.dependentDropdown.dropdowns[focusDD].elements[elementKey];
78         
79         if(focusElement) {
80                 if(focusElement.handlers) {
81                         try {
82                                 focusElement = focusElement.handlers[el.value];
83                         } catch(e) {
84                                 if(SUGAR.dependentDropdown.dropdowns.debugMode) {
85                                         debugger;
86                                 }
87                         }
88                 }
89                 SUGAR.dependentDropdown.generateElement(focusElement, elementRow, index, elementIndex);
90         } else {
91         }
92         */
93 }
94
95
96
97
98
99
100 SUGAR.dependentDropdown.generateElement = function(focusElement, elementRow, index, elementIndex) {
101         if(SUGAR.dependentDropdown.debugMode) SUGAR.dependentDropdown.utils.debugStack('generateElement');
102         
103         var tmp = null;
104         
105         if(focusElement) {
106                 /* get sandbox to play in */
107                 var sandbox = SUGAR.dependentDropdown.utils.generateElementContainer(focusElement, elementRow, index, elementIndex);
108                 
109                 /* handle labels that appear 'left' or 'top' */
110                 if(focusElement.label) {
111                         focusLabel = {
112                                 tag : 'span',
113                                 cls : 'routingLabel',
114                                 html : " " + focusElement.label + " "
115                         }
116                         
117                         switch(focusElement.label_pos) {
118                                 case "top":
119                                         focusLabel.html = focusElement.label + "<br />";
120                                 break;
121                                 
122                                 case "bottom": 
123                                         focusLabel.html = "<br />" + focusElement.label;
124                                 break;
125                         }
126                         
127                         if(focusElement.label_pos == 'left' || focusElement.label_pos == 'top') {
128                                 YAHOO.ext.DomHelper.append(sandbox, focusLabel);
129                         }
130                 }
131
132                 /**********************************************************************
133                  * FUN PART BELOW
134                  */
135                 switch(focusElement.type) {
136                         case 'input':
137                                 /*
138                                  * focusElement.values can be lazy-loaded via JS call
139                                  */
140                                 if(typeof(focusElement.values) == 'string') {
141                                         focusElement.values = eval(focusElement.values);
142                                 }
143                                 
144                                 /* Define the key-value that is to be used to pre-select a value in the dropdown */     
145                                 var preselect = SUGAR.dependentDropdown.utils.getPreselectKey(focusElement.name);
146
147                                 if(preselect.match(/::/))
148                                         preselect = '';
149
150                                 tmp = YAHOO.ext.DomHelper.append(sandbox, {
151                                         tag                     : 'input',
152                                         id                      : focusElement.grouping + "::" + index + ":::" + elementIndex + ":-:" + focusElement.id,
153                                         name            : focusElement.grouping + "::" + index + "::" + focusElement.name,
154                                         cls                     : 'input',
155                                         onchange        : focusElement.onchange,
156                                         value           : preselect
157                                 }, true);
158                                 var newElement = tmp.dom;
159                         break;
160
161
162                         case 'select':
163                                 tmp = YAHOO.ext.DomHelper.append(sandbox, {
164                                         tag                     : 'select',
165                                         id                      : focusElement.grouping + "::" + index + ":::" + elementIndex + ":-:" + focusElement.id,
166                                         name            : focusElement.grouping + "::" + index + "::" + focusElement.name,
167                                         cls                     : 'input',
168                                         onchange        : focusElement.onchange
169                                 }, true);
170                                 var newElement = tmp.dom;
171                                 
172                                 /*
173                                  * focusElement.values can be lazy-loaded via JS call
174                                  */
175                                 if(typeof(focusElement.values) == 'string') {
176                                         focusElement.values = eval(focusElement.values);
177                                 }
178                                 
179                                 /* Define the key-value that is to be used to pre-select a value in the dropdown */
180                                 var preselect = SUGAR.dependentDropdown.utils.getPreselectKey(focusElement.name);
181                                 
182                                 // Loop through the values (passed or generated) and preselect as needed
183                                 var i = 0;
184                                 for(var key in focusElement.values) {
185                                         var selected = (preselect == key) ? true : false;
186                                         newElement.options[i] = new Option(focusElement.values[key], key, selected);
187
188                                         // ie6/7 workaround
189                                         if(selected) {
190                                                 newElement.options[i].selected = true;
191                                         }
192                                         i++;
193                                 }
194                         break;
195                         
196                         case 'none':
197                         break;
198                         
199                         case 'checkbox':
200                                 alert('implement checkbox pls');
201                         break;
202                         case 'multiple':
203                                 alert('implement multiple pls');
204                         break;
205                         
206                         default:
207                                 if(SUGAR.dependentDropdown.dropdowns.debugMode) {
208                                         alert("Improper type defined: [ " + focusElement.type + "]");
209                                 }
210                                 return;
211                         break;
212                 }
213
214                 /* handle label placement *after* or *below* the drop-down */
215                 if(focusElement.label) {
216                         if(focusElement.label_pos == 'right' || focusElement.label_pos == 'bottom') {
217                                 YAHOO.ext.DomHelper.append(sandbox, focusLabel);
218                         }
219                 }
220
221                 /* trigger dependent dropdown action to cascade dependencies */
222                 try {
223                         newElement.onchange();
224                         //eval(focusElement.onchange); "this" has no reference
225                 } catch(e) {
226                         if(SUGAR.dependentDropdown.dropdowns.debugMode) {
227                                 debugger;
228                         }
229                 }
230
231         } else {
232         }
233 }
234
235
236
237 ///////////////////////////////////////////////////////////////////////////////
238 ////    UTILS
239 SUGAR.dependentDropdown.utils = {
240         /**
241          * creates a DIV container for a given element
242          * @param object focusElement Element in focus' metadata
243          * @param object elementRow Parent DIV container's DOM object
244          * @param int index Index of current elementRow
245          * @param int elementIndex Index of the element in focus relative to others in the definition
246          * @return obj Reference DOM object generated
247          */
248         generateElementContainer : function(focusElement, elementRow, index, elementIndex) {
249                 /* clear out existing element if exists */
250                 var oldElement = document.getElementById('elementContainer' + focusElement.grouping + "::" + index + ":::" + elementIndex);
251         
252                 if(oldElement) {
253                         SUGAR.dependentDropdown.utils.removeChildren(oldElement);
254                 }
255                 
256                 /* create sandbox to ease removal */
257                 var tmp = YAHOO.ext.DomHelper.append(elementRow, {
258                         tag : 'span',
259                         id      : 'elementContainer' + focusElement.grouping + "::" + index + ":::" + elementIndex
260                 }, true);
261                 
262                 return tmp.dom;
263         },
264         /**
265          * Finds the preselect key from the User's saved (loaded into memory) metadata
266          * @param string elementName Name of form element - functions as key to user's saved value
267          */
268         getPreselectKey : function(elementName) {
269                 try {
270                         if(SUGAR.dependentDropdown.currentAction.action[elementName]) {
271                                 return SUGAR.dependentDropdown.currentAction.action[elementName];
272                         } else {
273                                 return '';
274                         }
275                 } catch(e) {
276                         if(SUGAR.dependentDropdown.dropdowns.debugMode) {
277                                 //debugger;
278                         }
279                         return '';
280                 }
281         },
282         
283         /**
284          * provides a list of methods called in order when debugging
285          * @param object
286          */
287         debugStack : function(func) {
288                 if(!SUGAR.dependentDropdown._stack) {
289                         SUGAR.dependentDropdown._stack = new Array();
290                 }
291                 
292                 SUGAR.dependentDropdown._stack.push(func);
293         },
294         
295         /**
296          * Removes all child nodes from the passed DOM element
297          */
298         removeChildren : function(el) {
299                 for(i=el.childNodes.length - 1; i >= 0; i--) {
300                         if(el.childNodes[i]) {
301                                 el.removeChild(el.childNodes[i]);
302                         }
303                 }
304         }
305 }