Skip to content

Commit

Permalink
Moving mapping back to package
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimonka2 committed Jan 16, 2020
1 parent 28983f5 commit 89f25b5
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 93 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
56 changes: 2 additions & 54 deletions config/flatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
];
5 changes: 5 additions & 0 deletions src/FlatformService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 0 additions & 36 deletions src/Form/Column.php

This file was deleted.

6 changes: 6 additions & 0 deletions src/Form/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/Form/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -110,6 +113,7 @@ public function getAttribute($name)
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
return $this;
}

public function renderElement()
Expand Down Expand Up @@ -158,6 +162,7 @@ protected function requireID()
if(is_null($this->id)) {
$this->id = $this->context->getID($this->name ?? 'id');
}
return $this;
}

}
4 changes: 3 additions & 1 deletion src/Form/ElementContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -48,6 +49,7 @@ public function readItems(array $items)
$item = $this->createElement($item);
$this->elements->push($item);
}
return $this;
}

protected function addTextElement($text)
Expand Down
8 changes: 7 additions & 1 deletion src/Form/ElementFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
61 changes: 61 additions & 0 deletions src/Form/ElementMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace dimonka2\flatform\Form;

class ElementMapping
{
public const bindings = [
// inputs
'text' => 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,
];
}
38 changes: 38 additions & 0 deletions src/Form/Elements/Column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace dimonka2\flatform\Form\Elements;

use dimonka2\flatform\Form\ElementContainer;
use dimonka2\flatform\Form\Contracts\IContext;

// Bootstrap column

class Column extends ElementContainer
{
protected $col;
protected $md;
protected $lg;
protected $xl;
protected $xs;

protected function read(array $element)
{
$this->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';
}

}
23 changes: 23 additions & 0 deletions src/Form/Elements/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace dimonka2\flatform\Form\Elements;

use dimonka2\flatform\Form\Element;
use dimonka2\flatform\Form\Contracts\IContext;

class Image extends Element
{
protected $src;
protected function read(array $element)
{
$this->readSettings($element, ['src']);
parent::read($element);
}

public function getOptions(array $keys)
{
$options = parent::getOptions($keys);
if($this->src) $options['src'] = $this->src;
return $options;
}
}
File renamed without changes.

0 comments on commit 89f25b5

Please sign in to comment.