1 /*********************************************************************************
2 * SugarCRM Community Edition is a customer relationship management program developed by
3 * SugarCRM, Inc. Copyright (C) 2004-2012 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 ********************************************************************************/
36 SUGAR.dependentDropdown = {
38 * Container for "action" metadata - allows DD to parse saved choices and apply them at display time
42 * Flag to turn on debug mode.
43 * Current debug output:
44 * SUGAR.dependentDropdown._stack - simple list of this class' called methods
50 * Handle drop-down dependencies
51 * @param object HTML form element object
53 SUGAR.dependentDropdown.handleDependentDropdown = function(el) {
57 * PROTOTYPE THIS METHOD TO CUSTOMIZE RESPONSES FOR YOUR DEPENDENT DROPDOWNS
63 if(SUGAR.dependentDropdown.debugMode) SUGAR.dependentDropdown.utils.debugStack('handleDependentDropdown');
67 * "criteriaGroup::0:::0:-:crit0id"
68 * [grouping from metadata]::[index]:::[elementIndex]:-:[assignedID from metadata]
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(":-:"));
76 var elementKey = "element" + elementIndex;
77 var focusElement = SUGAR.dependentDropdown.dropdowns[focusDD].elements[elementKey];
80 if(focusElement.handlers) {
82 focusElement = focusElement.handlers[el.value];
84 if(SUGAR.dependentDropdown.dropdowns.debugMode) {
89 SUGAR.dependentDropdown.generateElement(focusElement, elementRow, index, elementIndex);
100 SUGAR.dependentDropdown.generateElement = function(focusElement, elementRow, index, elementIndex) {
101 if(SUGAR.dependentDropdown.debugMode) SUGAR.dependentDropdown.utils.debugStack('generateElement');
106 /* get sandbox to play in */
107 var sandbox = SUGAR.dependentDropdown.utils.generateElementContainer(focusElement, elementRow, index, elementIndex);
109 /* handle labels that appear 'left' or 'top' */
110 if(focusElement.label) {
113 cls : 'routingLabel',
114 html : " " + focusElement.label + " "
117 switch(focusElement.label_pos) {
119 focusLabel.html = focusElement.label + "<br />";
123 focusLabel.html = "<br />" + focusElement.label;
127 if(focusElement.label_pos == 'left' || focusElement.label_pos == 'top') {
128 YAHOO.ext.DomHelper.append(sandbox, focusLabel);
132 /**********************************************************************
135 switch(focusElement.type) {
138 * focusElement.values can be lazy-loaded via JS call
140 if(typeof(focusElement.values) == 'string') {
141 focusElement.values = eval(focusElement.values);
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);
147 if(preselect.match(/::/))
150 tmp = YAHOO.ext.DomHelper.append(sandbox, {
152 id : focusElement.grouping + "::" + index + ":::" + elementIndex + ":-:" + focusElement.id,
153 name : focusElement.grouping + "::" + index + "::" + focusElement.name,
155 onchange : focusElement.onchange,
158 var newElement = tmp.dom;
163 tmp = YAHOO.ext.DomHelper.append(sandbox, {
165 id : focusElement.grouping + "::" + index + ":::" + elementIndex + ":-:" + focusElement.id,
166 name : focusElement.grouping + "::" + index + "::" + focusElement.name,
168 onchange : focusElement.onchange
170 var newElement = tmp.dom;
173 * focusElement.values can be lazy-loaded via JS call
175 if(typeof(focusElement.values) == 'string') {
176 focusElement.values = eval(focusElement.values);
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);
182 // Loop through the values (passed or generated) and preselect as needed
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);
190 newElement.options[i].selected = true;
200 alert('implement checkbox pls');
203 alert('implement multiple pls');
207 if(SUGAR.dependentDropdown.dropdowns.debugMode) {
208 alert("Improper type defined: [ " + focusElement.type + "]");
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);
221 /* trigger dependent dropdown action to cascade dependencies */
223 newElement.onchange();
224 //eval(focusElement.onchange); "this" has no reference
226 if(SUGAR.dependentDropdown.dropdowns.debugMode) {
237 ///////////////////////////////////////////////////////////////////////////////
239 SUGAR.dependentDropdown.utils = {
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
248 generateElementContainer : function(focusElement, elementRow, index, elementIndex) {
249 /* clear out existing element if exists */
250 var oldElement = document.getElementById('elementContainer' + focusElement.grouping + "::" + index + ":::" + elementIndex);
253 SUGAR.dependentDropdown.utils.removeChildren(oldElement);
256 /* create sandbox to ease removal */
257 var tmp = YAHOO.ext.DomHelper.append(elementRow, {
259 id : 'elementContainer' + focusElement.grouping + "::" + index + ":::" + elementIndex
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
268 getPreselectKey : function(elementName) {
270 if(SUGAR.dependentDropdown.currentAction.action[elementName]) {
271 return SUGAR.dependentDropdown.currentAction.action[elementName];
276 if(SUGAR.dependentDropdown.dropdowns.debugMode) {
284 * provides a list of methods called in order when debugging
287 debugStack : function(func) {
288 if(!SUGAR.dependentDropdown._stack) {
289 SUGAR.dependentDropdown._stack = new Array();
292 SUGAR.dependentDropdown._stack.push(func);
296 * Removes all child nodes from the passed DOM element
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]);