[richfaces-svn-commits] JBoss Rich Faces SVN: r5462 - branches/3.1.x/framework/impl/src/main/javascript/ajaxjsf.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Thu Jan 17 16:21:22 EST 2008


Author: alexsmirnov
Date: 2008-01-17 16:21:21 -0500 (Thu, 17 Jan 2008)
New Revision: 5462

Modified:
   branches/3.1.x/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js
Log:
Merge changes from trunk

Modified: branches/3.1.x/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js
===================================================================
--- branches/3.1.x/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js	2008-01-17 20:12:54 UTC (rev 5461)
+++ branches/3.1.x/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js	2008-01-17 21:21:21 UTC (rev 5462)
@@ -89,7 +89,8 @@
 		      			}
 		      			if(_this.onready){
 		      				_this.onready(_this);
-		      			}
+		      			}      			
+		      			
 		      		} else {
 		      			_this._errorMessage = "Reqest error, status : "+requestStatus +" " + requestStatusText ;
 		      			LOG.error(_this._errorMessage);
@@ -97,10 +98,11 @@
 		      				_this._onerror(_this,requestStatus,_this._errorMessage);
 		      			}
 		      		}
-		      		if(_this.onfinish){
+		      		if (_this.onfinish)
+		      		{
 		      			_this.onfinish(_this);
 		      		}
-			      	_this = undefined;
+					_this = undefined;		      		
 		      	}
 	}; //this._onReady;
     try{
@@ -233,8 +235,12 @@
 			LOG.debug("selectNodes found "+elements.length);
 	    }
 	    catch (ex) {
-			elements = element.getElementsByTagName(elementname);
-			LOG.debug("getElementsByTagName found "+elements.length);	    	
+	    	try {
+				elements = element.getElementsByTagName(elementname);
+				LOG.debug("getElementsByTagName found "+elements.length);	    	
+	    	} catch(nf){
+				LOG.debug("getElementsByTagName found no elements, "+nf.Message);	    		    		
+	    	}
 	    }
 //	    return document.getElementsByTagName(tagName);
 //		elements = element.getElementsByTagNameNS("http://www.w3.org/1999/xhtml",elementname);
@@ -284,10 +290,11 @@
 		return data;
 	},
 	
-	evalScripts : function(node){
+	evalScripts : function(node, isLast){
 			var newscripts = this.getElementsByTagName("script",node);
 	        LOG.debug("Scripts in updated part count : " + newscripts.length);
 			if( newscripts.length > 0 ){
+			  var _this = this;
 		      window.setTimeout(function() {
 		        for (var i = 0; i < newscripts.length; i++){
 		          var newscript = Sarissa.getText( newscripts[i],true ) ; // TODO - Mozilla disable innerHTML in XML page ..."";
@@ -303,14 +310,39 @@
 		          	  }
 			    }
 			    newscripts = null;
+			    if (isLast)
+			    {
+			    	if(_this.onfinish){
+					_this.onfinish(_this);
+					_this=undefined;
+					}
+			    }
 			  },50);
+		    } else
+		    {
+			    if (isLast)
+			    {
+			    	if(this.onfinish){
+					this.onfinish(this);
+					}
+			    }
 		    }
+		    
 		
 	},
+	
+	beforeUpdatePagePart: function (id)
+	{
+		var oldnode = window.document.getElementById(id);
+		if ( oldnode  ) {
+			
+		}		
+	},
+	
 	/**
 	 * Update DOM element with given ID by element with same ID in parsed responseXML
 	 */
