From 7ce1f84866eb9787488737b721e5531694351765 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Mon, 22 Jul 2024 15:39:44 +0800 Subject: [PATCH] open beetl functions form --- .../resources/views/functions/form/attrs.html | 25 +++++++ .../resources/views/functions/form/items.html | 27 +++++++ .../resources/views/functions/form/path.html | 72 +++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 modules/core/src/main/resources/views/functions/form/attrs.html create mode 100644 modules/core/src/main/resources/views/functions/form/items.html create mode 100644 modules/core/src/main/resources/views/functions/form/path.html diff --git a/modules/core/src/main/resources/views/functions/form/attrs.html b/modules/core/src/main/resources/views/functions/form/attrs.html new file mode 100644 index 00000000..a1c1c30b --- /dev/null +++ b/modules/core/src/main/resources/views/functions/form/attrs.html @@ -0,0 +1,25 @@ +<%/* Copyright (c) 2013-Now http://jeesite.com All rights reserved. + * No deletion without permission, or be held responsible to law. */ + +/** + * 编译控件属性 + * @author ThinkGem + * @version 2019-3-5 + */ +var p = para0; + +// 编译除内置属性以外的属性到元素。 +p.attrs = p.attrs!''; +for (var attr in p.thisTag.attrs){ + if (attr.key != '$cols' && !@ListUtils.inString(attr.key, p.exclAttrs)){ + if(attr.key == 'readonly' && !toBoolean(attr.value)){ + continue; + } + var s = { + %> ${attr.key}="${attr.value}"<% + }; + p.attrs = p.attrs + s; + } +} + +%> \ No newline at end of file diff --git a/modules/core/src/main/resources/views/functions/form/items.html b/modules/core/src/main/resources/views/functions/form/items.html new file mode 100644 index 00000000..894405b4 --- /dev/null +++ b/modules/core/src/main/resources/views/functions/form/items.html @@ -0,0 +1,27 @@ +<%/* Copyright (c) 2013-Now http://jeesite.com All rights reserved. + * No deletion without permission, or be held responsible to law. */ + +/** + * 编译items属性 + * @author ThinkGem + * @version 2017-3-5 + */ +var p = para0; + +// 如果设置了字典,则使用字典数据 +if (isNotEmpty(p.dictType) && type.name(dictType) == 'String'){ + p.items = @DictUtils.getDictList(p.dictType); + if (isBlank(p.itemLabel)){ + p.itemLabel = 'dictLabel'; + } + if (isBlank(p.itemValue)){ + p.itemValue = 'dictValue'; + } +} + +// 如果为空,则赋一个空数组 +if (isEmpty(p.items!)){ + p.items = []; +} + +%> \ No newline at end of file diff --git a/modules/core/src/main/resources/views/functions/form/path.html b/modules/core/src/main/resources/views/functions/form/path.html new file mode 100644 index 00000000..9e25cff6 --- /dev/null +++ b/modules/core/src/main/resources/views/functions/form/path.html @@ -0,0 +1,72 @@ +<%/* Copyright (c) 2013-Now http://jeesite.com All rights reserved. + * No deletion without permission, or be held responsible to law. */ + +/** + * 编译path属性,当设置path属性时,自动根据model获取value + * @author ThinkGem + * @version 2017-3-5 + */ +var p = para0, tag = p.thisTag; + +if (tag != null) { + var parent = @tag.getParentByTagName('form'); + var readonly = parent.attrs.readonly!; + if (isNotEmpty(readonly) && toBoolean(readonly)) { + p.readonly = toBoolean(readonly!false); + p.attrs = p.attrs!'' + 'readonly="'+p.readonly+'"'; + } + // path 优先级高于 name + if (isNotEmpty(p.path) || isNotEmpty(p.labelPath)) { + var model = parent.attrs.model!; + if (isNotEmpty(p.path)){ + p.name = p.path; + if (isNotEmpty(model)){ + p.value = @ReflectUtils.invokeGetter(model, p.name); + if (toBoolean(xss!false) && type.name(p.value) == 'String'){ + p.value = @EncodeUtils.xssFilter(p.value); + }else if (toBoolean(encodeHtml!false) && type.name(p.value) == 'String'){ + p.value = @EncodeUtils.encodeHtml(p.value); + } + } + } + if (isNotEmpty(p.labelPath)){ + p.labelName = p.labelPath; + if (isNotEmpty(model)){ + p.labelValue = @ReflectUtils.invokeGetter(model, p.labelName); + if (toBoolean(xss!false) && type.name(p.value) == 'String'){ + p.labelValue = @EncodeUtils.xssFilter(p.labelValue); + }if (toBoolean(encodeHtml!false) && type.name(p.value) == 'String'){ + p.labelValue = @EncodeUtils.encodeHtml(p.labelValue); + } + } + } + } +} + +// defaultValue 默认值 +if (isNotBlank(p.name)){ + if (p.defaultValue != null && isBlank(p.value)){ + p.value = p.defaultValue; + } +} +if (isNotBlank(p.labelName)) { + if (p.defaultLabel != null && isBlank(p.labelValue)){ + p.labelValue = p.defaultLabel; + } + if (isBlank(p.labelValue)) { + p.labelValue = p.value; + } +} + +// 如果没有设置id,则与name相同 +if (isBlank(p.id)){ + p.id = p.name; + // 替换id中的非法字符 + p.id = @StringUtils.replace(p.id, '.', '_'); + p.id = @StringUtils.replace(p.id, '\'', '_'); + p.id = @StringUtils.replace(p.id, '\"', '_'); + p.id = @StringUtils.replace(p.id, '[', '_'); + p.id = @StringUtils.replace(p.id, ']', '_'); +} + +%> \ No newline at end of file