Files
EdgeAdmin/web/public/js/components/common/values-box.js

109 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-07-22 22:19:39 +08:00
Vue.component("values-box", {
props: ["values", "v-values", "size", "maxlength", "name", "placeholder"],
2020-07-22 22:19:39 +08:00
data: function () {
let values = this.values;
if (values == null) {
2020-07-22 22:19:39 +08:00
values = [];
}
if (this.vValues != null && typeof this.vValues == "object") {
values = this.vValues
}
2020-07-22 22:19:39 +08:00
return {
"realValues": values,
2020-07-22 22:19:39 +08:00
"isUpdating": false,
"isAdding": false,
"index": 0,
2021-09-29 20:12:27 +08:00
"value": "",
isEditing: false
2020-07-22 22:19:39 +08:00
}
},
methods: {
create: function () {
this.isAdding = true;
var that = this;
setTimeout(function () {
that.$refs.value.focus();
}, 200);
},
update: function (index) {
this.cancel()
this.isUpdating = true;
this.index = index;
this.value = this.realValues[index];
2020-07-22 22:19:39 +08:00
var that = this;
setTimeout(function () {
that.$refs.value.focus();
}, 200);
},
confirm: function () {
if (this.value.length == 0) {
return
}
2020-07-22 22:19:39 +08:00
if (this.isUpdating) {
Vue.set(this.realValues, this.index, this.value);
2020-07-22 22:19:39 +08:00
} else {
this.realValues.push(this.value);
2020-07-22 22:19:39 +08:00
}
this.cancel()
this.$emit("change", this.realValues)
2020-07-22 22:19:39 +08:00
},
remove: function (index) {
this.realValues.$remove(index)
this.$emit("change", this.realValues)
2020-07-22 22:19:39 +08:00
},
cancel: function () {
this.isUpdating = false;
this.isAdding = false;
this.value = "";
2021-07-14 22:45:52 +08:00
},
updateAll: function (values) {
this.realValues = values
},
addValue: function (v) {
this.realValues.push(v)
2021-09-29 20:12:27 +08:00
},
startEditing: function () {
this.isEditing = !this.isEditing
2022-08-21 20:47:29 +08:00
},
allValues: function () {
return this.realValues
2020-07-22 22:19:39 +08:00
}
},
template: `<div>
<div v-show="!isEditing && realValues.length > 0">
<div class="ui label tiny basic" v-for="(value, index) in realValues" style="margin-top:0.4em;margin-bottom:0.4em">{{value}}</div>
2021-09-29 20:12:27 +08:00
<a href="" @click.prevent="startEditing" style="font-size: 0.8em; margin-left: 0.2em">[修改]</a>
</div>
<div v-show="isEditing || realValues.length == 0">
<div style="margin-bottom: 1em" v-if="realValues.length > 0">
<div class="ui label tiny basic" v-for="(value, index) in realValues" style="margin-top:0.4em;margin-bottom:0.4em">{{value}}
<input type="hidden" :name="name" :value="value"/>
&nbsp; <a href="" @click.prevent="update(index)" title="修改"><i class="icon pencil small" ></i></a>
<a href="" @click.prevent="remove(index)" title="删除"><i class="icon remove"></i></a>
</div>
<div class="ui divider"></div>
</div>
2021-10-01 16:24:42 +08:00
<!-- 添加|修改 -->
<div v-if="isAdding || isUpdating">
<div class="ui fields inline">
<div class="ui field">
<input type="text" :size="size" :maxlength="maxlength" :placeholder="placeholder" v-model="value" ref="value" @keyup.enter="confirm()" @keypress.enter.prevent="1"/>
</div>
<div class="ui field">
<button class="ui button small" type="button" @click.prevent="confirm()">确定</button>
</div>
<div class="ui field">
2021-01-14 16:35:25 +08:00
<a href="" @click.prevent="cancel()" title="取消"><i class="icon remove small"></i></a>
</div>
</div>
</div>
<div v-if="!isAdding && !isUpdating">
2021-07-14 22:45:52 +08:00
<button class="ui button tiny" type="button" @click.prevent="create()">+</button>
</div>
2021-09-29 20:12:27 +08:00
</div>
</div>`
2020-07-22 22:19:39 +08:00
});