Login   Register  
PHP Classes
elePHPant
Icontem

File: js/tkajax.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Adolfo Eloy  >  DABAJAX Toolkit  >  js/tkajax.js  >  Download  
File: js/tkajax.js
Role: Auxiliary data
Content type: text/plain
Description: the core for ajax toolkit
Class: DABAJAX Toolkit
Auto-complete fields with Oracle database data
Author: By
Last change: is was wrong script
Date: 2009-10-13 08:40
Size: 8,323 bytes
 

Contents

Class file image Download
var _ajaxobject         = "";
var _ajaxresultjsontext = "";
var _ajaxcallbackresult = "";
var reqHttp = false;

if (!window.TKAjax)	{var TKAjax 		= {}};
if (!window.TKAjaxData) {var TKAjaxData = {}};
if (!window.processingIcon) { var processingIcon = null; };
if (!window.starter) { var starter = null; };

function do_submit( sform )
{
    var vform = document.getElementById( sform );
    vform.submit();
}

TKAjaxData = function( value , data )
{
    this.value = value;
    this.data = data;
};

/****************************************************************************************************************************************************************************************************
  * object Widget with the AJAX implementations
  * created at 2007
  * changes:
  *   -> 20090604 - created makePostFields
  *               - added dependency (phpjs)
  *
  * @author Adolfo Eloy Nascimento
***************************************************************************************************************************************************************************************************/
TKAjax = function()
{
    //var req = false;			//object to be an XMLHttpRequest object 
    this.data = "";				// the data to send to server
    this.serverScript = ""; 	// script to be executed on Server
    this.processReq = null;
    
    this.getReq = function()
    {
       return reqHttp;
    };
    
    /**
      * function to set the icon element to show when processing sth
      * @param string    vElement   element id of an object
    */
    this.setProcessingIcon = function( vElement )
    {
       processingIcon = vElement;
    };
    
    this.setStarter = function( v1 )
    {
		starter = v1;    
    };
    
    /**
       * function to set the json format data
       * @param variant    vData     any data to be stringified by json object
    */    
    this.setJSONData = function( vData )
    {
    	this.data = JSON.stringify( vData );
    };
    
    /**
       * function to set the serverScript attribute
       * @param   string   strServerScript    script to be executed on Server
    */
    this.setServerScript = function( strServerScript )
    {
    	this.serverScript = strServerScript;
    };

    /**
      * specify a callback to treat the ajax result instead of use the default ProcessReqChange
      * @param  string     callback
    */
    this.setProcessReq = function( callback )
    {
        this.processReq = callback;
    };
    
	/**
	   * function to send the data to server asyncrhonously
	 */
    this.sendDataToServer = function( strOptions )
    {
		// local variables 
		var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
		
		// some validations
		if ( this.serverScript.length ==0  ) 
		{
			alert( "Server Script Property was not set" );
			return false;
		}

		try
		{
		
			if ( window.XMLHttpRequest ) 
			{	
		
			   // searches for a native  XMLHttpRequest object 
				try 
				{
					reqHttp = new XMLHttpRequest();
				} 
				catch (e) 
				{
					reqHttp = false;
				}
				
			} 
			else 
			{
		
				// searches for  IE/Windows ActiveX 
				try 
				{			
					reqHttp = new ActiveXObject("Msxml2.XMLHTTP");
					
				} 
				catch (e) 
				{
				
					try 
					{
						reqHttp = new ActiveXObject("Microsoft.XMLHTTP");
					} 
					catch (e) 
					{
						reqHttp = false;
					}
					
				}
		
			}
		} catch (e) {
			reqHttp = false;
		}


		if ( reqHttp ) 
		{
			
			// setting needed properties for XMLHttpRequest object
			if ( window.navigator.appName != "Microsoft Internet Explorer") 
			{
		        reqHttp.onload = null;
		    }

		    if (processingIcon != null) 
		       processingIcon.style.display = "inline";
		    
		    if (starter != null)
		       starter.disabled = true;

		    if (this.processReq != null) {
		       reqHttp.onreadystatechange 	= this.processReq;
		    } else {
			   reqHttp.onreadystatechange 	= this.processReqChange;
			}

			reqHttp.open( "POST", this.serverScript , true );
	      	reqHttp.setRequestHeader( "Content-type", contentType );
	        
	        if ( strOptions.length ) {	        		        	
	        	reqHttp.send( "jsonstring=" + this.data + "&" + strOptions );	
	        } else {
	        	reqHttp.send( "jsonstring=" + this.data );
	        }
			
		} 
		else 
		{		   
		   alert( "O Browser Utilizado, Nao Esta Permitindo Trabalhar Com XMLHttpRequest" );		   
		}
    	
    };

	/**
	  * this method build an array with TKAjaxData with form element values
	*/
    this.makePostFields = function( formId ) {
    
	  var i = 0;
	  var k = 0;
	  var form = document.getElementById( formId );
	  var arr_dados     = new Array();
	  var alreadyLoaded = new Array();
	  
	  // pass through all form elements
	  for(i = 0; i < form.elements.length; i++) {
	      
	      // the element must have a name and must be enabled
	  	  if (form.elements[i].name.length > 0 && 
	  	      !form.elements[i].disabled) {
	  	  
	  	      // radio button treatment
		  	  var el = form.elements[i];
		  	  if (el.type == 'radio' && el.name.length > 0) {
		  	    var radio = document.getElementsByName(el.name);
		  	    
				for (k = 0; k < radio.length; k++) {
					
					if (radio[k].checked && !in_array(el.name, alreadyLoaded)) {
			          dados = new TKAjaxData(el.name, radio[k].value );
			          arr_dados.push(dados);
			          alreadyLoaded.push(el.name);
					}
				}
		      }
		      
		      // other elements
		      if (form.elements[i].type == 'hidden' ||
		          form.elements[i].type == 'text' || 
		          form.elements[i].type == 'select-one' || 
		          form.elements[i].type == 'textarea' ) {
		      	  
		          dados = new TKAjaxData(form.elements[i].name, form.elements[i].value );
		          arr_dados.push(dados);
		      }
		      
		      
		   }
	  }
	  
	  return arr_dados;
    };
    
	/**
	  * function to proccess the data received by XMLHttpRequest, and request the data from server
	*/
	this.processReqChange = function()
	{
		var strjson = "";
		
    	// apenas quando o estado for "completado"
    	if ( reqHttp.readyState == 4 ) 
    	{
        	// apenas se o servidor retornar "OK"
        	if ( reqHttp.status == 200 ) 
        	{
        	
            	// retornado nela, como texto HTML
            	strjson = reqHttp.responseText;
            	alert( strjson );
            	
            	if (processingIcon != null) 
            		processingIcon.style.display = "none";
            		
            	if (starter != null)
		           starter.disabled = false;
            	
            } 
            else 
            {
            	alert( "A Consulta Nao Retornou Nada" );
            	if (processingIcon != null) 
            		processingIcon.style.display = "none";
            	if (starter != null)
		           starter.disabled = false;
            }            
        }
	};
};

