This commit is contained in:
jokob-sk
2025-03-20 11:02:54 +11:00
parent 0512ddd143
commit 92fce6f14a
2 changed files with 116 additions and 87 deletions

View File

@@ -1891,13 +1891,18 @@ input[readonly] {
padding: 10px; padding: 10px;
} }
.workflow-card, .condition-list, .actions-list .workflow-card, .actions-list
{ {
display: grid; display: contents;
padding: 5px; padding: 5px;
padding-left: 10px; padding-left: 10px;
} }
.condition-list
{
z-index:1;
}
.condition .condition
{ {
padding: 5px; padding: 5px;

View File

@@ -52,6 +52,7 @@ let actionTypes = [
// -------------------------------------- // --------------------------------------
// Retrieve and process the data // Retrieve and process the data
function getData() { function getData() {
showSpinner(); showSpinner();
getSetting() getSetting()
@@ -60,9 +61,7 @@ function getData() {
workflows = res; workflows = res;
console.log(workflows); console.log(workflows);
// Store the updated workflows object back into cache updateWorkflowsJson(workflows);
setCache('workflows', JSON.stringify(workflows));
renderWorkflows(); renderWorkflows();
hideSpinner(); hideSpinner();
}); });
@@ -75,6 +74,11 @@ function renderWorkflows() {
let $container = $("#workflowContainer"); let $container = $("#workflowContainer");
$container.empty(); // Clear previous UI $container.empty(); // Clear previous UI
workflows = getWorkflowsJson();
console.log("renderWorkflows");
console.log(workflows);
$.each(workflows, function (index, wf) { $.each(workflows, function (index, wf) {
let $wfElement = generateWorkflowUI(wf, index); let $wfElement = generateWorkflowUI(wf, index);
$container.append($wfElement); $container.append($wfElement);
@@ -87,14 +91,14 @@ function renderWorkflows() {
function generateWorkflowUI(wf, wfIndex) { function generateWorkflowUI(wf, wfIndex) {
let $wfContainer = $("<div>", { let $wfContainer = $("<div>", {
class: "workflow-card box box-solid box-primary panel panel-default", class: "workflow-card col-sm-12 col-sx-12",
id: `wf-${wfIndex}-container` id: `wf-${wfIndex}-container`
}); });
// Workflow Name // Workflow Name
let $wfLinkWrap = $("<div>", let $wfLinkWrap = $("<div>",
{ {
class: " ", class: "box box-solid box-primary ",
id: `wf-${wfIndex}-header` id: `wf-${wfIndex}-header`
} }
) )
@@ -150,7 +154,7 @@ function generateWorkflowUI(wf, wfIndex) {
// Trigger Section with dropdowns // Trigger Section with dropdowns
let $triggerSection = $("<div>", let $triggerSection = $("<div>",
{ {
class: "condition-list box box-secondary" class: "box box-secondary col-sm-12 col-sx-12"
} }
).append($triggerTitle); ).append($triggerTitle);
@@ -203,7 +207,11 @@ function generateWorkflowUI(wf, wfIndex) {
lastActionIndex = 0 lastActionIndex = 0
$.each(wf.actions, function (actionIndex, action) { $.each(wf.actions, function (actionIndex, action) {
let $actionEl = $("<div>", { let $actionEl = $("<div>", {
class: "panel" class: "col-sm-11 col-sx-12"
});
let $actionElWrap = $("<div>", {
class: "panel col-sm-12 col-sx-12"
}); });
// Dropdown for action.field // Dropdown for action.field
@@ -241,10 +249,10 @@ function generateWorkflowUI(wf, wfIndex) {
// Actions // Actions
let $actionRemoveButtonWrap = $("<div>", { class: "button-container col-sm-12 col-sx-12" }); let $actionRemoveButtonWrap = $("<div>", { class: "button-container col-sm-1 col-sx-12" });
let $actionRemoveIcon = $("<i>", { let $actionRemoveIcon = $("<i>", {
class: "fa-solid fa-minus" class: "fa-solid fa-trash"
}); });
let $actionRemoveButton = $("<button>", { let $actionRemoveButton = $("<button>", {
@@ -252,13 +260,14 @@ function generateWorkflowUI(wf, wfIndex) {
actionIndex: actionIndex, actionIndex: actionIndex,
wfIndex: wfIndex wfIndex: wfIndex
}) })
.append($actionRemoveIcon) // Add icon .append($actionRemoveIcon)
.append("Remove Action"); // Add text
$actionRemoveButtonWrap.append($actionRemoveButton); $actionRemoveButtonWrap.append($actionRemoveButton);
$actionEl.append($actionRemoveButtonWrap);
$actionsContainer.append($actionEl); $actionElWrap.append($actionEl)
$actionElWrap.append($actionRemoveButtonWrap)
$actionsContainer.append($actionElWrap);
lastActionIndex = actionIndex lastActionIndex = actionIndex
}); });
@@ -303,7 +312,7 @@ function generateWorkflowUI(wf, wfIndex) {
// Render conditions recursively // Render conditions recursively
function renderConditions(wfIndex, parentIndexPath, conditionGroupsIndex, conditions) { function renderConditions(wfIndex, parentIndexPath, conditionGroupsIndex, conditions) {
let $conditionList = $("<div>", { let $conditionList = $("<div>", {
class: "condition-list ", class: "condition-list panel panel-secondary col-sm-12 col-sx-12",
parentIndexPath: parentIndexPath parentIndexPath: parentIndexPath
}); });
@@ -316,7 +325,7 @@ function renderConditions(wfIndex, parentIndexPath, conditionGroupsIndex, condit
if (condition.logic) { if (condition.logic) {
let $nestedCondition = $("<div>", let $nestedCondition = $("<div>",
{ {
class : "condition box box-secondary" class : "condition box box-secondary col-sm-12 col-sx-12"
} }
); );
@@ -340,11 +349,16 @@ function renderConditions(wfIndex, parentIndexPath, conditionGroupsIndex, condit
// INDIVIDUAL CONDITIONS // INDIVIDUAL CONDITIONS
let $conditionItem = $("<div>", let $conditionItem = $("<div>",
{ {
class: "panel", class: "panel col-sm-12 col-sx-12",
conditionIndex: conditionIndex, conditionIndex: conditionIndex,
wfIndex: wfIndex wfIndex: wfIndex
}); });
let $conditionItemsWrap = $("<div>",
{
class: "conditionItemsWrap col-sm-11 col-sx-12"
});
// Create dropdown for condition field // Create dropdown for condition field
let $fieldDropdown = createEditableDropdown( let $fieldDropdown = createEditableDropdown(
`${currentPath}.field`,"Field", `${currentPath}.field`,"Field",
@@ -371,22 +385,23 @@ function renderConditions(wfIndex, parentIndexPath, conditionGroupsIndex, condit
"condition-value-input" "condition-value-input"
); );
$conditionItem.append($fieldDropdown); // Append field dropdown $conditionItemsWrap.append($fieldDropdown); // Append field dropdown
$conditionItem.append($operatorDropdown); // Append operator dropdown $conditionItemsWrap.append($operatorDropdown); // Append operator dropdown
$conditionItem.append($editableInput); // Append editable input for condition value $conditionItemsWrap.append($editableInput); // Append editable input for condition value
let $conditionRemoveButtonWrap = $("<div>", { class: "button-container col-sm-12 col-sx-12" }); let $conditionRemoveButtonWrap = $("<div>", { class: "button-container col-sm-1 col-sx-12" });
let $conditionRemoveButtonIcon = $("<i>", { let $conditionRemoveButtonIcon = $("<i>", {
class: "fa-solid fa-minus" class: "fa-solid fa-trash"
}); });
let $conditionRemoveButton = $("<button>", { let $conditionRemoveButton = $("<button>", {
class : "btn btn-secondary remove-condition", class : "btn btn-secondary remove-condition col-sm-12 col-sx-12",
lastConditionIndex : lastConditionIndex, conditionIndex : conditionIndex,
wfIndex: wfIndex, wfIndex: wfIndex,
parentIndexPath: parentIndexPath parentIndexPath: parentIndexPath
}).append($conditionRemoveButtonIcon).append("Remove Condition") }).append($conditionRemoveButtonIcon)
$conditionRemoveButtonWrap .append($conditionRemoveButton); $conditionRemoveButtonWrap .append($conditionRemoveButton);
$conditionItem.append($conditionItemsWrap);
$conditionItem.append($conditionRemoveButtonWrap); $conditionItem.append($conditionRemoveButtonWrap);
$conditionList.append($conditionItem); $conditionList.append($conditionItem);
@@ -401,20 +416,19 @@ function renderConditions(wfIndex, parentIndexPath, conditionGroupsIndex, condit
if (conditionGroupsIndex != 0) { if (conditionGroupsIndex != 0) {
// Add Condition button // Add Condition button
let $conditionAddWrap = $("<div>", { class: "button-container col-sm-6 col-sx-12" }); let $conditionAddWrap = $("<div>", { class: "button-container col-sx-12" });
let $conditionAddIcon = $("<i>", { let $conditionAddIcon = $("<i>", {
class: "fa-solid fa-plus" class: "fa-solid fa-plus"
}); });
let $conditionAddButton = $("<button>", { let $conditionAddButton = $("<button>", {
class: "btn btn-secondary add-condition col-sx-12", class: "btn btn-secondary add-condition col-sx-12",
lastConditionIndex: lastConditionIndex,
wfIndex: wfIndex, wfIndex: wfIndex,
parentIndexPath: parentIndexPath parentIndexPath: parentIndexPath
}).append($conditionAddIcon).append("Add Condition"); }).append($conditionAddIcon).append("Add Condition");
$conditionAddWrap.append($conditionAddButton); $conditionAddWrap.append($conditionAddButton);
// Remove Condition Group button // Remove Condition Group button
let $conditionGroupRemoveWrap = $("<div>", { class: "button-container col-sm-6 col-sx-12" }); let $conditionGroupRemoveWrap = $("<div>", { class: "button-container col-sx-12" });
let $conditionGroupRemoveIcon = $("<i>", { let $conditionGroupRemoveIcon = $("<i>", {
class: "fa-solid fa-trash" class: "fa-solid fa-trash"
}); });
@@ -455,7 +469,7 @@ function renderConditions(wfIndex, parentIndexPath, conditionGroupsIndex, condit
function createEditableDropdown(jsonPath, labelText, options, selectedValue, id) { function createEditableDropdown(jsonPath, labelText, options, selectedValue, id) {
let $wrapper = $("<div>", { let $wrapper = $("<div>", {
class: "form-group col-xs-12" class: "form-group col-sm-12 col-sx-12"
}); });
let $label = $("<label>", { let $label = $("<label>", {
@@ -506,7 +520,7 @@ function createEditableInput(jsonPath, labelText, value, id, className = "") {
// prepare wrapper // prepare wrapper
$wrapper = $("<div>", { $wrapper = $("<div>", {
class: "form-group col-xs-12" class: "form-group col-sm-12 col-xs-12"
}); });
let $label = $("<label>", { let $label = $("<label>", {
@@ -554,7 +568,7 @@ function createEditableInput(jsonPath, labelText, value, id, className = "") {
// Updating the in-memory workflow object // Updating the in-memory workflow object
function updateWorkflowObject(newValue, jsonPath) { function updateWorkflowObject(newValue, jsonPath) {
// Load workflows from cache if available // Load workflows from cache if available
let workflows = JSON.parse(getCache('workflows')) || []; // Initialize as an array let workflows = getWorkflowsJson()
console.log("Initial workflows:", workflows); console.log("Initial workflows:", workflows);
@@ -562,8 +576,7 @@ function updateWorkflowObject(newValue, jsonPath) {
console.log("Updated workflows:", workflows); console.log("Updated workflows:", workflows);
// Store the updated workflows object back into cache updateWorkflowsJson(workflows)
setCache('workflows', JSON.stringify(workflows)); // Store as a string in localStorage
} }
@@ -635,25 +648,10 @@ function parsePath(path) {
// --------------------------------------------------- // ---------------------------------------------------
// Function to add a new Workflow // Function to add a new Workflow
function addWorkflow() { function addWorkflow(workflows) {
workflows.push({ workflows.push(getEmptyWorkflowJson());
"name": "New Workflow",
"trigger": {
"object_type": "Devices",
"event_type": "create"
},
"conditions": [
],
"actions": [
]
}
);
// // Ensure the workflows object is updated in memory updateWorkflowsJson(workflows)
// workflows = workflows;
// Update the cache with the modified workflows object
setCache("workflows", JSON.stringify(workflows));
// Re-render the UI // Re-render the UI
renderWorkflows(); renderWorkflows();
@@ -661,15 +659,11 @@ function addWorkflow() {
// --------------------------------------------------- // ---------------------------------------------------
// Function to remove a Workflow // Function to remove a Workflow
function removeWorkflow(wfIndex) { function removeWorkflow(workflows, wfIndex) {
workflows.splice(wfIndex, 1); workflows.splice(wfIndex, 1);
// // Ensure the workflows object is updated in memory updateWorkflowsJson(workflows)
// workflows = workflows;
// Update the cache with the modified workflows object
setCache("workflows", JSON.stringify(workflows));
// Re-render the UI // Re-render the UI
renderWorkflows(); renderWorkflows();
@@ -677,9 +671,11 @@ function removeWorkflow(wfIndex) {
// --------------------------------------------------- // ---------------------------------------------------
// Function to add a new condition // Function to add a new condition
function addCondition(wfIndex, parentIndexPath) { function addCondition(workflows, wfIndex, parentIndexPath) {
if (!parentIndexPath) return; if (!parentIndexPath) return;
workflows = getWorkflowsJson()
// Navigate to the target nested object // Navigate to the target nested object
let target = getNestedObject(workflows, parentIndexPath); let target = getNestedObject(workflows, parentIndexPath);
@@ -700,8 +696,7 @@ function addCondition(wfIndex, parentIndexPath) {
// Ensure the workflows object is updated in memory // Ensure the workflows object is updated in memory
workflows[wfIndex] = { ...workflows[wfIndex] }; workflows[wfIndex] = { ...workflows[wfIndex] };
// Update the cache with the modified workflows object updateWorkflowsJson(workflows)
setCache("workflows", JSON.stringify(workflows));
// Re-render the UI // Re-render the UI
renderWorkflows(); renderWorkflows();
@@ -709,7 +704,7 @@ function addCondition(wfIndex, parentIndexPath) {
// --------------------------------------------------- // ---------------------------------------------------
// Function to add a new condition group // Function to add a new condition group
function addConditionGroup(wfIndex, parentIndexPath) { function addConditionGroup(workflows, wfIndex, parentIndexPath) {
if (!parentIndexPath) return; if (!parentIndexPath) return;
// Navigate to the target nested object // Navigate to the target nested object
@@ -731,8 +726,7 @@ function addConditionGroup(wfIndex, parentIndexPath) {
// Ensure the workflows object is updated in memory // Ensure the workflows object is updated in memory
workflows[wfIndex] = { ...workflows[wfIndex] }; workflows[wfIndex] = { ...workflows[wfIndex] };
// Update the cache with the modified workflows object updateWorkflowsJson(workflows)
setCache("workflows", JSON.stringify(workflows));
// Re-render the UI // Re-render the UI
renderWorkflows(); renderWorkflows();
@@ -740,18 +734,21 @@ function addConditionGroup(wfIndex, parentIndexPath) {
// --------------------------------------------------- // ---------------------------------------------------
// Function to add a new action // Function to add a new action
function addAction(wfIndex) { function addAction(workflows, wfIndex) {
let newAction = { let newAction = {
type: actionTypes[0], type: actionTypes[0],
field: fieldOptions[0], field: fieldOptions[0],
value: "" value: ""
}; };
workflows[wfIndex].actions.push(newAction); workflows[wfIndex].actions.push(newAction);
updateWorkflowsJson(workflows)
renderWorkflows(); renderWorkflows();
} }
function removeCondition(wfIndex, parentIndexPath, lastConditionIndex) { function removeCondition(workflows, wfIndex, parentIndexPath, conditionIndex) {
if (!parentIndexPath || lastConditionIndex === undefined) return; if (!parentIndexPath || conditionIndex === undefined) return;
// Navigate to the target nested object // Navigate to the target nested object
let target = getNestedObject(workflows, parentIndexPath); let target = getNestedObject(workflows, parentIndexPath);
@@ -764,19 +761,18 @@ function removeCondition(wfIndex, parentIndexPath, lastConditionIndex) {
} }
// Remove the specified condition // Remove the specified condition
target.conditions.splice(lastConditionIndex, 1); target.conditions.splice(conditionIndex, 1);
// Ensure the workflows object is updated in memory // Ensure the workflows object is updated in memory
workflows[wfIndex] = { ...workflows[wfIndex] }; workflows[wfIndex] = { ...workflows[wfIndex] };
// Update the cache with the modified workflows object updateWorkflowsJson(workflows)
setCache("workflows", JSON.stringify(workflows));
// Re-render the UI // Re-render the UI
renderWorkflows(); renderWorkflows();
} }
function removeAction(wfIndex, actionIndex) { function removeAction(workflows, wfIndex, actionIndex) {
if (!actionIndex || actionIndex === undefined) return; if (!actionIndex || actionIndex === undefined) return;
// Navigate to the target nested object // Navigate to the target nested object
@@ -797,14 +793,13 @@ function removeAction(wfIndex, actionIndex) {
// Ensure the workflows object is updated in memory // Ensure the workflows object is updated in memory
workflows[wfIndex] = { ...workflows[wfIndex] }; workflows[wfIndex] = { ...workflows[wfIndex] };
// Update the cache with the modified workflows object updateWorkflowsJson(workflows)
setCache("workflows", JSON.stringify(workflows));
// Re-render the UI // Re-render the UI
renderWorkflows(); renderWorkflows();
} }
function removeConditionGroup(wfIndex, parentIndexPath) { function removeConditionGroup(workflows, wfIndex, parentIndexPath) {
if (!parentIndexPath) return; if (!parentIndexPath) return;
// Split the path by dots // Split the path by dots
@@ -835,8 +830,7 @@ function removeConditionGroup(wfIndex, parentIndexPath) {
// Ensure the workflows object is updated in memory // Ensure the workflows object is updated in memory
workflows[wfIndex] = { ...workflows[wfIndex] }; workflows[wfIndex] = { ...workflows[wfIndex] };
// Update the cache with the modified workflows object updateWorkflowsJson(workflows)
setCache("workflows", JSON.stringify(workflows));
// Re-render the UI // Re-render the UI
renderWorkflows(); renderWorkflows();
@@ -863,41 +857,73 @@ function getNestedObject(obj, path) {
}, obj); }, obj);
} }
// ---------------------------------------------------
// Get workflows JSON
function getWorkflowsJson()
{
return workflows = JSON.parse(getCache('workflows')) || getEmptyWorkflowJson();
}
// ---------------------------------------------------
// Get workflows JSON
function updateWorkflowsJson(workflows)
{
// Store the updated workflows object back into cache
setCache('workflows', JSON.stringify(workflows));
}
// ---------------------------------------------------
// Get empty workflow JSON
function getEmptyWorkflowJson()
{
return {
"name": "New Workflow",
"trigger": {
"object_type": "Devices",
"event_type": "create"
},
"conditions": [
],
"actions": [
]
}
}
// --------------------------------------------------- // ---------------------------------------------------
// Event listeners // Event listeners
$(document).on("click", ".add-workflow-btn", function () { $(document).on("click", ".add-workflow-btn", function () {
addWorkflow(); addWorkflow(getWorkflowsJson());
}); });
$(document).on("click", ".remove-wf", function () { $(document).on("click", ".remove-wf", function () {
let wfIndex = $(this).attr("wfindex"); let wfIndex = $(this).attr("wfindex");
removeWorkflow(wfIndex); removeWorkflow(getWorkflowsJson(), wfIndex);
}); });
$(document).on("click", ".add-condition", function () { $(document).on("click", ".add-condition", function () {
let wfIndex = $(this).attr("wfindex"); let wfIndex = $(this).attr("wfindex");
let parentIndexPath = $(this).attr("parentIndexPath"); let parentIndexPath = $(this).attr("parentIndexPath");
addCondition(wfIndex, parentIndexPath); addCondition(getWorkflowsJson(), wfIndex, parentIndexPath);
}); });
$(document).on("click", ".add-condition-group", function () { $(document).on("click", ".add-condition-group", function () {
let wfIndex = $(this).attr("wfindex"); let wfIndex = $(this).attr("wfindex");
let parentIndexPath = $(this).attr("parentIndexPath"); let parentIndexPath = $(this).attr("parentIndexPath");
addConditionGroup(wfIndex, parentIndexPath); addConditionGroup(getWorkflowsJson(), wfIndex, parentIndexPath);
}); });
$(document).on("click", ".add-action", function () { $(document).on("click", ".add-action", function () {
let wfIndex = $(this).attr("wfIndex"); let wfIndex = $(this).attr("wfIndex");
addAction(wfIndex); addAction(getWorkflowsJson(), wfIndex);
}); });
// Event Listeners for Removing Conditions // Event Listeners for Removing Conditions
$(document).on("click", ".remove-condition", function () { $(document).on("click", ".remove-condition", function () {
let wfIndex = $(this).attr("wfindex"); let wfIndex = $(this).attr("wfindex");
let parentIndexPath = $(this).attr("parentIndexPath"); let parentIndexPath = $(this).attr("parentIndexPath");
let lastConditionIndex = parseInt($(this).attr("lastConditionIndex"), 10); let conditionIndex = parseInt($(this).attr("conditionIndex"), 10);
removeCondition(wfIndex, parentIndexPath, lastConditionIndex); removeCondition(getWorkflowsJson(), wfIndex, parentIndexPath, conditionIndex);
}); });
// Event Listeners for Removing Actions // Event Listeners for Removing Actions
@@ -905,16 +931,14 @@ $(document).on("click", ".remove-action", function () {
let wfIndex = $(this).attr("wfindex"); let wfIndex = $(this).attr("wfindex");
let actionIndex = $(this).attr("actionIndex"); let actionIndex = $(this).attr("actionIndex");
removeAction(wfIndex, actionIndex); removeAction(getWorkflowsJson(), wfIndex, actionIndex);
}); });
// Event Listeners for Removing Condition Groups // Event Listeners for Removing Condition Groups
$(document).on("click", ".remove-condition-group", function () { $(document).on("click", ".remove-condition-group", function () {
let wfIndex = $(this).attr("wfindex"); let wfIndex = $(this).attr("wfindex");
let parentIndexPath = $(this).attr("parentIndexPath"); let parentIndexPath = $(this).attr("parentIndexPath");
let lastConditionIndex = parseInt($(this).attr("lastConditionIndex"), 10); removeConditionGroup(getWorkflowsJson(), wfIndex, parentIndexPath);
removeConditionGroup(wfIndex, parentIndexPath);
}); });
// --------------------------------------------------- // ---------------------------------------------------