(function() { // Return the table row element for a setting. function findSettingRow(id) { if (id) { return document.querySelector('#settings-table tr[data-id="' + id + '"]'); } else { return null; } } // Return the number of settings that have been edited. function changesCount() { return document.querySelectorAll("#settings-table tbody tr[data-edited=true]").length; } // Set the enabled status of the save button for submitting the form. function enableSaveButton() { const saveButton = document.querySelector("#super-settings-save-settings"); const discardButton = document.querySelector("#super-settings-discard-changes"); if (saveButton) { const count = changesCount(); const countSpan = saveButton.querySelector(".count"); if (count === 0) { saveButton.disabled = true; countSpan.innerHTML = ""; discardButton.disabled = true; } else { saveButton.disabled = false; countSpan.innerHTML = count; discardButton.disabled = false; } } } // Set the display value for a setting. function setSettingDisplayValue(element, setting) { if (setting.value === null || setting.value === undefined) { element.innerText = ""; } else if (Array.isArray(setting.value)) { let arrayHTML = ""; setting.value.map(function(val) { arrayHTML += `
${escapeHTML(val)}
`; }); element.innerHTML = arrayHTML; } else if (setting.value_type === "datetime") { try { const datetime = new Date(Date.parse(setting.value)); element.innerText = dateFormatter().format(datetime); } catch (e) { element.innerText = "" + setting.value } } else { element.innerText = "" + setting.value } } // Get the value of a setting from the edit form field. function getSettingEditValue(row) { if (row.querySelector(".super-settings-value input.js-setting-value[type=checkbox]")) { return row.querySelector(".super-settings-value input.js-setting-value[type=checkbox]").checked; } else { return row.querySelector(".super-settings-value .js-setting-value").value; } } // Helper function to pad time values with a zero for making ISO-8601 date and time formats. function padTimeVal(val) { return ("" + val).padStart(2, "0"); } // Escape special HTML characters in text. function escapeHTML(text) { if (text === null || text === undefined) { return ""; } const htmlEscapes = {'&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '/': '/'}; const htmlEscaper = /[&<>"'\/]/g; return ('' + text).replace(htmlEscaper, function(match) { return htmlEscapes[match]; }); } // Helper function for use with templates to replace the text {{id}} with a setting's id value. function mustacheSubstitute(html, setting) { return html.replaceAll("{{id}}", escapeHTML(setting.id)); } // Extract a new DOM element from a