Skip to content

Commit 6442010

Browse files
committed
Refactors handling display/hide on checking checkbox/radio
1 parent 1eed17e commit 6442010

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

web/assets/v5/js/custom.js

+33-14
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ PUPHPET.updateOtherInputOnCheck = function() {
188188
return 1;
189189
}
190190

191-
$target.prop('checked', checked);
191+
$target.prop('checked', checked).trigger('change');
192192

193193
return true;
194194
}
195195

196196
if (!$target.is(':radio') && !$target.is(':checkbox')) {
197-
$target.val(value);
197+
$target.val(value).trigger('change');
198198
}
199199
});
200200
});
@@ -442,32 +442,51 @@ PUPHPET.deleteBlock = function() {
442442
* Can hide or show an element depending on a radio element's state
443443
*/
444444
PUPHPET.toggleDisplayOnSelect = function() {
445-
$(document).on('change', '.toggle-on-select, .display-on-select, .hide-on-select', function(e){
446-
var dataValue = this.getAttribute('data-target');
445+
$(document).on('change', '.show-on-select', function(e) {
446+
var dataValue = this.getAttribute('data-vis-show-target');
447447

448448
if (dataValue == undefined) {
449449
return;
450450
}
451451

452-
var targetId = snakeCaseToDash(this.getAttribute('data-target'));
453-
console.log(targetId);
454-
var $target = $(targetId);
452+
var targetId = snakeCaseToDash(dataValue);
453+
$(targetId).hide().removeClass('hidden').slideDown();
454+
});
455455

456-
if ($(this).hasClass('display-on-select')) {
457-
$target.slideDown();
458-
$target.removeClass('hidden');
456+
$(document).on('change', '.hide-on-select', function(e) {
457+
var dataValue = this.getAttribute('data-vis-hide-target');
459458

459+
if (dataValue == undefined) {
460460
return;
461461
}
462462

463-
if ($(this).hasClass('hide-on-select')) {
464-
$target.slideUp();
463+
var targetId = snakeCaseToDash(dataValue);
464+
$(targetId).slideUp();
465+
});
465466

467+
$(document).on('change', '.toggle-on-select', function(e){
468+
var dataValue = this.getAttribute('data-vis-toggle-target');
469+
470+
if (dataValue == undefined) {
466471
return;
467472
}
468473

469-
$target.slideToggle();
470-
$target.removeClass('hidden');
474+
var targetId = snakeCaseToDash(dataValue);
475+
var $target = $(targetId);
476+
477+
// If unchecking, and target is visible, hide target
478+
if ($(this).not(':checked') && $target.css('display') != 'none') {
479+
$target.slideUp();
480+
481+
return true;
482+
}
483+
484+
// If checking, and target is invisible, show target
485+
if ($(this).is(':checked') && $target.css('display') == 'none') {
486+
$target.hide();
487+
$target.removeClass('hidden');
488+
$target.slideDown();
489+
}
471490
});
472491
};
473492

0 commit comments

Comments
 (0)