This commit is contained in:
ichynul 2020-01-29 14:48:18 +08:00
parent e72a283a5e
commit 6e6e5351f1
25 changed files with 736 additions and 116 deletions

View File

@ -2,8 +2,8 @@
namespace tpext\builder\common;
use think\response\View as ViewShow;
use think\facade\View;
use think\response\View as ViewShow;
use tpext\builder\common\Plugin;
use tpext\myadmin\common\Module;
@ -11,14 +11,18 @@ class Builder implements Renderable
{
private $view = '';
public $title = '';
protected $title = '';
public $desc = null;
protected $desc = null;
public $rows = [];
protected $rows = [];
protected $__row__ = null;
protected $js = [];
protected $css = [];
public function __construct($title = 'tpext-builder', $desc = '')
{
$this->title = $title;
@ -43,11 +47,23 @@ class Builder implements Renderable
return $this->__row__->column($size);
}
/**
* 获取一个form
*
* @param integer col大小
* @return Form
*/
public function form($size = 12)
{
return $this->column($size)->form();
}
/**
* 获取一个表格
*
* @param integer col大小
* @return Table
*/
public function table($size = 12)
{
return $this->column($size)->table();
@ -62,7 +78,7 @@ class Builder implements Renderable
];
$config = [];
$view = new ViewShow($this->view);
$instance = Module::getInstance();

View File

@ -9,13 +9,19 @@ class Column
{
public $size = 12;
public $elms = [];
protected $elms = [];
public function __construct($size = 12)
{
$this->size = $size;
}
/**
* 获取一个form
*
* @return Form
*/
public function form()
{
$form = new Form();
@ -23,6 +29,11 @@ class Column
return $form;
}
/**
* 获取一个表格
*
* @return Table
*/
public function table()
{
$table = new Table();
@ -34,4 +45,9 @@ class Column
{
return $this->elms;
}
public function getSize()
{
return $this->size;
}
}

View File

@ -2,24 +2,58 @@
namespace tpext\builder\common;
use tpext\builder\common\Plugin;
use think\response\View as ViewShow;
use tpext\builder\common\Plugin;
use tpext\builder\form\Row;
use tpext\builder\form\Wapper;
class Form
class Form extends Wapper implements Renderable
{
protected $view = '';
protected $cols = [];
public $action = '';
protected $rows = [];
public function render()
{
$this->view = Plugin::getInstance()->getRoot() . implode(DIRECTORY_SEPARATOR, ['src', 'view', 'content.html']);
$template = Plugin::getInstance()->getRoot() . implode(DIRECTORY_SEPARATOR, ['src', 'view', 'form.html']);
$config = [];
$view = new ViewShow($this->view);
return $view->assign([])->config($config)->isContent(false)->getContent();
$viewshow = new ViewShow($template);
$vars = [
'rows' => $this->rows,
'action' => $this->action,
];
return $viewshow->assign($vars)->config($config)->getContent();
}
public function addRow($row)
{
$this->rows[] = $row;
}
public function action($val)
{
$this->action = $val;
}
public function __call($name, $arguments)
{
$count = count($arguments);
if ($count > 0 && static::isDisplayer($name)) {
$row = new Row($arguments[0], $count > 1 ? $arguments[1] : '', $count > 2 ? $arguments[2] : 12, $count > 3 ? $arguments[3] : '', $count > 4 ? $arguments[4] : '');
$this->rows[] = $row;
return $row->$name($arguments[0], $count > 1 ? $arguments[1] : '');
}
throw new \UnexpectedValueException('未知调用');
}
}

View File

@ -4,7 +4,7 @@ namespace tpext\builder\common;
class Row
{
public $cols = [];
protected $cols = [];
public function column($size = 12)
{

View File

@ -4,7 +4,7 @@ namespace tpext\builder\common;
use tpext\builder\table\Column;
class Table
class Table implements Renderable
{
protected $headers = [];

11
src/displayer/Divider.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace tpext\builder\displayer;
class Divider extends Html
{
public function render()
{
return "<div class='divider'>{$this->value}</div>";
}
}

349
src/displayer/Field.php Normal file
View File

@ -0,0 +1,349 @@
<?php
namespace tpext\builder\displayer;
use think\response\View as ViewShow;
use tpext\builder\common\Plugin;
class Field
{
protected $name = '';
protected $label = '';
protected $js = [];
protected $css = [];
protected $view = '';
protected $value = '';
protected $icon = '';
protected $rules = '';
protected $options = [];
protected $editable = true;
protected $class = '';
protected $labelClass = '';
protected $attr = '';
protected $labelArrt = '';
protected $error = '';
protected $size = [2, 9];
protected $help = '';
protected $wapper = null;
protected static $helptempl;
protected static $labeltempl;
public function __construct($name, $label = '')
{
if (empty($label)) {
$label = ucfirst($name);
}
$this->name = $name;
$this->label = $label;
}
/**
* Undocumented function
*
* @param array $options
* @return $this
*/
public function options($options)
{
$this->options = $options;
return $this;
}
/**
* Undocumented function
*
* @param string $val
* @return $this
*/
public function value($val)
{
$this->value = $val;
return $this;
}
/**
* Undocumented function
*
* @param string $val
* @return $this
*/
public function name($val)
{
$this->name = $val;
return $this;
}
/**
* Undocumented function
*
* @param string $val
* @return $this
*/
public function label($val)
{
$this->label = $val;
return $this;
}
/**
* Undocumented function
*
* @param string $val
* @return $this
*/
function class ($val)
{
$this->class = $val;
return $this;
}
/**
* Undocumented function
*
* @param string $val
* @return $this
*/
public function labelClass($val)
{
$this->labelClass = $val;
return $this;
}
/**
* Undocumented function
*
* @param string $val
* @return $this
*/
public function attr($val)
{
$this->attr = $val;
return $this;
}
/**
* Undocumented function
*
* @param string $val
* @return $this
*/
public function labelAttr($val)
{
$this->labelAttr = $val;
return $this;
}
/**
* Undocumented function
*
* @param array $val
* @return $this
*/
public function size($val)
{
$this->size = $val;
return $this;
}
/**
* Undocumented function
*
* @param string $val
* @return $this
*/
public function help($val)
{
$this->help = $val;
return $this;
}
/**
* Undocumented function
*
* @param string $val
* @return $this
*/
public function error($val)
{
$this->error = $val;
$this->row->error($val);
return $this;
}
/**
* Undocumented function
*
* @param \tpext\builder\form\Row $wapper
* @return $this
*/
public function setWapper($wapper)
{
$this->wapper = $wapper;
return $this;
}
/**
* Undocumented function
*
* @return \tpext\builder\form\Row
*/
public function getWapper()
{
return $this->wapper;
}
/**
* Undocumented function
*
* @return string
*/
public function getClass()
{
return $this->class;
}
/**
* Undocumented function
*
* @return string
*/
public function getAttr()
{
return $this->attr;
}
/**
* Undocumented function
*
* @return string
*/
public function getLabelClass()
{
return $this->labelClass;
}
/**
* Undocumented function
*
* @return string
*/
public function getLabelAttr()
{
return $this->labelArrt;
}
/**
* Undocumented function
*
* @param boolean $editable
* @return $this
*/
public function editable($editable = true)
{
$this->editable = $editable;
return $this;
}
/**
* Undocumented function
*
* @return boolean
*/
public function isEditable()
{
return $this->editable;
}
/**
* Undocumented function
*
* @return array
*/
public function getJs()
{
return $this->js;
}
/**
* Undocumented function
*
* @return array
*/
public function getCss()
{
return $this->css;
}
/**
* Undocumented function
*
* @return mixed
*/
public function render()
{
return $this->value;
}
protected function getViewInstance()
{
$template = Plugin::getInstance()->getRoot() . implode(DIRECTORY_SEPARATOR, ['src', 'view', 'displayer', $this->view . '.html']);
$viewshow = new ViewShow($template);
return $viewshow;
}
/**
* Undocumented function
*
* @return array
*/
public function commonVars()
{
if (empty(static::$helptempl)) {
static::$helptempl = Plugin::getInstance()->getRoot() . implode(DIRECTORY_SEPARATOR, ['src', 'view', 'displayer', 'helptempl.html']);
}
if (empty(static::$labeltempl)) {
static::$labeltempl = Plugin::getInstance()->getRoot() . implode(DIRECTORY_SEPARATOR, ['src', 'view', 'displayer', 'labeltempl.html']);
}
$vars = [
'label' => $this->label,
'name' => $this->name,
'value' => $this->value,
'class' => $this->class,
'attr' => $this->attr,
'error' => $this->error,
'size' => $this->size,
'labelClass' => $this->size[0] < 12 ? $this->labelClass . ' control-label' : $this->labelClass,
'labelAttr' => empty($this->labelAttr) ? '' : ' ' . $this->labelAttr,
'options' => $this->options,
'help' => $this->help,
'helptempl' => static::$helptempl,
'labeltempl' => static::$labeltempl,
];
return $vars;
}
}

15
src/displayer/Html.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace tpext\builder\displayer;
class Html extends Text
{
protected $view = 'html';
public function __construct($html = '')
{
$this->size([0, 12]);
$this->value = $html;
}
}

8
src/displayer/Raw.php Normal file
View File

@ -0,0 +1,8 @@
<?php
namespace tpext\builder\displayer;
class Raw extends text
{
protected $view = 'raw';
}

19
src/displayer/Text.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace tpext\builder\displayer;
class Text extends Field
{
protected $view = 'text';
public function render()
{
$vars = $this->commonVars();
$config = [];
$viewshow = $this->getViewInstance();
return $viewshow->assign($vars)->config($config)->getContent();
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace tpext\builder\displayer;
class Textarea extends Text
{
protected $view = 'textarea';
}

View File

@ -1,47 +0,0 @@
<?php
namespace tpext\builder\displayer;
use tpext\builder\common\Renderable;
class Field
{
protected $view = '';
protected $value = '';
protected $icon = '';
protected $rules = '';
protected $options = [];
protected $editable = true;
public function __construct($options = [])
{
$this->options = $options;
}
public function options($options)
{
$this->options = $options;
return $this;
}
public function value($val)
{
$this->value = $val;
return $this;
}
public function setEditable($editable = true)
{
$this->editable = $editable;
}
public function render()
{
return $this->value;
}
}

View File

@ -1,8 +0,0 @@
<?php
namespace tpext\builder\displayer;
class Text extends Field
{
protected $view = 'html';
}

View File

@ -1,8 +0,0 @@
<?php
namespace tpext\builder\displayer;
class Text extends Field
{
protected $view = 'text';
}

125
src/form/Row.php Normal file
View File

@ -0,0 +1,125 @@
<?php
namespace tpext\builder\form;
use tpext\builder\common\Renderable;
use tpext\builder\displayer\Field;
class Row extends Wapper implements Renderable
{
protected $attributes = [];
protected $name = '';
protected $label = '';
protected $size = 12;
protected $class = '';
protected $attr = '';
protected $error = '';
/**
* Displayer
*
* @var \tpext\builder\displayer\Field
*/
protected $displayer;
protected $options = [];
public function __construct($name, $label = '', $colSize = 12, $colClass = '', $colAttr = '')
{
if (empty($label)) {
$label = ucfirst($name);
}
$this->name = $name;
$this->label = $label;
$this->size = $colSize;
$this->class = $colClass;
$this->attr = $colAttr;
$this->displayer = new Field($name, $label);
$this->displayer->setWapper($this);
return $this;
}
public function createDisplayer($class, $arguments)
{
$this->displayer = new $class($arguments[0], $arguments[1]);
$this->displayer->setWapper($this);
return $this->displayer;
}
public function options($options)
{
$this->options = array_merge($this->options, $options);
return $this;
}
public function size($val)
{
$this->size = $val;
return $this;
}
public function error($val)
{
$this->error = $val;
return $this;
}
public function getSize()
{
return $this->size;
}
public function getClass()
{
return empty($this->class) ? '' : ' ' . $this->class;
}
public function getError()
{
return empty($this->error) ? '' : ' ' . $this->error;
}
public function getAttr()
{
return empty($this->attr) ? '' : ' ' . $this->attr;
}
public function addClass($val)
{
$this->class .= ' ' . $val;
return $this;
}
public function addAttr($val)
{
$this->attr .= ' ' . $val;
return $this;
}
public function render()
{
return $this->displayer->render();
}
public function __call($name, $arguments)
{
if (static::isDisplayer($name)) {
$class = static::$displayerMap[$name];
return $this->createDisplayer($class, $arguments);
}
throw new \UnexpectedValueException('未知调用');
}
}

73
src/form/Wapper.php Normal file
View File

@ -0,0 +1,73 @@
<?php
namespace tpext\builder\form;
/**
* Class Wapper.
*
* @method \tpext\builder\displayer\Text text($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Checkbox checkbox($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Radio radio($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Select select($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\MultipleSelect multipleSelect($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Textarea textarea($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Hidden hidden($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Id id($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Ip ip($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Url url($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Color color($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Email email($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Mobile mobile($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Slider slider($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\File file($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Image image($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Date date($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Datetime datetime($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Time time($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Year year($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Month month($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\DateRange dateRange($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\DateTimeRange datetimeRange($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\TimeRange timeRange($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Number number($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Currency currency($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\SwitchField switch($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Display display($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Rate rate($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Divider divider($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Password password($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Decimal decimal($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Html html(name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Raw raw($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Tags tags($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\Icon icon($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\MultipleImage multipleImage($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
* @method \tpext\builder\displayer\MultipleFile multipleFile($name, $label = '', $cloSize = 12, $colClass = '', $colAttr = '')
*/
class Wapper
{
protected static $displayers = [];
public static $displayerMap = [
'text' => \tpext\builder\displayer\Text::class,
'textarea' => \tpext\builder\displayer\Textarea::class,
'html' => \tpext\builder\displayer\Html::class,
'divider' => \tpext\builder\displayer\Divider::class,
'raw' => \tpext\builder\displayer\Raw::class,
];
public static function isDisplayer($name)
{
if (empty(static::$displayers)) {
static::$displayers = array_keys(static::$displayerMap);
}
return in_array($name, static::$displayers);
}
public static function extend($pair)
{
static::$displayerMap = array_merge(static::$displayerMap, $pair);
}
}

View File

@ -2,42 +2,13 @@
namespace tpext\builder\table;
use tpext\builder\common\Renderable;
use tpext\builder\displayer\Text;
use tpext\builder\displayer\Field;
class Column implements Renderable
use tpext\builder\form\Row;
class Column extends Row
{
protected $attributes = [];
protected $name;
protected $label;
protected $displayer;
protected $options = [];
public function __construct($name, $label)
{
$this->name = $name;
$this->label = $label;
$this->displayer = new Field();
}
public function text($options = [])
{
$this->displayer = new Text();
$this->options = $options;
}
public function options($options)
{
$this->options = array_merge($this->options, $options);
}
public function render()
{
}
}

View File

@ -12,9 +12,9 @@
<div class="card-body">
{volist name="rows" id="row"}
<div class="row">
{volist name="row->cols" id="col"}
<div class="col-md-{$col->size}">
{volist name="col->elms" id="elm"}
{volist name="row->getCols()" id="col"}
<div class="col-md-{$col->getSize()}">
{volist name="col->getElms()" id="elm"}
{:$elm->render()}
{/volist}
</div>
@ -28,7 +28,9 @@
{/block}
{block name="script"}
<script src="js/jquery-tags-input/jquery.tagsinput.min.js"></script>
<script type="text/javascript" src="js/jquery-validate/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/jquery-validate/messages_zh.min.js"></script>
{/block}
</body>
</html>

View File

@ -0,0 +1,3 @@
{notempty name="help"}
<div class="help-block"><i class="mdi mdi-information"></i>{$help|raw}</div>
{/notempty}

View File

@ -0,0 +1,4 @@
<div class="col-md-{$size[1]}">
{$value|raw}
{include file="$helptempl" /}
</div>

View File

@ -0,0 +1 @@
<label class="col-md-{$size[0]} {$labelClass}"{$labelAttr|raw} for="form-{$name}">{$label}</label>

View File

@ -0,0 +1,5 @@
{include file="$labeltempl" /}
<div class="col-md-{$size[1]}">
{$value|raw}
{include file="$helptempl" /}
</div>

View File

@ -0,0 +1,7 @@
{include file="$labeltempl" /}
<div class="col-md-{$size[1]}">
<input type="text" class="form-control {$class}"
placeholder="请输入{$label}"
value="{$value}" name="{$name}" id="form-{$name}" {$attr}>
{include file="$helptempl" /}
</div>

View File

@ -0,0 +1,7 @@
{include file="$labeltempl" /}
<div class="col-md-{$size[1]}">
<textarea class="form-control {$class}" placeholder="请输入{$label}"
rows="{$options.rows|default=3}"
name="{$name}" id="form-{$name}" {$attr}>{$value}</textarea>
{include file="$helptempl" /}
</div>

9
src/view/form.html Normal file
View File

@ -0,0 +1,9 @@
<form action="{$action}" class="form-horizontal" method="post" enctype="multipart/form-data" id="thefrom">
<div class="row">
{volist name="rows" id="row"}
<div class="form-group col-md-{$row->getSize()}{$row->getClass()}{$row->getError()}"{$row->getAttr()|raw}>
{:$row->render()}
</div>
{/volist}
</div>
</form>