// 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 } var optionCssFiles = JSON.parse($('#optionCssFiles').val()); //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')) { $(item).val((inheritPossible ? 'inherit' : false)); } 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')) { $(item).val((inheritPossible ? 'inherit' : false)); } 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