- Corrected inputs handling from view to compiled states
- Added UiSelect widget - Fixed UiForm widget - Fixed UiInput widget - Added mocks of UiMessage and UiPopup. Not even a WIP at this stage.
This commit is contained in:
		
							parent
							
								
									4bf08def27
								
							
						
					
					
						commit
						f28ace745d
					
				@ -230,7 +230,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( $args[0], ENT_NOQUOTES );
 | 
					            $this->content = htmlspecialchars( $set, ENT_NOQUOTES );
 | 
				
			||||||
            return $this;
 | 
					            return $this;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										24
									
								
								src/Component/UiMessage.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/Component/UiMessage.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Picea\Ui\Component;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Picea\Ui\Common\UiElement;
 | 
				
			||||||
 | 
					use Picea\Extension\Extension;
 | 
				
			||||||
 | 
					use Picea\Extension\ExtensionTrait;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class UiMessage extends UiElement implements Extension {
 | 
				
			||||||
 | 
					    use ExtensionTrait;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public string $token = "ui.message";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public string $tag = "input";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public array $attributes = [
 | 
				
			||||||
 | 
					       'class' => 'ui-message',
 | 
				
			||||||
 | 
					    ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return "";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										24
									
								
								src/Component/UiPopup.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/Component/UiPopup.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Picea\Ui\Component;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Picea\Ui\Common\UiElement;
 | 
				
			||||||
 | 
					use Picea\Extension\Extension;
 | 
				
			||||||
 | 
					use Picea\Extension\ExtensionTrait;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class UiPopup extends UiElement implements Extension {
 | 
				
			||||||
 | 
					    use ExtensionTrait;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public string $token = "ui.popup";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public string $tag = "input";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public array $attributes = [
 | 
				
			||||||
 | 
					       'class' => 'ui-popup',
 | 
				
			||||||
 | 
					    ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return "";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -11,7 +11,7 @@ class UiForm extends UiElement implements Extension {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public string $defaultMethod = "get";
 | 
					    public string $defaultMethod = "get";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public array $token = [ "ui.form", "ui.endform", "ui.form.get", "ui.form.post" ];
 | 
					    public array $token = [ "ui.form", "ui.endform", "ui.form.get", "ui.form.post", "ui.form.patch", "ui.form.delete", "ui.form.put" ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public string $tag = "form";
 | 
					    public string $tag = "form";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -21,33 +21,39 @@ class UiForm extends UiElement implements Extension {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
 | 
					    public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if ( $token === 'ui.endform' ) {
 | 
					 | 
				
			||||||
            return "</form>";
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        list($action, $options) = array_pad(explode(',', $arguments), 2, null);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $action = $this->trim($action);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        switch($token) {
 | 
					        switch($token) {
 | 
				
			||||||
 | 
					            case 'ui.endform':
 | 
				
			||||||
 | 
					                return "</form>";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            case "ui.form.get":
 | 
					            case "ui.form.get":
 | 
				
			||||||
               $this->attributes['method'] = "get";
 | 
					                $method = "get";
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            case "ui.form.patch":
 | 
				
			||||||
 | 
					                $method = "patch";
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            case "ui.form.delete":
 | 
				
			||||||
 | 
					                $method = "delete";
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            case "ui.form.put":
 | 
				
			||||||
 | 
					                $method = "put";
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            case "ui.form.post":
 | 
					            case "ui.form.post":
 | 
				
			||||||
               $this->attributes['method'] = "post";
 | 
					               $method = "post";
 | 
				
			||||||
               $this->attributes['enctype'] = "multipart/form-data";
 | 
					 | 
				
			||||||
            break;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            default:
 | 
					 | 
				
			||||||
            case $this->token:
 | 
					 | 
				
			||||||
               $this->attributes['method'] = $this->defaultMethod;
 | 
					 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $method ??= $this->defaultMethod;
 | 
				
			||||||
 | 
					        return "<?php echo ( new \\" . static::class . "() )->buildHtml('$method', $arguments) ?>";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function buildHtml(string $method, string $action, array $attributes = []) : string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
        $this->option('tag-type', 'single');
 | 
					        $this->option('tag-type', 'single');
 | 
				
			||||||
        $this->attributes['action'] = $this->trim($action);
 | 
					        $this->attributes([ 'action' => $action ] + $attributes);
 | 
				
			||||||
        $this->attributes($this->parseOptions($options));
 | 
					        return $this->render() . PHP_EOL;
 | 
				
			||||||
        return $this->render();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -19,14 +19,15 @@ class UiInput extends UiElement implements Extension {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
 | 
					    public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        list($name, $value, $options) = array_pad(explode(',', $arguments), 3, null);
 | 
					        return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>";
 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->attributes['name'] = $this->trim($name);
 | 
					 | 
				
			||||||
        $this->setValue($this->trim($value));
 | 
					 | 
				
			||||||
        $this->attributes($this->parseOptions($options));
 | 
					 | 
				
			||||||
        return $this->render();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function buildHtml(string $name, string $value, array $attributes = []) : string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->setValue($value);
 | 
				
			||||||
 | 
					        $this->attributes([ 'name' => $name ] + $attributes + $this->attributes);
 | 
				
			||||||
 | 
					        return $this->render();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected function setValue($value) : void
 | 
					    protected function setValue($value) : void
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										68
									
								
								src/Form/UiSelect.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								src/Form/UiSelect.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,68 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Picea\Ui\Form;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Picea\Ui\Common\UiElement;
 | 
				
			||||||
 | 
					use Picea\Extension\Extension;
 | 
				
			||||||
 | 
					use Picea\Extension\ExtensionTrait;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class UiSelect extends UiElement implements Extension {
 | 
				
			||||||
 | 
					    use ExtensionTrait;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public string $token = "ui.select";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public string $tag = "select";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public array $attributes = [
 | 
				
			||||||
 | 
					       'class' => 'ui-select',
 | 
				
			||||||
 | 
					    ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function buildHtml(string $name, array $list, $value, array $attributes = [], bool $strictComparison = true) : string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->attributes([ 'name' => $name ] + $attributes + $this->attributes);
 | 
				
			||||||
 | 
					        $this->setList($list, $value, $strictComparison);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $this->render();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected function setValue($value) : void
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->attributes['value'] = $value;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected function setList(array $list, $selected, bool $strict = true) : void
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        foreach($list as $key => $item) {
 | 
				
			||||||
 | 
					            if ($item instanceof UiElement) {
 | 
				
			||||||
 | 
					                $this->append($item);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            elseif ( is_array($item) ) {
 | 
				
			||||||
 | 
					                $obj = new UiElement("optgroup");
 | 
				
			||||||
 | 
					                $obj->text($key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                foreach($item as $subKey => $subItem) {
 | 
				
			||||||
 | 
					                    $obj->append($this->createOption($subItem, $subKey, $strict ? $subKey === $selected : $subKey == $selected));
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else {
 | 
				
			||||||
 | 
					                $obj = $this->createOption($item, $key, $strict ? $key === $selected : $key == $selected);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $this->append($obj);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected function createOption(string $name, string $value, bool $selected, array $attributes = []) : UiElement
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $option = new UiElement("option");
 | 
				
			||||||
 | 
					        $option->text($name);
 | 
				
			||||||
 | 
					        $option->attributes($attributes + [ 'value' => $value ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $option;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -27,6 +27,9 @@ class Ui {
 | 
				
			|||||||
        $compiler->registerExtension(new Form\UiTime());
 | 
					        $compiler->registerExtension(new Form\UiTime());
 | 
				
			||||||
        $compiler->registerExtension(new Form\UiUrl());
 | 
					        $compiler->registerExtension(new Form\UiUrl());
 | 
				
			||||||
        $compiler->registerExtension(new Form\UiWeek());
 | 
					        $compiler->registerExtension(new Form\UiWeek());
 | 
				
			||||||
 | 
					        $compiler->registerExtension(new Form\UiSelect());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $compiler->registerExtension(new Component\UiPopup());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user