-	updatePagePart : function(id){
+	updatePagePart : function(id, isLast){
 		var newnode = this.getElementById(id);
 		if( ! newnode ) { LOG.error("New node for ID "+id+" is not present in response");return;}
 		var oldnode = window.document.getElementById(id);
@@ -343,34 +375,83 @@
 	// re-execute all script fragments in imported subtree...
 	// TODO - opera 8 run scripts at replace content stage.
 			if(!A4J.AJAX._scriptEvaluated){
-				this.evalScripts(newnode);
+				this.evalScripts(newnode, isLast);
 			}
 	        LOG.debug("Update part of page for Id: "+id + " successful");
 		} else {
 			LOG.warn("Node for replace by response with id "+id+" not found in document");
 		}
 		
+		if (A4J.AJAX._scriptEvaluated && isLast)
+	    {
+	    	if(this.onfinish){
+			this.onfinish(this);
+			}
+	    }
+		
 	},
 	
 	appendNewHeadElements : function(){
-        	  // Append scripts and styles to head, if not presented in page before.
-        	  this._appendNewElements("script","src",["type","language","charset"]);
-        	  this._appendNewElements("link","href",["type","rel","rev","media"]);		
+        // Append scripts and styles to head, if not presented in page before.
+        this._appendNewElements("script","src",null,null,["type","language","charset"]);
+        
+        var _this = this;
+        this._appendNewElements("link","href","class",["component","user"],["type","rev","media"],{"class": "className"},
+        		function (element, script) {
+        			//IE requires to re-set rel or href after insertion to initialize correctly
+        			//see http://jira.jboss.com/jira/browse/RF-1627#action_12394642
+        			_this._copyAttribute(element,script,"rel");
+        		}
+        );		
 	}, 
 	
