Compare commits
1 Commits
e50be2ee66
...
43f69066af
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
43f69066af |
@@ -193,8 +193,13 @@ class AssetMenu {
|
|||||||
return normalizeApiCategory(key, node.softwareType || key, payload.data);
|
return normalizeApiCategory(key, node.softwareType || key, payload.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Non-dispatching populate (matches the wireEvents version). The
|
||||||
|
// load path below explicitly walks supplier -> type -> model ->
|
||||||
|
// unit in order using saved node.* values, so auto-dispatched
|
||||||
|
// change events (which previously cascaded through wireEvents'
|
||||||
|
// listeners and double-populated everything) are no longer needed.
|
||||||
function populate(selectEl, items = [], selectedValue, mapFn, placeholderText = 'Select...') {
|
function populate(selectEl, items = [], selectedValue, mapFn, placeholderText = 'Select...') {
|
||||||
const previous = selectEl.value;
|
if (!selectEl) return;
|
||||||
const mapper = typeof mapFn === 'function'
|
const mapper = typeof mapFn === 'function'
|
||||||
? mapFn
|
? mapFn
|
||||||
: (value) => ({ value, label: value });
|
: (value) => ({ value, label: value });
|
||||||
@@ -227,9 +232,6 @@ class AssetMenu {
|
|||||||
} else {
|
} else {
|
||||||
selectEl.value = '';
|
selectEl.value = '';
|
||||||
}
|
}
|
||||||
if (selectEl.value !== previous) {
|
|
||||||
selectEl.dispatchEvent(new Event('change'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const categoryKey = resolveCategoryKey();
|
const categoryKey = resolveCategoryKey();
|
||||||
@@ -305,6 +307,28 @@ class AssetMenu {
|
|||||||
getEventInjectionCode(nodeName) {
|
getEventInjectionCode(nodeName) {
|
||||||
return `
|
return `
|
||||||
// Asset event wiring for ${nodeName}
|
// Asset event wiring for ${nodeName}
|
||||||
|
//
|
||||||
|
// The supplier -> type -> model -> unit chain is a strict downward
|
||||||
|
// cascade: each select rebuilds the next based on the currently
|
||||||
|
// selected value above it. Two earlier bugs in this code:
|
||||||
|
//
|
||||||
|
// 1. populate() auto-dispatched a synthetic 'change' event whenever
|
||||||
|
// the value of the rebuilt select differed from before the
|
||||||
|
// rebuild. That triggered the *child* select's listener mid-way
|
||||||
|
// through the *parent* listener, which then continued and
|
||||||
|
// blindly overwrote the child select with empty content. Net
|
||||||
|
// effect: model dropdown showed 'Awaiting Type Selection' even
|
||||||
|
// though a type was clearly selected.
|
||||||
|
//
|
||||||
|
// 2. Each downstream wipe ran unconditionally inside the parent
|
||||||
|
// handler, instead of being driven by the actual current value
|
||||||
|
// of the child select.
|
||||||
|
//
|
||||||
|
// Fix: populate() no longer dispatches change. Cascade is explicit
|
||||||
|
// via cascadeFromSupplier() / cascadeFromType() / cascadeFromModel()
|
||||||
|
// which are called from each handler. The same helpers run on
|
||||||
|
// initial load so behaviour is identical whether the user picked the
|
||||||
|
// value or it came from a saved node.
|
||||||
window.EVOLV.nodes.${nodeName}.assetMenu.wireEvents = function(node) {
|
window.EVOLV.nodes.${nodeName}.assetMenu.wireEvents = function(node) {
|
||||||
const menuAsset = window.EVOLV.nodes.${nodeName}.menuData.asset || {};
|
const menuAsset = window.EVOLV.nodes.${nodeName}.menuData.asset || {};
|
||||||
const categories = menuAsset.categories || {};
|
const categories = menuAsset.categories || {};
|
||||||
@@ -316,11 +340,17 @@ class AssetMenu {
|
|||||||
unit: document.getElementById('node-input-unit')
|
unit: document.getElementById('node-input-unit')
|
||||||
};
|
};
|
||||||
|
|
||||||
function populate(selectEl, items = [], selectedValue, mapFn, placeholderText = 'Select...') {
|
// populate(): rebuild a <select> with a placeholder + items.
|
||||||
const previous = selectEl.value;
|
// No change-event dispatch — cascading is done explicitly by the
|
||||||
|
// caller via cascadeFrom*() so the order of operations is
|
||||||
|
// predictable.
|
||||||
|
function populate(selectEl, items, selectedValue, mapFn, placeholderText) {
|
||||||
|
if (!selectEl) return;
|
||||||
|
if (!Array.isArray(items)) items = [];
|
||||||
|
if (!placeholderText) placeholderText = 'Select...';
|
||||||
const mapper = typeof mapFn === 'function'
|
const mapper = typeof mapFn === 'function'
|
||||||
? mapFn
|
? mapFn
|
||||||
: (value) => ({ value, label: value });
|
: (value) => ({ value: value, label: value });
|
||||||
|
|
||||||
selectEl.innerHTML = '';
|
selectEl.innerHTML = '';
|
||||||
|
|
||||||
@@ -331,11 +361,9 @@ class AssetMenu {
|
|||||||
placeholder.selected = true;
|
placeholder.selected = true;
|
||||||
selectEl.appendChild(placeholder);
|
selectEl.appendChild(placeholder);
|
||||||
|
|
||||||
items.forEach((item) => {
|
items.forEach(function (item) {
|
||||||
const option = mapper(item);
|
const option = mapper(item);
|
||||||
if (!option || typeof option.value === 'undefined') {
|
if (!option || typeof option.value === 'undefined') return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
const opt = document.createElement('option');
|
const opt = document.createElement('option');
|
||||||
opt.value = option.value;
|
opt.value = option.value;
|
||||||
opt.textContent = option.label;
|
opt.textContent = option.label;
|
||||||
@@ -344,111 +372,112 @@ class AssetMenu {
|
|||||||
|
|
||||||
if (selectedValue) {
|
if (selectedValue) {
|
||||||
selectEl.value = selectedValue;
|
selectEl.value = selectedValue;
|
||||||
if (!selectEl.value) {
|
if (!selectEl.value) selectEl.value = '';
|
||||||
selectEl.value = '';
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
selectEl.value = '';
|
selectEl.value = '';
|
||||||
}
|
}
|
||||||
if (selectEl.value !== previous) {
|
|
||||||
selectEl.dispatchEvent(new Event('change'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolveCategoryKey = () => {
|
function resolveCategoryKey() {
|
||||||
if (node.softwareType && categories[node.softwareType]) {
|
if (node.softwareType && categories[node.softwareType]) return node.softwareType;
|
||||||
return node.softwareType;
|
if (node.category && categories[node.category]) return node.category;
|
||||||
}
|
|
||||||
if (node.category && categories[node.category]) {
|
|
||||||
return node.category;
|
|
||||||
}
|
|
||||||
return defaultCategory;
|
return defaultCategory;
|
||||||
};
|
}
|
||||||
|
function getActiveCategory() {
|
||||||
const getActiveCategory = () => {
|
|
||||||
const key = resolveCategoryKey();
|
const key = resolveCategoryKey();
|
||||||
return key ? categories[key] : null;
|
return key ? categories[key] : null;
|
||||||
};
|
}
|
||||||
|
|
||||||
node.category = resolveCategoryKey();
|
node.category = resolveCategoryKey();
|
||||||
|
|
||||||
elems.supplier.addEventListener('change', () => {
|
// Lookup helpers — read from the *currently selected* values in the
|
||||||
const category = getActiveCategory();
|
// DOM, not from node.* (which may not yet be in sync).
|
||||||
const supplier = category
|
function findSupplier() {
|
||||||
? category.suppliers.find(
|
const cat = getActiveCategory();
|
||||||
(item) => String(item.id || item.name) === String(elems.supplier.value)
|
if (!cat || !Array.isArray(cat.suppliers)) return null;
|
||||||
)
|
const id = String(elems.supplier.value);
|
||||||
: null;
|
return cat.suppliers.find(function (s) {
|
||||||
|
return String(s.id || s.name) === id;
|
||||||
|
}) || null;
|
||||||
|
}
|
||||||
|
function findType(supplier) {
|
||||||
|
if (!supplier || !Array.isArray(supplier.types)) return null;
|
||||||
|
const id = String(elems.type.value);
|
||||||
|
return supplier.types.find(function (t) {
|
||||||
|
return String(t.id || t.name) === id;
|
||||||
|
}) || null;
|
||||||
|
}
|
||||||
|
function findModel(type) {
|
||||||
|
if (!type || !Array.isArray(type.models)) return null;
|
||||||
|
const id = String(elems.model.value);
|
||||||
|
return type.models.find(function (m) {
|
||||||
|
return String(m.id || m.name) === id;
|
||||||
|
}) || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Cascade rebuild functions ==========================
|
||||||
|
// Each one rebuilds the dropdown for the *level it owns* plus all
|
||||||
|
// levels below it, using the current values in the DOM. Called by
|
||||||
|
// the corresponding change handler AND by initial load so both
|
||||||
|
// paths produce identical state.
|
||||||
|
|
||||||
|
function cascadeFromSupplier() {
|
||||||
|
const supplier = findSupplier();
|
||||||
const types = supplier ? supplier.types : [];
|
const types = supplier ? supplier.types : [];
|
||||||
populate(
|
populate(
|
||||||
elems.type,
|
elems.type,
|
||||||
types,
|
types,
|
||||||
node.assetType,
|
node.assetType,
|
||||||
(type) => ({ value: type.id || type.name, label: type.name }),
|
function (t) { return { value: t.id || t.name, label: t.name }; },
|
||||||
supplier ? 'Select...' : 'Awaiting Supplier Selection'
|
supplier ? 'Select...' : 'Awaiting Supplier Selection'
|
||||||
);
|
);
|
||||||
node.modelMetadata = null;
|
// After repopulating type, propagate down. cascadeFromType()
|
||||||
populate(elems.model, [], '', undefined, 'Awaiting Type Selection');
|
// will read the new elems.type.value (which was set by populate
|
||||||
populate(elems.unit, [], '', undefined, 'Awaiting Type Selection');
|
// to either the saved node.assetType or '') and rebuild model.
|
||||||
});
|
cascadeFromType();
|
||||||
|
}
|
||||||
|
|
||||||
elems.type.addEventListener('change', () => {
|
function cascadeFromType() {
|
||||||
const category = getActiveCategory();
|
const supplier = findSupplier();
|
||||||
const supplier = category
|
const type = findType(supplier);
|
||||||
? category.suppliers.find(
|
|
||||||
(item) => String(item.id || item.name) === String(elems.supplier.value)
|
|
||||||
)
|
|
||||||
: null;
|
|
||||||
const type = supplier
|
|
||||||
? supplier.types.find(
|
|
||||||
(item) => String(item.id || item.name) === String(elems.type.value)
|
|
||||||
)
|
|
||||||
: null;
|
|
||||||
const models = type ? type.models : [];
|
const models = type ? type.models : [];
|
||||||
populate(
|
populate(
|
||||||
elems.model,
|
elems.model,
|
||||||
models,
|
models,
|
||||||
node.model,
|
node.model,
|
||||||
(model) => ({ value: model.id || model.name, label: model.name }),
|
function (m) { return { value: m.id || m.name, label: m.name }; },
|
||||||
type ? 'Select...' : 'Awaiting Type Selection'
|
type ? 'Select...' : 'Awaiting Type Selection'
|
||||||
);
|
);
|
||||||
node.modelMetadata = null;
|
node.modelMetadata = null;
|
||||||
populate(
|
cascadeFromModel();
|
||||||
elems.unit,
|
}
|
||||||
[],
|
|
||||||
'',
|
|
||||||
undefined,
|
|
||||||
type ? 'Awaiting Model Selection' : 'Awaiting Type Selection'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
elems.model.addEventListener('change', () => {
|
function cascadeFromModel() {
|
||||||
const category = getActiveCategory();
|
const supplier = findSupplier();
|
||||||
const supplier = category
|
const type = findType(supplier);
|
||||||
? category.suppliers.find(
|
const model = findModel(type);
|
||||||
(item) => String(item.id || item.name) === String(elems.supplier.value)
|
|
||||||
)
|
|
||||||
: null;
|
|
||||||
const type = supplier
|
|
||||||
? supplier.types.find(
|
|
||||||
(item) => String(item.id || item.name) === String(elems.type.value)
|
|
||||||
)
|
|
||||||
: null;
|
|
||||||
const model = type
|
|
||||||
? type.models.find(
|
|
||||||
(item) => String(item.id || item.name) === String(elems.model.value)
|
|
||||||
)
|
|
||||||
: null;
|
|
||||||
node.modelMetadata = model;
|
node.modelMetadata = model;
|
||||||
node.modelName = model ? model.name : '';
|
node.modelName = model ? model.name : '';
|
||||||
populate(
|
populate(
|
||||||
elems.unit,
|
elems.unit,
|
||||||
model ? model.units || [] : [],
|
model ? (model.units || []) : [],
|
||||||
node.unit,
|
node.unit,
|
||||||
(unit) => ({ value: unit, label: unit }),
|
function (u) { return { value: u, label: u }; },
|
||||||
model ? 'Select...' : type ? 'Awaiting Model Selection' : 'Awaiting Type Selection'
|
model ? 'Select...' : (type ? 'Awaiting Model Selection' : 'Awaiting Type Selection')
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
elems.supplier.addEventListener('change', cascadeFromSupplier);
|
||||||
|
elems.type.addEventListener('change', cascadeFromType);
|
||||||
|
elems.model.addEventListener('change', cascadeFromModel);
|
||||||
|
|
||||||
|
// Expose the cascades so loadData() (or future code) can re-run
|
||||||
|
// them after async data arrives without duplicating logic.
|
||||||
|
window.EVOLV.nodes.${nodeName}.assetMenu._cascade = {
|
||||||
|
fromSupplier: cascadeFromSupplier,
|
||||||
|
fromType: cascadeFromType,
|
||||||
|
fromModel: cascadeFromModel,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user