- Refractoring on the old code base

This commit is contained in:
Dave M. 2023-03-29 15:11:54 +00:00
parent 72940b3615
commit 6aaa05b37d
6 changed files with 53 additions and 52 deletions

View File

@ -2,20 +2,35 @@
namespace Picea\Ui\Common; namespace Picea\Ui\Common;
enum UiElementInsertMode
{
case append;
case prepend;
}
class UiElement implements \JsonSerializable { class UiElement implements \JsonSerializable {
public const TAG_CONFIG_OPTIONS = [
static array $config = []; "!doctype" => [ "attributes" => [ "html" ], "options" => [ "tag-type" => "single" ] ],
"js" => [ "tag" => "script", "attributes" => [ "type" => "text/javascript" ] ],
const INSERT_MODE_APPEND = 1; "css" => [ "tag" => "link", "attributes" => [ "rel" => "stylesheet" , "type" => "text/css" ] ],
const INSERT_MODE_PREPEND = 2; "document" => [ "options" => [ "no-tag" => true ] ],
"link" => [ "options" => [ "tag-type" => "single" ] ],
public string $tag = 'div'; "meta" => [ "options" => [ "tag-type" => "single" ] ],
"img" => [ "options" => [ "tag-type" => "single" ] ],
public array $attributes = [ "input" => [ "options" => [ "tag-type" => "single" ] ],
'style' => [], "br" => [ "options" => [ "tag-type" => "single" ] ],
'class' => [], "hr" => [ "options" => [ "tag-type" => "single" ] ],
"iframe" => [ "options" => [ "tag-type" => "single" ] ],
"area" => [ "options" => [ "tag-type" => "single" ] ],
"col" => [ "options" => [ "tag-type" => "single" ] ],
"frame" => [ "options" => [ "tag-type" => "single" ] ],
"param" => [ "options" => [ "tag-type" => "single" ] ],
"video" => [ "options" => [ "tag-type" => "single" ] ],
"wbr" => [ "options" => [ "tag-type" => "single" ] ]
]; ];
public array $attributes = [ 'style' => [], 'class' => [], ];
public array $childs = []; public array $childs = [];
/** /**
@ -33,26 +48,11 @@ class UiElement implements \JsonSerializable {
public array $options = []; public array $options = [];
public $selected = null;
public string $content = ""; public string $content = "";
public function __construct( public function __construct(
? string $tag = null public string $tag = "div",
) { ) {}
if ( ! static::$config ) {
static::pushConfigArray( include(dirname(__FILE__) . "/taglist.php") );
}
if ($tag !== null) {
$this->tag = $tag;
}
}
public static function pushConfigArray(array $array) : void
{
static::$config = array_replace_recursive(static::$config, $array);
}
public static function stylesheet(string $href, $attributes = [], $options = []) : self public static function stylesheet(string $href, $attributes = [], $options = []) : self
{ {
@ -108,7 +108,7 @@ class UiElement implements \JsonSerializable {
$obj->attributes($attributes); $obj->attributes($attributes);
} }
if ( false !== ( $custom = static::$config["tags"][$tag] ?? false ) ) { if ( false !== ( $custom = static::TAG_CONFIG_OPTIONS[strtolower($tag)] ?? false ) ) {
if ( $custom['tag'] ?? false ) { if ( $custom['tag'] ?? false ) {
$obj->tag = $custom['tag']; $obj->tag = $custom['tag'];
} }
@ -252,23 +252,23 @@ class UiElement implements \JsonSerializable {
public function append( ...$arguments ) : self public function append( ...$arguments ) : self
{ {
return $this->insert( static::INSERT_MODE_APPEND, ...$arguments); return $this->insert( UiElementInsertMode::append, ...$arguments);
} }
public function prepend( ...$arguments ) : self public function prepend( ...$arguments ) : self
{ {
return $this->insert( static::INSERT_MODE_PREPEND, ...( is_array($arguments) ? array_reverse($arguments) : $arguments) ); return $this->insert( UiElementInsertMode::prepend, ...( is_array($arguments) ? array_reverse($arguments) : $arguments) );
} }
protected function insert(int $mode, ...$elements) : self protected function insert(UiElementInsertMode $mode, ...$elements) : self
{ {
foreach($elements as $item) { foreach($elements as $item) {
switch($mode) { switch($mode) {
case static::INSERT_MODE_APPEND: case UiElementInsertMode::append:
array_push($this->childs, $item); array_push($this->childs, $item);
break; break;
case static::INSERT_MODE_PREPEND: case UiElementInsertMode::prepend:
array_unshift($this->childs, $item); array_unshift($this->childs, $item);
break; break;
} }

View File

@ -11,12 +11,15 @@ class UiPopup extends UiElement implements Extension {
public string $token = "ui:popup"; public string $token = "ui:popup";
public string $tag = "div";
public array $attributes = [ public array $attributes = [
'class' => 'ui-popup', 'class' => 'ui-popup',
]; ];
public function __construct(
public string $tag = "div",
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
return "<?php echo 'ui-popup=\"' . ( new \\" . static::class . "() )->buildAttributes($arguments) . '\"' ?>"; return "<?php echo 'ui-popup=\"' . ( new \\" . static::class . "() )->buildAttributes($arguments) . '\"' ?>";

View File

@ -13,18 +13,15 @@ class UiForm extends UiElement implements Extension {
public array $token = [ "ui:form", "ui:endform" ]; public array $token = [ "ui:form", "ui:endform" ];
public string $tag = "form";
public array $attributes = [ public array $attributes = [
'class' => 'ui-form', 'class' => 'ui-form',
]; ];
public function __construct( public function __construct(
public string $tag = "form",
public bool $enctype = true, public bool $enctype = true,
public bool $csrf = true, public bool $csrf = true,
) { ) {}
parent::__construct(null);
}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {

View File

@ -11,8 +11,6 @@ class UiInput extends UiElement implements Extension {
public string $token = "ui:input"; public string $token = "ui:input";
public string $tag = "input";
public array $attributes = [ public array $attributes = [
'class' => 'ui-input', 'class' => 'ui-input',
]; ];
@ -24,7 +22,11 @@ class UiInput extends UiElement implements Extension {
protected /* ? mixed */ $value; protected /* ? mixed */ $value;
protected string $name; protected string $name;
public function __construct(
public string $tag = "input",
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>"; return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>";

View File

@ -11,12 +11,14 @@ class UiSelect extends UiElement implements Extension {
public string $token = "ui:select"; public string $token = "ui:select";
public string $tag = "select";
public array $attributes = [ public array $attributes = [
'class' => 'ui-select', 'class' => 'ui-select',
]; ];
public function __construct(
public string $tag = "select",
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>"; return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>";

View File

@ -6,8 +6,6 @@ class UiTextarea extends UiInput {
public array $tokens = [ "ui:textarea" ]; public array $tokens = [ "ui:textarea" ];
public string $tag = "textarea";
public array $attributes = [ public array $attributes = [
'class' => "ui-textarea", 'class' => "ui-textarea",
]; ];
@ -17,10 +15,9 @@ class UiTextarea extends UiInput {
protected bool $echoRaw = false; protected bool $echoRaw = false;
public function __construct( public function __construct(
public bool $raw = false public string $tag = "textarea",
) { public bool $raw = false,
parent::__construct(null); ) {}
}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {