优化HasWhen,使用fields包围下级

This commit is contained in:
ichynul 2024-06-26 18:30:40 +08:00
parent 80d9729a51
commit c05035b485
No known key found for this signature in database
GPG Key ID: FB4F1E1AAA3F5C1F
8 changed files with 60 additions and 31 deletions

View File

@ -396,7 +396,7 @@ class Search extends SWrapper implements Renderable
$this->hidden('__page__')->value(1);
$this->hidden('__pagesize__');
$this->hidden('__search__')->value($this->id);
$this->hidden('__search__')->value($this->getFormId());
$this->hidden('__table__')->value($this->tableId);
$this->hidden('__sort__');
$this->hidden('__columns__')->value(implode(',', $this->chooseColumns));

View File

@ -139,7 +139,6 @@ class Map extends Text
{
return [
'maptype' => $this->type,
'textTempl' => Module::getInstance()->getViewsPath() . 'displayer' . DIRECTORY_SEPARATOR . 'text.html',
'mapStyle' => 'style="width: ' . $this->width . ';height: ' . $this->height . ';max-width: 100%;"',
];
}

View File

@ -16,8 +16,6 @@ class Matche extends Raw
public function renderValue()
{
$this->checked = '' . $this->value;
if (isset($this->options[$this->value])) {
$this->value = $this->options[$this->value];
} else if (isset($this->options['__default__'])) {
@ -35,6 +33,8 @@ class Matche extends Raw
public function customVars()
{
$this->checked = (string)$this->value;
return array_merge(parent::customVars(), [
'checked' => $this->checked,
]);

View File

@ -43,8 +43,6 @@ class Matches extends Raw
public function renderValue()
{
$this->checked = is_array($this->value) ? implode(',', $this->value) : '' . $this->value;
$values = explode(',', $this->value);
$texts = [];
@ -61,8 +59,10 @@ class Matches extends Raw
public function customVars()
{
return array_merge(parent::customVars(), [
$this->checked = is_array($this->value) ? implode(',', $this->value) : (string)$this->value;
return [
'checked' => $this->checked,
]);
];
}
}

View File

@ -90,7 +90,7 @@ class Select extends Field
*/
public function disabledOptions($val)
{
$this->disabledOptions = $val;
$this->disabledOptions = is_array($val) ? $val : explode(',', $val);
return $this;
}
@ -305,9 +305,7 @@ EOT;
EOT;
}
if (empty($prev)) {
$this->script[] = $script;
}
$this->script[] = $script;
return $script;
}
@ -429,10 +427,7 @@ EOT;
if (!$this->group && !isset($this->options[''])) {
$this->options = ['' => $this->jsOptions['placeholder']] + $this->options;
}
if ($this->disabledOptions && !is_array($this->disabledOptions)) {
$this->disabledOptions = explode(',', $this->disabledOptions);
}
foreach ($this->disabledOptions as &$di) {
$di = '-' . $di;
}
@ -450,4 +445,10 @@ EOT;
return $viewshow->assign($vars)->getContent();
}
public function destroy()
{
$this->prevSelect = null;
parent::destroy();
}
}

View File

@ -34,7 +34,7 @@ class SelectTree extends Tree
*
* @return string
*/
protected function s2ztreeScript()
protected function ztreeScript()
{
if (!($this->value === '' || $this->value === null || $this->value === [])) {
$this->checked = is_array($this->value) ? $this->value : explode(',', $this->value);
@ -90,7 +90,7 @@ EOT;
public function beforRender()
{
$this->s2ztreeScript();
$this->ztreeScript();
return parent::beforRender();
}

View File

@ -15,10 +15,10 @@ class UEditor extends Field
protected $minify = false;
protected $js = [
'/assets/builderueditor/ueditor.config.js',
'/assets/builderueditor/ueditor.all.min.js',
];
protected $configJsPath = '/assets/builderueditor/ueditor.all.min.js';
protected $configJsPath = '/assets/builderueditor/ueditor.config.js';
protected $uploadUrl = '';
@ -68,7 +68,7 @@ EOT;
public function beforRender()
{
$this->js[] = $this->configJsPath;
$this->js = array_merge([$this->configJsPath], $this->js);
if (!$this->readonly) {
$this->editorScript();

View File

@ -23,11 +23,13 @@ trait HasWhen
*/
protected $__when__ = null;
protected $whenWrapper = false;
/**
* Undocumented function
*
* @param string|int|array $cases 如:'1' '1 + 2' ['1 + 2', '2 + 3']
* @param mixed ...$toggleFields
* @param array|\Closure|mixed ...$toggleFields
* @return $this
*/
public function when($cases, ...$toggleFields)
@ -41,8 +43,15 @@ trait HasWhen
if (count($toggleFields)) {
if ($toggleFields[0] instanceof \Closure) { //如果是匿名回调
$toggleFields[0]($when);
//fields包围优化
$this->makeWhenWrapper();
$toggleFields[0]($form, $when);
} else {
//->when('1',$field1,$field2)
//或
//->when('1',[$field1,$field2])
//无法fields包围优化无多层次when时不影响
if (is_array($toggleFields[0])) {
$toggleFields = $toggleFields[0];
}
@ -54,11 +63,26 @@ trait HasWhen
$form->whenEnd();
$this->whenEnd();
//如果此处传入[toggleFields]参数那么就结束后面就不要再调用with($toggleFields)方法了。否则后面可以继续调用with($toggleFields)方法;
} else {
//fields包围优化
$this->makeWhenWrapper();
}
return $this;
}
protected function makeWhenWrapper()
{
//创建一个fields把后面的 toggleFields装进去解决多层when的嵌套的一些问题
$form = $this->getForm();
$whenWrapper = $form->fields(preg_replace('/\W/', '', $this->getName()) . '_when_' . count($this->whens))
->showLabel(false)
->addClass('when-wrapper')
->size(0, 12);
$this->__when__->toggle($whenWrapper);
$this->whenWrapper = true;
}
/**
* Undocumented function
*
@ -72,7 +96,7 @@ trait HasWhen
/**
* Undocumented function
*
* @param mixed ...$toggleFields
* @param array|\Closure|mixed ...$toggleFields
* @return $this
*/
public function with(...$toggleFields)
@ -86,18 +110,23 @@ trait HasWhen
if (count($toggleFields)) {
if ($toggleFields[0] instanceof \Closure) {
$toggleFields[0]($form);
} else {
if (is_array($toggleFields[0])) {
$toggleFields = $toggleFields[0];
}
foreach ($toggleFields as $field) {
$this->__when__->toggle($field);
}
}
// else {
// if (is_array($toggleFields[0])) {
// $toggleFields = $toggleFields[0];
// }
// foreach ($toggleFields as $field) {
// $this->__when__->toggle($field);
// }
// }
}
$form->whenEnd();
if ($this->whenWrapper) {
$form->fieldsEnd();
}
return $this;
}