dynamic dropdown support in FE - core app feature 💠

This commit is contained in:
Jokob-sk
2024-03-10 21:50:04 +11:00
parent a66df76f74
commit e38d2f9055
17 changed files with 465 additions and 77 deletions

18
front/js/db_methods.js Executable file
View File

@@ -0,0 +1,18 @@
// -----------------------------------------------------------------------------
// General utilities to interact with teh database
// -----------------------------------------------------------------------------
// Read data and place intotarget location, callback processies the results
function readData(sqlQuery, processDataCallback, targetLocation) {
var apiUrl = `php/server/dbHelper.php?action=read&rawSql=${encodeURIComponent(sqlQuery)}`;
$.get(apiUrl, function(data) {
// Process the JSON data using the provided callback function
data = JSON.parse(data)
var htmlResult = processDataCallback(data);
// Place the resulting HTML into the specified placeholder div
$("#" + targetLocation).replaceWith(htmlResult);
});
}

View File

@@ -112,15 +112,42 @@ function deleteAllCookies() {
function cacheSettings()
{
$.get('api/table_settings.json?nocache=' + Date.now(), function(res) {
settingsJSON = res;
data = settingsJSON["data"];
$.get('api/table_settings.json?nocache=' + Date.now(), function(resSet) {
data.forEach((set) => {
setCache(`pia_set_${set.Code_Name}`, set.Value)
});
$.get('api/plugins.json?nocache=' + Date.now(), function(resPlug) {
pluginsData = resPlug["data"];
settingsData = resSet["data"];
settingsData.forEach((set) => {
resolvedValue = set.Value;
setPlugObj = {};
options_params = [];
setPlugObj = getPluginSettingObject(pluginsData, set.Code_Name)
if(setPlugObj != {} && setPlugObj["options_params"])
{
options_params = setPlugObj["options_params"]
}
// check if options contains parameters and resolve
if(set.Value.includes("{value}"))
{
resolvedValue = resolveParams(options_params, set.Value)
console.log(resolvedValue)
}
setCache(`pia_set_${set.Code_Name}`, resolvedValue)
});
});
})
}
@@ -146,6 +173,8 @@ function getSetting (key) {
function cacheStrings()
{
console.log("aaaaaaaaaaaaaaaaaaaaaa")
// handle core strings and translations
var allLanguages = ["en_us", "es_es", "de_de", "fr_fr", "ru_ru", "nb_no"]; // needs to be same as in lang.php
@@ -907,7 +936,77 @@ function setupSmoothScrolling() {
}
}
// -------------------------------------------------------------------
// Function to check if options_params contains a parameter with type "sql"
function hasSqlType(params) {
for (let param of params) {
if (param.type === "sql") {
return true; // Found a parameter with type "sql"
}
}
return false; // No parameter with type "sql" found
}
// -------------------------------------------------------------------
// Function to check if string is SQL query
function isSQLQuery(query) {
// Regular expression to match common SQL keywords and syntax with word boundaries
var sqlRegex = /\b(SELECT|INSERT|UPDATE|DELETE|CREATE|DROP|ALTER|FROM|JOIN|WHERE|SET|VALUES|GROUP BY|ORDER BY|LIMIT)\b/i;
return sqlRegex.test(query);
}
// -------------------------------------------------------------------
// Get corresponding plugin setting object
function getPluginSettingObject(pluginsData, setting_key, unique_prefix ) {
result = {}
unique_prefix == undefined ? unique_prefix = setting_key.split("_")[0] : unique_prefix = unique_prefix;
$.each(pluginsData, function (i, plgnObj){
// go thru plugins
if(plgnObj.unique_prefix == unique_prefix)
{
// go thru plugin settings
$.each(plgnObj["settings"], function (j, setObj){
if(`${unique_prefix}_${setObj.function}` == setting_key)
{
result = setObj
}
});
}
});
return result
}
// -------------------------------------------------------------------
// Resolve all option parameters
function resolveParams(params, template) {
params.forEach(param => {
// Check if the template includes the parameter name
if (template.includes("{" + param.name + "}")) {
// If the parameter type is 'setting', retrieve setting value
if (param.type == "setting") {
var value = getSetting(param.value);
// Replace placeholder with setting value
template = template.replace("{" + param.name + "}", value);
} else {
// If the parameter type is not 'setting', use the provided value
template = template.replace("{" + param.name + "}", param.value);
}
}
});
// Log the resolved template
console.log(template);
// Return the resolved template
return template;
}
// -----------------------------------------------------------------------------
// initialize
@@ -955,8 +1054,11 @@ var pialert_common_init = sessionStorage.getItem(sessionStorageKey) === "true";
// Define a function that will execute the code only once
function executeOnce() {
if (!pialert_common_init) {
console.log("ffffffffffffffffffffffffffffffffffffff")
showSpinner()
// Your initialization code here
@@ -964,7 +1066,8 @@ function executeOnce() {
cacheStrings();
initDeviceListAll_JSON();
// Set the flag in sessionStorage to indicate that the code has been executed and save time when last time the page for initialized
// Set the flag in sessionStorage to indicate that the code has been executed
// and save time when last time the page for initialized
sessionStorage.setItem(sessionStorageKey, "true");
const millisecondsNow = Date.now();
sessionStorage.setItem(sessionStorageKey + '_time', millisecondsNow);

View File

@@ -163,4 +163,9 @@
}
return true; // Return true if no schedules are found
}
}

View File

@@ -107,6 +107,74 @@ $(function () {
});
// -----------------------------------------------------------------------------
// Initiate dropdown
function initSettingDropdown(settingKey, targetLocation)
{
var optionsHtml = ""
var targetLocation_options = settingKey + "_initSettingDropdown"
setVal = getSetting(settingKey)
// check if the result is a SQL query
if(isSQLQuery(setVal))
{
optionsHtml += `<option id="${targetLocation_options}"></option>`;
readData(setVal, generateDropdownOptions, targetLocation_options);
} else // this should be already an array, e.g. from a setting or pre-defined
{
options = createArray(setVal);
values = createArray(set['Value']);
options.forEach(option => {
let selected = values.includes(option) ? 'selected' : '';
optionsHtml += `<option value="${option}" ${selected}>${option}</option>`;
});
// Place the resulting HTML into the specified placeholder div
$("#" + targetLocation).replaceWith(optionsHtml);
}
}
// -----------------------------------------------------------------------------
// Data processors
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Processor to generate options for a dropdown menu
function generateDropdownOptions(data) {
var optionsHtml = "";
data.forEach(function(item) {
optionsHtml += `<option value="${item.id}">${item.name}</option>`;
});
return `${optionsHtml}`;
}
// -----------------------------------------------------------------------------
// Processor to generate a list
function generateList(data) {
var listHtml = "";
data.forEach(function(item) {
listHtml += `<li>${item.name}</li>`;
});
listHtml += "";
return listHtml;
}
// -----------------------------------------------------------------------------
// initialize
// -----------------------------------------------------------------------------