Created
March 3, 2011 15:54
-
-
Save justinrainbow/852981 to your computer and use it in GitHub Desktop.
an effort to make using the ui js modules easier {$yui->onAvailable("#myselector")->plug("flashpanel", $cfg)}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class YuiMethod | |
{ | |
protected $selector; | |
protected $helpers; | |
protected $code; | |
public function __construct($selector, $helper = null) | |
{ | |
$this->selector = $selector; | |
$this->helper = $helper; | |
$this->methods = new JavaScript_Method_Chain(); | |
$this->__call('ui', array($selector)); | |
} | |
public function plug($plugin, $config = array()) | |
{ | |
return $this->__call('plug', array($plugin, $config)); | |
} | |
public function __call($method, $args) | |
{ | |
$method = new JavaScript_Method($method); | |
$method->setArguments($args); | |
$this->methods->add($method); | |
return $this; | |
} | |
public function __toString() | |
{ | |
return (string) $this->methods; | |
} | |
} | |
class JavaScript_Collection extends ArrayIterator | |
{ | |
protected $methods = array(); | |
protected $separator = "."; | |
public function __toString() | |
{ | |
return implode($this->separator, iterator_to_array($this)); | |
} | |
} | |
class JavaScript_Methods extends JavaScript_Collection | |
{ | |
protected $separator = ";"; | |
public function add(JavaScript_Collection $chain) | |
{ | |
$this->append($chain); | |
} | |
} | |
class JavaScript_Method_Chain extends JavaScript_Collection | |
{ | |
public function add(JavaScript_Method $method) | |
{ | |
$this->append($method); | |
} | |
} | |
class JavaScript_Arguments extends JavaScript_Collection | |
{ | |
protected $separator = ", "; | |
public function current() | |
{ | |
$item = parent::current(); | |
if (!$item instanceof JavaScript_Argument) { | |
$item = new JavaScript_Argument($item); | |
$this->offsetSet($this->key(), $item); | |
} | |
return $item; | |
} | |
} | |
class JavaScript_Argument | |
{ | |
protected $value; | |
public function __construct($value) | |
{ | |
$this->value = $value; | |
} | |
public function __toString() | |
{ | |
$arg = $this->value; | |
if (is_numeric($arg)) { | |
return $arg; | |
} | |
if ($arg instanceof JavaScript_Function) { | |
return (string) $arg; | |
} | |
return json_encode($arg); | |
} | |
} | |
class JavaScript_Method | |
{ | |
protected $name; | |
protected $args; | |
protected $useConstructor; | |
public function __construct($name, $args = array()) | |
{ | |
$this->name = $name; | |
$this->args = new JavaScript_Arguments($args); | |
} | |
public function enableConstructor() | |
{ | |
$this->useConstructor = true; | |
} | |
public function disableConstructor() | |
{ | |
$this->useConstructor = false; | |
} | |
public function setArguments($args) | |
{ | |
$this->args = new JavaScript_Arguments($args); | |
} | |
public function addArgument($arg) | |
{ | |
$this->args->append($arg); | |
} | |
public function setArgument($index, $arg) | |
{ | |
$this->args[$index] = $arg; | |
} | |
public function __toString() | |
{ | |
$method = sprintf('%s(%s)', $this->name, $this->args); | |
if (true === $this->useConstructor) { | |
$method = sprintf('(new %s)', $method); | |
} | |
return $method; | |
} | |
} | |
class JavaScript_Function | |
{ | |
protected $name; | |
protected $content; | |
protected $anonymous = false; | |
public function __construct($name, $content = null) | |
{ | |
if (is_array($name)) { | |
$this->anonymous = true; | |
$this->content = new JavaScript_Methods($name); | |
} | |
} | |
public function __toString() | |
{ | |
$code = (string) $this->content; | |
if (!$this->anonymous) { | |
return sprintf('function %s() { %s }', $this->name, $code); | |
} | |
return sprintf('function() { %s }', $code); | |
} | |
} | |
$method = new JavaScript_Method('ui'); | |
$method->setArguments(array('#selector')); | |
echo $method . PHP_EOL; | |
$ui = new YuiMethod('#mydiv'); | |
echo $ui->plug('navbar', array('visible' => false))->render() . PHP_EOL; | |
$loader = new JavaScript_Method('YAHOO.util.YUILoader'); | |
$loader->enableConstructor(); | |
$loader->addArgument(array( | |
'require' => array('event','containercore'), | |
'onSuccess' => new JavaScript_Function(array( | |
new JavaScript_Method('alert', array('test')) | |
)) | |
)); | |
echo $loader . PHP_EOL; | |
$func = new JavaScript_Function(array( | |
new JavaScript_Method('alert', array('test')) | |
)); | |
echo $func . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment