// Custom options-page JavaScript for this theme (used because config.xml sets
// custom). This is a copy of the core package
// assets/packages/themeoptions-core/themeoptions-core.js, patched so that
// .action_hide_on_inherit rows are also hidden when "Inherit everything" is on
// (the custom page has no per-category tabs to disable).
// When updating, diff against the core file and re-apply that patch.
var ThemeOptions = function () {
"use strict";
//////////////////
// Define necessary globals
// general_inherit_active is not present at global level
// see: https://github.com/LimeSurvey/LimeSurvey/blob/1cbfa11b081f54763b28364472926b155efea5dc/themes/survey/vanilla/options/options.twig#L71
var inheritPossible = ($('#general_inherit_active').length > 0);
//get option Object from Template configuration options
var optionObject = {
"general_inherit": 1
}
// THEME ADDITION (row 115, generalized row 135): legacy hex values of the
// color-chooser options, captured right after parseOptionObject — later startup
// steps (prepareSelectField val() with a non-matching value, then
// updateFieldSettings) can clobber optionObject, so the migration in colorModeUI
// must use this snapshot.
var legacyColorValues = {};
// row 157: legacy non-hex MODE values of the chooser options (e.g. "primary"/
// "secondary" stored by the old 4-way grouptitle/groupdescription textcolor
// dropdowns) — mapped to color1/color2 in colorModeUI
var legacyModeValues = {};
// row 135: all options that use the color-chooser scheme (mode dropdown with
// class selector_color_mode + swatch chips + a custom colorpicker child);
// row 157: the group title/description text colors joined the scheme;
// row 180 / plan P2: the Questions "title" section keys as well
// (questiontitletextcolor, questiontitlebackgroundcolor, questionbackgroundcolor);
// row 184 / plan P3: answerstextcolor as well; row 186 / plan P4:
// questionhelpbackgroundcolor as well; row 191 / plan P6: the six per-button
// choosers (previous/next/submit button background + text color); row 195:
// checkboxcolor as well; row 197: questionhelpfontcolor + questionbordercolor.
// Must stay in sync with aColorChooserKeys in options/options.twig.
var aColorChooserKeys = ['navbarfontcolor', 'headerbackgroundcolor', 'progressbarcolor', 'progressbarbackgroundcolor', 'progressbarfontcolor', 'bodybackgroundcolor', 'grouptitletextcolor', 'groupdescriptiontextcolor', 'questiontitletextcolor', 'questiontitlebackgroundcolor', 'questionbackgroundcolor', 'answerstextcolor', 'questionhelpbackgroundcolor', 'questionhelpfontcolor', 'questionbordercolor', 'checkboxcolor', 'previousbuttonbackgroundcolor', 'previousbuttontextcolor', 'nextbuttonbackgroundcolor', 'nextbuttontextcolor', 'submitbuttonbackgroundcolor', 'submitbuttontextcolor'];
// the 5 presets, in the SAME order as the dropdown options in config.xml
// (object key order = swatch chip order) — row 125: "Default background color"
// is the SECOND choice, right after "Default font color".
var colorPresets = { 'default': 'fontcolor', 'defaultbackgroundcolor': 'defaultbackgroundcolor', 'color1': 'primarycolor', 'color2': 'secondarycolor', 'color3': 'tertiarycolor' };
// one refreshAll per chooser, called by the shared delegated handlers
var colorModeRefreshers = [];
// THEME FIX: on the GLOBAL theme-options page the hidden #optionCssFiles can
// hold the literal string "inherit" (not JSON) — an unguarded JSON.parse then
// throws inside this constructor, so run() never executes: no change handlers
// and no save handler get bound, and Save silently posts the stale options
// JSON (nothing changed in the UI is saved). Fall back to an empty file list.
var optionCssFiles;
try {
optionCssFiles = JSON.parse($('#optionCssFiles').val());
} catch (e) {
optionCssFiles = { add: [], replace: [], remove: [] };
}
//get the global form
var globalForm = $('.action_update_options_string_form');
/////////////////
// Define methods run on startup
// #TemplateConfiguration_options is the id of the Options field in advanced option
// getter for generalInherit
var generalInherit = function () {
return $('#TemplateConfiguration_options').val() === 'inherit';
};
//parse the options as set in the advanced form
var parseOptionObject = function () {
// If no general inherit, then pass the value of the "Options" field in advanced option to the object optionObject
if ($('#TemplateConfiguration_options').length > 0 && !generalInherit()) {
try {
optionObject = JSON.parse($('#TemplateConfiguration_options').val());
} catch (e) {
console.ls ? console.ls.error('No valid option field!') : console.log('No valid option field!');
}
}
};
// Show/Hide fields on generalInherit
// To hide a simple option on generalInherit: just add the class "action_hide_on_inherit" to the rows continaing it
var startupGeneralInherit = function () {
if (!inheritPossible) return false;
if (generalInherit()) {
$('#general_inherit_on').prop('checked', true).trigger('change').closest('label').addClass('active');
$('.action_hide_on_inherit').addClass('d-none');
$('.tab_action_hide_on_inherit').addClass('ls-tab-disabled');
} else {
$('#general_inherit_off').prop('checked', true).trigger('change').closest('label').addClass('active');
$('.action_hide_on_inherit_wrapper').addClass('d-none');
$('.action_hide_on_inherit').removeClass('d-none');
$('.tab_action_hide_on_inherit').removeClass('ls-tab-disabled');
}
};
// So this function find the selectors in the forum, and pass their values to the advanced options
var updateFieldSettings = function () {
if ($('#general_inherit_on').prop('checked')) {
$('#TemplateConfiguration_options').val('inherit');
return;
}
globalForm.find('.selector_option_value_field').each(function (i, item) {
//disabled items should be inherit or false
if ($(item).prop('disabled')) {
// THEME ADDITION (row 115, navbarfontcolor working example): a child of a
// "disable"-mode parent (data-keep-value) keeps its stored value instead of
// being reset — the grayed-out picker must remember the custom color even
// when another mode is selected and the form is saved.
if ($(item).data('keep-value')) {
if (optionObject[$(item).attr('name')] === undefined) {
optionObject[$(item).attr('name')] = ($(item).data('inheritvalue') !== undefined ? String($(item).data('inheritvalue')) : '');
}
} else {
$(item).val((inheritPossible ? 'inherit' : false));
optionObject[$(item).attr('name')] = getOptionFieldValue(item);
}
} else {
optionObject[$(item).attr('name')] = getOptionFieldValue(item);
}
});
globalForm.find('.selector_image_selector').each(function (i, item) {
// disable the preview image button if the image
// selected could not be mapped to one of the images
// that actually exists within the theme
const src = $(item).find('option:selected').data('lightbox-src');
const missing = src === '';
const itemId = $(item).attr('id');
const button = $(`button[data-bs-target="#${itemId}"]`);
button.prop('disabled', missing);
// add some feedback to the user, mark field invalid
$(item).toggleClass('is-invalid', missing);
});
globalForm.find('.selector_option_radio_field ').each(function (i, item) {
//disabled items should be inherit or false
if ($(item).prop('disabled')) {
$(item).val((inheritPossible ? 'inherit' : false));
}
if ($(item).attr('type') == 'radio') {
if ($(item).prop('checked')) {
optionObject[$(item).attr('name')] = $(item).val();
}
}
});
// THEME ADDITION: checkboxes (checked = "on"); an untouched box that was
// "inherit" keeps "inherit" (see prepareCheckboxField)
globalForm.find('.selector_option_checkbox_field').each(function (i, item) {
if ($(item).prop('disabled')) {
optionObject[$(item).attr('name')] = (inheritPossible ? 'inherit' : 'off');
} else if ($(item).data('was-inherit')) {
optionObject[$(item).attr('name')] = 'inherit';
} else {
optionObject[$(item).attr('name')] = $(item).prop('checked') ? 'on' : 'off';
}
});
globalForm.find('.selector_text_option_value_field').each(function (i, item) {
//disabled items should be inherit or false
if ($(item).prop('disabled')) {
// THEME ADDITION (row 115): see the selector_option_value_field loop above —
// data-keep-value children keep their stored value while disabled.
if ($(item).data('keep-value')) {
if (optionObject[$(item).attr('name')] === undefined) {
optionObject[$(item).attr('name')] = ($(item).data('inheritvalue') !== undefined ? String($(item).data('inheritvalue')) : '');
}
} else {
$(item).val((inheritPossible ? 'inherit' : false));
optionObject[$(item).attr('name')] = $(item).val();
}
} else {
optionObject[$(item).attr('name')] = $(item).val();
}
});
var newOptionObject = $.extend(true, {}, optionObject);
delete newOptionObject.general_inherit;
$('#TemplateConfiguration_options').val(JSON.stringify(newOptionObject));
};
///////////////
// Utility Methods
// -- small utilities i.g. for images or similar, or very specialized functions
// Color picker text fields are left empty when the value is inherited, so they show the translated placeholder instead of "inherit"
var getOptionFieldValue = function (item) {
if ($(item).hasClass('selector__color-picker') && $(item).val() === '' && inheritPossible) {
return 'inherit';
}
return $(item).val();
};
var applyColorPickerValue = function (item) {
if ($(item).hasClass('selector__color-picker')) {
console.ls.log($(item).data('inheritvalue'), $(item).val(), item);
if ($(item).val() === 'inherit') {
$(item).val('');
$(item).closest('.input-group').find('.selector__colorpicker-inherit-value').val($(item).data('inheritvalue')).trigger('change');
} else {
$(item).closest('.input-group').find('.selector__colorpicker-inherit-value').val($(item).val()).trigger('change');
}
}
};
//Special code for numerical
var parseNumeric = function (item) {
if ($(item).hasClass('selector-numerical-input')) {
if (!(/^((\d+)|(inherit))$/.test($(item).val()))) {
$(item).val((inheritPossible ? 'inherit' : 1000));
}
}
};
// display inherited options as tooltips
var showInheritedValue = function () {
$.each($('#simple_edit_add_css > option'), function (i, option) {
$.each(optionCssFiles.add, function (i, item) {
if (option.value === item && $('#simple_edit_add_css option:first').val() === 'inherit') {
$('#simple_edit_add_css option:first').text($('#simple_edit_add_css option:first').text() + ' ' + option.text + ']');
}
});
});
globalForm.find('.selector-numerical-input').each(function (i, item) {
var element = $(item);
//element.attr('title', element.attr('title')+optionObjectInheritedValues[$(item).attr('name')]);
element.tooltip();
});
};
//Parses the option value for an item
var parseOptionValue = function (item, fallbackValue) {
if (fallbackValue == undefined) fallbackValue = false;
// If general inherit, then the value of the dropdown is inherit, else it's the value defined in advanced options
var itemValue = generalInherit() ? 'inherit' : optionObject[$(item).attr('name')];
// If anything goes wrong (manual edit or anything else), we make sure it will have a correct value
if (itemValue == null || itemValue == undefined) {
itemValue = inheritPossible ? 'inherit' : fallbackValue;
optionObject[$(item).attr('name')] = itemValue;
}
return itemValue;
};
//Set value and propagate to bootstrapSwitch
var setAndPropageteToSwitch = function (item) {
$(item).prop('checked', true).trigger('change');
//$(item).closest('label').addClass('active');
};
///////////////
// Parser methods
// -- These methods will either parse through existing fields, or set existing fields to their correct values
// Update values in the form to the template options
// selector_option_value_field are the select dropdown (like variations and fonts)
// TODO: This seems to be designed for select fields only, but it is also used for other input types. Should be reviewed.
var prepareSelectField = function () {
globalForm.find('.selector_option_value_field').each(function (i, item) {
var itemValue = parseOptionValue(item);
$(item).val(itemValue);
applyColorPickerValue(item);
});
};
// THEME ADDITION: checkbox options (type "checkbox", class
// .selector_option_checkbox_field): checked = "on", unchecked = "off".
// When the saved value is "inherit" (survey-level inherit), the box shows
// the inherited state (data-inheritvalue) and is marked data was-inherit,
// so updateFieldSettings() keeps writing "inherit" until the user actually
// toggles the box (the change handler in hotSwapFields clears the mark).
var prepareCheckboxField = function () {
globalForm.find('.selector_option_checkbox_field').each(function (i, item) {
var itemValue = parseOptionValue(item, 'off');
var effective = (itemValue === 'inherit') ? $(item).data('inheritvalue') : itemValue;
$(item).prop('checked', String(effective) === 'on');
if (itemValue === 'inherit') {
$(item).data('was-inherit', '1');
$(item).attr('title', 'Inherited value: ' + $(item).data('inheritvalue'));
}
});
};
// Generate the state of switches (On/Off/Inherit)
var parseParentSwitchFields = function () {
globalForm.find('.selector_option_radio_field').each(function (i, item) {
var itemValue = parseOptionValue(item, 'off');
//if it is a radio selector, check it and propagate the change to bootstrapSwitch
if ($(item).val() == itemValue) {
setAndPropageteToSwitch(item);
}
});
};
var prepareFontField = function () {
var currentPackageObject = 'inherit';
// THEME CHANGE: fallback is the bundled default font of this theme
// ('noto_sans', self-hosted), never a core package font like roboto/ibm-sans
// (privacy: no external font hosts may ever be requested).
optionObject.font = optionObject.font || (inheritPossible ? 'inherit' : 'noto_sans');
if (optionObject.font !== 'inherit') {
$('#simple_edit_options_font').val(optionObject.font);
}
updateFieldSettings();
};
var prepareFruityThemeField = function () {
var currentThemeObject = 'inherit';
if ($('#TemplateConfiguration_files_css').val() !== 'inherit' && $('body').hasClass('fruity')) {
currentThemeObject = {
"add": ['css/animate.css', 'css/ajaxify.css', 'css/variations/sea_green.css', 'css/theme.css', 'custom.css']
};
try {
currentThemeObject = JSON.parse($('#TemplateConfiguration_files_css').val());
} catch (e) {
console.error('No valid monochrom theme field!');
}
if (currentThemeObject.add) {
$('#simple_edit_add_css').val(currentThemeObject.add.filter(function (item, i) {
return /^css\/variations\/.*$/.test(item);
}));
}
}
};
// Update values of 'text' options in the form
var prepareTextField = function () {
globalForm.find('.selector_text_option_value_field').each(function (i, item) {
var itemValue = parseOptionValue(item, "");
$(item).val(itemValue);
});
};
// updates the disabled status of a child field
// based on the parent element
// NOTE:
// for font and variations dropdowns, the childfield
// class is added and the data-parent attr exists,
// but no parent element exists in the markup
// so if we actually have a parent element, enable/disable
// based on that, otherwise we enable by default
const updateChild = function(parentEl, childEl) {
let enabled = true;
if(parentEl.length) {
const wrapper = $(childEl).closest('.option-visibility');
const showValues = String(wrapper.data('show-if-value') || 'on').split('|');
// THEME ADDITION (Prompt U): the parent may also be a