From bfa155df784d7fae9f766e5c3624d7310ecc4b41 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sun, 17 Nov 2024 01:00:08 -0500 Subject: [PATCH] feat: support repeated dict Signed-off-by: Jinzhe Zeng --- src/components/dargs/DargsInput.vue | 2 +- src/components/dargs/DargsItem.vue | 65 ++++++++++++++++++++++++++++- src/locales/en.json | 1 + src/locales/zh.json | 1 + 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/components/dargs/DargsInput.vue b/src/components/dargs/DargsInput.vue index 66a1fa23..825f4470 100644 --- a/src/components/dargs/DargsInput.vue +++ b/src/components/dargs/DargsInput.vue @@ -4,7 +4,7 @@ v-model="file" :label="$t('message.load_json')" accept=".json" - @change="from_json_file(file[0])" + @change="from_json_file(file)" /> diff --git a/src/components/dargs/DargsItem.vue b/src/components/dargs/DargsItem.vue index f6a3115d..3735545c 100644 --- a/src/components/dargs/DargsItem.vue +++ b/src/components/dargs/DargsItem.vue @@ -32,7 +32,12 @@ - + @@ -51,6 +56,17 @@ {{ jdata.name }} - Item {{ index }} + + + { + return [ + this.keys[ii], + Object.keys(this.jdata.sub_fields).length + ? Object.fromEntries( + new Map( + this.$refs[`subitem_${ii}`] + .filter((vv) => !vv.jdata.optional || vv.check) + .map((vv) => [vv.jdata.name, vv.dvalue()]), + ), + ) + : Object(), + ]; + }), + ), + ); } else if (this.select_type == "dict") { const sub_fields = Object.keys(this.jdata.sub_fields).length ? Object.fromEntries( @@ -459,6 +497,29 @@ export default { } else if (this.jdata.type.includes("NoneType") && obj === null) { this.value = obj; this.select_type = "NoneType"; + } else if (this.jdata.type.includes("dict") && this.jdata.repeat) { + this.repeat_jdata = []; + this.keys = Object.keys(obj); + [...Array(obj.length).keys()].forEach((ii) => { + this.repeat_jdata.push(this.jdata); + }); + [...Array(obj.length).keys()].forEach((ii) => { + var subobj = obj[this.keys[ii]]; + if (this.$refs[`subitem_${ii}`]) + this.$refs[`subitem_${ii}`].forEach((vv) => { + // check if it exists, name and alias + if (vv.jdata.name in subobj) { + // exists + vv.load(subobj[vv.jdata.name]); + } + vv.jdata.alias.forEach((aa) => { + if (aa in subobj) vv.load(subobj[aa]); + }); + if (vv.jdata.optional) { + vv.check = vv.jdata.name in subobj; + } + }); + }); } else if (this.jdata.type.includes("dict")) { this.select_type = "dict"; load_subitem(); @@ -493,9 +554,11 @@ export default { }, add_repeat: function () { this.repeat_jdata.push(this.jdata); + this.keys.push(`item_${this.repeat_jdata.length - 1}`); }, remove_repeat: function (index) { this.repeat_jdata.splice(index, 1); + this.keys.splice(index, 1); }, }, }; diff --git a/src/locales/en.json b/src/locales/en.json index dfd82439..664b84be 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -27,6 +27,7 @@ "management": "Management", "add_repeat": "Add Item", "remove_repeat": "Remove Item {index}", + "dict_key": "Item key", "languages": "Languages" } } diff --git a/src/locales/zh.json b/src/locales/zh.json index 18af85b1..7af31340 100644 --- a/src/locales/zh.json +++ b/src/locales/zh.json @@ -27,6 +27,7 @@ "management": "管理", "add_repeat": "添加新项", "remove_repeat": "移除第{index}项", + "dict_key": "键名", "languages": "语言" } }