<?php
/**
 * ------------------------ 
 *  版权所有  www.tecmz.com
 *  商业版本请购买正版授权使用
 * ------------------------
*/ namespace ModStart\Field\Concern; use Illuminate\Support\Arr; use ModStart\Core\Util\ArrayUtil; use ModStart\Field\AbstractField; use ModStart\Field\Checkbox; use ModStart\Field\Radio; use ModStart\Field\Select; use ModStart\Field\SwitchField; use ModStart\Form\Form; use ModStart\ModStart; trait CanCascadeFields { protected $conditions = array(); protected $cascadeGroups = array(); public function when($hanfW, $tzAUI, $YX_0r = null) { goto JCt7B; MJrxa: $this->formatValues($hanfW, $tzAUI); goto is0Gz; JCt7B: if (func_num_args() == 2) { goto Pf7y_; FMM2K: $tzAUI = $hanfW; goto fzpxX; Pf7y_: $YX_0r = $tzAUI; goto FMM2K; fzpxX: $hanfW = $this->getDefaultOperator(); goto pk2Zg; pk2Zg: } goto MJrxa; is0Gz: $this->addDependents($hanfW, $tzAUI, $YX_0r); goto oOsNQ; oOsNQ: return $this; goto jjM51; jjM51: } protected function getDefaultOperator() { if ($this instanceof Checkbox) { return 'in'; } return '='; } protected function formatValues($hanfW, &$tzAUI) { if (in_array($hanfW, array('in', 'notIn'))) { $tzAUI = Arr::wrap($tzAUI); } if (is_array($tzAUI)) { $tzAUI = array_map('strval', $tzAUI); } else { $tzAUI = strval($tzAUI); } } protected function addDependents($hanfW, $tzAUI, \Closure $YX_0r) { goto i1Rx3; Hx8Vh: $this->conditions[] = array('operator' => $hanfW, 'value' => $tzAUI, 'closure' => $YX_0r, 'index' => $XIWRg); goto mvv82; i1Rx3: $XIWRg = count($this->conditions); goto Hx8Vh; mvv82: $this->context->cascadeGroup($YX_0r, array('id' => $this->id(), 'index' => $XIWRg)); goto A0m2y; A0m2y: } protected function addCascadeScript() { goto r72g8; d3iLr: ModStart::script($li24p); goto EbsvZ; cG7yF: $li24p = "(function () {\n    var operatorTable = {\n       '=': function(a, b) {\n           if (\$.isArray(a) && \$.isArray(b)) {\n               return \$(a).not(b).length === 0 && \$(b).not(a).length === 0;\n           }\n           return String(a) === String(b);\n       },\n       '>': function(a, b) {\n           return a > b;\n       },\n       '<': function(a, b) {\n           return a < b;\n       },\n       '>=': function(a, b) { return a >= b; },\n       '<=': function(a, b) { return a <= b; },\n       '!=': function(a, b) {\n            return ! operatorTable['='](a, b);\n       }\n   };\n   var compare = function (a, b, o) {\n       if (! \$.isArray(b) ) return operatorTable[o](a, b);\n       if (o === '!=') {\n           for (var i in b) {\n               if (! operatorTable[o](a, b[i])) return false;\n           }\n           return true;\n       }\n       for (var i in b) {\n           if (operatorTable[o](a, b[i])) return true;\n       }\n       return false;\n   };\n   var cascadeGroups = {$oo7UU};\n   var cascadeChange = function(value){\n       cascadeGroups.forEach(function (group) {\n           var groupDom = \$('#{$this->id()}_group_' + group.index);\n           var pass = compare(value, group.value, group.operator);\n           // console.log(value, group.operator, group.value, pass);\n           if (pass) {\n               groupDom.removeClass('cascade-group-hide');\n           } else {\n               groupDom.addClass('cascade-group-hide');\n           }\n        });\n   };\n   {$this->getFieldNormalizedScript()}\n})();"; goto d3iLr; r72g8: if (empty($this->conditions)) { return; } goto DsS4z; DsS4z: $oo7UU = collect($this->conditions)->map(function ($eVURJ) use(&$XIWRg) { return ArrayUtil::keepKeys($eVURJ, array('operator', 'value', 'index')); })->toJson(); goto EVQoQ; EVQoQ: $UFOLC = $this->id(); goto cG7yF; EbsvZ: } protected function getFieldNormalizedScript() { if ($this->context instanceof Form) { switch (get_class($this)) { case Select::class: return "\$('#{$this->id()} select').on('change', function (e) {\n    cascadeChange(\$(this).val());\n}).trigger('change');"; case Radio::class: return "\$('#{$this->id()}').on('click', function (e) {\n    cascadeChange(\$('#{$this->id()} [type=radio]:checked').val());\n}).trigger('click');"; case Checkbox::class: return "\$('#{$this->id()}').on('click', function (e) {\n    var value = \$('#{$this->id()} [type=checkbox]:checked').map(function(){\n      return \$(this).val();\n    }).get();\n    cascadeChange(value);\n}).trigger('click');"; case SwitchField::class: return "\$('#{$this->id()}').on('click', function (e) {\n    cascadeChange(\$('#{$this->id()} [type=checkbox]').is(':checked')?1:0);\n}).trigger('click');"; default: throw new \InvalidArgumentException('Invalid form field type'); } } else { $tzAUI = json_encode($this->value); switch (get_class($this)) { case Select::class: return "cascadeChange({$tzAUI});"; case Radio::class: return "cascadeChange({$tzAUI});"; case Checkbox::class: return "cascadeChange({$tzAUI});"; case SwitchField::class: return "cascadeChange({$tzAUI});"; default: throw new \InvalidArgumentException('Invalid form field type'); } } } }