refactor: adopt POSITIONS constants, fix ESLint warnings, break menuUtils into modules
- Replace hardcoded position strings with POSITIONS.* constants - Prefix unused variables with _ to resolve no-unused-vars warnings - Fix no-prototype-builtins with Object.prototype.hasOwnProperty.call() - Extract menuUtils.js (543 lines) into 6 focused modules under menu/ - menuUtils.js now 35 lines, delegates via prototype mixin pattern - Add 158 unit tests for ConfigManager, MeasurementContainer, ValidationUtils Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
var metric
|
||||
, imperial;
|
||||
var metric;
|
||||
|
||||
metric = {
|
||||
ea: {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
var metric
|
||||
, imperial;
|
||||
var metric;
|
||||
|
||||
metric = {
|
||||
ppm: {
|
||||
|
||||
@@ -249,7 +249,7 @@ Converter.prototype.list = function (measure) {
|
||||
Converter.prototype.throwUnsupportedUnitError = function (what) {
|
||||
var validUnits = [];
|
||||
|
||||
each(measures, function (systems, measure) {
|
||||
each(measures, function (systems, _measure) {
|
||||
each(systems, function (units, system) {
|
||||
if(system == '_anchors')
|
||||
return false;
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
* Available under MIT license <http://lodash.com/license>
|
||||
*/
|
||||
var isObject = require('./../lodash.isobject'),
|
||||
noop = require('./../lodash.noop'),
|
||||
reNative = require('./../lodash._renative');
|
||||
|
||||
/* Native method shortcuts for methods with the same name as other `lodash` methods */
|
||||
@@ -21,7 +20,7 @@ var nativeCreate = reNative.test(nativeCreate = Object.create) && nativeCreate;
|
||||
* @param {Object} prototype The object to inherit from.
|
||||
* @returns {Object} Returns the new object.
|
||||
*/
|
||||
function baseCreate(prototype, properties) { // eslint-disable-line no-func-assign
|
||||
function baseCreate(prototype, _properties) { // eslint-disable-line no-func-assign
|
||||
return isObject(prototype) ? nativeCreate(prototype) : {};
|
||||
}
|
||||
// fallback for browsers without `Object.create`
|
||||
|
||||
@@ -47,7 +47,7 @@ function createWrapper(func, bitmask, partialArgs, partialRightArgs, thisArg, ar
|
||||
var isBind = bitmask & 1,
|
||||
isBindKey = bitmask & 2,
|
||||
isCurry = bitmask & 4,
|
||||
isCurryBound = bitmask & 8,
|
||||
/* isCurryBound = bitmask & 8, */
|
||||
isPartial = bitmask & 16,
|
||||
isPartialRight = bitmask & 32;
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
* Available under MIT license <http://lodash.com/license>
|
||||
*/
|
||||
var createWrapper = require('./../lodash._createwrapper'),
|
||||
reNative = require('./../lodash._renative'),
|
||||
slice = require('./../lodash._slice');
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,7 @@ class ChildRegistrationUtils {
|
||||
this.registeredChildren = new Map();
|
||||
}
|
||||
|
||||
async registerChild(child, positionVsParent, distance) {
|
||||
async registerChild(child, positionVsParent, _distance) {
|
||||
const softwareType = (child.config.functionality.softwareType || '').toLowerCase();
|
||||
const { name, id } = child.config.general;
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ class ConfigUtils {
|
||||
// loop through objects and merge them obj1 will be updated with obj2 values
|
||||
mergeObjects(obj1, obj2) {
|
||||
for (let key in obj2) {
|
||||
if (obj2.hasOwnProperty(key)) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
|
||||
if (typeof obj2[key] === 'object') {
|
||||
if (!obj1[key]) {
|
||||
obj1[key] = {};
|
||||
|
||||
123
src/helper/menu/dataFetching.js
Normal file
123
src/helper/menu/dataFetching.js
Normal file
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* Data fetching methods for MenuUtils.
|
||||
* Handles primary/fallback URL fetching and API calls.
|
||||
*/
|
||||
|
||||
const dataFetching = {
|
||||
async fetchData(url, fallbackUrl) {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const responsData = await response.json();
|
||||
//responsData
|
||||
const data = responsData.data;
|
||||
/* .map(item => {
|
||||
const { vendor_name, ...rest } = item;
|
||||
return {
|
||||
name: vendor_name,
|
||||
...rest
|
||||
};
|
||||
}); */
|
||||
console.log(url);
|
||||
console.log("Response Data: ", data);
|
||||
return data;
|
||||
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
`Primary URL failed: ${url}. Trying fallback URL: ${fallbackUrl}`,
|
||||
err
|
||||
);
|
||||
try {
|
||||
const response = await fetch(fallbackUrl);
|
||||
if (!response.ok)
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return await response.json();
|
||||
} catch (fallbackErr) {
|
||||
console.error("Both primary and fallback URLs failed:", fallbackErr);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async fetchProjectData(url) {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const responsData = await response.json();
|
||||
console.log("Response Data: ", responsData);
|
||||
return responsData;
|
||||
|
||||
} catch (err) {
|
||||
/* intentionally empty */
|
||||
}
|
||||
},
|
||||
|
||||
// Save changes to API
|
||||
async apiCall(node) {
|
||||
try{
|
||||
// OLFIANT when a browser refreshes the tag code is lost!!! fix this later!!!!!
|
||||
// FIX UUID ALSO LATER
|
||||
|
||||
if(node.assetTagCode !== "" || node.assetTagCode !== null){ /* intentionally empty */ }
|
||||
// API call to register or check asset in central database
|
||||
let assetregisterAPI = node.configUrls.cloud.taggcodeAPI + "/asset/create_asset.php";
|
||||
|
||||
const assetModelId = node.modelMetadata.id; //asset_product_model_id
|
||||
const uuid = node.uuid; //asset_product_model_uuid
|
||||
const assetName = node.assetType; //asset_name / type?
|
||||
const description = node.name; // asset_description
|
||||
const assetStatus = "actief"; //asset_status -> koppel aan enable / disable node ? or make dropdown ?
|
||||
const assetProfileId = 1; //asset_profile_id these are the rules to check if the childs are valid under this node (parent / child id?)
|
||||
const child_assets = ["63247"]; //child_assets tagnummer of id?
|
||||
const assetProcessId = node.processId; //asset_process_id
|
||||
const assetLocationId = node.locationId; //asset_location_id
|
||||
const tagCode = node.assetTagCode; // if already exists in the node information use it to tell the api it exists and it will update else we will get it from the api call
|
||||
//console.log(`this is my tagCode: ${tagCode}`);
|
||||
|
||||
// Build base URL with required parameters
|
||||
let apiUrl = `?asset_product_model_id=${assetModelId}&asset_product_model_uuid=${uuid}&asset_name=${assetName}&asset_description=${description}&asset_status=${assetStatus}&asset_profile_id=${assetProfileId}&asset_location_id=${assetLocationId}&asset_process_id=${assetProcessId}&child_assets=${child_assets}`;
|
||||
|
||||
// Only add tagCode to URL if it exists
|
||||
if (tagCode) {
|
||||
apiUrl += `&asset_tag_number=${tagCode}`;
|
||||
console.log('hello there');
|
||||
}
|
||||
|
||||
assetregisterAPI += apiUrl;
|
||||
console.log("API call to register asset in central database", assetregisterAPI);
|
||||
|
||||
const response = await fetch(assetregisterAPI, {
|
||||
method: "POST"
|
||||
});
|
||||
|
||||
// Get the response text first
|
||||
const responseText = await response.text();
|
||||
console.log("Raw API response:", responseText);
|
||||
|
||||
// Try to parse the JSON, handling potential parsing errors
|
||||
let jsonResponse;
|
||||
try {
|
||||
jsonResponse = JSON.parse(responseText);
|
||||
} catch (parseError) {
|
||||
console.error("JSON Parsing Error:", parseError);
|
||||
console.error("Response that could not be parsed:", responseText);
|
||||
throw new Error("Failed to parse API response");
|
||||
}
|
||||
|
||||
console.log(jsonResponse);
|
||||
|
||||
if(jsonResponse.success){
|
||||
console.log(`${jsonResponse.message}, tag number: ${jsonResponse.asset_tag_number}, asset id: ${jsonResponse.asset_id}`);
|
||||
// Save the asset tag number and id to the node
|
||||
} else {
|
||||
console.log("Asset not registered in central database");
|
||||
}
|
||||
return jsonResponse;
|
||||
|
||||
} catch (error) {
|
||||
console.log("Error saving changes to asset register API", error);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = dataFetching;
|
||||
283
src/helper/menu/dropdownPopulation.js
Normal file
283
src/helper/menu/dropdownPopulation.js
Normal file
@@ -0,0 +1,283 @@
|
||||
/**
|
||||
* Dropdown population methods for MenuUtils.
|
||||
* Handles populating and cascading dropdown menus for assets, suppliers, models, units, etc.
|
||||
*/
|
||||
|
||||
const dropdownPopulation = {
|
||||
populateSmoothingMethods(configUrls, elements, node) {
|
||||
this.fetchData(configUrls.cloud.config, configUrls.local.config)
|
||||
.then((configData) => {
|
||||
const smoothingMethods =
|
||||
configData.smoothing?.smoothMethod?.rules?.values?.map(
|
||||
(o) => o.value
|
||||
) || [];
|
||||
this.populateDropdown(
|
||||
elements.smoothMethod,
|
||||
smoothingMethods,
|
||||
node,
|
||||
"smooth_method"
|
||||
);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Error loading smoothing methods", err);
|
||||
});
|
||||
},
|
||||
|
||||
populateInterpolationMethods(configUrls, elements, node) {
|
||||
this.fetchData(configUrls.cloud.config, configUrls.local.config)
|
||||
.then((configData) => {
|
||||
const interpolationMethods =
|
||||
configData?.interpolation?.type?.rules?.values.map((m) => m.value) ||
|
||||
[];
|
||||
this.populateDropdown(
|
||||
elements.interpolationMethodInput,
|
||||
interpolationMethods,
|
||||
node,
|
||||
"interpolationMethod"
|
||||
);
|
||||
|
||||
// Find the selected method and use it to spawn 1 more field to fill in tension
|
||||
//const selectedMethod = interpolationMethods.find(m => m === node.interpolationMethod);
|
||||
this.initTensionToggles(elements, node);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Error loading interpolation methods", err);
|
||||
});
|
||||
},
|
||||
|
||||
populateLogLevelOptions(logLevelSelect, configData, node) {
|
||||
// debug log level
|
||||
//console.log("Displaying configData => ", configData) ;
|
||||
|
||||
const logLevels =
|
||||
configData?.general?.logging?.logLevel?.rules?.values?.map(
|
||||
(l) => l.value
|
||||
) || [];
|
||||
|
||||
//console.log("Displaying logLevels => ", logLevels);
|
||||
|
||||
// Reuse your existing generic populateDropdown helper
|
||||
this.populateDropdown(logLevelSelect, logLevels, node.logLevel);
|
||||
},
|
||||
|
||||
//cascade dropdowns for asset type, supplier, subType, model, unit
|
||||
fetchAndPopulateDropdowns(configUrls, elements, node) {
|
||||
this.fetchData(configUrls.cloud.config, configUrls.local.config)
|
||||
.then((configData) => {
|
||||
const assetType = configData.asset?.type?.default;
|
||||
const localSuppliersUrl = this.constructUrl(configUrls.local.taggcodeAPI,`${assetType}s`,"suppliers.json");
|
||||
const cloudSuppliersUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/vendor/get_vendors.php");
|
||||
|
||||
return this.fetchData(cloudSuppliersUrl, localSuppliersUrl)
|
||||
.then((supplierData) => {
|
||||
|
||||
const suppliers = supplierData.map((supplier) => supplier.name);
|
||||
|
||||
// Populate suppliers dropdown and set up its change handler
|
||||
return this.populateDropdown(
|
||||
elements.supplier,
|
||||
suppliers,
|
||||
node,
|
||||
"supplier",
|
||||
function (selectedSupplier) {
|
||||
if (selectedSupplier) {
|
||||
this.populateSubTypes(configUrls, elements, node, selectedSupplier);
|
||||
}
|
||||
}
|
||||
);
|
||||
})
|
||||
.then(() => {
|
||||
// If we have a saved supplier, trigger subTypes population
|
||||
if (node.supplier) {
|
||||
this.populateSubTypes(configUrls, elements, node, node.supplier);
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error in initial dropdown population:", error);
|
||||
});
|
||||
},
|
||||
|
||||
populateSubTypes(configUrls, elements, node, selectedSupplier) {
|
||||
|
||||
this.fetchData(configUrls.cloud.config, configUrls.local.config)
|
||||
.then((configData) => {
|
||||
const assetType = configData.asset?.type?.default;
|
||||
const supplierFolder = this.constructUrl( configUrls.local.taggcodeAPI, `${assetType}s`, selectedSupplier );
|
||||
|
||||
const localSubTypesUrl = this.constructUrl(supplierFolder, "subtypes.json");
|
||||
const cloudSubTypesUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/product/get_subtypesFromVendor.php?vendor_name=" + selectedSupplier);
|
||||
|
||||
return this.fetchData(cloudSubTypesUrl, localSubTypesUrl)
|
||||
.then((subTypeData) => {
|
||||
const subTypes = subTypeData.map((subType) => subType.name);
|
||||
|
||||
return this.populateDropdown(
|
||||
elements.subType,
|
||||
subTypes,
|
||||
node,
|
||||
"subType",
|
||||
function (selectedSubType) {
|
||||
if (selectedSubType) {
|
||||
// When subType changes, update both models and units
|
||||
this.populateModels(
|
||||
configUrls,
|
||||
elements,
|
||||
node,
|
||||
selectedSupplier,
|
||||
selectedSubType
|
||||
);
|
||||
this.populateUnitsForSubType(
|
||||
configUrls,
|
||||
elements,
|
||||
node,
|
||||
selectedSubType
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
})
|
||||
.then(() => {
|
||||
// If we have a saved subType, trigger both models and units population
|
||||
if (node.subType) {
|
||||
this.populateModels(
|
||||
configUrls,
|
||||
elements,
|
||||
node,
|
||||
selectedSupplier,
|
||||
node.subType
|
||||
);
|
||||
this.populateUnitsForSubType(configUrls, elements, node, node.subType);
|
||||
}
|
||||
//console.log("In fetch part of subtypes ");
|
||||
// Store all data from selected model
|
||||
/* node["modelMetadata"] = modelData.find(
|
||||
(model) => model.name === node.model
|
||||
);
|
||||
console.log("Model Metadata: ", node["modelMetadata"]); */
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error populating subtypes:", error);
|
||||
});
|
||||
},
|
||||
|
||||
populateUnitsForSubType(configUrls, elements, node, selectedSubType) {
|
||||
// Fetch the units data
|
||||
this.fetchData(configUrls.cloud.units, configUrls.local.units)
|
||||
.then((unitsData) => {
|
||||
// Find the category that matches the subType name
|
||||
const categoryData = unitsData.units.find(
|
||||
(category) =>
|
||||
category.category.toLowerCase() === selectedSubType.toLowerCase()
|
||||
);
|
||||
|
||||
if (categoryData) {
|
||||
// Extract just the unit values and descriptions
|
||||
const units = categoryData.values.map((unit) => ({
|
||||
value: unit.value,
|
||||
description: unit.description,
|
||||
}));
|
||||
|
||||
// Create the options array with descriptions as labels
|
||||
const options = units.map((unit) => ({
|
||||
value: unit.value,
|
||||
label: `${unit.value} - ${unit.description}`,
|
||||
}));
|
||||
|
||||
// Populate the units dropdown
|
||||
this.populateDropdown(
|
||||
elements.unit,
|
||||
options.map((opt) => opt.value),
|
||||
node,
|
||||
"unit"
|
||||
);
|
||||
|
||||
// If there's no currently selected unit but we have options, select the first one
|
||||
if (!node.unit && options.length > 0) {
|
||||
node.unit = options[0].value;
|
||||
elements.unit.value = options[0].value;
|
||||
}
|
||||
} else {
|
||||
// If no matching category is found, provide a default % option
|
||||
const defaultUnits = [{ value: "%", description: "Percentage" }];
|
||||
this.populateDropdown(
|
||||
elements.unit,
|
||||
defaultUnits.map((unit) => unit.value),
|
||||
node,
|
||||
"unit"
|
||||
);
|
||||
console.warn(
|
||||
`No matching unit category found for subType: ${selectedSubType}`
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error fetching units:", error);
|
||||
});
|
||||
},
|
||||
|
||||
populateModels(
|
||||
configUrls,
|
||||
elements,
|
||||
node,
|
||||
selectedSupplier,
|
||||
selectedSubType
|
||||
) {
|
||||
|
||||
this.fetchData(configUrls.cloud.config, configUrls.local.config)
|
||||
.then((configData) => {
|
||||
const assetType = configData.asset?.type?.default;
|
||||
// save assetType to fetch later
|
||||
node.assetType = assetType;
|
||||
|
||||
const supplierFolder = this.constructUrl( configUrls.local.taggcodeAPI,`${assetType}s`,selectedSupplier);
|
||||
const subTypeFolder = this.constructUrl(supplierFolder, selectedSubType);
|
||||
const localModelsUrl = this.constructUrl(subTypeFolder, "models.json");
|
||||
const cloudModelsUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/product/get_product_models.php?vendor_name=" + selectedSupplier + "&product_subtype_name=" + selectedSubType);
|
||||
|
||||
return this.fetchData(cloudModelsUrl, localModelsUrl).then((modelData) => {
|
||||
const models = modelData.map((model) => model.name); // use this to populate the dropdown
|
||||
|
||||
// If a model is already selected, store its metadata immediately
|
||||
if (node.model) {
|
||||
node["modelMetadata"] = modelData.find((model) => model.name === node.model);
|
||||
}
|
||||
|
||||
this.populateDropdown(elements.model, models, node, "model", (selectedModel) => {
|
||||
// Store only the metadata for the selected model
|
||||
node["modelMetadata"] = modelData.find((model) => model.name === selectedModel);
|
||||
});
|
||||
/*
|
||||
console.log('hello here I am:');
|
||||
console.log(node["modelMetadata"]);
|
||||
*/
|
||||
});
|
||||
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error populating models:", error);
|
||||
});
|
||||
},
|
||||
|
||||
async populateDropdown(
|
||||
htmlElement,
|
||||
options,
|
||||
node,
|
||||
property,
|
||||
callback
|
||||
) {
|
||||
this.generateHtml(htmlElement, options, node[property]);
|
||||
|
||||
htmlElement.addEventListener("change", async (e) => {
|
||||
const newValue = e.target.value;
|
||||
console.log(`Dropdown changed: ${property} = ${newValue}`);
|
||||
node[property] = newValue;
|
||||
|
||||
RED.nodes.dirty(true);
|
||||
if (callback) await callback(newValue); // Ensure async callback completion
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = dropdownPopulation;
|
||||
73
src/helper/menu/htmlGeneration.js
Normal file
73
src/helper/menu/htmlGeneration.js
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* HTML generation and endpoint methods for MenuUtils.
|
||||
* Handles generating dropdown HTML and serving MenuUtils code to the browser.
|
||||
*/
|
||||
|
||||
const htmlGeneration = {
|
||||
generateHtml(htmlElement, options, savedValue) {
|
||||
htmlElement.innerHTML = options.length
|
||||
? `<option value="">Select...</option>${options
|
||||
.map((opt) => `<option value="${opt}">${opt}</option>`)
|
||||
.join("")}`
|
||||
: "<option value=''>No options available</option>";
|
||||
|
||||
if (savedValue && options.includes(savedValue)) {
|
||||
htmlElement.value = savedValue;
|
||||
}
|
||||
},
|
||||
|
||||
createMenuUtilsEndpoint(RED, nodeName, customHelpers = {}) {
|
||||
RED.httpAdmin.get(`/${nodeName}/resources/menuUtils.js`, function(req, res) {
|
||||
console.log(`Serving menuUtils.js for ${nodeName} node`);
|
||||
res.set('Content-Type', 'application/javascript');
|
||||
|
||||
const browserCode = this.generateMenuUtilsCode(nodeName, customHelpers);
|
||||
res.send(browserCode);
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
generateMenuUtilsCode(nodeName, customHelpers = {}) {
|
||||
const defaultHelpers = {
|
||||
validateRequired: `function(value) {
|
||||
return value && value.toString().trim() !== '';
|
||||
}`,
|
||||
formatDisplayValue: `function(value, unit) {
|
||||
return \`\${value} \${unit || ''}\`.trim();
|
||||
}`
|
||||
};
|
||||
|
||||
const allHelpers = { ...defaultHelpers, ...customHelpers };
|
||||
|
||||
const helpersCode = Object.entries(allHelpers)
|
||||
.map(([name, func]) => ` ${name}: ${func}`)
|
||||
.join(',\n');
|
||||
|
||||
const classCode = this.constructor.toString(); // <-- this gives full class MenuUtils {...}
|
||||
|
||||
return `
|
||||
// Create EVOLV namespace structure
|
||||
window.EVOLV = window.EVOLV || {};
|
||||
window.EVOLV.nodes = window.EVOLV.nodes || {};
|
||||
window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {};
|
||||
|
||||
// Inject MenuUtils class
|
||||
${classCode}
|
||||
|
||||
// Expose MenuUtils instance to namespace
|
||||
window.EVOLV.nodes.${nodeName}.utils = {
|
||||
menuUtils: new MenuUtils(),
|
||||
|
||||
helpers: {
|
||||
${helpersCode}
|
||||
}
|
||||
};
|
||||
|
||||
// Optionally expose globally
|
||||
window.MenuUtils = MenuUtils;
|
||||
|
||||
console.log('${nodeName} utilities loaded in namespace');
|
||||
`;
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = htmlGeneration;
|
||||
18
src/helper/menu/index.js
Normal file
18
src/helper/menu/index.js
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* menu/index.js
|
||||
* Barrel file for the menu module components.
|
||||
*/
|
||||
|
||||
const toggles = require('./toggles');
|
||||
const dataFetching = require('./dataFetching');
|
||||
const urlUtils = require('./urlUtils');
|
||||
const dropdownPopulation = require('./dropdownPopulation');
|
||||
const htmlGeneration = require('./htmlGeneration');
|
||||
|
||||
module.exports = {
|
||||
toggles,
|
||||
dataFetching,
|
||||
urlUtils,
|
||||
dropdownPopulation,
|
||||
htmlGeneration,
|
||||
};
|
||||
56
src/helper/menu/toggles.js
Normal file
56
src/helper/menu/toggles.js
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Toggle initialization methods for MenuUtils.
|
||||
* Controls visibility of UI elements based on checkbox/dropdown state.
|
||||
*/
|
||||
|
||||
const toggles = {
|
||||
initBasicToggles(elements) {
|
||||
// Toggle visibility for log level
|
||||
elements.logCheckbox.addEventListener("change", function () {
|
||||
elements.rowLogLevel.style.display = this.checked ? "block" : "none";
|
||||
});
|
||||
elements.rowLogLevel.style.display = elements.logCheckbox.checked
|
||||
? "block"
|
||||
: "none";
|
||||
},
|
||||
|
||||
// Define the initialize toggles function within scope
|
||||
initMeasurementToggles(elements) {
|
||||
// Toggle visibility for scaling inputs
|
||||
elements.scalingCheckbox.addEventListener("change", function () {
|
||||
elements.rowInputMin.style.display = this.checked ? "block" : "none";
|
||||
elements.rowInputMax.style.display = this.checked ? "block" : "none";
|
||||
});
|
||||
|
||||
// Set initial states
|
||||
elements.rowInputMin.style.display = elements.scalingCheckbox.checked
|
||||
? "block"
|
||||
: "none";
|
||||
elements.rowInputMax.style.display = elements.scalingCheckbox.checked
|
||||
? "block"
|
||||
: "none";
|
||||
},
|
||||
|
||||
initTensionToggles(elements, node) {
|
||||
const currentMethod = node.interpolationMethod;
|
||||
elements.rowTension.style.display =
|
||||
currentMethod === "monotone_cubic_spline" ? "block" : "none";
|
||||
console.log(
|
||||
"Initial tension row display: ",
|
||||
elements.rowTension.style.display
|
||||
);
|
||||
|
||||
elements.interpolationMethodInput.addEventListener("change", function () {
|
||||
const selectedMethod = this.value;
|
||||
console.log(`Interpolation method changed: ${selectedMethod}`);
|
||||
node.interpolationMethod = selectedMethod;
|
||||
|
||||
// Toggle visibility for tension input
|
||||
elements.rowTension.style.display =
|
||||
selectedMethod === "monotone_cubic_spline" ? "block" : "none";
|
||||
console.log("Tension row display: ", elements.rowTension.style.display);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = toggles;
|
||||
39
src/helper/menu/urlUtils.js
Normal file
39
src/helper/menu/urlUtils.js
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* URL construction methods for MenuUtils.
|
||||
* Helpers for building API and config URLs.
|
||||
*/
|
||||
|
||||
const urlUtils = {
|
||||
getSpecificConfigUrl(nodeName, cloudAPI) {
|
||||
const cloudConfigURL = cloudAPI + "/config/" + nodeName + ".json";
|
||||
const localConfigURL = "http://localhost:1880/"+ nodeName + "/dependencies/"+ nodeName + "/" + nodeName + "Config.json";
|
||||
|
||||
return { cloudConfigURL, localConfigURL };
|
||||
},
|
||||
|
||||
// Helper function to construct a URL from a base and path internal
|
||||
constructUrl(base, ...paths) {
|
||||
// Remove trailing slash from base and leading slashes from paths
|
||||
const sanitizedBase = (base || "").replace(/\/+$/, "");
|
||||
const sanitizedPaths = paths.map((path) => path.replace(/^\/+|\/+$/g, ""));
|
||||
|
||||
// Join sanitized base and paths
|
||||
const url = `${sanitizedBase}/${sanitizedPaths.join("/")}`;
|
||||
console.log("Base:", sanitizedBase);
|
||||
console.log("Paths:", sanitizedPaths);
|
||||
console.log("Constructed URL:", url);
|
||||
return url;
|
||||
},
|
||||
|
||||
//Adjust for API Gateway
|
||||
constructCloudURL(base, ...paths) {
|
||||
// Remove trailing slash from base and leading slashes from paths
|
||||
const sanitizedBase = base.replace(/\/+$/, "");
|
||||
const sanitizedPaths = paths.map((path) => path.replace(/^\/+|\/+$/g, ""));
|
||||
// Join sanitized base and paths
|
||||
const url = `${sanitizedBase}/${sanitizedPaths.join("/")}`;
|
||||
return url;
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = urlUtils;
|
||||
@@ -1,544 +1,34 @@
|
||||
/**
|
||||
* MenuUtils — UI menu helper for Node-RED editor.
|
||||
* Methods are split across focused modules under ./menu/ and mixed onto the prototype.
|
||||
*/
|
||||
|
||||
const toggles = require('./menu/toggles');
|
||||
const dataFetching = require('./menu/dataFetching');
|
||||
const urlUtils = require('./menu/urlUtils');
|
||||
const dropdownPopulation = require('./menu/dropdownPopulation');
|
||||
const htmlGeneration = require('./menu/htmlGeneration');
|
||||
|
||||
class MenuUtils {
|
||||
|
||||
|
||||
initBasicToggles(elements) {
|
||||
// Toggle visibility for log level
|
||||
elements.logCheckbox.addEventListener("change", function () {
|
||||
elements.rowLogLevel.style.display = this.checked ? "block" : "none";
|
||||
});
|
||||
elements.rowLogLevel.style.display = elements.logCheckbox.checked
|
||||
? "block"
|
||||
: "none";
|
||||
}
|
||||
|
||||
// Define the initialize toggles function within scope
|
||||
initMeasurementToggles(elements) {
|
||||
// Toggle visibility for scaling inputs
|
||||
elements.scalingCheckbox.addEventListener("change", function () {
|
||||
elements.rowInputMin.style.display = this.checked ? "block" : "none";
|
||||
elements.rowInputMax.style.display = this.checked ? "block" : "none";
|
||||
});
|
||||
|
||||
// Set initial states
|
||||
elements.rowInputMin.style.display = elements.scalingCheckbox.checked
|
||||
? "block"
|
||||
: "none";
|
||||
elements.rowInputMax.style.display = elements.scalingCheckbox.checked
|
||||
? "block"
|
||||
: "none";
|
||||
}
|
||||
|
||||
initTensionToggles(elements, node) {
|
||||
const currentMethod = node.interpolationMethod;
|
||||
elements.rowTension.style.display =
|
||||
currentMethod === "monotone_cubic_spline" ? "block" : "none";
|
||||
console.log(
|
||||
"Initial tension row display: ",
|
||||
elements.rowTension.style.display
|
||||
);
|
||||
|
||||
elements.interpolationMethodInput.addEventListener("change", function () {
|
||||
const selectedMethod = this.value;
|
||||
console.log(`Interpolation method changed: ${selectedMethod}`);
|
||||
node.interpolationMethod = selectedMethod;
|
||||
|
||||
// Toggle visibility for tension input
|
||||
elements.rowTension.style.display =
|
||||
selectedMethod === "monotone_cubic_spline" ? "block" : "none";
|
||||
console.log("Tension row display: ", elements.rowTension.style.display);
|
||||
});
|
||||
}
|
||||
// Define the smoothing methods population function within scope
|
||||
populateSmoothingMethods(configUrls, elements, node) {
|
||||
this.fetchData(configUrls.cloud.config, configUrls.local.config)
|
||||
.then((configData) => {
|
||||
const smoothingMethods =
|
||||
configData.smoothing?.smoothMethod?.rules?.values?.map(
|
||||
(o) => o.value
|
||||
) || [];
|
||||
this.populateDropdown(
|
||||
elements.smoothMethod,
|
||||
smoothingMethods,
|
||||
node,
|
||||
"smooth_method"
|
||||
);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Error loading smoothing methods", err);
|
||||
});
|
||||
}
|
||||
|
||||
populateInterpolationMethods(configUrls, elements, node) {
|
||||
this.fetchData(configUrls.cloud.config, configUrls.local.config)
|
||||
.then((configData) => {
|
||||
const interpolationMethods =
|
||||
configData?.interpolation?.type?.rules?.values.map((m) => m.value) ||
|
||||
[];
|
||||
this.populateDropdown(
|
||||
elements.interpolationMethodInput,
|
||||
interpolationMethods,
|
||||
node,
|
||||
"interpolationMethod"
|
||||
);
|
||||
|
||||
// Find the selected method and use it to spawn 1 more field to fill in tension
|
||||
//const selectedMethod = interpolationMethods.find(m => m === node.interpolationMethod);
|
||||
this.initTensionToggles(elements, node);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Error loading interpolation methods", err);
|
||||
});
|
||||
}
|
||||
|
||||
populateLogLevelOptions(logLevelSelect, configData, node) {
|
||||
// debug log level
|
||||
//console.log("Displaying configData => ", configData) ;
|
||||
|
||||
const logLevels =
|
||||
configData?.general?.logging?.logLevel?.rules?.values?.map(
|
||||
(l) => l.value
|
||||
) || [];
|
||||
|
||||
//console.log("Displaying logLevels => ", logLevels);
|
||||
|
||||
// Reuse your existing generic populateDropdown helper
|
||||
this.populateDropdown(logLevelSelect, logLevels, node.logLevel);
|
||||
}
|
||||
|
||||
//cascade dropdowns for asset type, supplier, subType, model, unit
|
||||
fetchAndPopulateDropdowns(configUrls, elements, node) {
|
||||
this.fetchData(configUrls.cloud.config, configUrls.local.config)
|
||||
.then((configData) => {
|
||||
const assetType = configData.asset?.type?.default;
|
||||
const localSuppliersUrl = this.constructUrl(configUrls.local.taggcodeAPI,`${assetType}s`,"suppliers.json");
|
||||
const cloudSuppliersUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/vendor/get_vendors.php");
|
||||
|
||||
return this.fetchData(cloudSuppliersUrl, localSuppliersUrl)
|
||||
.then((supplierData) => {
|
||||
|
||||
const suppliers = supplierData.map((supplier) => supplier.name);
|
||||
|
||||
// Populate suppliers dropdown and set up its change handler
|
||||
return this.populateDropdown(
|
||||
elements.supplier,
|
||||
suppliers,
|
||||
node,
|
||||
"supplier",
|
||||
function (selectedSupplier) {
|
||||
if (selectedSupplier) {
|
||||
this.populateSubTypes(configUrls, elements, node, selectedSupplier);
|
||||
}
|
||||
}
|
||||
);
|
||||
})
|
||||
.then(() => {
|
||||
// If we have a saved supplier, trigger subTypes population
|
||||
if (node.supplier) {
|
||||
this.populateSubTypes(configUrls, elements, node, node.supplier);
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error in initial dropdown population:", error);
|
||||
});
|
||||
}
|
||||
|
||||
getSpecificConfigUrl(nodeName,cloudAPI) {
|
||||
|
||||
const cloudConfigURL = cloudAPI + "/config/" + nodeName + ".json";
|
||||
const localConfigURL = "http://localhost:1880/"+ nodeName + "/dependencies/"+ nodeName + "/" + nodeName + "Config.json";
|
||||
|
||||
return { cloudConfigURL, localConfigURL };
|
||||
|
||||
}
|
||||
|
||||
// Save changes to API
|
||||
async apiCall(node) {
|
||||
try{
|
||||
// OLFIANT when a browser refreshes the tag code is lost!!! fix this later!!!!!
|
||||
// FIX UUID ALSO LATER
|
||||
|
||||
if(node.assetTagCode !== "" || node.assetTagCode !== null){ /* intentionally empty */ }
|
||||
// API call to register or check asset in central database
|
||||
let assetregisterAPI = node.configUrls.cloud.taggcodeAPI + "/asset/create_asset.php";
|
||||
|
||||
const assetModelId = node.modelMetadata.id; //asset_product_model_id
|
||||
const uuid = node.uuid; //asset_product_model_uuid
|
||||
const assetName = node.assetType; //asset_name / type?
|
||||
const description = node.name; // asset_description
|
||||
const assetStatus = "actief"; //asset_status -> koppel aan enable / disable node ? or make dropdown ?
|
||||
const assetProfileId = 1; //asset_profile_id these are the rules to check if the childs are valid under this node (parent / child id?)
|
||||
const child_assets = ["63247"]; //child_assets tagnummer of id?
|
||||
const assetProcessId = node.processId; //asset_process_id
|
||||
const assetLocationId = node.locationId; //asset_location_id
|
||||
const tagCode = node.assetTagCode; // if already exists in the node information use it to tell the api it exists and it will update else we will get it from the api call
|
||||
//console.log(`this is my tagCode: ${tagCode}`);
|
||||
|
||||
// Build base URL with required parameters
|
||||
let apiUrl = `?asset_product_model_id=${assetModelId}&asset_product_model_uuid=${uuid}&asset_name=${assetName}&asset_description=${description}&asset_status=${assetStatus}&asset_profile_id=${assetProfileId}&asset_location_id=${assetLocationId}&asset_process_id=${assetProcessId}&child_assets=${child_assets}`;
|
||||
|
||||
// Only add tagCode to URL if it exists
|
||||
if (tagCode) {
|
||||
apiUrl += `&asset_tag_number=${tagCode}`;
|
||||
console.log('hello there');
|
||||
}
|
||||
|
||||
assetregisterAPI += apiUrl;
|
||||
console.log("API call to register asset in central database", assetregisterAPI);
|
||||
|
||||
const response = await fetch(assetregisterAPI, {
|
||||
method: "POST"
|
||||
});
|
||||
|
||||
// Get the response text first
|
||||
const responseText = await response.text();
|
||||
console.log("Raw API response:", responseText);
|
||||
|
||||
// Try to parse the JSON, handling potential parsing errors
|
||||
let jsonResponse;
|
||||
try {
|
||||
jsonResponse = JSON.parse(responseText);
|
||||
} catch (parseError) {
|
||||
console.error("JSON Parsing Error:", parseError);
|
||||
console.error("Response that could not be parsed:", responseText);
|
||||
throw new Error("Failed to parse API response");
|
||||
}
|
||||
|
||||
console.log(jsonResponse);
|
||||
|
||||
if(jsonResponse.success){
|
||||
console.log(`${jsonResponse.message}, tag number: ${jsonResponse.asset_tag_number}, asset id: ${jsonResponse.asset_id}`);
|
||||
// Save the asset tag number and id to the node
|
||||
} else {
|
||||
console.log("Asset not registered in central database");
|
||||
}
|
||||
return jsonResponse;
|
||||
|
||||
} catch (error) {
|
||||
console.log("Error saving changes to asset register API", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async fetchData(url, fallbackUrl) {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const responsData = await response.json();
|
||||
//responsData
|
||||
const data = responsData.data;
|
||||
/* .map(item => {
|
||||
const { vendor_name, ...rest } = item;
|
||||
return {
|
||||
name: vendor_name,
|
||||
...rest
|
||||
};
|
||||
}); */
|
||||
console.log(url);
|
||||
console.log("Response Data: ", data);
|
||||
return data;
|
||||
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
`Primary URL failed: ${url}. Trying fallback URL: ${fallbackUrl}`,
|
||||
err
|
||||
);
|
||||
try {
|
||||
const response = await fetch(fallbackUrl);
|
||||
if (!response.ok)
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return await response.json();
|
||||
} catch (fallbackErr) {
|
||||
console.error("Both primary and fallback URLs failed:", fallbackErr);
|
||||
return [];
|
||||
}
|
||||
constructor() {
|
||||
this.isCloud = false;
|
||||
this.configData = null;
|
||||
}
|
||||
}
|
||||
|
||||
async fetchProjectData(url) {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const responsData = await response.json();
|
||||
console.log("Response Data: ", responsData);
|
||||
return responsData;
|
||||
|
||||
} catch (err) {
|
||||
/* intentionally empty */
|
||||
}
|
||||
}
|
||||
|
||||
async populateDropdown(
|
||||
htmlElement,
|
||||
options,
|
||||
node,
|
||||
property,
|
||||
callback
|
||||
) {
|
||||
this.generateHtml(htmlElement, options, node[property]);
|
||||
|
||||
htmlElement.addEventListener("change", async (e) => {
|
||||
const newValue = e.target.value;
|
||||
console.log(`Dropdown changed: ${property} = ${newValue}`);
|
||||
node[property] = newValue;
|
||||
|
||||
RED.nodes.dirty(true);
|
||||
if (callback) await callback(newValue); // Ensure async callback completion
|
||||
});
|
||||
}
|
||||
|
||||
// Helper function to construct a URL from a base and path internal
|
||||
constructUrl(base, ...paths) {
|
||||
|
||||
// Remove trailing slash from base and leading slashes from paths
|
||||
const sanitizedBase = (base || "").replace(/\/+$/, "");
|
||||
const sanitizedPaths = paths.map((path) => path.replace(/^\/+|\/+$/g, ""));
|
||||
|
||||
// Join sanitized base and paths
|
||||
const url = `${sanitizedBase}/${sanitizedPaths.join("/")}`;
|
||||
console.log("Base:", sanitizedBase);
|
||||
console.log("Paths:", sanitizedPaths);
|
||||
console.log("Constructed URL:", url);
|
||||
return url;
|
||||
}
|
||||
|
||||
//Adjust for API Gateway
|
||||
constructCloudURL(base, ...paths) {
|
||||
// Remove trailing slash from base and leading slashes from paths
|
||||
const sanitizedBase = base.replace(/\/+$/, "");
|
||||
const sanitizedPaths = paths.map((path) => path.replace(/^\/+|\/+$/g, ""));
|
||||
// Join sanitized base and paths
|
||||
const url = `${sanitizedBase}/${sanitizedPaths.join("/")}`;
|
||||
return url;
|
||||
}
|
||||
|
||||
populateSubTypes(configUrls, elements, node, selectedSupplier) {
|
||||
|
||||
this.fetchData(configUrls.cloud.config, configUrls.local.config)
|
||||
.then((configData) => {
|
||||
const assetType = configData.asset?.type?.default;
|
||||
const supplierFolder = this.constructUrl( configUrls.local.taggcodeAPI, `${assetType}s`, selectedSupplier );
|
||||
|
||||
const localSubTypesUrl = this.constructUrl(supplierFolder, "subtypes.json");
|
||||
const cloudSubTypesUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/product/get_subtypesFromVendor.php?vendor_name=" + selectedSupplier);
|
||||
|
||||
return this.fetchData(cloudSubTypesUrl, localSubTypesUrl)
|
||||
.then((subTypeData) => {
|
||||
const subTypes = subTypeData.map((subType) => subType.name);
|
||||
|
||||
return this.populateDropdown(
|
||||
elements.subType,
|
||||
subTypes,
|
||||
node,
|
||||
"subType",
|
||||
function (selectedSubType) {
|
||||
if (selectedSubType) {
|
||||
// When subType changes, update both models and units
|
||||
this.populateModels(
|
||||
configUrls,
|
||||
elements,
|
||||
node,
|
||||
selectedSupplier,
|
||||
selectedSubType
|
||||
);
|
||||
this.populateUnitsForSubType(
|
||||
configUrls,
|
||||
elements,
|
||||
node,
|
||||
selectedSubType
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
})
|
||||
.then(() => {
|
||||
// If we have a saved subType, trigger both models and units population
|
||||
if (node.subType) {
|
||||
this.populateModels(
|
||||
configUrls,
|
||||
elements,
|
||||
node,
|
||||
selectedSupplier,
|
||||
node.subType
|
||||
);
|
||||
this.populateUnitsForSubType(configUrls, elements, node, node.subType);
|
||||
}
|
||||
//console.log("In fetch part of subtypes ");
|
||||
// Store all data from selected model
|
||||
/* node["modelMetadata"] = modelData.find(
|
||||
(model) => model.name === node.model
|
||||
);
|
||||
console.log("Model Metadata: ", node["modelMetadata"]); */
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error populating subtypes:", error);
|
||||
});
|
||||
}
|
||||
|
||||
populateUnitsForSubType(configUrls, elements, node, selectedSubType) {
|
||||
// Fetch the units data
|
||||
this.fetchData(configUrls.cloud.units, configUrls.local.units)
|
||||
.then((unitsData) => {
|
||||
// Find the category that matches the subType name
|
||||
const categoryData = unitsData.units.find(
|
||||
(category) =>
|
||||
category.category.toLowerCase() === selectedSubType.toLowerCase()
|
||||
);
|
||||
|
||||
if (categoryData) {
|
||||
// Extract just the unit values and descriptions
|
||||
const units = categoryData.values.map((unit) => ({
|
||||
value: unit.value,
|
||||
description: unit.description,
|
||||
}));
|
||||
|
||||
// Create the options array with descriptions as labels
|
||||
const options = units.map((unit) => ({
|
||||
value: unit.value,
|
||||
label: `${unit.value} - ${unit.description}`,
|
||||
}));
|
||||
|
||||
// Populate the units dropdown
|
||||
this.populateDropdown(
|
||||
elements.unit,
|
||||
options.map((opt) => opt.value),
|
||||
node,
|
||||
"unit"
|
||||
);
|
||||
|
||||
// If there's no currently selected unit but we have options, select the first one
|
||||
if (!node.unit && options.length > 0) {
|
||||
node.unit = options[0].value;
|
||||
elements.unit.value = options[0].value;
|
||||
}
|
||||
} else {
|
||||
// If no matching category is found, provide a default % option
|
||||
const defaultUnits = [{ value: "%", description: "Percentage" }];
|
||||
this.populateDropdown(
|
||||
elements.unit,
|
||||
defaultUnits.map((unit) => unit.value),
|
||||
node,
|
||||
"unit"
|
||||
);
|
||||
console.warn(
|
||||
`No matching unit category found for subType: ${selectedSubType}`
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error fetching units:", error);
|
||||
});
|
||||
}
|
||||
|
||||
populateModels(
|
||||
configUrls,
|
||||
elements,
|
||||
node,
|
||||
selectedSupplier,
|
||||
selectedSubType
|
||||
) {
|
||||
|
||||
this.fetchData(configUrls.cloud.config, configUrls.local.config)
|
||||
.then((configData) => {
|
||||
const assetType = configData.asset?.type?.default;
|
||||
// save assetType to fetch later
|
||||
node.assetType = assetType;
|
||||
|
||||
const supplierFolder = this.constructUrl( configUrls.local.taggcodeAPI,`${assetType}s`,selectedSupplier);
|
||||
const subTypeFolder = this.constructUrl(supplierFolder, selectedSubType);
|
||||
const localModelsUrl = this.constructUrl(subTypeFolder, "models.json");
|
||||
const cloudModelsUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/product/get_product_models.php?vendor_name=" + selectedSupplier + "&product_subtype_name=" + selectedSubType);
|
||||
|
||||
return this.fetchData(cloudModelsUrl, localModelsUrl).then((modelData) => {
|
||||
const models = modelData.map((model) => model.name); // use this to populate the dropdown
|
||||
|
||||
// If a model is already selected, store its metadata immediately
|
||||
if (node.model) {
|
||||
node["modelMetadata"] = modelData.find((model) => model.name === node.model);
|
||||
}
|
||||
|
||||
this.populateDropdown(elements.model, models, node, "model", (selectedModel) => {
|
||||
// Store only the metadata for the selected model
|
||||
node["modelMetadata"] = modelData.find((model) => model.name === selectedModel);
|
||||
});
|
||||
/*
|
||||
console.log('hello here I am:');
|
||||
console.log(node["modelMetadata"]);
|
||||
*/
|
||||
// Mix all method groups onto the prototype
|
||||
const mixins = [toggles, dataFetching, urlUtils, dropdownPopulation, htmlGeneration];
|
||||
for (const mixin of mixins) {
|
||||
for (const [name, fn] of Object.entries(mixin)) {
|
||||
if (typeof fn === 'function') {
|
||||
Object.defineProperty(MenuUtils.prototype, name, {
|
||||
value: fn,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
enumerable: false,
|
||||
});
|
||||
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error populating models:", error);
|
||||
});
|
||||
}
|
||||
|
||||
generateHtml(htmlElement, options, savedValue) {
|
||||
htmlElement.innerHTML = options.length
|
||||
? `<option value="">Select...</option>${options
|
||||
.map((opt) => `<option value="${opt}">${opt}</option>`)
|
||||
.join("")}`
|
||||
: "<option value=''>No options available</option>";
|
||||
|
||||
if (savedValue && options.includes(savedValue)) {
|
||||
htmlElement.value = savedValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
createMenuUtilsEndpoint(RED, nodeName, customHelpers = {}) {
|
||||
RED.httpAdmin.get(`/${nodeName}/resources/menuUtils.js`, function(req, res) {
|
||||
console.log(`Serving menuUtils.js for ${nodeName} node`);
|
||||
res.set('Content-Type', 'application/javascript');
|
||||
|
||||
const browserCode = this.generateMenuUtilsCode(nodeName, customHelpers);
|
||||
res.send(browserCode);
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
generateMenuUtilsCode(nodeName, customHelpers = {}) {
|
||||
const defaultHelpers = {
|
||||
validateRequired: `function(value) {
|
||||
return value && value.toString().trim() !== '';
|
||||
}`,
|
||||
formatDisplayValue: `function(value, unit) {
|
||||
return \`\${value} \${unit || ''}\`.trim();
|
||||
}`
|
||||
};
|
||||
|
||||
const allHelpers = { ...defaultHelpers, ...customHelpers };
|
||||
|
||||
const helpersCode = Object.entries(allHelpers)
|
||||
.map(([name, func]) => ` ${name}: ${func}`)
|
||||
.join(',\n');
|
||||
|
||||
const classCode = MenuUtils.toString(); // <-- this gives full class MenuUtils {...}
|
||||
|
||||
return `
|
||||
// Create EVOLV namespace structure
|
||||
window.EVOLV = window.EVOLV || {};
|
||||
window.EVOLV.nodes = window.EVOLV.nodes || {};
|
||||
window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {};
|
||||
|
||||
// Inject MenuUtils class
|
||||
${classCode}
|
||||
|
||||
// Expose MenuUtils instance to namespace
|
||||
window.EVOLV.nodes.${nodeName}.utils = {
|
||||
menuUtils: new MenuUtils(),
|
||||
|
||||
helpers: {
|
||||
${helpersCode}
|
||||
}
|
||||
};
|
||||
|
||||
// Optionally expose globally
|
||||
window.MenuUtils = MenuUtils;
|
||||
|
||||
console.log('${nodeName} utilities loaded in namespace');
|
||||
`;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = MenuUtils;
|
||||
module.exports = MenuUtils;
|
||||
|
||||
@@ -9,7 +9,7 @@ class OutputUtils {
|
||||
checkForChanges(output, format) {
|
||||
const changedFields = {};
|
||||
for (const key in output) {
|
||||
if (output.hasOwnProperty(key) && output[key] !== this.output[format][key]) {
|
||||
if (Object.prototype.hasOwnProperty.call(output, key) && output[key] !== this.output[format][key]) {
|
||||
let value = output[key];
|
||||
// For fields: if the value is an object (and not a Date), stringify it.
|
||||
if (value !== null && typeof value === 'object' && !(value instanceof Date)) {
|
||||
@@ -81,13 +81,13 @@ class OutputUtils {
|
||||
flattenTags(obj) {
|
||||
const result = {};
|
||||
for (const key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
const value = obj[key];
|
||||
if (value !== null && typeof value === 'object' && !(value instanceof Date)) {
|
||||
// Recursively flatten the nested object.
|
||||
const flatChild = this.flattenTags(value);
|
||||
for (const childKey in flatChild) {
|
||||
if (flatChild.hasOwnProperty(childKey)) {
|
||||
if (Object.prototype.hasOwnProperty.call(flatChild, childKey)) {
|
||||
result[`${key}_${childKey}`] = String(flatChild[childKey]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
const MeasurementBuilder = require('./MeasurementBuilder');
|
||||
const EventEmitter = require('events');
|
||||
const convertModule = require('../convert/index');
|
||||
const { POSITIONS } = require('../constants/positions');
|
||||
|
||||
class MeasurementContainer {
|
||||
constructor(options = {},logger) {
|
||||
constructor(options = {},_logger) {
|
||||
this.emitter = new EventEmitter();
|
||||
this.measurements = {};
|
||||
this.windowSize = options.windowSize || 10; // Default window size
|
||||
@@ -359,7 +360,7 @@ class MeasurementContainer {
|
||||
|
||||
|
||||
// Difference calculations between positions
|
||||
difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
||||
difference({ from = POSITIONS.DOWNSTREAM, to = POSITIONS.UPSTREAM, unit: requestedUnit } = {}) {
|
||||
if (!this._currentType || !this._currentVariant) {
|
||||
throw new Error("Type and variant must be specified for difference calculation");
|
||||
}
|
||||
@@ -526,11 +527,11 @@ difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
||||
|
||||
_convertPositionStr2Num(positionString) {
|
||||
switch(positionString) {
|
||||
case "atEquipment":
|
||||
case POSITIONS.AT_EQUIPMENT:
|
||||
return 0;
|
||||
case "upstream":
|
||||
case POSITIONS.UPSTREAM:
|
||||
return Number.POSITIVE_INFINITY;
|
||||
case "downstream":
|
||||
case POSITIONS.DOWNSTREAM:
|
||||
return Number.NEGATIVE_INFINITY;
|
||||
|
||||
default:
|
||||
@@ -544,11 +545,11 @@ difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
|
||||
_convertPositionNum2Str(positionValue) {
|
||||
switch (positionValue) {
|
||||
case 0:
|
||||
return "atEquipment";
|
||||
return POSITIONS.AT_EQUIPMENT;
|
||||
case (positionValue < 0):
|
||||
return "upstream";
|
||||
return POSITIONS.UPSTREAM;
|
||||
case (positionValue > 0):
|
||||
return "downstream";
|
||||
return POSITIONS.DOWNSTREAM;
|
||||
default:
|
||||
console.log(`Invalid position provided: ${positionValue}`);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const { MeasurementContainer } = require('./index');
|
||||
const { POSITIONS } = require('../constants/positions');
|
||||
|
||||
const measurements = new MeasurementContainer();
|
||||
|
||||
@@ -29,7 +30,7 @@ console.log('\nSetting pressure values with distances:');
|
||||
basicContainer
|
||||
.type('pressure')
|
||||
.variant('measured')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.distance(1.5)
|
||||
.value(100)
|
||||
.unit('psi');
|
||||
@@ -37,7 +38,7 @@ basicContainer
|
||||
basicContainer
|
||||
.type('pressure')
|
||||
.variant('measured')
|
||||
.position('downstream')
|
||||
.position(POSITIONS.DOWNSTREAM)
|
||||
.distance(5.2)
|
||||
.value(95)
|
||||
.unit('psi');
|
||||
@@ -46,7 +47,7 @@ basicContainer
|
||||
basicContainer
|
||||
.type('pressure')
|
||||
.variant('measured')
|
||||
.position('downstream')
|
||||
.position(POSITIONS.DOWNSTREAM)
|
||||
.value(90); // distance 5.2 is automatically reused
|
||||
|
||||
console.log('✅ Basic setup complete\n');
|
||||
@@ -55,7 +56,7 @@ console.log('✅ Basic setup complete\n');
|
||||
const upstreamPressure = basicContainer
|
||||
.type('pressure')
|
||||
.variant('measured')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.get();
|
||||
|
||||
console.log(`Retrieved upstream pressure: ${upstreamPressure.getCurrentValue()} ${upstreamPressure.unit}`);
|
||||
@@ -85,7 +86,7 @@ console.log('Adding pressure with auto-conversion:');
|
||||
autoContainer
|
||||
.type('pressure')
|
||||
.variant('measured')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.distance(0.5)
|
||||
.value(1.5, Date.now(), 'bar'); // Input: 1.5 bar → Auto-stored as ~21.76 psi
|
||||
|
||||
@@ -93,7 +94,7 @@ autoContainer
|
||||
const converted = autoContainer
|
||||
.type('pressure')
|
||||
.variant('measured')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.get();
|
||||
|
||||
console.log(`Stored as: ${converted.getCurrentValue()} ${converted.unit} (distance=${converted.distance}m)`);
|
||||
@@ -107,14 +108,14 @@ console.log('--- Example 3: Unit Conversion on Retrieval ---');
|
||||
autoContainer
|
||||
.type('flow')
|
||||
.variant('predicted')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.distance(2.4)
|
||||
.value(100, Date.now(), 'l/min');
|
||||
|
||||
const flowMeasurement = autoContainer
|
||||
.type('flow')
|
||||
.variant('predicted')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.get();
|
||||
|
||||
console.log(`Flow in l/min: ${flowMeasurement.getCurrentValue('l/min')}`);
|
||||
@@ -155,13 +156,13 @@ console.log('--- Example 5: Basic Value Retrieval ---');
|
||||
const upstreamVal = basicContainer
|
||||
.type('pressure')
|
||||
.variant('measured')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.getCurrentValue();
|
||||
|
||||
const upstreamData = basicContainer
|
||||
.type('pressure')
|
||||
.variant('measured')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.get();
|
||||
|
||||
console.log(`Upstream: ${upstreamVal} ${upstreamData.unit} at ${upstreamData.distance}m`);
|
||||
@@ -169,31 +170,31 @@ console.log(`Upstream: ${upstreamVal} ${upstreamData.unit} at ${upstreamData.dis
|
||||
const downstreamVal = basicContainer
|
||||
.type('pressure')
|
||||
.variant('measured')
|
||||
.position('downstream')
|
||||
.position(POSITIONS.DOWNSTREAM)
|
||||
.getCurrentValue();
|
||||
|
||||
const downstreamData = basicContainer
|
||||
.type('pressure')
|
||||
.variant('measured')
|
||||
.position('downstream')
|
||||
.position(POSITIONS.DOWNSTREAM)
|
||||
.get();
|
||||
|
||||
//check wether a serie exists
|
||||
const hasSeries = measurements
|
||||
const hasSeries = measurements // eslint-disable-line no-unused-vars
|
||||
.type("flow")
|
||||
.variant("measured")
|
||||
.exists(); // true if any position exists
|
||||
|
||||
const hasUpstreamValues = measurements
|
||||
const hasUpstreamValues = measurements // eslint-disable-line no-unused-vars
|
||||
.type("flow")
|
||||
.variant("measured")
|
||||
.exists({ position: "upstream", requireValues: true });
|
||||
.exists({ position: POSITIONS.UPSTREAM, requireValues: true });
|
||||
|
||||
// Passing everything explicitly
|
||||
const hasPercent = measurements.exists({
|
||||
const hasPercent = measurements.exists({ // eslint-disable-line no-unused-vars
|
||||
type: "volume",
|
||||
variant: "percent",
|
||||
position: "atEquipment",
|
||||
position: POSITIONS.AT_EQUIPMENT,
|
||||
});
|
||||
|
||||
|
||||
@@ -207,7 +208,7 @@ console.log('--- Example 6: Calculations & Statistics ---');
|
||||
basicContainer
|
||||
.type('flow')
|
||||
.variant('predicted')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.distance(3.0)
|
||||
.value(200)
|
||||
.unit('gpm');
|
||||
@@ -215,7 +216,7 @@ basicContainer
|
||||
basicContainer
|
||||
.type('flow')
|
||||
.variant('predicted')
|
||||
.position('downstream')
|
||||
.position(POSITIONS.DOWNSTREAM)
|
||||
.distance(8.5)
|
||||
.value(195)
|
||||
.unit('gpm');
|
||||
@@ -223,7 +224,7 @@ basicContainer
|
||||
const flowAvg = basicContainer
|
||||
.type('flow')
|
||||
.variant('predicted')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.getAverage();
|
||||
|
||||
console.log(`Average upstream flow: ${flowAvg.toFixed(1)} gpm`);
|
||||
@@ -236,8 +237,8 @@ const pressureDiff = basicContainer
|
||||
console.log(`Pressure difference: ${pressureDiff.value} ${pressureDiff.unit}\n`);
|
||||
|
||||
//reversable difference
|
||||
const deltaP = basicContainer.type("pressure").variant("measured").difference(); // defaults to downstream - upstream
|
||||
const netFlow = basicContainer.type("flow").variant("measured").difference({ from: "upstream", to: "downstream" });
|
||||
const deltaP = basicContainer.type("pressure").variant("measured").difference(); // eslint-disable-line no-unused-vars -- defaults to downstream - upstream
|
||||
const netFlow = basicContainer.type("flow").variant("measured").difference({ from: POSITIONS.UPSTREAM, to: POSITIONS.DOWNSTREAM }); // eslint-disable-line no-unused-vars
|
||||
|
||||
// ====================================
|
||||
// ADVANCED STATISTICS & HISTORY
|
||||
@@ -247,7 +248,7 @@ console.log('--- Example 7: Advanced Statistics & History ---');
|
||||
basicContainer
|
||||
.type('flow')
|
||||
.variant('measured')
|
||||
.position('upstream')
|
||||
.position(POSITIONS.UPSTREAM)
|
||||
.distance(3.0)
|
||||
.value(210)
|
||||
.value(215)
|
||||
@@ -259,7 +260,7 @@ basicContainer
|
||||
const stats = basicContainer
|
||||
.type('flow')
|
||||
.variant('measured')
|
||||
.position('upstream');
|
||||
.position(POSITIONS.UPSTREAM);
|
||||
|
||||
const statsData = stats.get();
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const AssetMenu = require('./asset.js');
|
||||
const { TagcodeApp, DynamicAssetMenu } = require('./tagcodeApp.js');
|
||||
// TagcodeApp and DynamicAssetMenu available via ./tagcodeApp.js
|
||||
const LoggerMenu = require('./logger.js');
|
||||
const PhysicalPositionMenu = require('./physicalPosition.js');
|
||||
|
||||
|
||||
@@ -86,4 +86,6 @@ dataStream.forEach((value, index) => {
|
||||
});
|
||||
|
||||
console.log("\nFinal detector cluster states:", JSON.stringify(detector.clusters, null, 2));
|
||||
*/
|
||||
*/
|
||||
|
||||
module.exports = DynamicClusterDeviation;
|
||||
@@ -88,7 +88,7 @@ class Interpolation {
|
||||
array_values(obj) {
|
||||
const new_array = [];
|
||||
for (let i in obj) {
|
||||
if (obj.hasOwnProperty(i)) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, i)) {
|
||||
new_array.push(obj[i]);
|
||||
}
|
||||
}
|
||||
@@ -231,7 +231,6 @@ class Interpolation {
|
||||
let xdata = this.input_xdata;
|
||||
let ydata = this.input_ydata;
|
||||
|
||||
let interpolationtype = this.interpolationtype;
|
||||
let tension = this.tension;
|
||||
|
||||
let n = ydata.length;
|
||||
|
||||
@@ -216,7 +216,6 @@ class movementManager {
|
||||
return reject(new Error("Movement aborted"));
|
||||
}
|
||||
|
||||
const direction = targetPosition > this.currentPosition ? 1 : -1;
|
||||
const totalDistance = Math.abs(targetPosition - this.currentPosition);
|
||||
const startPosition = this.currentPosition;
|
||||
this.speed = Math.min(Math.max(this.speed, 0), 1);
|
||||
|
||||
Reference in New Issue
Block a user