// -------------------------------------------------------------------
// Get all plugin prefixes of a given type
function getPluginsByType(pluginsData, pluginType, onlyEnabled) {
var result = [];
pluginsData.forEach((plug) => {
if (plug.plugin_type == pluginType) {
// collect all, or if only enabled, check if NOT disabled
if (
onlyEnabled == false ||
(onlyEnabled && getSetting(plug.unique_prefix + "_RUN") != "disabled")
) {
result.push(plug.unique_prefix);
}
}
});
return result;
}
// -------------------------------------------------------------------
// Get plugin code name base on prefix
function getPluginCodeName(pluginsData, prefix) {
var result = "";
pluginsData.forEach((plug) => {
if (plug.unique_prefix == prefix) {
id = plug.code_name;
// console.log(id)
result = plug.code_name;
}
});
return result;
}
// -------------------------------------------------------------------
// Get plugin type base on prefix
function getPluginType(pluginsData, prefix) {
var result = "core";
pluginsData.forEach((plug) => {
if (plug.unique_prefix == prefix) {
id = plug.plugin_type;
// console.log(id)
result = plug.plugin_type;
}
});
return result;
}
// -------------------------------------------------------------------
// Get plugin config based on prefix
function getPluginConfig(pluginsData, prefix) {
result = "";
pluginsData.forEach((plug) => {
if (plug.unique_prefix == prefix) {
// console.log(id)
result = plug;
}
});
return result;
}
// -------------------------------------------------------------------
// Generate plugin HTML card based on prefixes in an array
function pluginCards(prefixesOfEnabledPlugins, includeSettings) {
html = "";
prefixesOfEnabledPlugins.forEach((prefix) => {
includeSettings_html = "";
includeSettings.forEach((set) => {
includeSettings_html += `
`;
});
return html;
}
// -----------------------------------------------------------------------------
// Open or close all settings
// -----------------------------------------------------------------------------
function toggleAllSettings(openOrClose = "") {
inStr = " in";
allOpen = true;
openIcon = "fa-angle-double-down";
closeIcon = "fa-angle-double-up";
$(".panel-collapse").each(function () {
if ($(this).attr("class").indexOf(inStr) == -1) {
allOpen = false;
}
});
if (allOpen == false || openOrClose == "open") {
// open all
openAllSettings();
$("#toggleSettings").attr(
"class",
$("#toggleSettings").attr("class").replace(openIcon, closeIcon)
);
} else {
// close all
$('div[data-myid="collapsible"]').each(function () {
$(this).attr("class", "panel-collapse collapse ");
});
$("#toggleSettings").attr(
"class",
$("#toggleSettings").attr("class").replace(closeIcon, openIcon)
);
}
}
function openAllSettings() {
$('div[data-myid="collapsible"]').each(function () {
$(this).attr("class", "panel-collapse collapse in");
});
$('div[data-myid="collapsible"]').each(function () {
$(this).attr("style", "height:inherit");
});
}
// -------------------------------------------------------------------
// Checks if all schedules are the same
function schedulesAreSynchronized(prefixesOfEnabledPlugins, pluginsData) {
plug_schedules = [];
prefixesOfEnabledPlugins.forEach((prefix) => {
pluginsData.forEach((plug) => {
if (plug.unique_prefix == prefix) {
plug_schedules.push(
getSetting(prefix + "_RUN_SCHD").replace(/\s/g, "")
); // replace all white characters to compare them easier
}
});
});
// Check if all plug_schedules are the same
if (plug_schedules.length > 0) {
const firstSchedule = plug_schedules[0];
return plug_schedules.every((schedule) => schedule === firstSchedule);
}
return true; // Return true if no schedules are found
}
// -------------------------------------------------------------------
// Checks if value is already encoded
function isSHA256(value) {
// Check if the value is a string and has a length of 64 characters
if (typeof value === "string" && value.length === 64) {
// Check if the value contains only hexadecimal characters
return /^[0-9a-fA-F]+$/.test(value);
} else {
return false;
}
}
// -------------------------------------------------------------------
// Utility function to check if the value is already Base64
function isBase64(value) {
const base64Regex =
/^(?:[A-Za-z0-9+\/]{4})*?(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/;
return base64Regex.test(value);
}
// -------------------------------------------------------------------
// Validation
// -------------------------------------------------------------------
function settingsCollectedCorrectly(settingsArray, settingsJSON_DB) {
// check if the required UI_LANG setting is in the array - if not something went wrong
$.each(settingsArray, function (index, value) {
if (value[1] == "UI_LANG") {
if (isEmpty(value[3]) == true) {
console.log(`⚠ Error: Required setting UI_LANG not found`);
showModalOk("ERROR", getString("settings_missing_block"));
return false;
}
}
});
const settingsCodeNames = settingsJSON_DB.map((setting) => setting.setKey);
const detailedCodeNames = settingsArray.map((item) => item[1]);
const missingCodeNamesOnPage = detailedCodeNames.filter(
(setKey) => !settingsCodeNames.includes(setKey)
);
const missingCodeNamesInDB = settingsCodeNames.filter(
(setKey) => !detailedCodeNames.includes(setKey)
);
// check if the number of settings on the page and in the DB are the same
if (missingCodeNamesOnPage.length !== missingCodeNamesInDB.length) {
console.log(
`⚠ Error: The following settings are missing in the DB or on the page (Reload page to fix):`
);
console.log(missingCodeNamesOnPage);
console.log(missingCodeNamesInDB);
showModalOk("ERROR", getString("settings_missing_block"));
return false;
}
// all OK
return true;
}
// -------------------------------------------------------------------
// Manipulating Editable List options
// -------------------------------------------------------------------
// ---------------------------------------------------------
// Add item to list
function addList(element, clearInput = true) {
const fromId = $(element).attr("my-input-from");
const toId = $(element).attr("my-input-to");
const input = $(`#${fromId}`).val();
console.log(`fromId | toId | input : ${fromId} | ${toId} | ${input}`);
const newOption = $("")
.attr("value", input)
.text(input);
// add new option
$(`#${toId}`).append(newOption);
// clear input
if (clearInput) {
$(`#${fromId}`).val("");
}
// Initialize interaction options only for the newly added option
initListInteractionOptions(newOption);
// flag something changes to prevent navigating from page
settingsChanged();
}
// ---------------------------------------------------------
function removeFromList(element) {
settingsChanged();
$(`#${$(element).attr("my-input-to")}`)
.find("option:last")
.remove();
}
// -------------------------------------------------------------------
// Function to remove an item from the select element
function removeOptionItem(option) {
settingsChanged();
option.remove();
}
// -------------------------------------------------------------------
// Update value of an item from the select element
function updateOptionItem(option, value) {
settingsChanged();
option.html(value);
option.val(value);
}
// -------------------------------------------------------------------
// Remove all options
function removeAllOptions(element) {
settingsChanged();
$(`#${$(element).attr("my-input-to")}`).empty();
}
// -------------------------------------------------------------------
// Add all options
function selectAll(element) {
settingsChanged();
// Get the