[richfaces-svn-commits] JBoss Rich Faces SVN: r14541 - in branches/community/3.3.X/ui/menu-components/src/main: resources/org/richfaces/renderkit/html/scripts and 1 other directory.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Fri Jun 5 12:19:29 EDT 2009


Author: nbelaevski
Date: 2009-06-05 12:19:29 -0400 (Fri, 05 Jun 2009)
New Revision: 14541

Modified:
   branches/community/3.3.X/ui/menu-components/src/main/java/org/richfaces/renderkit/html/MenuItemRendererBase.java
   branches/community/3.3.X/ui/menu-components/src/main/resources/org/richfaces/renderkit/html/scripts/menu.js
Log:
https://jira.jboss.org/jira/browse/RF-7322

Modified: branches/community/3.3.X/ui/menu-components/src/main/java/org/richfaces/renderkit/html/MenuItemRendererBase.java
===================================================================
--- branches/community/3.3.X/ui/menu-components/src/main/java/org/richfaces/renderkit/html/MenuItemRendererBase.java	2009-06-05 16:18:59 UTC (rev 14540)
+++ branches/community/3.3.X/ui/menu-components/src/main/java/org/richfaces/renderkit/html/MenuItemRendererBase.java	2009-06-05 16:19:29 UTC (rev 14541)
@@ -23,7 +23,9 @@
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -40,7 +42,9 @@
 import org.ajax4jsf.renderkit.AjaxRendererUtils;
 import org.ajax4jsf.renderkit.ComponentVariables;
 import org.ajax4jsf.renderkit.ComponentsVariableResolver;
+import org.ajax4jsf.renderkit.RendererUtils;
 import org.ajax4jsf.renderkit.RendererUtils.HTML;
+import org.ajax4jsf.renderkit.RendererUtils.ScriptHashVariableWrapper;
 import org.richfaces.component.MenuComponent;
 import org.richfaces.component.UIMenuItem;
 import org.richfaces.component.util.ViewUtil;
@@ -135,38 +139,35 @@
     	
     	return params;
     }
