(function ($) {
// Ensure parent category is bold only if it has children
$('.categorychecklist li').each(function () {
var $this = $(this);
if ($this.children('.children').length) {
$this.children('label').css('font-weight', 'bold');
}
});
// Collapsible subcategories
$('.categorychecklist li').each(function () {
if ($(this).children('.children').length) {
$(this)
.children('.children')
.hide();
$(this)
.children('label')
.before(
'[+][-]'
);
}
});
$('.categorychecklist').on('click', 'a.toggle-subcategories', function (e) {
e.preventDefault();
e.stopPropagation();
var $this = $(this);
$this.parent()
.children('.children')
.slideToggle('fast', function () {
$this.toggle();
$this.siblings('.toggle-subcategories').toggle();
});
});
// Auto-select parent categories when child is selected
$('.categorychecklist').on('change', ':checkbox', function () {
var $this = $(this);
if ($this.is(':checked')) {
// Check all parent categories
$this.parents('li').children('input:checkbox').prop('checked', true);
} else {
// Uncheck child categories if parent is unchecked
$this.closest('li').find('input:checkbox').prop('checked', false);
}
});
// Prevent default category from selecting unrelated categories
$('.categorychecklist input[type="checkbox"]').on('change', function () {
const defaultCategoryId = 1; // Replace with the ID of your default category (General Knowledge Mcqs)
if ($(this).val() == defaultCategoryId) {
if ($(this).is(':checked')) {
// Uncheck all other categories
$('.categorychecklist input[type="checkbox"]')
.not(this)
.prop('checked', false);
// Clear 'Make Primary' for unrelated categories
$('.categorychecklist li').each(function () {
if (
$(this).find('input[type="checkbox"]').prop('checked') === false
) {
$(this).find('.cat-item-action').remove();
}
});
}
}
});
// Update 'Make Primary' visibility based on checked categories
$('.categorychecklist input[type="checkbox"]').on('change', function () {
const $parentLi = $(this).closest('li');
const isChecked = $(this).prop('checked');
if (isChecked) {
$parentLi.find('.cat-item-action').show();
} else {
$parentLi.find('.cat-item-action').remove();
}
});
})(jQuery);