Skip to content

Commit

Permalink
[Task]: 字段设计布局优化 #6565
Browse files Browse the repository at this point in the history
  • Loading branch information
tujiajun committed Mar 8, 2024
1 parent bbed1e4 commit 0bac73d
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"actions": [
{
"actionType": "custom",
"script": "//整理分组数据\nlet setting_groups = _.cloneDeep(event.data.groups);\nif (!_.find(setting_groups, { is_default: true })) {\n setting_groups.unshift({\n \"id\": \"未分组\",\n \"group_name\": \"未分组\",\n \"visible_on\": \"\",\n \"is_default\": true\n })\n}\nif (!_.find(setting_groups, { is_hidden: true })) {\n setting_groups.push({\n id: \"隐藏\",\n group_name: \"隐藏\",\n visible_on: \"\",\n is_hidden: true\n });\n}\nsetting_groups.forEach(function (group) {\n if (group.is_hidden) {\n group.group_name = \"隐藏\";\n group.visible_on = \"\";\n }\n if (group.is_default) {\n group.group_name = \"未分组\";\n group.visible_on = \"\";\n }\n if (!group.id) {\n group.id = group.group_name;\n }\n if (!group.visible_on) {\n group.visible_on = \"\"\n }\n})\n\n//整理字段与分组关系的数据\nlet fieldForGroup = {};\nlet oldGroup = _.cloneDeep(event.data.fieldForGroup);\nsetting_groups.forEach(function (group) {\n if (_.has(oldGroup, group.id)) {\n fieldForGroup[group.id] = oldGroup[group.id];\n oldGroup = _.omit(oldGroup, group.id);\n } else {\n fieldForGroup[group.id] = [];\n }\n})\nif (oldGroup && !_.isEmpty(oldGroup)) {\n _.forEach(oldGroup, function (value, key) {\n fieldForGroup[\"未分组\"] = _.unionBy(fieldForGroup[\"未分组\"], value);\n });\n}\n\n//未分组 放在所有分组开头,隐藏 放在所有分组最后\nconst defaultGroup = fieldForGroup[\"未分组\"];\nconst hiddenGroup = fieldForGroup[\"隐藏\"];\ndelete fieldForGroup[\"未分组\"];\ndelete fieldForGroup[\"隐藏\"];\nfieldForGroup = _.merge({ \"未分组\": defaultGroup }, fieldForGroup, { \"隐藏\": hiddenGroup });\n\n//根据fieldForGroup调整groups顺序,设置分组与保存时需要groups按照顺序\nconst keys = _.keys(fieldForGroup);\nsetting_groups = _.sortBy(setting_groups, function (group) { return _.findIndex(keys, function (key) { return key == group.group_name }) });\n\ndoAction({\n actionType: 'setValue',\n componentId: 'service_field_design',\n args: {\n value: {\n groups: setting_groups\n }\n }\n});\n\ndoAction({\n actionType: 'setValue',\n componentId: \"form_field_design\",\n args: {\n value: { design_field: fieldForGroup }\n }\n});"
"script": "///整理分组数据\nconst defaultGroupName = \"未分组\";\nconst hiddenGroupName = \"隐藏\";\nlet setting_groups = _.cloneDeep(event.data.groups);\nif (!_.find(setting_groups, { is_default: true })) {\n setting_groups.unshift({\n \"id\": defaultGroupName,\n \"group_name\": defaultGroupName,\n \"visible_on\": \"\",\n \"is_default\": true\n })\n}\nif (!_.find(setting_groups, { is_hidden: true })) {\n setting_groups.push({\n id: hiddenGroupName,\n group_name: hiddenGroupName,\n visible_on: \"\",\n is_hidden: true\n });\n}\nsetting_groups.forEach(function (group) {\n if (group.is_hidden) {\n group.group_name = hiddenGroupName;\n group.visible_on = \"\";\n }\n if (group.is_default) {\n group.group_name = defaultGroupName;\n group.visible_on = \"\";\n }\n if (!group.id) {\n group.id = group.group_name;\n }\n if (!group.visible_on) {\n group.visible_on = \"\"\n }\n})\n\n//整理字段与分组关系的数据\nlet fieldForGroup = {};\nlet oldGroup = _.cloneDeep(event.data.fieldForGroup);\nsetting_groups.forEach(function (group) {\n if (_.has(oldGroup, group.id)) {\n fieldForGroup[group.id] = oldGroup[group.id];\n oldGroup = _.omit(oldGroup, group.id);\n } else {\n fieldForGroup[group.id] = [];\n }\n})\nif (oldGroup && !_.isEmpty(oldGroup)) {\n _.forEach(oldGroup, function (value, key) {\n fieldForGroup[defaultGroupName] = _.unionBy(fieldForGroup[defaultGroupName], value);\n });\n}\n\n//未分组 放在所有分组开头,隐藏 放在所有分组最后\nconst defaultGroup = fieldForGroup[defaultGroupName];\nconst hiddenGroup = fieldForGroup[hiddenGroupName];\ndelete fieldForGroup[defaultGroupName];\ndelete fieldForGroup[hiddenGroupName];\nfieldForGroup = _.merge({ [defaultGroupName]: defaultGroup }, fieldForGroup, { [hiddenGroupName]: hiddenGroup });\n\n//根据fieldForGroup调整groups顺序,设置分组与保存时需要groups按照顺序\nconst keys = _.keys(fieldForGroup);\nsetting_groups = _.sortBy(setting_groups, function (group) { return _.findIndex(keys, function (key) { return key == group.group_name }) });\n\ndoAction({\n actionType: 'setValue',\n componentId: 'service_field_design',\n args: {\n value: {\n groups: setting_groups\n }\n }\n});\n\ndoAction({\n actionType: 'setValue',\n componentId: \"form_field_design\",\n args: {\n value: { design_field: fieldForGroup }\n }\n});"
}
]
}
Expand Down Expand Up @@ -321,7 +321,7 @@
"headers": {
"Authorization": "Bearer ${context.tenantId},${context.authToken}"
},
"requestAdaptor": "var graphqlOrder = \"\";\n//先修改字段,使系统对象自行自定义\n//根据design_field,修改对象字段的sort_no与groups\nvar index = 1;\n_.forEach(api.data.design_field, function (items, key) {\n let group = key;\n if (key == \"未分组\" || key == \"隐藏\") {\n group = null;\n }\n _.forEach(items, function (item) {\n const field = _.find(api.data.fields, { 'id': item });\n //如果是在隐藏分组里的字段,就给visible_on属性赋予{{false}};如果不在隐藏分组里,就判断来自数据库内的visible_on属性是不是{{false}},是就清除,不是就保持不变\n const visible_on = key == \"隐藏\" ? \"{{false}}\" : (field.attr_visible_on == \"{{false}}\" ? \"{{true}}\" : field.attr_visible_on);\n let docJson = {\n sort_no: index * 10,\n group, visible_on\n }\n if (field.id == field._id && field.is_system) {\n docJson = {\n object: api.data.$self.designObjectName,\n type: field.type,\n _name: field._name,\n label: field.label,\n sort_no: index * 10,\n group,\n visible_on,\n is_system: field.is_system\n }\n }\n if (key != \"隐藏\") {\n docJson.hidden = false;\n }\n let itemOrder = 'upsert' + index + ':object_fields__upsert(id:\"' + item + '\" , doc:' + JSON.stringify(JSON.stringify(docJson)) + '){_id}\\n';\n graphqlOrder += itemOrder;\n index++;\n })\n})\n\n//根据groups,修改对象的字段分组field_groups\nvar field_groups = _.cloneDeep(api.data.groups);\n_.remove(field_groups, { is_default: true });\n_.remove(field_groups, { is_hidden: true });\nfield_groups = field_groups.map(function (group) {\n return _.omit(group, 'id');\n})\nconst keys = _.keys(api.data.design_field);\nfield_groups = _.sortBy(field_groups, function (group) { return _.findIndex(keys, function (key) { return key == group.group_name }) });\nconst groupOrder = 'upsert0:objects__upsert(id:\"' + api.data.$self.designObjectId + '\" , doc:' + JSON.stringify(JSON.stringify({ field_groups, name: api.data.$self.designObjectName })) + ') {_id}';\ngraphqlOrder += groupOrder;\n\n\ngraphqlOrder = 'mutation {' + graphqlOrder + '}';\nreturn {\n ...api,\n data: {\n query: graphqlOrder\n }\n}",
"requestAdaptor": "var graphqlOrder = \"\";\nvar field_groups = _.cloneDeep(api.data.groups);\n//先修改字段,使系统对象自行自定义\n//根据design_field,修改对象字段的sort_no与groups\nvar index = 1;\nconst defaultGroupName = _.find(field_groups, { 'is_default': true })?.name || \"未分组\";\nconst hiddenGroupName = _.find(field_groups, { 'is_hidden': true })?.name || \"隐藏\";\n_.forEach(api.data.design_field, function (items, key) {\n let group = key;\n if (key == defaultGroupName || key == hiddenGroupName) {\n group = null;\n }\n _.forEach(items, function (item) {\n const field = _.find(api.data.fields, { 'id': item });\n //如果是在隐藏分组里的字段,就给visible_on属性赋予{{false}};如果不在隐藏分组里,就判断来自数据库内的visible_on属性是不是{{false}},是就清除,不是就保持不变\n const visible_on = key == hiddenGroupName ? \"{{false}}\" : (field.attr_visible_on == \"{{false}}\" ? \"{{true}}\" : field.attr_visible_on);\n let docJson = {\n sort_no: index * 10,\n group, visible_on\n }\n if (field.id == field._id && field.is_system) {\n docJson = {\n object: api.data.$self.designObjectName,\n type: field.type,\n _name: field._name,\n label: field.label,\n sort_no: index * 10,\n group,\n visible_on,\n is_system: field.is_system\n }\n }\n if (key != hiddenGroupName) {\n docJson.hidden = false;\n }\n let itemOrder = 'upsert' + index + ':object_fields__upsert(id:\"' + item + '\" , doc:' + JSON.stringify(JSON.stringify(docJson)) + '){_id}\\n';\n graphqlOrder += itemOrder;\n index++;\n })\n})\n\n//根据groups,修改对象的字段分组field_groups\n_.remove(field_groups, { is_default: true });\n_.remove(field_groups, { is_hidden: true });\nfield_groups = field_groups.map(function (group) {\n return _.omit(group, 'id');\n})\nconst keys = _.keys(api.data.design_field);\nfield_groups = _.sortBy(field_groups, function (group) { return _.findIndex(keys, function (key) { return key == group.group_name }) });\nconst groupOrder = 'upsert0:objects__upsert(id:\"' + api.data.$self.designObjectId + '\" , doc:' + JSON.stringify(JSON.stringify({ field_groups, name: api.data.$self.designObjectName })) + ') {_id}';\ngraphqlOrder += groupOrder;\n\n\ngraphqlOrder = 'mutation {' + graphqlOrder + '}';\nreturn {\n ...api,\n data: {\n query: graphqlOrder\n }\n}",
"adaptor": "if (payload.errors) {\n payload.status = 2;\n payload.msg = window.t ? window.t(payload.errors[\n 0\n ].message) : payload.errors[\n 0\n ].message;\n}\nreturn payload;",
"messages": {},
"data": {
Expand Down Expand Up @@ -367,7 +367,7 @@
},
"messages": {},
"requestAdaptor": "",
"adaptor": "//筛选出可编辑的字段\r\nvar oldFields = _.filter(payload.data.fields, function (obj) { return obj.recordPermissions.allowEdit; });\r\n//将group为空的字段分为未分组的组中,将系统字段的_id改为对象名.字段名\r\noldFields = _.map(oldFields, function (obj) {\r\n obj.originId = obj._id;\r\n if (obj.hidden || obj.visible_on == \"{{false}}\") {\r\n obj.group = \"隐藏\";\r\n }else if (obj.group === null) {\r\n obj.group = \"未分组\";\r\n }\r\n if (obj.is_system) {\r\n obj._id = api.data.designObjectName + \".\" + obj._name;\r\n }\r\n return obj;\r\n});\r\n\r\n//整合出字段的属性集合\r\nconst fields = oldFields.map((field) => {\r\n return {\r\n \"id\": field._id,//用与steedos-board组件的关系分辨\r\n \"_name\": field._name,\r\n \"label\": field.label,\r\n \"_id\": field.originId,//用于打开steedos-objectform\r\n \"columnSpan\": field.is_wide ? 2 : 1,\r\n \"is_system\": field.is_system,\r\n \"type\": field.type,\r\n \"attr_visible_on\": field.visible_on\r\n }\r\n})\r\n\r\n//从字段的group属性中,整合出字段分组的属性集合\r\nlet field_groups = _.map(_.uniqBy(oldFields, \"group\"), function (obj) {\r\n return {\r\n \"id\": obj.group,\r\n \"group_name\": obj.group,\r\n \"visible_on\": \"\"\r\n };\r\n});\r\n\r\n//合并对象上的field_groups与字段上的group\r\nlet groups = _.unionBy(api.data.field_groups, field_groups, 'id');\r\n\r\n//判断是否第一次调用接口,不是的话(说明是触发了datachange事件),返回fields与fieldUpdateData\r\nif (api.data.dataInitialed) {\r\n const eventData = api.data.eventData;\r\n let fieldUpdateData = \"\";\r\n const fieldForGroup = api.data.fieldForGroup;\r\n let fieldForGroupLength = 0;\r\n _.forIn(fieldForGroup, function (value, key) {\r\n fieldForGroupLength += value.length;\r\n });\r\n if (fieldForGroupLength < fields.length) {\r\n fieldUpdateData = {\r\n type: \"insert\",\r\n id: eventData.result.data.recordId\r\n }\r\n } else if (fieldForGroupLength > fields.length) {\r\n fieldUpdateData = {\r\n type: \"delete\",\r\n id: eventData._id\r\n }\r\n }\r\n return payload = {\r\n data: {\r\n fields,\r\n fieldUpdateData\r\n }\r\n }\r\n}\r\n\r\n//整合字段与分组的关系\r\nconst oldFieldsInGroups = _.groupBy(oldFields, \"group\");\r\nlet fieldForGroup = _.mapValues(_.groupBy(groups, \"id\"), function (group,key) {\r\n if (oldFieldsInGroups[key]) {\r\n return _.map(oldFieldsInGroups[key], function (obj) {\r\n return obj._id;\r\n });\r\n } else {\r\n return [];\r\n }\r\n});\r\n\r\n//未分组 放在所有分组开头,隐藏 放在所有分组最后\r\nconst defaultGroup = fieldForGroup[\"未分组\"];\r\nconst hiddenGroup = fieldForGroup[\"隐藏\"];\r\ndelete fieldForGroup[\"未分组\"];\r\ndelete fieldForGroup[\"隐藏\"];\r\nfieldForGroup = _.merge({ \"未分组\": defaultGroup }, fieldForGroup, { \"隐藏\": hiddenGroup });\r\n\r\n//根据fieldForGroup调整groups顺序,设置分组与保存时需要groups按照顺序\r\nconst keys = _.keys(fieldForGroup);\r\ngroups = _.sortBy(groups, function (group) { return _.findIndex(keys, function (key) { return key == group.group_name }) });\r\n\r\nreturn payload = {\r\n data: {\r\n fields,\r\n groups,\r\n fieldForGroup,\r\n dataInitialed: true\r\n }\r\n};",
"adaptor": "//筛选出可编辑的字段\nvar oldFields = _.filter(payload.data.fields, function (obj) { return obj.recordPermissions.allowEdit; });\n//将group为空的字段分为未分组的组中,将系统字段的_id改为对象名.字段名\noldFields = _.map(oldFields, function (obj) {\n obj.originId = obj._id;\n if (obj.hidden || obj.visible_on == \"{{false}}\") {\n obj.group = \"隐藏\";\n }else if (obj.group === null) {\n obj.group = \"未分组\";\n }\n if (obj.is_system) {\n obj._id = api.data.designObjectName + \".\" + obj._name;\n }\n return obj;\n});\n\n//整合出字段的属性集合\nconst fields = oldFields.map((field) => {\n return {\n \"id\": field._id,//用与steedos-board组件的关系分辨\n \"_name\": field._name,\n \"label\": field.label,\n \"_id\": field.originId,//用于打开steedos-objectform\n \"columnSpan\": field.is_wide ? 2 : 1,\n \"is_system\": field.is_system,\n \"type\": field.type,\n \"attr_visible_on\": field.visible_on\n }\n})\n\n//从字段的group属性中,整合出字段分组的属性集合\nlet field_groups = _.map(_.uniqBy(oldFields, \"group\"), function (obj) {\n return {\n \"id\": obj.group,\n \"group_name\": obj.group,\n \"visible_on\": \"\"\n };\n});\n\n//合并对象上的field_groups与字段上的group\nlet groups = _.unionBy(api.data.field_groups, field_groups, 'id');\n\n//判断是否第一次调用接口,不是的话(说明是触发了datachange事件),返回fields与fieldUpdateData;目前新增与删除都不支持\nif (api.data.dataInitialed) {\n const eventData = api.data.eventData;\n let fieldUpdateData = \"\";\n const fieldForGroup = api.data.fieldForGroup;\n let fieldForGroupLength = 0;\n _.forIn(fieldForGroup, function (value, key) {\n fieldForGroupLength += value.length;\n });\n if (fieldForGroupLength < fields.length) {\n fieldUpdateData = {\n type: \"insert\",\n id: eventData.result.data.recordId\n }\n } else if (fieldForGroupLength > fields.length) {\n fieldUpdateData = {\n type: \"delete\",\n id: eventData._id\n }\n }\n return payload = {\n data: {\n fields,\n fieldUpdateData,\n \n }\n }\n}\n\n//整合字段与分组的关系\nconst oldFieldsInGroups = _.groupBy(oldFields, \"group\");\nlet fieldForGroup = _.mapValues(_.groupBy(groups, \"id\"), function (group,key) {\n if (oldFieldsInGroups[key]) {\n return _.map(oldFieldsInGroups[key], function (obj) {\n return obj._id;\n });\n } else {\n return [];\n }\n});\n\n//未分组 放在所有分组开头,隐藏 放在所有分组最后\nconst defaultGroupName = _.find(groups, { 'is_default': true })?.name || \"未分组\";\nconst hiddenGroupName = _.find(groups, { 'is_hidden': true })?.name || \"隐藏\";\nconst defaultGroup = fieldForGroup[defaultGroupName];\nconst hiddenGroup = fieldForGroup[hiddenGroupName];\ndelete fieldForGroup[defaultGroupName];\ndelete fieldForGroup[hiddenGroupName];\nfieldForGroup = _.merge({ [defaultGroupName]: defaultGroup }, fieldForGroup, { [hiddenGroupName]: hiddenGroup });\n\n//根据fieldForGroup调整groups顺序,设置分组与保存时需要groups按照顺序\nconst keys = _.keys(fieldForGroup);\ngroups = _.sortBy(groups, function (group) { return _.findIndex(keys, function (key) { return key == group.group_name }) });\n\nreturn payload = {\n data: {\n fields,\n groups,\n fieldForGroup,\n dataInitialed: true\n }\n};",
"sendOn": "!!this.designObjectId"
},
"messages": {},
Expand All @@ -376,7 +376,7 @@
"actions": [
{
"actionType": "custom",
"script": "\nconst fieldForGroup = context.props.data.fieldForGroup;\nconst fieldUpdateData = event.data.fieldUpdateData;\nif (fieldUpdateData && fieldUpdateData.type == \"delete\") {\n _.forIn(fieldForGroup, function (group, group_name) {\n _.remove(group, function (field) {\n return field === fieldUpdateData.id;\n })\n });\n} else if (fieldUpdateData && fieldUpdateData.type == \"insert\") {\n fieldForGroup[\"未分组\"].push(fieldUpdateData.id);\n}\n// doAction({\n// actionType: 'setValue',\n// componentId: \"service_field_design\",\n// args: {\n// value: { fieldForGroup }\n// }\n// });\n\ndoAction({\n actionType: 'setValue',\n componentId: \"form_field_design\",\n args: {\n value: { design_field:fieldForGroup }\n }\n});\n\n",
"script": "//新增或删除时才会触发,目前暂不支持\nconst fieldForGroup = context.props.data.fieldForGroup;\nconst fieldUpdateData = event.data.fieldUpdateData;\nif (fieldUpdateData && fieldUpdateData.type == \"delete\") {\n _.forIn(fieldForGroup, function (group, group_name) {\n _.remove(group, function (field) {\n return field === fieldUpdateData.id;\n })\n });\n} else if (fieldUpdateData && fieldUpdateData.type == \"insert\") {\n fieldForGroup[\"未分组\"].push(fieldUpdateData.id);\n}\n// doAction({\n// actionType: 'setValue',\n// componentId: \"service_field_design\",\n// args: {\n// value: { fieldForGroup }\n// }\n// });\n\ndoAction({\n actionType: 'setValue',\n componentId: \"form_field_design\",\n args: {\n value: { design_field:fieldForGroup }\n }\n});\n\n",
"expression": "${event.data.fieldUpdateData}"
}
]
Expand Down

0 comments on commit 0bac73d

Please sign in to comment.