Login   Register  
PHP Classes
elePHPant
Icontem

File: lib/rspacontroller.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of sunish  >  RSPA  >  lib/rspacontroller.js  >  Download  
File: lib/rspacontroller.js
Role: Auxiliary data
Content type: text/plain
Description: Supporting javascript
Class: RSPA
Process browser events on the server using AJAX
Author: By
Last change: Change for PHP 4 and enhancements
Date: 2006-07-20 11:05
Size: 12,999 bytes
 

Contents

Class file image Download
/**
  *
  *
  *  Author : Sunish Kumar M.V
  *
  *  This is licenced under LGPL licence.
  * 
  **/
  
var RSPAStatic={
	
	options: new Array(),
	__labels :  new Array(),
	__labelsrw:  new Array(),
	
	options : {
      widget:       document,
      parameters:   new Array(),
      controller: "rspa/framework/Controller.php",
      debug: false,
      loadStatus: "Loading...",
      statusPane: '__RSPA__loadStatus',
      oncomplete: null
      
    },
	
    showStatus:  function(){
  	 
  	    $(RSPAStatic.options.statusPane).innerHTML =this.options.loadStatus;
		if(RSPAStatic.options.statusPane == '__RSPA__loadStatus') {
			$('__RSPA__loadStatus').style.left = 10;
	    	$('__RSPA__loadStatus').style.top = 10;
			Effect.Appear('__RSPA__loadStatus',{duration: 0.1});
  	    }
  	 
  	},
  	
  setOptions: function(options) {
     
    Object.extend(RSPAStatic.options, options || {});
   
  },
   
  	showDebugWindow: function(txt){
  	 
  	   if(RSPAStatic.options.debug) {
  	   	
  	    	$('__RSPA__DebugWindow').innerHTML = "Debug Info: <textarea  style=\"height:138px; width:590px;background-color:#000000;color:#ffffff;\" >"+txt+"</textarea>";
  	        Element.show('__RSPA__DebugWindow');
  	   
  	   }
  	   	 
  	},
  	
    completed: function( originalRequest){
		if(RSPAStatic.options.statusPane == '__RSPA__loadStatus') {
		  setTimeout("Effect.DropOut('__RSPA__loadStatus')", 250);
		}
		
		RSPAStatic.showDebugWindow(originalRequest.responseText);
		
		
		var result = originalRequest.responseXML;
		
		var components = result.getElementsByTagName("component");
		
		for( var i=0; i< components.length;i++) {
			var nam  = components[i].getAttribute("name");
			if(components[i].getAttribute("value")!= null ) {
				$(nam).value = components[i].getAttribute("value");
			}
			if(components[i].getAttribute("disabled")!= null ) {
				$(nam).disabled = (components[i].getAttribute("disabled")=="true")?true:false;
			}
			/*if(components[i].getAttribute("style")!= null ) {
				$(nam).style =  components[i].getAttribute("style");
			}
			*/
			//$(components[i].getAttribute("name")).value=components[i].firstChild.nodeValue ;
			
			
		}
		
		var selects = result.getElementsByTagName("select");	
		
		for( var i=0; i< selects.length;i++) {
			
		    var opts = selects[i].getElementsByTagName("option");
		    var elem = $(selects[i].getAttribute("name"));
		    var selList = selects[i].getElementsByTagName("selected");
		   
			if(selects[i].getAttribute("disabled")!= null ) {
			   
				elem.disabled = (selects[i].getAttribute("disabled")=="true")?true:false;
			}
		    elem.options.length=0;
		    
			for( var j=0; j< opts.length;j++) {
				
				var codeObj = "";
				if( opts[j].getAttribute("value") != null) {
					 codeObj = opts[j].getAttribute("value") ;
				}
				
				var valueObj = "";
				if(opts[j].firstChild != null) {
					 valueObj = opts[j].firstChild.nodeValue ;
				}
				
				//alert(codeObj);
				
				elem[elem.options.length] = new Option(valueObj, codeObj);
				/*if(RSPA.isInSelected(selList,codeObj)){
					elem[elem.options.length].selected = true;
				}*/
				
		    }
		   
		   RSPAStatic.setSelections(selList,elem);
		   
		   
		   		   
		}
		
		var checks = result.getElementsByTagName("checkbox");	
		for( var i=0; i< checks.length;i++) {
			var elem = $(checks[i].getAttribute("name"));
		    var checked = checks[i].getElementsByTagName("checked")[0].firstChild.nodeValue;
		 	if(checked=="true") {
		 		elem.checked=true;
		 	}	
		 	else{
		 		elem.checked=false;
		 	}
		 	
		 	  
			if(checks[i].getAttribute("disabled")!= null ) {
				elem.disabled = (checks[i].getAttribute("disabled")=="true")?true:false;
			}
		}
		
		var radios = result.getElementsByTagName("radio");	
		for( var i=0; i< radios.length;i++) {
			var elem = $(radios[i].getAttribute("name"));
		    var checked = radios[i].getElementsByTagName("checked")[0].firstChild.nodeValue;
		 	if(checked=="true") {
		 		elem.checked=true;
		 	}	
		 	else{
		 		elem.checked=false;
		 	}
		 	
		 	if(radios[i].getAttribute("disabled")!= null ) {
				elem.disabled = (radios[i].getAttribute("disabled")=="true")?true:false;
			}
		}
		
		var components = result.getElementsByTagName("label");
		
		for( var i=0; i< components.length;i++) {
			$(components[i].getAttribute("name")).innerHTML=components[i].firstChild.nodeValue ;
		}
				
		components = result.getElementsByTagName("redirect");
		
		if(components != null && components.length>0) {
			var redir = components[0].firstChild.nodeValue ;
			if(redir != null) {
				//window.location.replace(redir);
				window.location.href= redir;
			}
			
		}
		
	
		var js = result.getElementsByTagName("clientJs");
		
		if(js != null) {
			var js2 = js[0].firstChild.nodeValue ;
			
			if(js2 != null) {
			  //eval('alert("hello");');
			  eval(js2);
			}
			
		}		
		
		if(RSPAStatic.options.oncomplete != null){
			RSPAStatic.options.oncomplete();
		}
		
		$(RSPAStatic.options.statusPane).innerHTML = "&nbsp;";
		
		
		
						  
	} ,
	
	setSelections : function(selectList,elm) {
		for( var i=0; i< selectList.length;i++) {
			var sel = selectList[i].firstChild.nodeValue;
			
			for(var j=0;j<elm.options.length;j++) {
				if(elm.options[j].value == sel) {
					elm.options[j].selected = true;
				}
			}
		}	  
		return false;
	} , 
	
	
		
   parseElementsByRSPAId: function(parentElement) {
   
       this.__labels = new Array();
       this.__labelsrw = new Array();
      
      var children = ($(parentElement) || document.body).getElementsByTagName('div');
      for(var i=0;i<children.length; i++) {
      	if(children[i].getAttribute("RSPAID") =="label") {
      		RSPAStatic.__labels.push(children[i]);
      	
      	}
      	else if(children[i].getAttribute("RSPAID") =="labelrw") {
      		RSPAStatic.__labelsrw.push(children[i]);
      	}
      }
   
   },
   
   registerAutocomplete: function() {
  		//RSPAStatic.showDebugWindow();
   		
   		document.write("<style> div.auto_complete ul { border: 1px solid #888;	margin: 0;	padding: 0;	width: 100%;	list-style-type: none;} div.auto_complete ul li { margin: 0; padding: 3px;}div.auto_complete ul li.selected { background-color: #ffb; }div.auto_complete ul strong.highlight { 	color: #800; 	margin: 0;	padding: 0;}</style>");  
   		document.write("<div id=\"__RSPA_autocomplete_choices\" class=\"auto_complete\" ></div>\n");
 		
 		var elms = document.getElementsByTagName("input");
   		 
		for(var i=0;i<elms.length;i++) {
		   
		    if(elms[i].getAttribute("RSPA_autocomplete") ) {
		      
		        var clsMthd = elms[i].getAttribute("RSPA_autocomplete").split(".");
		        //alert(elms[i].id+"  \n   "+ "\n"+"autocomplete___"+elms[i].name+"\n"+RSPAStatic.options.controller+"?__autocomplete=yes&__class="+clsMthd[0]+"&__methord="+clsMthd[1]+"&autocomplete___"+elms[i].name+"="+elms[i].value);
		    	new Ajax.Autocompleter(elms[i].id, "__RSPA_autocomplete_choices", RSPAStatic.options.controller+"?__autocomplete=yes&__class="+clsMthd[0]+"&__methord="+clsMthd[1], {paramName: "autocomplete___"+elms[i].name});
		    	//window.open(RSPAStatic.options.controller+"?__autocomplete=yes&__class="+clsMthd[0]+"&__methord="+clsMthd[1]+"&autocomplete___"+elms[i].name+"="+elms[i].value);
		    }
  			
   		}
   		
   }
   
   
}




