phplibrary/core/widgets/widget.php
2016-07-14 16:29:26 +03:00

87 lines
No EOL
2.2 KiB
PHP

<?php
require_once 'core/json.php';
function forceUrl($name)
{
if(is_callable($name)) {
return call_user_func($name);
}
return $name;
}
/**
* Класс для генерации и управления активными компонентами страницы
* Компонент состоит из следующих частей
* PHP - Управление компонентом (Генерация, Инициализация, Обработка событий)
* HTML - Необходимые шаблоны
* CSS - Стили
* Javascript - Клиентская часть управления компонентом
*/
class Widget
{
private static $prefix = "_g";
private static $idx = 0;
private $id;
public $data = array();
public $view;
protected $template;
public function __construct()
{
$this->id = self::$idx ++;
$this->setUp();
}
function setUp()
{
}
public function getName()
{
return self::$prefix . intval($this->id);
}
/**
* Генерация кода инициализации компонента на стороне клиента
*/
public function getCodeBefore()
{
$result =
// "alert('".$this->getName()."');" .
"var " . $this->getName() . " = new " . get_class($this) . "("
. json::encode($this->data) . ");";
return $result;
}
public function setData($name, $value)
{
$this->data[$name] = $value;
}
public function getCodeAfter()
{
return $this->getName() . ".appendTo(document.getElementById('" . $this->getName() . "'));\n";
}
public function postMake()
{
}
/**
* Генерация компонента
*/
function make(Controller $parent)
{
$this->view = $parent->getView($this->template); // Controller
$this->view->index = $this->getName();
foreach ($this->data as $name => $value) {
$this->view->set($name, $value);
}
$this->view->addScriptRaw($this->getCodeBefore(), true);
$this->postMake();
$this->view->addScriptRaw($this->getCodeAfter(), true);
}
}
?>