From 478ed5562852cabc36c6117e594f3103cc71ba60 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 29 Dec 2023 15:41:49 +0100 Subject: [PATCH] wip #6571 @3 --- src/app/Magic/Fields/Field.php | 31 ++++++++++++++++++++-- src/resources/views/when.blade.php | 41 +++++++++++++++++------------- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index cb49bec..8bae170 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -406,8 +406,8 @@ class Field implements \ArrayAccess $wrapperAttributes['data-default'] = $this->filterDefault($this->getAttribute('default')); $wrapperAttributes['data-name'] = $this->getAttribute('name'); - if ($this->hasAttribute('when')) { - $wrapperAttributes['data-when'] = json_encode($this->getAttribute('when')); + if ($when = $this->when()) { + $wrapperAttributes['data-when'] = $when; } if (!$this->getAttribute('non_default_tracking')) { @@ -416,6 +416,33 @@ class Field implements \ArrayAccess $this->setAttribute('wrapperAttributes', $wrapperAttributes); } + protected function when(): bool|string + { + $defaultOperator = '='; + if (!$this->hasAttribute('when')) { + return false; + } + + $when = $this->getAttribute('when'); + $res = []; + foreach ($when as $attr => $w) { + if (is_scalar($w)) { + $w = ['values' => [$w], 'operator' => $defaultOperator]; + } else { + if (isset($w['values'])) { + if (!isset($w['operator'])) { + $w['operator'] = $defaultOperator; + } + } else { + $w = ['values' => $w, 'operator' => $defaultOperator]; + } + } + $res[$attr] = $w; + } + + return json_encode($res); + } + public function setPermissions() { if (null !== $this->getAttribute('can', null)) { diff --git a/src/resources/views/when.blade.php b/src/resources/views/when.blade.php index e5ce471..1396342 100644 --- a/src/resources/views/when.blade.php +++ b/src/resources/views/when.blade.php @@ -31,14 +31,12 @@ var when = $(this).data('when-normalized'); var match = true; - var order = 1000; + var order = 10000; $.each(when, function (property, conditions) { var e = findCloserInput(element, property); - if ($(element).is('[bp-field-name]') && $(element).attr('bp-field-name').indexOf('logattempts') >= 0) { - console.log(property, element, e); - } var val; if ($(e).length === 0) { + console.log(property, 'property value not found') match = false; return false; } @@ -62,16 +60,15 @@ } var matchproperty = false; - $.each(conditions, function (k, condition) { - if ((condition.id === '_not_empty_' && val.toString().length > 0) || - condition.id === val) { - + $.each(conditions.values, function (k, condition) { + if (checkCondition(condition, conditions.operator, val)) { + console.log('match', property, conditions, val); matchproperty = true; order = Math.min(order, condition.order); - return false; } }); if (matchproperty === false) { + console.log('not match', property, conditions, val); match = false return false; } @@ -86,6 +83,18 @@ //orderWhen(); }; + function checkCondition(condition, operator, val) { + if (condition.id === '_not_empty_' || condition.operator === 'not_empty') { + return val.toString().length > 0; + } + if (operator === '=') { + return condition.id == val; + } + if (operator === '!=' || operator === '!') { + return condition.id != val; + } + } + function isNumber(n) { return typeof n === 'number' && !isNaN(n); } @@ -113,13 +122,9 @@ } function normalizeWhen(w) { - var res = {}; $.each(w, function (property, conditions) { - if (!Array.isArray(conditions)) { - conditions = [conditions]; - } var normalizedConditions = []; - $.each(conditions, function (k, condition) { + $.each(conditions.values, function (k, condition) { if (!isObject(condition)) { condition = {id: condition}; } @@ -129,9 +134,9 @@ condition.id = condition.id.toString(); normalizedConditions[k] = condition; }); - res[property] = normalizedConditions; + w[property].values = normalizedConditions; }); - return res; + return w; } function isObject(value) { @@ -142,9 +147,9 @@ let found; if ($(element).is('form')) { - found = $(element).find('[data-name="' + inputName + '"]').eq(0); + found = $(element).find('[bp-field-name="' + inputName + '"]').eq(0); } else { - found = $(element).siblings('[data-name="' + inputName + '"]'); + found = $(element).siblings('[bp-field-name="' + inputName + '"]'); } if (found.length === 0) { let parent = $(element).parent(); -- 2.39.5