-	_appendNewElements : function(tag,href,attributes){
+	_appendNewElements : function(tag,href,role,roles,attributes,mappings,callback){
+			  var head = document.getElementsByTagName("head")[0]||document.documentElement;
 		      var newscripts = this.getElementsByTagName(tag);
         	  var oldscripts = document.getElementsByTagName(tag);
-        	  var head = document.getElementsByTagName("head")[0]||document.documentElement;
+        	  var mappedRole = (mappings && mappings[role]) || role;
+        	  
+        	  var roleAnchors = {};
+			  if (roles) {
+	        	  var i = 0;
+	        	  
+	        	  for(var j = 0; j < oldscripts.length; j++){
+					  var oldscript = oldscripts[j];
+					  var scriptRole = oldscript[mappedRole];
+	        	  
+					  for ( ; i < roles.length && roles[i] != scriptRole; i++) {
+						  roleAnchors[roles[i]] = oldscript;
+					  }
+					  
+					  if (i == roles.length) {
+						  break;
+					  }
+	        	  }
+			  }
+        	  
         	  for(var i=0 ; i<newscripts.length;i++){
         	  	 var element = newscripts[i];
         	  	 var src = element.getAttribute(href);
+        	  	 var elementRole;
+        	  	 
+        	  	 if (roles) {
+        	  		 elementRole = element.getAttribute(role);
+        	  	 }
+        	  	 
         	  	 if(src){
         	  	 	var exist = false;
         	  	 	LOG.debug("<"+tag+"> in response with src="+src);
         	  				for(var j = 0 ; j < oldscripts.length; j++){
         	  					if(this._noSessionHref(src) == this._noSessionHref(oldscripts[j].getAttribute(href))){
         	  						LOG.debug("Such element exist in document");
+
+        	  						if (role) {
+        	  							var oldRole = oldscripts[j][mappedRole];
+        	  							if ((!elementRole ^ !oldRole) || (elementRole && oldRole && elementRole != oldRole)) {
+                	  						LOG.warn("Roles are different");
+        	  							}
+        	  						}
+        	  						
         	  						exist = true;
         	  						break;
         	  					}
@@ -382,8 +463,23 @@
         	  		 	for(var j = 0 ; j < attributes.length; j++){
         	  		 		this._copyAttribute(element,script,attributes[j]);
         	  		 	}
+        	  		 	
+        	  		 	if (elementRole) {
+        	  		 		script[mappedRole] = elementRole;
+        	  		 	}
+
         	  		 	LOG.debug("append element to document");
-        	  		 	head.appendChild(script);
+        	  		 	
+        	  		 	var anchor = roleAnchors[elementRole];
+        	  		 	if (anchor && anchor.parentNode) {
+            	  		 	anchor.parentNode.insertBefore(script, anchor);
+        	  		 	} else {
+            	  		 	head.appendChild(script);
+        	  		 	}
+        	  		 	
+        	  		 	if (callback) {
+        	  		 		callback(element,script);
+        	  		 	}
         	  		 }     	  	 	
         	  	 }
         	  }
@@ -447,7 +543,7 @@
 	}
 	A4J.AJAX._pollers[options.pollId] = window.setTimeout(function(){
 		A4J.AJAX._pollers[options.pollId]=undefined;
-		if((typeof(options.onsubmit) == 'function') && (!options.onsubmit())){
+		if((typeof(options.onsubmit) == 'function') && (options.onsubmit()==false)){
 			// Onsubmit disable current poll, start next interval.
 			A4J.AJAX.Poll(containerId,form,options);			
 		} else {
@@ -479,18 +575,16 @@
 		      	if (request.readyState == 4  ) {
 		      		try {
 		      		if(request.status == 200){
-//		      			A4J.AJAX._pollers[options.pushId]=undefined;
 				        A4J.AJAX.SubmitRequest(containerId,form||options.dummyForm,null,options);
 		      		}
 		      		} catch(e){
 		      			// Network error.
 		      		}
+		      		// Clear variables.
+		      		request=null;
+					A4J.AJAX._pollers[options.pushId] = null;
 		      		// Re-send request.
 		      		A4J.AJAX.Push( containerId, form, options );
-		      		request=null;
-//					A4J.AJAX._pollers[options.pushId] = window.setTimeout(function(){
-//						A4J.AJAX.SendPush( request,options );
-//					},options.pushinterval);
 		      	}
 		}
 		A4J.AJAX.SendPush( request,options );
@@ -504,13 +598,13 @@
 		if(options.timeout){
 			request.setRequestHeader( "Timeout", options.timeout);			
 		}
-		request.send();	
+		request.send(null);	
 }
 
 A4J.AJAX.StopPush =  function( Id ) {
 	if(A4J.AJAX._pollers[Id]){
 		window.clearTimeout(A4J.AJAX._pollers[Id]);
-		A4J.AJAX._pollers[Id] = undefined;
+		A4J.AJAX._pollers[Id] = null;
 	}
 };
 
@@ -720,7 +814,7 @@
 	        	  	req.appendNewHeadElements();
 					for ( var k =0; k < options.affected.length ; k++ ) {
 						LOG.debug("Update page part from call parameter for ID " + options.affected[k]);
-						req.updatePagePart(options.affected[k]);
+						req.updatePagePart(options.affected[k], k==childs.length-1);
 					};
 		// if resopnce contains element with ID "ajax:update" get id's from
 		// child text element . like :
@@ -737,7 +831,7 @@
 	        	for ( var k=0 ; k < childs.length ; k++ ) {
 	        		var id = childs[k];
 	        		LOG.debug("Attempt to update part of page for Id: "+id);
-					if ( id ) {req.updatePagePart(id);};
+					if ( id ) {req.updatePagePart(id, k==childs.length-1);};
 				};
         	  } else {
         			// if none of above - error ?
@@ -894,22 +988,20 @@
    	    var options = request.options;
 	     // we can set listener for complete request - for example,
 	     // it can shedule next request for update page.
-	     var oncomplete = request.getElementById('org.ajax4jsf.oncomplete');
-	     if(oncomplete) {
+	     var oncomp = request.getElementById('org.ajax4jsf.oncomplete');
+	     if(oncomp) {
    			LOG.debug( "Call request oncomplete function after processing updates" );
    			window.setTimeout(function(){
    				var event = request.domEvt;
    				var data = request.getJSON('_ajax:data');
    				try {
-	   				var newscript = Sarissa.getText(oncomplete,true);
-					if (!window.data) {
-						window.data = data;
-					} else {
-						for (property in data) {
-							window.data[property] = data[property];
-						}
-					}  			
-   	      				window.eval(newscript);
+	   				var newscript = Sarissa.getText(oncomp,true);
+	   				var oncomplete = new Function("request","event","data",newscript);
+	   				var target = null;
+					if (event) {
+						 target = event.target ? event.target : event.srcElement;
+					};
+					oncomplete.call(target,request,event,data);					
    				} catch(e){
    					LOG.error('Error evaluate oncomplete function '+e.Message);
    				}




More information about the richfaces-svn-commits mailing list