-    public String encodeParamsAsObject(FacesContext context, UIMenuItem component) throws IOException {
+    
+    public Map<String, Object> getParamsAsMap(FacesContext context, UIMenuItem component) throws IOException {
+    	Map<String, Object> paramsMap = new LinkedHashMap<String, Object>();
     	
-    	UIMenuItem menuItem = component;
-    	StringBuffer buff = new StringBuffer("{");
-    	
-    	List children = menuItem.getChildren();
-    	for (Iterator iterator = children.iterator(); iterator.hasNext();) {
-    		UIComponent child = (UIComponent) iterator.next();
-				
-    		if(child instanceof UIParameter){
+    	for (UIComponent child: component.getChildren()) {
+    		if(child instanceof UIParameter) {
 					
     			UIParameter param = (UIParameter)child;
 				String name = param.getName();
 				
 				if (name != null) {
-					Object value = param.getValue();
-					buff.append(ScriptUtils.toScript(name));
-					buff.append(":");
-					buff.append(ScriptUtils.toScript(value));
-					buff.append(",");
+					paramsMap.put(name, param.getValue());
 				}
 			}
     	}
-    	if (buff.length()>1) {
-    		buff.deleteCharAt(buff.length()-1);
-    		buff.append("}");
-    		return buff.toString();
-    	} else {
-    		return null;
-    	}
+    
+    	return paramsMap;
     }    
     
+    private boolean isNestedInMenu(UIComponent component) {
+    	for (UIComponent c = component; c != null; c = c.getParent()) {
+    		if (c instanceof MenuComponent) {
+    			return true;
+    		}
+    	}
+    	
+    	return false;
+    }
+    
     public void initializeResources(FacesContext context, UIMenuItem menuItem)
             throws IOException {
         ComponentVariables variables =
@@ -181,61 +182,55 @@
         }
         variables.setVariable("icon", resource);
         
-        // create attributes string for item without parent
-        StringBuffer attr = new StringBuffer();
-        String attrStr = "";
-        if (!(menuItem.getParent() instanceof MenuComponent))
-        {
-        	String styleClass = (String) menuItem.getAttributes().get(HTML.STYLE_CLASS_ATTR);
-        	String str = "";
-        	attr.append(",{");
-        	if (null!=styleClass && styleClass.length()>0) {
-        		attr.append("styleClass:");
-        	    attr.append(ScriptUtils.toScript(styleClass));
-        	    str = ",";
-        	}
-        	String onselect = (String) menuItem.getAttributes().get(HTML.onselect_ATTRIBUTE);
-        	if (null!=onselect && onselect.length()>0) {
-        		attr.append(str);
-        		attr.append("onselect:function(event){");
-        		attr.append(onselect);
-        		attr.append("}");
-        	}
-        	attr.append("}");
-        	if (attr.length()>3) attrStr = attr.toString();
-        }
-        
+    	RendererUtils rendererUtils = getUtils();
+
         if (menuItem.isDisabled()) {
             variables.setVariable("iconDisabledClasses",
                     "rich-menu-item-icon-disabled");
         } else {
+            Map<String, Object> menuItemAttributes = menuItem.getAttributes();
 
+            // create attributes string for item without parent
+            Map<String, Object> attrMap = new HashMap<String, Object>(3);
+			if (!isNestedInMenu(menuItem)) {
+                rendererUtils.addToScriptHash(attrMap, "styleClass", menuItemAttributes.get(HTML.STYLE_CLASS_ATTR), 
+                	null, ScriptHashVariableWrapper.DEFAULT);
+
+                rendererUtils.addToScriptHash(attrMap, "onselect", menuItemAttributes.get(HTML.onselect_ATTRIBUTE), 
+                    	null, ScriptHashVariableWrapper.EVENT_HANDLER);
+            }
+
             variables.setVariable("onmouseoutInlineStyles",
                     collectInlineStyles(context, menuItem, false));
             variables.setVariable("onmouseoverInlineStyles",
             		collectInlineStyles(context, menuItem, true));
 
             //-----------------------------------
-            StringBuffer scriptValue = new StringBuffer();
+            StringBuilder scriptValue = new StringBuilder();
             String mode = resolveSubmitMode(menuItem);
             
             if (MenuComponent.MODE_AJAX.equalsIgnoreCase(mode)) {
             	scriptValue.append("RichFaces.Menu.updateItem(event,this");
-            	scriptValue.append(attrStr);
+            	
+            	if (!attrMap.isEmpty()) {
+            		scriptValue.append(',');
+                	scriptValue.append(ScriptUtils.toScript(attrMap));
+            	}
+
             	scriptValue.append(");");
             	String event = null;
-            	Object onclick = menuItem.getAttributes().get(HTML.onclick_ATTRIBUTE);
+            	Object onclick = menuItemAttributes.get(HTML.onclick_ATTRIBUTE);
             	if(onclick != null && onclick.toString().length()>0){
             		event = HTML.onclick_ATTRIBUTE;
             	}else{
-            		Object onselect = menuItem.getAttributes().get(HTML.onselect_ATTRIBUTE);
+            		Object onselect = menuItemAttributes.get(HTML.onselect_ATTRIBUTE);
             		if(onselect != null && onselect.toString().length()>0){
             			event = HTML.onselect_ATTRIBUTE;
             		}
             	}
             	scriptValue.append(AjaxRendererUtils.buildOnEvent(
                         menuItem, context, event).toString());
-            	menuItem.getAttributes().put(HTML.onselect_ATTRIBUTE, null);
+            	menuItemAttributes.put(HTML.onselect_ATTRIBUTE, null);
             } else if (MenuComponent.MODE_SERVER.equalsIgnoreCase(mode)) {
             	
             /*
@@ -269,29 +264,35 @@
 	        scriptValue.append(",");
 	        scriptValue.append("params);return false;");
 	        */
-            	Object onclick = menuItem.getAttributes().get(HTML.onclick_ATTRIBUTE);
+            	Object onclick = menuItemAttributes.get(HTML.onclick_ATTRIBUTE);
             	if(onclick != null && onclick.toString().length()>0){
             		scriptValue.append(onclick.toString());
             		scriptValue.append(";");
             	}
             	scriptValue.append("RichFaces.Menu.submitForm(event,this");
-            	String params = encodeParamsAsObject(context, menuItem);
-            	if (null!=params) {
-            		scriptValue.append(",");
-            		scriptValue.append(params);
+            	
+            	Map<String, Object> scriptOptionsMap = new HashMap<String, Object>(5);
+            	rendererUtils.addToScriptHash(scriptOptionsMap, "a", attrMap, null, ScriptHashVariableWrapper.DEFAULT);
+            	
+            	Map<String, Object> paramsMap = getParamsAsMap(context, menuItem);
+            	rendererUtils.addToScriptHash(scriptOptionsMap, "p", paramsMap, null, ScriptHashVariableWrapper.DEFAULT);
+
+            	String target = (String) menuItemAttributes.get("target");
+            	rendererUtils.addToScriptHash(scriptOptionsMap, "t", target, null, ScriptHashVariableWrapper.DEFAULT);
+
+            	if (!scriptOptionsMap.isEmpty()) {
+            		scriptValue.append(',');
+            		scriptValue.append(ScriptUtils.toScript(scriptOptionsMap));
             	}
-    	        String target = (String) menuItem.getAttributes().get("target");
-    	        if (null != target && target.length()>0) {
-    	        	scriptValue.append(",");
-    	 			scriptValue.append(ScriptUtils.toScript(target));
-    	        }
-    	        
-            	scriptValue.append(attrStr);
+            	
             	scriptValue.append(")");
             	
             } else {
             	scriptValue.append("RichFaces.Menu.updateItem(event,this");
-            	scriptValue.append(attrStr);
+            	if (!attrMap.isEmpty()) {
+            		scriptValue.append(',');
+                	scriptValue.append(ScriptUtils.toScript(attrMap));
+            	}
             	scriptValue.append(");");
                 scriptValue.append(getStringAttributeOrEmptyString(menuItem, HTML.onclick_ATTRIBUTE));
             }

Modified: branches/community/3.3.X/ui/menu-components/src/main/resources/org/richfaces/renderkit/html/scripts/menu.js
===================================================================
--- branches/community/3.3.X/ui/menu-components/src/main/resources/org/richfaces/renderkit/html/scripts/menu.js	2009-06-05 16:18:59 UTC (rev 14540)
+++ branches/community/3.3.X/ui/menu-components/src/main/resources/org/richfaces/renderkit/html/scripts/menu.js	2009-06-05 16:19:29 UTC (rev 14541)
@@ -1500,13 +1500,19 @@
 	}
 }
 
-RichFaces.Menu.submitForm = function (event, element, params, target, attr) {
-	RichFaces.Menu.updateItem(event, element, attr);
-	var form=A4J.findForm(element);
-	if (!params) params={};
-	if (!target) target='';
-	params[element.id+':hidden']=element.id;
-	Richfaces.jsFormSubmit(element.id,form.id,target,params);
+RichFaces.Menu.submitForm = function (event, element, options) {
+	if (!options) {
+		options = {};
+	}
+	
+	RichFaces.Menu.updateItem(event, element, options.a);
+	
+	var form = A4J.findForm(element);
+	var params = options.p || {};
+	var target = options.t || '';
+
+	params[element.id+':hidden'] = element.id;
+	Richfaces.jsFormSubmit(element.id, form.id, target, params);
 	return false;	
 }
 




More information about the richfaces-svn-commits mailing list