diff --git a/README.md b/README.md index 504372d..c9d4759 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Laravel Flatform -=== + [![Packagist](https://img.shields.io/packagist/v/dimonka2/flatform.svg)](https://packagist.org/packages/dimonka2/flatform) [![Packagist](https://img.shields.io/packagist/l/dimonka2/flatform.svg)](https://packagist.org/packages/dimonka2/flatform) diff --git a/config/flatform.php b/config/flatform.php index c3f9051..75b9dd3 100644 --- a/config/flatform.php +++ b/config/flatform.php @@ -115,58 +115,6 @@ 'Form' => Collective\Html\FormFacade::class, 'HTML' => Collective\Html\HtmlFacade::class, ], - - 'bindings' => [ - // inputs - 'text' => dimonka2\flatform\Form\Inputs\Text::class, - 'password' => dimonka2\flatform\Form\Inputs\Password::class, - 'number' => dimonka2\flatform\Form\Inputs\Number::class, - 'textarea' => dimonka2\flatform\Form\Inputs\Textarea::class, - 'summernote' => dimonka2\flatform\Form\Inputs\Summernote::class, - 'select' => dimonka2\flatform\Form\Inputs\Select::class, - 'select2' => dimonka2\flatform\Form\Inputs\Select2::class, - 'bselect' => dimonka2\flatform\Form\Inputs\BootstrapSelect::class, - 'file' => dimonka2\flatform\Form\Inputs\File::class, - 'checkbox' => dimonka2\flatform\Form\Inputs\Checkbox::class, - 'radio' => dimonka2\flatform\Form\Inputs\Radio::class, - 'date' => dimonka2\flatform\Form\Inputs\Date::class, - 'hidden' => dimonka2\flatform\Form\Inputs\Hidden::class, - - // components - 'tabs' => dimonka2\flatform\Form\Components\Tabs::class, - 'widget' => dimonka2\flatform\Form\Components\Widget::class, - 'dropdown' => dimonka2\flatform\Form\Components\Dropdown::class, - 'dd-item' => dimonka2\flatform\Form\Components\DropdownItem::class, - 'datatable' => dimonka2\flatform\Form\Components\Datatable::class, - 'breadcrumbs' => dimonka2\flatform\Form\Components\Breadcrumbs::class, - 'progress' => dimonka2\flatform\Form\Components\Progress::class, - 'alert' => dimonka2\flatform\Form\Components\Alert::class, - - // links and buttons - 'a' => dimonka2\flatform\Form\Link::class, - 'submit' => dimonka2\flatform\Form\Components\Button::class, - 'button' => dimonka2\flatform\Form\Components\Button::class, - - 'form' => dimonka2\flatform\Form\Form::class, - - 'div' => dimonka2\flatform\Form\ElementContainer::class, - 'span' => dimonka2\flatform\Form\ElementContainer::class, - 'col' => dimonka2\flatform\Form\Column::class, - 'i' => dimonka2\flatform\Form\ElementContainer::class, - 'b' => dimonka2\flatform\Form\ElementContainer::class, - 'u' => dimonka2\flatform\Form\ElementContainer::class, - 'ul' => dimonka2\flatform\Form\ElementContainer::class, - 'li' => dimonka2\flatform\Form\ElementContainer::class, - 'label' => dimonka2\flatform\Form\Label::class, - '_text' => dimonka2\flatform\Form\Element::class, - '_template' => dimonka2\flatform\Form\Element::class, - 'option' => dimonka2\flatform\Form\Element::class, - - // blade directives - 'include' => dimonka2\flatform\Form\BladeDirective::class, - 'stack' => dimonka2\flatform\Form\BladeDirective::class, - 'yield' => dimonka2\flatform\Form\BladeDirective::class, - 'extends' => dimonka2\flatform\Form\BladeDirective::class, - 'section' => dimonka2\flatform\Form\BladeDirective::class, - ], + // add more flatform elements here + 'bindings' => [], ]; diff --git a/src/FlatformService.php b/src/FlatformService.php index 5cf8808..3690a50 100644 --- a/src/FlatformService.php +++ b/src/FlatformService.php @@ -20,6 +20,11 @@ public static function render(array $element) return (new Context($element))->setOptions($options)->render(); } + public static function context() + { + return new Context(); + } + public static function isIncluded($element_name) { return in_array($element_name, static::$includes); diff --git a/src/Form/Column.php b/src/Form/Column.php deleted file mode 100644 index ea794e5..0000000 --- a/src/Form/Column.php +++ /dev/null @@ -1,36 +0,0 @@ -readSettings($element, ['col', 'col-md', 'col-lg', 'col-xl']); - parent::read($element); - if(!is_null($this->col)) $this->addClass('col-' . $this->col); - if(!is_null($this->col_md)) $this->addClass('col-md-' . $this->col_md); - if(!is_null($this->col_lg)) $this->addClass('col-lg-' . $this->col_lg); - if(!is_null($this->col_xl)) $this->addClass('col-xl-' . $this->col_xl); - if ( - is_null($this->col) && is_null($this->col_md) && is_null($this->col_md) && is_null($this->col_md) - ) $this->addClass(config('flatform.form.col', 'col-6')); - // dd($this); - } - - public function getTag() - { - return 'div'; - } - -} diff --git a/src/Form/Context.php b/src/Form/Context.php index 3dff374..dc5afe6 100644 --- a/src/Form/Context.php +++ b/src/Form/Context.php @@ -29,6 +29,12 @@ public function __construct(array $elements = []) $this->elements->readItems($elements); } + public function add(array $elements = []) + { + $this->elements->readItems($elements); + return $this; + } + public function getID($name) { preg_match('/[-_A-Za-z0-9]+/', $name, $matches); diff --git a/src/Form/Element.php b/src/Form/Element.php index 1444024..64a4d49 100644 --- a/src/Form/Element.php +++ b/src/Form/Element.php @@ -58,11 +58,13 @@ public function processAttributes($element) public function addClass($class) { $this->class = ($this->class ?? '') . ' ' . $class; + return $this; } public function addStyle($style) { $this->style = ($this->style ?? '') . ' ' . $style; + return $this; } protected function read(array $element) @@ -71,6 +73,7 @@ protected function read(array $element) if(!is_null($this->hidden)) $this->hidden = !!$this->hidden; if(!is_null($this->exclude)) $this->hidden = !!$this->exclude; $this->processAttributes($element); + return $this; } protected function getTemplate($tag = null) @@ -110,6 +113,7 @@ public function getAttribute($name) public function setAttribute($name, $value) { $this->attributes[$name] = $value; + return $this; } public function renderElement() @@ -158,6 +162,7 @@ protected function requireID() if(is_null($this->id)) { $this->id = $this->context->getID($this->name ?? 'id'); } + return $this; } } diff --git a/src/Form/ElementContainer.php b/src/Form/ElementContainer.php index 75604df..ce002d8 100644 --- a/src/Form/ElementContainer.php +++ b/src/Form/ElementContainer.php @@ -34,12 +34,13 @@ protected function read(array $element) $this->addTextElement( $this->text); } // echo $this->hash() . " Read items: \r\n"; - + return $this; } public function push(Element $item) { $this->elements->push($item); + return $this; } public function readItems(array $items) @@ -48,6 +49,7 @@ public function readItems(array $items) $item = $this->createElement($item); $this->elements->push($item); } + return $this; } protected function addTextElement($text) diff --git a/src/Form/ElementFactory.php b/src/Form/ElementFactory.php index 5328abb..fe34154 100644 --- a/src/Form/ElementFactory.php +++ b/src/Form/ElementFactory.php @@ -11,12 +11,14 @@ class ElementFactory { private const tag_template = '/[_-a-zA-Z0-9]+/'; private $context; + private $user_binds = []; private $binds = []; public function __construct(IContext $context) { $this->context = $context; - $this->binds = config('flatform.bindings'); + $this->user_binds = config('flatform.bindings'); + $this->binds = ElementMapping::bindings; } protected static function _createElement($class, array $element, $context): IElement @@ -67,6 +69,10 @@ public function createElement(array $element): IElement $element = self::mergeTemplate($element, $this->context->getTemplate('input') ); } + if (isset($this->user_binds[$type])) { + return self::_createElement($this->user_binds[$type], $element, $this->context); + } + if (isset($this->binds[$type])) { return self::_createElement($this->binds[$type], $element, $this->context); } diff --git a/src/Form/ElementMapping.php b/src/Form/ElementMapping.php new file mode 100644 index 0000000..12c919b --- /dev/null +++ b/src/Form/ElementMapping.php @@ -0,0 +1,61 @@ + Inputs\Text::class, + 'password' => Inputs\Password::class, + 'number' => Inputs\Number::class, + 'textarea' => Inputs\Textarea::class, + 'summernote' => Inputs\Summernote::class, + 'select' => Inputs\Select::class, + 'select2' => Inputs\Select2::class, + 'bselect' => Inputs\BootstrapSelect::class, + 'file' => Inputs\File::class, + 'checkbox' => Inputs\Checkbox::class, + 'radio' => Inputs\Radio::class, + 'date' => Inputs\Date::class, + 'hidden' => Inputs\Hidden::class, + + // components + 'tabs' => Components\Tabs::class, + 'widget' => Components\Widget::class, + 'dropdown' => Components\Dropdown::class, + 'dd-item' => Components\DropdownItem::class, + 'datatable' => Components\Datatable::class, + 'breadcrumbs' => Components\Breadcrumbs::class, + 'progress' => Components\Progress::class, + 'alert' => Components\Alert::class, + + // links and buttons + 'a' => Link::class, + 'submit' => Components\Button::class, + 'button' => Components\Button::class, + + 'form' => Form::class, + 'img' => Elements\Image::class, + 'col' => Elements\Column::class, + 'label' => Elements\Label::class, + + 'div' => ElementContainer::class, + 'span' => ElementContainer::class, + 'i' => ElementContainer::class, + 'b' => ElementContainer::class, + 'u' => ElementContainer::class, + 'ul' => ElementContainer::class, + 'li' => ElementContainer::class, + '_text' => Element::class, + '_template' => Element::class, + 'option' => Element::class, + + // blade directives + 'include' => BladeDirective::class, + 'stack' => BladeDirective::class, + 'yield' => BladeDirective::class, + 'extends' => BladeDirective::class, + 'section' => BladeDirective::class, + ]; +} diff --git a/src/Form/Elements/Column.php b/src/Form/Elements/Column.php new file mode 100644 index 0000000..05441fe --- /dev/null +++ b/src/Form/Elements/Column.php @@ -0,0 +1,38 @@ +readSettings($element, ['col', 'md', 'lg', 'xl', 'xs']); + parent::read($element); + if($this->col ) $this->addClass('col-' . $this->col); + if($this->md ) $this->addClass('col-md-' . $this->md); + if($this->lg ) $this->addClass('col-lg-' . $this->lg); + if($this->xl ) $this->addClass('col-xl-' . $this->xl); + if($this->xs ) $this->addClass('col-xs-' . $this->xs); + + if (!$this->col && !$this->md && !$this->lg && !$this->xl && !$this->xs) { + $this->addClass(config('flatform.form.col', 'col-6')); + } + } + + public function getTag() + { + return 'div'; + } + +} diff --git a/src/Form/Elements/Image.php b/src/Form/Elements/Image.php new file mode 100644 index 0000000..dee61e1 --- /dev/null +++ b/src/Form/Elements/Image.php @@ -0,0 +1,23 @@ +readSettings($element, ['src']); + parent::read($element); + } + + public function getOptions(array $keys) + { + $options = parent::getOptions($keys); + if($this->src) $options['src'] = $this->src; + return $options; + } +} diff --git a/src/Form/Label.php b/src/Form/Elements/Label.php similarity index 100% rename from src/Form/Label.php rename to src/Form/Elements/Label.php