fix: gate function template downloads
This commit is contained in:
@@ -246,7 +246,7 @@ public final class EngineclassDelta {
|
||||
}
|
||||
}
|
||||
|
||||
// ── 功能模板下载模板管理/设置 · 去增删按钮 + 改标题 ─────────────────────────
|
||||
// ── 功能模板下载模板管理/设置 · 去增删按钮 + 改标题 + 附件门控 ─────────────────
|
||||
/**
|
||||
* FuncDownloadComponent.refreshRightLayout 的 EIP4 等价改写。转换后 type=3 树页结构=
|
||||
* {@code grid → 列[左 input-tree][右 button-toolbar[添加] + form]}。改写:
|
||||
@@ -254,10 +254,11 @@ public final class EngineclassDelta {
|
||||
* <li>① 移除右侧 button-toolbar 内「添加/删除」按钮(老 cl.removeComponent(insert)/(del)——附件记录随
|
||||
* 菜单/实体生成不应手工增删);toolbar 因此变空则整条移除。</li>
|
||||
* <li>② 右表单标题改「编辑模板附件」(老 panel.setCaption("编辑模板附件"))。</li>
|
||||
* <li>③ 表单 initApi 取回 {@code VIEW_TEMPLATEID} 后,未关联功能模板的菜单不显示附件编辑项和保存按钮
|
||||
* (老 pojo.VIEW_TEMPLATEID 非空才 add 右表单)。</li>
|
||||
* </ul>
|
||||
* <b>未做</b>:老「仅 pojo.VIEW_TEMPLATEID 非空才显示右表单」的门控——amis 表单 {@code visibleOn} 依赖
|
||||
* 自身 initApi 数据成循环(隐藏则不 fetch),正解需树节点携带 VIEW_TEMPLATEID(改 TemplateTreeController),
|
||||
* 价值低,暂延后。
|
||||
* 注:不能直接给 form 加 {@code visibleOn=${VIEW_TEMPLATEID}},否则 form 隐藏时不会发 initApi。这里保留
|
||||
* form 壳子加载当前树节点,再把 body/action 按返回数据门控。
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void funcDownloadDelta(Map<String, Object> schema, ConversionResult result) {
|
||||
@@ -291,6 +292,7 @@ public final class EngineclassDelta {
|
||||
} else if ("form".equals(m.get("type"))) {
|
||||
// ② 右表单标题改「编辑模板附件」
|
||||
Object old = m.put("title", "编辑模板附件");
|
||||
applyFuncDownloadTemplateGate(m, result);
|
||||
if (result != null) {
|
||||
result.note("engineclass[FuncDownloadComponent] 右表单标题改写: "
|
||||
+ old + " → 编辑模板附件");
|
||||
@@ -303,6 +305,69 @@ public final class EngineclassDelta {
|
||||
}
|
||||
}
|
||||
|
||||
/** 按表单 initApi 返回的 VIEW_TEMPLATEID 门控附件编辑项;不隐藏 form 本身,避免阻断 initApi。 */
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void applyFuncDownloadTemplateGate(Map<String, Object> form, ConversionResult result) {
|
||||
Object bodyObj = form.get("body");
|
||||
if (bodyObj instanceof List<?> body) {
|
||||
List<Object> gated = new ArrayList<>();
|
||||
gated.add(mapOf(
|
||||
"type", "tpl",
|
||||
"id", "funcDownloadNoTemplateTip",
|
||||
"tpl", "<span class=\"text-muted\">该菜单未关联功能模板,暂无附件可编辑</span>",
|
||||
"visibleOn", "${selId && !VIEW_TEMPLATEID}"));
|
||||
for (Object item : body) {
|
||||
if (item instanceof Map<?, ?> itemMap && shouldGateFuncDownloadItem((Map<String, Object>) itemMap)) {
|
||||
addVisibleOn((Map<String, Object>) itemMap, "VIEW_TEMPLATEID");
|
||||
}
|
||||
gated.add(item);
|
||||
}
|
||||
form.put("body", gated);
|
||||
} else if (bodyObj instanceof Map<?, ?> bodyMap) {
|
||||
Map<String, Object> item = (Map<String, Object>) bodyMap;
|
||||
if (shouldGateFuncDownloadItem(item)) {
|
||||
addVisibleOn(item, "VIEW_TEMPLATEID");
|
||||
}
|
||||
}
|
||||
|
||||
Object actionsObj = form.get("actions");
|
||||
if (actionsObj instanceof List<?> actions) {
|
||||
for (Object action : actions) {
|
||||
if (action instanceof Map<?, ?> actionMap) {
|
||||
addVisibleOn((Map<String, Object>) actionMap, "VIEW_TEMPLATEID");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result != null) {
|
||||
result.note("engineclass[FuncDownloadComponent] 右表单按 VIEW_TEMPLATEID 门控附件编辑");
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean shouldGateFuncDownloadItem(Map<String, Object> item) {
|
||||
if (Boolean.TRUE.equals(item.get("hidden"))) {
|
||||
return false;
|
||||
}
|
||||
return !"hidden".equals(item.get("type"));
|
||||
}
|
||||
|
||||
private static void addVisibleOn(Map<String, Object> item, String conditionExpr) {
|
||||
String old = item.get("visibleOn") == null ? "" : String.valueOf(item.get("visibleOn")).trim();
|
||||
if (old.isBlank()) {
|
||||
item.put("visibleOn", "${" + conditionExpr + "}");
|
||||
return;
|
||||
}
|
||||
item.put("visibleOn", "${(" + unwrapAmisExpr(old) + ") && (" + conditionExpr + ")}");
|
||||
}
|
||||
|
||||
private static String unwrapAmisExpr(String expr) {
|
||||
String trimmed = expr == null ? "" : expr.trim();
|
||||
if (trimmed.startsWith("${") && trimmed.endsWith("}") && trimmed.length() >= 3) {
|
||||
return trimmed.substring(2, trimmed.length() - 1);
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
/**
|
||||
* 剥除 button-toolbar 内「添加/删除」按钮(重建 buttons 列表回填,兼容不可变源);剥后为空返回 true(供丢弃整条)。
|
||||
*/
|
||||
|
||||
@@ -1451,11 +1451,12 @@ class TemplateToAmisConverterTest {
|
||||
|
||||
/**
|
||||
* engineclass=FuncDownloadComponent(功能模板下载模板管理/设置,type=3 树页)→ 整页改写器(机制① MUTATORS):
|
||||
* 移除右表单「添加」按钮(空 toolbar 整条移除)+ 右表单标题改「编辑模板附件」。复刻 refreshRightLayout。
|
||||
* 移除右表单「添加」按钮(空 toolbar 整条移除)+ 右表单标题改「编辑模板附件」
|
||||
* + 按 VIEW_TEMPLATEID 门控附件编辑。复刻 refreshRightLayout。
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void engineclass_FuncDownload_去添加按钮并改标题() {
|
||||
void engineclass_FuncDownload_去添加按钮改标题并按模板门控() {
|
||||
// 模拟转换后 type=3 树页:grid → 列[左 input-tree][右 button-toolbar[添加] + form]
|
||||
Map<String, Object> addBtn = new LinkedHashMap<>();
|
||||
addBtn.put("type", "button");
|
||||
@@ -1466,6 +1467,18 @@ class TemplateToAmisConverterTest {
|
||||
Map<String, Object> form = new LinkedHashMap<>();
|
||||
form.put("type", "form");
|
||||
form.put("title", "功能模板下载模板设置");
|
||||
Map<String, Object> document = new LinkedHashMap<>();
|
||||
document.put("type", "input-file");
|
||||
document.put("name", "DOCUMENT");
|
||||
Map<String, Object> hiddenTemplate = new LinkedHashMap<>();
|
||||
hiddenTemplate.put("type", "input-text");
|
||||
hiddenTemplate.put("name", "VIEW_TEMPLATEID");
|
||||
hiddenTemplate.put("hidden", true);
|
||||
Map<String, Object> submit = new LinkedHashMap<>();
|
||||
submit.put("type", "submit");
|
||||
submit.put("label", "保存");
|
||||
form.put("body", new ArrayList<>(List.of(document, hiddenTemplate)));
|
||||
form.put("actions", new ArrayList<>(List.of(submit)));
|
||||
Map<String, Object> rightCol = new LinkedHashMap<>();
|
||||
rightCol.put("body", new ArrayList<>(List.of(toolbar, form)));
|
||||
Map<String, Object> tree = new LinkedHashMap<>();
|
||||
@@ -1487,6 +1500,13 @@ class TemplateToAmisConverterTest {
|
||||
.anyMatch(o -> o instanceof Map && "button-toolbar".equals(((Map<String, Object>) o).get("type")));
|
||||
assertFalse(hasToolbar, "添加按钮已删 → 空 button-toolbar 应整条移除");
|
||||
assertEquals("编辑模板附件", form.get("title"), "右表单标题应改为「编辑模板附件」");
|
||||
List<Object> formBody = (List<Object>) form.get("body");
|
||||
Map<String, Object> tip = (Map<String, Object>) formBody.get(0);
|
||||
assertEquals("tpl", tip.get("type"));
|
||||
assertEquals("${selId && !VIEW_TEMPLATEID}", tip.get("visibleOn"));
|
||||
assertEquals("${VIEW_TEMPLATEID}", document.get("visibleOn"), "附件编辑项应仅在 VIEW_TEMPLATEID 非空时显示");
|
||||
assertFalse(hiddenTemplate.containsKey("visibleOn"), "隐藏字段不参与视觉门控,避免影响提交上下文");
|
||||
assertEquals("${VIEW_TEMPLATEID}", submit.get("visibleOn"), "保存按钮应仅在 VIEW_TEMPLATEID 非空时显示");
|
||||
// 左树不受影响、表单仍在
|
||||
assertEquals("input-tree", tree.get("type"));
|
||||
boolean hasForm = rb.stream()
|
||||
|
||||
Reference in New Issue
Block a user