Improve JS enum_val() with caching and single-value lookup
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -403,33 +403,45 @@ class Database_BundleIntegration extends BundleIntegration_Abstract
|
||||
}
|
||||
|
||||
// Generate enum value getter with Proxy for maintaining order
|
||||
$content .= " static {$column}_enum_val() {\n";
|
||||
$content .= " const data = {};\n";
|
||||
$content .= " const order = [];\n";
|
||||
$content .= " static __{$column}_enum_val = null;\n";
|
||||
$content .= " static {$column}_enum_val(enum_value) {\n";
|
||||
$content .= " if (!this.__{$column}_enum_val) {\n";
|
||||
$content .= " const data = {};\n";
|
||||
$content .= " const order = [];\n";
|
||||
|
||||
// Generate the sorted entries
|
||||
foreach ($enum_values as $value => $props) {
|
||||
$value_json = json_encode($value);
|
||||
$props_json = json_encode($props, JSON_UNESCAPED_SLASHES);
|
||||
$content .= " data[{$value_json}] = {$props_json};\n";
|
||||
$content .= " order.push({$value_json});\n";
|
||||
$content .= " data[{$value_json}] = {$props_json};\n";
|
||||
$content .= " order.push({$value_json});\n";
|
||||
}
|
||||
|
||||
$content .= " // Return Proxy that maintains sort order for enumeration\n";
|
||||
$content .= " return new Proxy(data, {\n";
|
||||
$content .= " ownKeys() {\n";
|
||||
$content .= " return order.map(String);\n";
|
||||
$content .= " },\n";
|
||||
$content .= " getOwnPropertyDescriptor(target, prop) {\n";
|
||||
$content .= " if (prop in target) {\n";
|
||||
$content .= " return {\n";
|
||||
$content .= " enumerable: true,\n";
|
||||
$content .= " configurable: true,\n";
|
||||
$content .= " value: target[prop]\n";
|
||||
$content .= " };\n";
|
||||
$content .= " // Cache Proxy that maintains sort order for enumeration\n";
|
||||
$content .= " this.__{$column}_enum_val = new Proxy(data, {\n";
|
||||
$content .= " ownKeys() {\n";
|
||||
$content .= " return order.map(String);\n";
|
||||
$content .= " },\n";
|
||||
$content .= " getOwnPropertyDescriptor(target, prop) {\n";
|
||||
$content .= " if (prop in target) {\n";
|
||||
$content .= " return {\n";
|
||||
$content .= " enumerable: true,\n";
|
||||
$content .= " configurable: true,\n";
|
||||
$content .= " value: target[prop]\n";
|
||||
$content .= " };\n";
|
||||
$content .= " }\n";
|
||||
$content .= " }\n";
|
||||
$content .= " });\n";
|
||||
$content .= " }\n";
|
||||
$content .= " if (enum_value !== undefined) {\n";
|
||||
$content .= " const result = this.__{$column}_enum_val[enum_value];\n";
|
||||
$content .= " if (!result) {\n";
|
||||
$content .= " console.error(`Invalid enum value '\${enum_value}' for {$column}`);\n";
|
||||
$content .= " return null;\n";
|
||||
$content .= " }\n";
|
||||
$content .= " });\n";
|
||||
$content .= " return result;\n";
|
||||
$content .= " }\n";
|
||||
$content .= " return this.__{$column}_enum_val;\n";
|
||||
$content .= " }\n\n";
|
||||
|
||||
// Generate enum_select() - Selectable items for dropdowns (respects selectable: false)
|
||||
|
||||
Reference in New Issue
Block a user