Location: PHPKode > projects > So Easy PHP > SoEasy/EasyHtml/HtmlMaker/JavaScript/JavaScriptConcrete.class.php
<?php
include_once "HtmlMaker/JavaScriptTranslatable.interface.php";
include_once "HtmlMaker/HtmlComponent/HtmlElements/JavaScript.class.php";

/**
 * JavaScriptConcrete | Implementa a interface JavaScriptTranslatable
 * JavaScriptConcrete.class.php
 * @author	Ricardo <hide@address.com>
 * @since	22/10/2006
 * @package EasyHtml
 * @subpackage JavaScript
 */
class JavaScriptConcrete implements JavaScriptTranslatable {
	
	/**
	 * @var string nome da funcao javascript que ira executar a logica
	 * @access private
	 */
	private $functionName = null;
	
	/**
	 * @var string codigo javascript para a logica
	 * @access private
	 */
	private $javaScriptCode = null;
	
	/**
	 * @var array codigo javascript para uma funcao "secundária" 
	 */
	private $executableCode = array();
	
	/**
	 * Construtor
	 * @param string c├│digo javascript para ser executado
	 */
	function JavaScriptConcrete($code = null) {
		if (!is_null($code)) {
			$this->setJavaScriptCode($code);
		}
	}

	public function getJavaScript() {
		return $this->javaScriptCode;
	}
	
	public function getJavaScriptName() {
		if ( !strlen($this->functionName) ) {
			$this->functionName = "EasyJS_" . md5(microtime() . "," . time());
		}
		return $this->functionName;
	}
	
	public function getJavaScriptNameAsFunction() {
		return $this->getJavaScriptName() . "()";
	}
	
	public function setJavaScriptName($scriptName) {
		$this->functionName = $scriptName;
	}

	/**
	 * Retorna o codigo javascript ja como funcao
	 * @return JavaScript
	 * @access public
	 */
	public function getJavaScriptFunction() {
		$tag = new JavaScript();
		$tag->addCustomCode($this->getFunctionCode());
		return $tag;
	}
	
	public function getJavaScriptExecute() {
		$tag = new JavaScript();
		$code = $this->getFunctionCode();
		$code.= $this->getJavaScriptName() . "();";
		$tag->addCustomCode($code);
		return $tag;
	}

	/**
	 * Retorna o codigo JavaScript encapsulado pela funcao
	 * @return string
	 * @access private
	 */
	protected function getFunctionCode() {
		$secondaryFnCode = null;

		for($x=0; $x < count($this->executableCode); $x++) {
			$secondaryFnCode .= $this->executableCode[$x];
		}

		$return = $secondaryFnCode;

		#TODO: se houver parametros eles precisam ser inseridos aqui.
		$return.= "function " . $this->getJavaScriptName() ."() {";
		$return.= $this->javaScriptCode;
		$return.= "}";
		return $return;
	}
	
	/**
	 * Define o codigo javascript
	 * @param string codigo javascript
	 * @return void
	 * @access protected
	 */
	protected function setJavaScriptCode($code) { 
		$this->javaScriptCode .= $code;
	}
	
	/**
	 * Retorna o "nome java script" de um HtmlElement
	 * @param HtmlElement
	 * @return string
	 * @access public
	 */
	public function getJavaScriptFieldName(HtmlElement $e) {
		return "document.getElementById('". $e->getElementId() ."')";
	}
	
	//	Esse cara pode receber um objeto list com HtmlComponent, strings, int...
	//	Tem que saber reconhecer esses fulanos e, na hora de gerar o codigo javascript fazer as
	//	chamadas getHtml devidas (quando for o caso)
	public function setJavaScriptParams($param) {
		// nao sei se eh util
	}
	
	public function addExecutable($code) {
		$this->executableCode[] = $code;
	}
}
?>
Return current item: So Easy PHP