- WIP on forms and UI widgets

This commit is contained in:
Dave Mc Nicoll 2020-03-17 16:14:47 -04:00
parent 657ceb2863
commit b1129def47
7 changed files with 31 additions and 18 deletions

View File

@ -108,7 +108,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
$obj->attributes($attributes); $obj->attributes($attributes);
} }
if ( $custom = static::$config["tags"][$tag] ?? false ) { if ( false !== ( $custom = static::$config["tags"][$tag] ?? false ) ) {
if ( $custom['tag'] ?? false ) { if ( $custom['tag'] ?? false ) {
$obj->tag = $custom['tag']; $obj->tag = $custom['tag'];
} }
@ -129,34 +129,39 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
{ {
$element = static::create($tag, $attributes); $element = static::create($tag, $attributes);
$this->append($element); $this->append($element);
return $element; return $element;
} }
public function addClass(string $classname) : self public function addClass(string $classname) : self
{ {
$this->attributes['class'] = $classname; $this->attributes['class'] = $classname;
return $this; return $this;
} }
public function removeClass($classname) { 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]); unset($this->attributes['class'][$key]);
} }
return $this; return $this;
} }
public function option(string $var, $value = 1) { public function option(string $var, $value = 1) : self
{
$this->options[$var] = $value; $this->options[$var] = $value;
return $this; return $this;
} }
public function render() { public function render() : string
{
$attributesList = []; $attributesList = [];
foreach ( $this->attributes as $key => $value ) { foreach ( $this->attributes as $key => $value ) {
if ( is_array($value) ) { if ( is_array($value) ) {
if (empty($value)) continue; if ( empty($value) ) continue;
if ($key === 'style') { if ($key === 'style') {
$style = []; $style = [];
@ -183,7 +188,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
} }
$content = ""; $content = "";
foreach ( $this->childs as $item ) { foreach ( $this->childs as $item ) {
if ( is_object($item) ) { if ( is_object($item) ) {
$content .= $item->render(); $content .= $item->render();
@ -197,22 +202,22 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
return $this->content . $content; return $this->content . $content;
} }
else { 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) # Force the node to contain a certain opening tag (php is a good example)
if ( in_array('force-tag-open', $this->options) ) { if ( in_array('force-tag-open', $this->options) ) {
$opentag = $this->options['force-tag-open'] . $attributesstr; $opentag = $this->options['force-tag-open'] . $compiledAttributes;
} }
else { else {
$opentag = $this->tag ? "<{$this->tag}" . $attributesstr . ">" : ""; $opentag = $this->tag ? "<{$this->tag}" . $compiledAttributes . ">" : "";
} }
if ( $this->options['tag-type'] ?? false ) { if ( $this->options['tag-type'] ?? false ) {
if ( $this->options['tag-type'] === "single" ) { 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']; $closetag = $this->options['force-tag-close'];
} }
else { else {
@ -226,6 +231,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
public function html($set = null) { public function html($set = null) {
if ($set !== null) { if ($set !== null) {
$this->content = $set; $this->content = $set;
return $this; return $this;
} }
@ -235,6 +241,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
public function text(?string $set = null) { public function text(?string $set = null) {
if ($set !== null) { if ($set !== null) {
$this->content = htmlspecialchars( $set, ENT_NOQUOTES ); $this->content = htmlspecialchars( $set, ENT_NOQUOTES );
return $this; return $this;
} }
@ -263,13 +270,11 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
case static::INSERT_MODE_PREPEND: case static::INSERT_MODE_PREPEND:
array_unshift($this->childs, $item); array_unshift($this->childs, $item);
break; break;
} }
} }
return $this; return $this;
} }
public function attributes(array $attributes) : self public function attributes(array $attributes) : self
{ {

View File

@ -47,10 +47,11 @@ class UiForm extends UiElement implements Extension {
} }
$method ??= $this->defaultMethod; $method ??= $this->defaultMethod;
return "<?php echo ( new \\" . static::class . "() )->buildHtml('$method', $arguments) ?>"; 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); $method = strtolower($method);
@ -59,7 +60,8 @@ class UiForm extends UiElement implements Extension {
$this->attributes([ 'method' => $method, 'action' => $action ] + $attributes); $this->attributes([ 'method' => $method, 'action' => $action ] + $attributes);
if ( $method !== "get" ) { 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" ]); $this->attributes([ 'enctype' => "multipart/form-data" ]);
} }

View File

@ -16,6 +16,10 @@ class UiInput extends UiElement implements Extension {
public array $attributes = [ public array $attributes = [
'class' => 'ui-input', 'class' => 'ui-input',
]; ];
public array $options = [
'tag-type' => "single",
];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string 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->setValue($value);
$this->attributes([ 'name' => $name ] + $attributes + $this->attributes); $this->attributes([ 'name' => $name ] + $attributes + $this->attributes);
return $this->render(); return $this->render();
} }

View File

@ -77,6 +77,7 @@ class UiSelect extends UiElement implements Extension {
protected function isSelected($check, $value, bool $strict = true) : bool protected function isSelected($check, $value, bool $strict = true) : bool
{ {
if (false !== ( $f = filter_var($value, FILTER_VALIDATE_INT) ) ) { if (false !== ( $f = filter_var($value, FILTER_VALIDATE_INT) ) ) {
$value = $f; $value = $f;
} }

View File

@ -11,6 +11,8 @@ class UiTextarea extends UiInput {
public array $attributes = [ public array $attributes = [
'class' => "ui-textarea", 'class' => "ui-textarea",
]; ];
public array $options = [];
protected function setValue($value) : void protected function setValue($value) : void
{ {

View File

@ -36,7 +36,7 @@ class FormHandler {
if ( $this->sent ) { if ( $this->sent ) {
if ( $this->form->validate($this->context) ) { if ( $this->form->validate($this->context) ) {
$this->form->run($this->context); $this->form->execute($this->context);
} }
} }
} }

View File

@ -8,5 +8,5 @@ interface FormInterface
public function validate(FormContextInterface $context) : bool; public function validate(FormContextInterface $context) : bool;
public function run(FormContextInterface $context) : void; public function execute(FormContextInterface $context) : void;
} }