- WIP on forms and UI widgets
This commit is contained in:
parent
657ceb2863
commit
b1129def47
|
@ -108,7 +108,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
|
|||
$obj->attributes($attributes);
|
||||
}
|
||||
|
||||
if ( $custom = static::$config["tags"][$tag] ?? false ) {
|
||||
if ( false !== ( $custom = static::$config["tags"][$tag] ?? false ) ) {
|
||||
if ( $custom['tag'] ?? false ) {
|
||||
$obj->tag = $custom['tag'];
|
||||
}
|
||||
|
@ -129,34 +129,39 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
|
|||
{
|
||||
$element = static::create($tag, $attributes);
|
||||
$this->append($element);
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
public function addClass(string $classname) : self
|
||||
{
|
||||
$this->attributes['class'] = $classname;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeClass($classname) {
|
||||
if ( $key = array_search(strtolower($classname), array_map('strtolower', $this->attributes['class'])) ) {
|
||||
if ( false !== $key = array_search(strtolower($classname), array_map('strtolower', $this->attributes['class'])) ) {
|
||||
unset($this->attributes['class'][$key]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function option(string $var, $value = 1) {
|
||||
public function option(string $var, $value = 1) : self
|
||||
{
|
||||
$this->options[$var] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function render() {
|
||||
public function render() : string
|
||||
{
|
||||
$attributesList = [];
|
||||
|
||||
foreach ( $this->attributes as $key => $value ) {
|
||||
if ( is_array($value) ) {
|
||||
if (empty($value)) continue;
|
||||
if ( empty($value) ) continue;
|
||||
|
||||
if ($key === 'style') {
|
||||
$style = [];
|
||||
|
@ -183,7 +188,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
|
|||
}
|
||||
|
||||
$content = "";
|
||||
|
||||
|
||||
foreach ( $this->childs as $item ) {
|
||||
if ( is_object($item) ) {
|
||||
$content .= $item->render();
|
||||
|
@ -197,22 +202,22 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
|
|||
return $this->content . $content;
|
||||
}
|
||||
else {
|
||||
$attributesstr = (count($attributesList) && ! in_array('no-attr', $this->options) ? " " . implode(" ", $attributesList) : "");
|
||||
$compiledAttributes = (count($attributesList) && ! in_array('no-attr', $this->options) ? " " . implode(" ", $attributesList) : "");
|
||||
|
||||
# Force the node to contain a certain opening tag (php is a good example)
|
||||
if ( in_array('force-tag-open', $this->options) ) {
|
||||
$opentag = $this->options['force-tag-open'] . $attributesstr;
|
||||
$opentag = $this->options['force-tag-open'] . $compiledAttributes;
|
||||
}
|
||||
else {
|
||||
$opentag = $this->tag ? "<{$this->tag}" . $attributesstr . ">" : "";
|
||||
$opentag = $this->tag ? "<{$this->tag}" . $compiledAttributes . ">" : "";
|
||||
}
|
||||
|
||||
if ( $this->options['tag-type'] ?? false ) {
|
||||
if ( $this->options['tag-type'] === "single" ) {
|
||||
return $opentag;
|
||||
return $opentag . $content;
|
||||
}
|
||||
}
|
||||
else if ( in_array('force-tag-close', $this->options) ) {
|
||||
elseif ( in_array('force-tag-close', $this->options) ) {
|
||||
$closetag = $this->options['force-tag-close'];
|
||||
}
|
||||
else {
|
||||
|
@ -226,6 +231,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
|
|||
public function html($set = null) {
|
||||
if ($set !== null) {
|
||||
$this->content = $set;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -235,6 +241,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
|
|||
public function text(?string $set = null) {
|
||||
if ($set !== null) {
|
||||
$this->content = htmlspecialchars( $set, ENT_NOQUOTES );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -263,13 +270,11 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
|
|||
case static::INSERT_MODE_PREPEND:
|
||||
array_unshift($this->childs, $item);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function attributes(array $attributes) : self
|
||||
{
|
||||
|
|
|
@ -47,10 +47,11 @@ class UiForm extends UiElement implements Extension {
|
|||
}
|
||||
|
||||
$method ??= $this->defaultMethod;
|
||||
|
||||
return "<?php echo ( new \\" . static::class . "() )->buildHtml('$method', $arguments) ?>";
|
||||
}
|
||||
|
||||
public function buildHtml(string $method = "get", string $action = "", array $attributes = []) : string
|
||||
public function buildHtml(string $method = "get", string $name = "", string $action = "", array $attributes = []) : string
|
||||
{
|
||||
$method = strtolower($method);
|
||||
|
||||
|
@ -59,7 +60,8 @@ class UiForm extends UiElement implements Extension {
|
|||
$this->attributes([ 'method' => $method, 'action' => $action ] + $attributes);
|
||||
|
||||
if ( $method !== "get" ) {
|
||||
$this->append( ( new UiHidden() )->attributes([ 'name' =>"picea-csrf-protection", 'value' => "abcdefg" ]) );
|
||||
$this->append( ( new UiHidden() )->attributes([ 'name' =>"picea-ui-form[$name]", 'value' => md5( $name . microtime() ) ]) );
|
||||
|
||||
$this->attributes([ 'enctype' => "multipart/form-data" ]);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,10 @@ class UiInput extends UiElement implements Extension {
|
|||
public array $attributes = [
|
||||
'class' => 'ui-input',
|
||||
];
|
||||
|
||||
public array $options = [
|
||||
'tag-type' => "single",
|
||||
];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
|
||||
{
|
||||
|
@ -26,7 +30,6 @@ class UiInput extends UiElement implements Extension {
|
|||
{
|
||||
$this->setValue($value);
|
||||
$this->attributes([ 'name' => $name ] + $attributes + $this->attributes);
|
||||
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
|
|
|
@ -77,6 +77,7 @@ class UiSelect extends UiElement implements Extension {
|
|||
|
||||
protected function isSelected($check, $value, bool $strict = true) : bool
|
||||
{
|
||||
|
||||
if (false !== ( $f = filter_var($value, FILTER_VALIDATE_INT) ) ) {
|
||||
$value = $f;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,8 @@ class UiTextarea extends UiInput {
|
|||
public array $attributes = [
|
||||
'class' => "ui-textarea",
|
||||
];
|
||||
|
||||
public array $options = [];
|
||||
|
||||
protected function setValue($value) : void
|
||||
{
|
||||
|
|
|
@ -36,7 +36,7 @@ class FormHandler {
|
|||
|
||||
if ( $this->sent ) {
|
||||
if ( $this->form->validate($this->context) ) {
|
||||
$this->form->run($this->context);
|
||||
$this->form->execute($this->context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,5 +8,5 @@ interface FormInterface
|
|||
|
||||
public function validate(FormContextInterface $context) : bool;
|
||||
|
||||
public function run(FormContextInterface $context) : void;
|
||||
public function execute(FormContextInterface $context) : void;
|
||||
}
|
Loading…
Reference in New Issue