57 lines
2.0 KiB
JavaScript
57 lines
2.0 KiB
JavaScript
(function (root, factory) {
|
|
'use strict';
|
|
var api = factory();
|
|
if (typeof module === 'object' && module.exports) module.exports = api;
|
|
else root.AKSRTWidgetAdminCore = api;
|
|
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
|
|
'use strict';
|
|
|
|
function parseDependency(raw) {
|
|
if (Array.isArray(raw)) return raw;
|
|
try {
|
|
var parsed = JSON.parse(String(raw || '[]'));
|
|
return Array.isArray(parsed) ? parsed : [];
|
|
} catch (_error) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function dependencyVisible(rule, current) {
|
|
rule = parseDependency(rule);
|
|
if (rule.length < 3) return true;
|
|
if (Array.isArray(rule[0])) {
|
|
return rule.every(function (part) { return dependencyVisible(part, current && typeof current === 'object' ? current[part[0]] : ''); });
|
|
}
|
|
var ids = String(rule[0]).split('|');
|
|
var operators = String(rule[1]).split('|');
|
|
var expected = String(rule[2]).split('|');
|
|
var values = current && typeof current === 'object' ? current : Object.fromEntries([[ids[0], current]]);
|
|
return ids.every(function (id, index) {
|
|
var actual = String(values[id] == null ? '' : values[id]);
|
|
var wanted = String(expected[index] == null ? expected[0] : expected[index]);
|
|
var operator = operators[index] || operators[0] || '!=';
|
|
if (operator === '==') return actual === wanted;
|
|
if (operator === 'any') return wanted.split(',').indexOf(actual) !== -1;
|
|
return actual !== wanted;
|
|
});
|
|
}
|
|
|
|
function replaceRepeaterIndexes(name, rowIndexes) {
|
|
var foundWidgetNumber = false;
|
|
var cursor = 0;
|
|
return String(name || '').replace(/\[(\d+|__index_[a-f0-9]+__)\]/g, function (match, index) {
|
|
if (!foundWidgetNumber && /^\d+$/.test(index)) {
|
|
foundWidgetNumber = true;
|
|
return match;
|
|
}
|
|
return cursor < rowIndexes.length ? '[' + Number(rowIndexes[cursor++]) + ']' : match;
|
|
});
|
|
}
|
|
|
|
return {
|
|
parseDependency: parseDependency,
|
|
dependencyVisible: dependencyVisible,
|
|
replaceRepeaterIndexes: replaceRepeaterIndexes
|
|
};
|
|
});
|