Skip to content

Commit

Permalink
Merge pull request #91 from kprajapatii/master
Browse files Browse the repository at this point in the history
Conditional field set default value changes - CHANGED
  • Loading branch information
kprajapatii authored Aug 7, 2024
2 parents 42f2716 + b813942 commit df39742
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
3 changes: 3 additions & 0 deletions change-log.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
= 0.2.23 - TBD =
* Conditional field set default value changes - CHANGED

= 0.2.22 - 2024-08-01 =
* Conditional field don't reset field value on hide element - FIXED

Expand Down
37 changes: 21 additions & 16 deletions includes/ayecode-ui-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2939,15 +2939,18 @@ function aui_cf_field_get_default_value($el) {
/**
* Reset field default value.
*/
function aui_cf_field_reset_default_value($el) {
function aui_cf_field_reset_default_value($el, bHide, setVal) {
var type = aui_cf_field_get_type($el), key = $el.data('rule-key'), field = aui_cf_field_default_values[key];
if (typeof setVal === 'undefined' || (typeof setVal !== 'undefined' && setVal === null)) {
setVal = field.value;
}

switch (type) {
case 'text':
case 'number':
case 'date':
case 'textarea':
$el.find('input:text,input[type="number"],textarea').val(field.value);
$el.find('input:text,input[type="number"],textarea').val(setVal);
break;
case 'phone':
case 'email':
Expand All @@ -2956,22 +2959,22 @@ function aui_cf_field_reset_default_value($el) {
case 'hidden':
case 'password':
case 'file':
$el.find('input[type="' + type + '"]').val(field.value);
$el.find('input[type="' + type + '"]').val(setVal);
break;
case 'select':
$el.find('select').find('option').prop('selected', false);
$el.find('select').val(field.value);
$el.find('select').val(setVal);
$el.find('select').trigger('change');
break;
case 'multiselect':
$el.find('select').find('option').prop('selected', false);
if ((typeof field.value === 'object' || typeof field.value === 'array') && !field.value.length && $el.find('select option:first').text() == '') {
if ((typeof setVal === 'object' || typeof setVal === 'array') && !setVal.length && $el.find('select option:first').text() == '') {
$el.find('select option:first').remove(); // Clear first option to show placeholder.
}
if (typeof field.value === 'string') {
$el.find('select').val(field.value);
if (typeof setVal === 'string') {
$el.find('select').val(setVal);
} else {
jQuery.each(field.value, function(i, v) {
jQuery.each(setVal, function(i, v) {
$el.find('select').find('option[value="' + v + '"]').attr('selected', true);
});
}
Expand All @@ -2980,20 +2983,20 @@ function aui_cf_field_reset_default_value($el) {
case 'checkbox':
if ($el.find('input[type="checkbox"]:checked').length >= 1) {
$el.find('input[type="checkbox"]:checked').prop('checked', false);
if (Array.isArray(field.value)) {
jQuery.each(field.value, function(i, v) {
if (Array.isArray(setVal)) {
jQuery.each(setVal, function(i, v) {
$el.find('input[type="checkbox"][value="' + v + '"]').attr('checked', true);
});
} else {
$el.find('input[type="checkbox"][value="' + field.value + '"]').attr('checked', true);
$el.find('input[type="checkbox"][value="' + setVal + '"]').attr('checked', true);
}
}
break;
case 'radio':
if ($el.find('input[type="radio"]:checked').length >= 1) {
setTimeout(function() {
$el.find('input[type="radio"]:checked').prop('checked', false);
$el.find('input[type="radio"][value="' + field.value + '"]').attr('checked', true);
$el.find('input[type="radio"][value="' + setVal + '"]').attr('checked', true);
}, 100);
}
break;
Expand Down Expand Up @@ -3055,13 +3058,15 @@ function aui_cf_field_apply_action($el, rule, isTrue) {
}
aui_cf_field_show_element($destEl);
} else if (rule.action === 'show' && !isTrue) {
if (!$destEl.is(':hidden') && !$destEl.hasClass('aui-cf-skip-reset')) {
aui_cf_field_reset_default_value($destEl);
if ((!$destEl.is(':hidden') || ($destEl.is(':hidden') && ($destEl.hasClass('aui-cf-force-reset') || ($destEl.closest('.aui-cf-use-parent').length && $destEl.closest('.aui-cf-use-parent').is(':hidden'))))) && !$destEl.hasClass('aui-cf-skip-reset')) {
var _setVal = $destEl.hasClass('aui-cf-force-empty') ? '' : null;
aui_cf_field_reset_default_value($destEl, true, _setVal);
}
aui_cf_field_hide_element($destEl);
} else if (rule.action === 'hide' && isTrue) {
if (!$destEl.is(':hidden') && !$destEl.hasClass('aui-cf-skip-reset')) {
aui_cf_field_reset_default_value($destEl);
if ((!$destEl.is(':hidden') || ($destEl.is(':hidden') && ($destEl.hasClass('aui-cf-force-reset') || ($destEl.closest('.aui-cf-use-parent').length && $destEl.closest('.aui-cf-use-parent').is(':hidden'))))) && !$destEl.hasClass('aui-cf-skip-reset')) {
var _setVal = $destEl.hasClass('aui-cf-force-empty') ? '' : null;
aui_cf_field_reset_default_value($destEl, true, _setVal);
}
aui_cf_field_hide_element($destEl);
} else if (rule.action === 'hide' && !isTrue) {
Expand Down

0 comments on commit df39742

Please sign in to comment.