var RSPA = {
    Version: '0.1'
   // prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1]),
  	
 };


RSPA.Autocompleter = Class.create();

RSPA.Autocompleter.prototype = {
	
	initialize: function(elem,clss, options) {
			
			this.setOptions(options);
			this.registerAutocomplete(elem,clss);
		    },

	setOptions: function(options) {
			this.options = {
			debug: false,
			widget:       document,
			parameters:   new Array(),
			controller: "rspa/framework/Controller.php"
			}
			Object.extend(this.options, options || {});
			
		    },
	registerAutocomplete: function(elem,clss) {
			
			document.write("<div id=\"__RSPA_autocomplete_choices"+elem+"\" class=\"auto_complete\" ></div>\n");

			var clsMthd = clss.split(".");

			new Ajax.Autocompleter(elem, "__RSPA_autocomplete_choices"+elem, this.options.controller+"?__autocomplete=yes&__class="+clsMthd[0]+"&__methord="+clsMthd[1], {paramName: "autocomplete___"+$(elem).name});
			
			
		
	}

};



RSPA.Execute = Class.create();

RSPA.Execute.prototype = {
  
  initialize: function(clss, options) {
    this.transport = Ajax.getTransport();
    this.setOptions(options);
    this.clssMethd = clss;
    this.request();
    
  },
  setOptions: function(options) {
    this.options = {
      widget:       document,
      parameters:   new Array(),
      controller: "rspa/framework/Controller.php",
      debug: false,
      loadStatus: "Loading...",
      statusPane: '__RSPA__loadStatus',
      oncomplete: null
    }
    Object.extend(this.options, options || {});
    RSPAStatic.setOptions(options);
  },
   
  request: function(){
  		
  		var clss ="", mthd="";
	 	var i;
	 	for(i=0;i<this.clssMethd.length;i++) {
	 		
	 		if(this.clssMethd.charAt(i) ==".") {
	 		 
	 		 break;
	 		}
	 		clss += this.clssMethd.charAt(i);
	 	}
	 	
	 	//i++;
	 	while(i<this.clssMethd.length) {
	 		i++;
	 		if(this.clssMethd.charAt(i) =="(") {
	 		 
	 		 break;
	 		}
	 		mthd += this.clssMethd.charAt(i);
	 		
	 	}
	 	
	 	var doc;
	 	if(this.options.widget == document) {	 	
	 		doc = document
	 	}
	 	else{
	 		doc = ($(this.options.widget) || document.body);
	 	}
	 
	 	RSPAStatic.showStatus();
	 	
	 	
 		
	   //var url = "framework/Controller.php";
	   // var pars = Form.serialize(obj);
	 	var pars = "__class="+clss+"&__methord="+mthd+"&";
	 	
	 	for (i = 0;i<this.options.parameters.length;i++) {
	 		pars +="__parameters[]="+this.options.parameters[i]+"&";
	 	
	 	}
	 	
	 	/*if(this.options.parameters != "") {
	 		pars += "__parameters="+this.options.parameters+"&";
	 	}*/
	 	pars += this.serialize(doc);
	 	
	 	
	 	
	 //	var controller = this.options.controller;
	 	
	 //	var compltd = this.completed;
	 	//alert(compltd);
	 	var myAjax = new Ajax.Request( RSPAStatic.options.controller,
	    	{method: 'post', parameters: pars, onComplete:RSPAStatic.completed }
		);
		//alert(pars);
		//window.open(RSPAStatic.options.controller + "?"+pars);
  		
   },
  
  serialize: function(parentElement) {
		
		var doc = ($(parentElement) || document.body);
		
		var pars ="", pars1;
		var elms = doc.getElementsByTagName("input");
		for(var i=0;i<elms.length;i++) {
  			pars1 = this.getElementNameValue(elms[i])+"&";
  			if(! pars1 == false) {
  			  pars+=pars1;
  			}
   		}
   		
   		elms = doc.getElementsByTagName("select");
   		
   		for(var i=0;i<elms.length;i++) {
   		   
  			pars1 = this.getElementNameValue(elms[i])+"&";
  			if(! pars1 == false) {
  			  pars+=pars1;
  			}
   		}
		
		elms = doc.getElementsByTagName("textarea");
		for(var i=0;i<elms.length;i++) {
  			pars1 = this.getElementNameValue(elms[i])+"&";
  			if(! pars1 == false) {
  			  pars+=pars1;
  			}
   		}
   		
   		RSPAStatic.parseElementsByRSPAId(doc);
   		
   		for(var i=0;i < RSPAStatic.__labels.length;i++) {
   			var pars1= "label___"+RSPAStatic.__labels[i].id+"=&";
   			pars+=pars1;
   		}
   		
   		for(var i=0;i < RSPAStatic.__labelsrw.length;i++) {
   			var pars1= "label___"+RSPAStatic.__labelsrw[i].id+"=" +escape(RSPAStatic.__labelsrw[i].innerHTML)+"&";
   			pars+=pars1;
   		}
   		
		return pars;
		
	},
  
  	getElementNameValue: function(element) {
	    
	    if(element.tagName.toLowerCase()=="select") {
	       
	        
	        var sels ="" ;
	        for(var i=0;i<element.options.length;i++) {
	        	if(element.options[i].selected){
	        	    if(i > 0) sels +="&";
	        		sels +=  element.type.toLowerCase() +"___"+element.id+"[]="+element.options[i].value;
	        	}
	        }
	        
	        if(sels =="") {
	        	return element.type.toLowerCase() +"___"+element.id+"[]=";
	        }
	    	return sels;
	    	
	    }
	    if(element.tagName.toLowerCase()=="textarea") {
	    	return "textarea___"+element.id+"="+escape(element.value);
	    
	    }
	    
		switch (element.type.toLowerCase()) {
		      case 'submit':
		      case 'hidden':
		      case 'password':
		      case 'text':
		      case 'button':
		      
		        return element.type.toLowerCase() +"___"+element.id+"="+element.value;
		      case 'checkbox':
		      case 'radio':
		        return element.type.toLowerCase() +"___"+element.id+"="+element.checked;
		      
		    }
		    return false;
	}
  	
}