AjaxResultManager = function()
{
	   
   this.treatResults = function() {
	     
      var reqHttp = _ajaxobject.getReq();
      if ( reqHttp.readyState == 4 ) 
      {
         if ( reqHttp.status == 200 ) 
         {
            strjson = reqHttp.responseText;
            	
            // put the result into the global variable
            _ajaxresultjsontext = JSON.parse( strjson );

            _ajaxcallbackresult();
            	
         } 
         else 
         {
       	   alert( "There is no results." );
         }            
      }
   };
	   
	this.getCallback = function() {
	      return this.treatResults;
	}
};
	
AjaxFilter = function() 
{
	   this.NUMERIC = 0;
	   this.ALPHA   = 1;
	   this.DATE    = 2;
       this.CONTAINS = 3;
       this.STARTS_WITH = 4;
       this.ENDS_WITH = 5;

	   this.filters = new Array();
	   this.results = new Array();
	   this.sql = "";
       this.rownum = 1;

      this.setRownum = function( rownum ) {
         this.rownum = rownum;      
      };
            	   
	   this.addFilter = function( id, value, type ) {
	      var item = "";
	      item = item + id + ';@;' + value + ';@;' + type;
	      this.filters.push( item );
	   };
	   
	   this.addResult = function( id ) {
	      this.results.push( id );
	   };
	   
	   this.setSql = function( sql ) {
	      this.sql = sql;
	   };
	   
	   this.getResults = function() {
	      return this.results;
	   };
};