优化hasWhen
This commit is contained in:
parent
b19c6790fb
commit
c4a36fb7cc
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
//
|
||||
|
|
|
@ -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 . '"');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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('<br/>')->optionsData($displayer->getOptions(), 'name');
|
||||
$row->matches($fieldName, $row->getLabel())->separator('<br/>')->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)) {
|
||||
|
||||
|
|
Loading…
Reference in New Issue