Skip to content

Commit fbb6315

Browse files
authored
feature: add array property editor (#167)
* feat: add array property editor * fix some problem * clean useless codes
1 parent cfb3ee7 commit fbb6315

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

src/components/PluginDialog/index.vue

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
:prop="key"
9696
>
9797
<!-- 分情况讨论 -->
98+
<!-- number property -->
9899
<el-input-number
99100
v-if="schema.properties[key].type === 'integer' || schema.properties[key].type === 'number'"
100101
v-model="data[key]"
@@ -104,11 +105,12 @@
104105
@change="onPropertyChange(key, $event)"
105106
/>
106107

108+
<!-- enum property -->
107109
<el-select
108110
v-if="schema.properties[key].hasOwnProperty('enum')"
109111
v-model="data[key]"
110112
:clearable="true"
111-
:placeholder="&quot;Select a &quot; + key"
113+
:placeholder="`Select a ${key}`"
112114
@change="onPropertyChange(key, $event)"
113115
>
114116
<el-option
@@ -119,19 +121,41 @@
119121
/>
120122
</el-select>
121123

124+
<!-- string property -->
122125
<el-input
123126
v-if="schema.properties[key].type === 'string' && !schema.properties[key].hasOwnProperty('enum')"
124127
v-model="data[key]"
125128
:placeholder="key"
126129
@input="onPropertyChange(key, $event)"
127130
/>
128131

132+
<!-- boolean property -->
129133
<el-switch
130134
v-if="schema.properties[key].type === 'boolean' && !schema.properties[key].hasOwnProperty('enum')"
131135
v-model="data[key]"
132136
active-color="#13ce66"
133137
inactive-color="#ff4949"
134138
/>
139+
140+
<!-- array property -->
141+
<div
142+
v-if="schema.properties[key].type === 'array'"
143+
class="array-input-container"
144+
>
145+
<el-input
146+
v-for="(arrayIndex) in arrayPropertiesLength[key]"
147+
:key="arrayIndex"
148+
v-model="data[key][arrayIndex]"
149+
:placeholder="`${key} [${arrayIndex}]`"
150+
@input="isDataChanged = true"
151+
/>
152+
153+
<el-button
154+
@click="addArrayItem(key)"
155+
>
156+
{{ $t('button.addValue') }}
157+
</el-button>
158+
</div>
135159
</el-form-item>
136160
</el-form>
137161
<span
@@ -141,14 +165,14 @@
141165
<el-button
142166
@click="onCancel"
143167
>
144-
Cancel
168+
{{ $t('button.cancel') }}
145169
</el-button>
146170
<el-button
147171
type="primary"
148172
:disabled="!isDataChanged && oneOfPropHasEmptyValue"
149173
@click="onSave"
150174
>
151-
Confirm
175+
{{ $t('button.confirm') }}
152176
</el-button>
153177
</span>
154178
</el-dialog>
@@ -177,6 +201,7 @@ export default class extends Vue {
177201
private data: any = {}
178202
private isDataChanged: boolean = false
179203
private showDialog: boolean = false
204+
private arrayPropertiesLength = {}
180205
181206
@Watch('show')
182207
private onShowChange(value: boolean) {
@@ -242,8 +267,34 @@ export default class extends Vue {
242267
243268
this.rules = rules
244269
270+
// Generate initial data and merge current data
271+
let schemaKeys = {}
272+
for (let key in schema.properties) {
273+
if (schema.properties[key].default) {
274+
schemaKeys[key] = schema.properties[key].default
275+
continue
276+
}
277+
278+
switch (schema.properties[key].type) {
279+
case 'array':
280+
schemaKeys[key] = []
281+
this.arrayPropertiesLength[key] = [...new Array(this.pluginData[key] ? this.pluginData[key].length : schema.properties[key].minItems).keys()]
282+
break
283+
case 'object':
284+
schemaKeys[key] = {}
285+
break
286+
case 'boolean':
287+
schemaKeys[key] = false
288+
break
289+
default:
290+
schemaKeys[key] = ''
291+
}
292+
}
293+
245294
if (this.pluginData) {
246-
this.data = Object.assign({}, this.pluginData)
295+
this.data = Object.assign(schemaKeys, this.pluginData)
296+
} else {
297+
this.data = schemaKeys
247298
}
248299
249300
if (this.name === 'key-auth' && !this.pluginData) {
@@ -284,13 +335,26 @@ export default class extends Vue {
284335
if (valid) {
285336
this.data = this.processOneOfProp(this.data)
286337
this.$emit('save', this.name, this.data)
287-
this.$message.warning('Your data will be saved after you click the Save button')
338+
this.$message.warning(`${this.$t('message.clickSaveButton')}`)
288339
} else {
289340
return false
290341
}
291342
})
292343
}
293344
345+
/**
346+
* Add item to array property
347+
* @param key
348+
*/
349+
private addArrayItem(key: any) {
350+
if (this.arrayPropertiesLength[key].length < this.schema.properties[key].maxItems) {
351+
this.arrayPropertiesLength[key].push(this.arrayPropertiesLength[key].length)
352+
this.$forceUpdate()
353+
} else {
354+
this.$message.warning(`${this.$t('message.cannotAddMoreItems')}`)
355+
}
356+
}
357+
294358
private onPropertyChange(key: any, value: any) {
295359
this.data[key] = value
296360
this.isDataChanged = true
@@ -314,6 +378,15 @@ export default class extends Vue {
314378
315379
private processOneOfProp(data: any) {
316380
if (!this.schema.oneOf) {
381+
// remove empty field
382+
for (let key in data) {
383+
if (data[key] === '') {
384+
delete data[key]
385+
}
386+
if (typeof data[key] === 'object' && Object.keys(data[key]).length === 0) {
387+
delete data[key]
388+
}
389+
}
317390
return data
318391
}
319392
@@ -341,5 +414,10 @@ export default class extends Vue {
341414
margin-left: 10px;
342415
}
343416
}
417+
418+
.array-input-container > * {
419+
display: flex;
420+
margin-top: 5px;
421+
}
344422
}
345423
</style>

src/lang/en.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export default {
6464
},
6565
button: {
6666
save: 'Save',
67+
confirm: 'Confirm',
6768
cancel: 'Cancel',
6869
add_plugin: 'Add Plugin',
6970
add_node: 'Add Node',
@@ -85,5 +86,9 @@ export default {
8586
},
8687
keyTip: 'You can edit and input any value here,and press Enter to confirm.',
8788
hashOnTip: 'Select a hash on.'
89+
},
90+
message: {
91+
cannotAddMoreItems: 'You cannot add more item!',
92+
clickSaveButton: 'Your data will be saved after you click the Save button!'
8893
}
8994
}

src/lang/zh.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export default {
7272
},
7373
button: {
7474
save: '保存',
75+
confirm: '确认',
7576
cancel: '取消',
7677
add_plugin: '添加 Plugin',
7778
add_node: '添加 Node',
@@ -93,5 +94,9 @@ export default {
9394
},
9495
keyTip: '你可以编辑并输入任何值,输入值后回车确认。',
9596
hashOnTip: '选择 chash 参数来源。'
97+
},
98+
message: {
99+
cannotAddMoreItems: '不能添加更多项了!',
100+
clickSaveButton: '你的数据将在点击保存按钮后被保存!'
96101
}
97102
}

0 commit comments

Comments
 (0)