From c4a36fb7cc950806eb981f3794e61fcfbc87fc13 Mon Sep 17 00:00:00 2001 From: ichynul Date: Thu, 11 Jul 2024 17:35:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96hasWhen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/displayer/Field.php | 14 ++++--- src/displayer/Matche.php | 10 ++++- src/displayer/Matches.php | 10 ++++- src/form/When.php | 71 +++++++++++++++++++++++----------- src/traits/HasDom.php | 2 +- src/traits/actions/HasView.php | 9 +++-- 6 files changed, 82 insertions(+), 34 deletions(-) diff --git a/src/displayer/Field.php b/src/displayer/Field.php index 74037a4..0fa3721 100644 --- a/src/displayer/Field.php +++ b/src/displayer/Field.php @@ -76,9 +76,9 @@ class Field implements Fillable /** * Undocumented variable * - * @var \Closure + * @var \Closure[] */ - protected $rendering = null; + protected $rendering = []; public function __construct($name, $label = '') { @@ -1113,8 +1113,12 @@ EOT; Builder::getInstance()->addStyleSheet($this->stylesheet); } - if ($this->rendering && $this->rendering instanceof \Closure) { - $this->rendering->call($this, $this); + if (count($this->rendering)) { + foreach ($this->rendering as $rd) { + if ($rd instanceof \Closure) { + $rd->call($this, $this); + } + } } ExtLoader::trigger('tpext_displayer_befor_render', $this); @@ -1345,7 +1349,7 @@ EOT; */ public function rendering($callback) { - $this->rendering = $callback; + $this->rendering[] = $callback; return $this; } diff --git a/src/displayer/Matche.php b/src/displayer/Matche.php index 0687e47..c9e44f4 100644 --- a/src/displayer/Matche.php +++ b/src/displayer/Matche.php @@ -14,8 +14,16 @@ class Matche extends Raw protected $checked = ''; + public function beforRender() + { + $this->checked = !($this->value === '' || $this->value === null) ? $this->value : $this->default; + return parent::beforRender(); + } + public function renderValue() { + $this->value = !($this->value === '' || $this->value === null) ? $this->value : $this->default; + if (isset($this->options[$this->value])) { $this->value = $this->options[$this->value]; } else if (isset($this->options['__default__'])) { @@ -33,7 +41,7 @@ class Matche extends Raw public function customVars() { - $this->checked = (string)$this->value; + $this->checked = (string)$this->checked; return array_merge(parent::customVars(), [ 'checked' => $this->checked, diff --git a/src/displayer/Matches.php b/src/displayer/Matches.php index 53e89ad..375ad0d 100644 --- a/src/displayer/Matches.php +++ b/src/displayer/Matches.php @@ -41,8 +41,16 @@ class Matches extends Raw return $this; } + public function beforRender() + { + $this->checked = !($this->value === '' || $this->value === null || $this->value === []) ? $this->value : $this->default; + return parent::beforRender(); + } + public function renderValue() { + $this->value = !($this->value === '' || $this->value === null || $this->value === []) ? $this->value : $this->default; + $values = explode(',', $this->value); $texts = []; @@ -59,7 +67,7 @@ class Matches extends Raw public function customVars() { - $this->checked = is_array($this->value) ? implode(',', $this->value) : (string)$this->value; + $this->checked = is_array($this->checked) ? implode(',', $this->checked) : (string)$this->checked; return array_merge(parent::customVars(), [ 'checked' => $this->checked, diff --git a/src/form/When.php b/src/form/When.php index e40c10d..62be27e 100644 --- a/src/form/When.php +++ b/src/form/When.php @@ -29,6 +29,12 @@ class When */ protected $fields = []; + /** + * + * @var bool|null + */ + protected $matchCase = null; + /** * Undocumented variable * @@ -47,7 +53,7 @@ class When { $this->watchFor = $watchFor; if (!is_array($cases)) { - $cases = ['' . $cases]; + $cases = [(string)$cases]; } $this->cases = $cases; // @@ -55,15 +61,14 @@ class When } /** - * Undocumented function - * - * @param displayer\Field $field - * @return $this + * 判断是否匹配 + * @return bool */ - public function toggle($field) + public function judgeMatchCase() { - //防止不同case中有重复字段的一些问题,因为trigger('change')调用时机,js处理重name/id有局限。 - $key = preg_replace('/[^\w\-]/', '-', $this->watchFor->getName()) . md5(json_encode($this->cases)); + if (!is_null($this->matchCase)) { + return $this->matchCase; + } $watchForValue = $this->watchFor->renderValue(); @@ -100,23 +105,45 @@ class When $matchCase = in_array($watchForValue, $this->cases); } - if ($field instanceof displayer\Fields) { - $field->extKey('-watch-' . $key) //防止id重复 - ->getWrapper()->addClass($matchCase ? '' : 'hidden'); - $rows = $field->getContent()->getRows(); + $this->matchCase = $matchCase; - foreach ($rows as $row) { - $sf = $row->getDisplayer(); - $sf->extKey('-watch-' . $key) //防止id重复 + return $this->matchCase; + } + + /** + * Undocumented function + * + * @param displayer\Field $field + * @return $this + */ + public function toggle($field) + { + $that = $this; + //防止不同case中有重复字段的一些问题,因为trigger('change')调用时机,js处理重name/id有局限。 + $key = preg_replace('/[^\w\-]/', '-', $that->watchFor->getName()) . md5(json_encode($that->cases)); + $this->watchFor->rendering(function () use ($field, $that, $key) { + $matchCase = $that->judgeMatchCase(); + if ($field instanceof displayer\Fields) { + $field->extKey('-watch-' . $key) //防止id重复 + ->getWrapper()->addClass($matchCase ? 'match-case' : 'hidden'); + $rows = $field->getContent()->getRows(); + $subFields = null; + foreach ($rows as $row) { + $subFields = $row->getDisplayer(); + if ($subFields instanceof displayer\Fields) { + continue; + } + $subFields->extKey('-watch-' . $key) //防止id重复 + ->addAttr('data-name="' . $subFields->getName() . ($subFields->isArrayValue() ? '[]' : '') . '"') + ->extNameKey('_' . $key); //防止name重复。真实name放在[data-name]中,case选中时替换到name属性中 + } + } else { + $field->extKey('-watch-' . $key) //防止id重复 ->addAttr('data-name="' . $field->getName() . ($field->isArrayValue() ? '[]' : '') . '"') - ->extNameKey('_' . $key); //防止name重复。真实name放在[data-name]中,case选中时替换到name属性中 + ->extNameKey('_' . $key) //防止name重复。真实name放在[data-name]中,case选中时替换到name属性中 + ->getWrapper()->addClass($matchCase ? 'match-case' : 'hidden'); } - } else { - $field->extKey('-watch-' . $key) //防止id重复 - ->addAttr('data-name="' . $field->getName() . ($field->isArrayValue() ? '[]' : '') . '"') - ->extNameKey('_' . $key) //防止name重复。真实name放在[data-name]中,case选中时替换到name属性中 - ->getWrapper()->addClass($matchCase ? '' : 'hidden'); - } + }); $this->fields[] = $field; // diff --git a/src/traits/HasDom.php b/src/traits/HasDom.php index 3204ed2..b0e83a8 100644 --- a/src/traits/HasDom.php +++ b/src/traits/HasDom.php @@ -104,7 +104,7 @@ trait HasDom public function getAttrWithStyle() { - return $this->attr . (empty($this->style) ? '' : ' style="' . $this->style . '"'); + return implode(' ', array_unique(explode(' ', $this->attr))) . (empty($this->style) ? '' : ' style="' . $this->style . '"'); } /** diff --git a/src/traits/actions/HasView.php b/src/traits/actions/HasView.php index 23e6416..457793d 100644 --- a/src/traits/actions/HasView.php +++ b/src/traits/actions/HasView.php @@ -72,6 +72,7 @@ trait HasView $displayer = $row->getDisplayer(); $fieldName = $displayer->getOriginName(); + $default = $displayer->getDefault(); if ( $displayer instanceof displayer\Button || $displayer instanceof displayer\Show || $displayer instanceof displayer\Raw @@ -112,18 +113,18 @@ trait HasView ) { // checkbox / multipleSelect(非ajax) / Transfer $displayer->whenScript(true); - $row->matches($fieldName, $row->getLabel())->options($displayer->getOptions()); + $row->matches($fieldName, $row->getLabel())->options($displayer->getOptions())->default($default); } else if ($displayer instanceof displayer\Radio || $displayer instanceof displayer\Select) { // radio / select(非ajax) $displayer->whenScript(true); - $row->match($fieldName, $row->getLabel())->options($displayer->getOptions()); + $row->match($fieldName, $row->getLabel())->options($displayer->getOptions())->default($default); } else if ($displayer instanceof displayer\Tree) { - $row->matches($fieldName, $row->getLabel())->separator('
')->optionsData($displayer->getOptions(), 'name'); + $row->matches($fieldName, $row->getLabel())->separator('
')->optionsData($displayer->getOptions(), 'name')->default($default); } else if ($displayer instanceof displayer\SwitchBtn) { $pair = $displayer->getPair(); $options = [$pair['on'] => __blang('bilder_option_on'), $pair['off'] => __blang('bilder_option_off')]; - $row->match($fieldName, $row->getLabel())->options($options); + $row->match($fieldName, $row->getLabel())->options($options)->default($default); } else if (!($displayer instanceof displayer\MultipleFile || $displayer instanceof displayer\Divider || $displayer instanceof displayer\Html)) {