$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')) {
$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)) {
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;
}
}
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;
}
//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);
}
}
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};
}
condition.id = condition.id.toString();
normalizedConditions[k] = condition;
});
- res[property] = normalizedConditions;
+ w[property].values = normalizedConditions;
});
- return res;
+ return w;
}
function isObject(value) {
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();