ultrawidify/src/common/components/JsonEditor/JsonElement.vue

89 lines
2.0 KiB
Vue
Raw Normal View History

2020-03-11 00:15:53 +01:00
<template>
2020-03-11 23:12:18 +01:00
<div class="flex flex-row json-level-indent">
<div>
<b>
<span v-if="label" class="item-key">"{{label}}" </span>
:
</b>
2020-03-11 00:15:53 +01:00
</div>
2020-03-11 23:12:18 +01:00
<div v-if="typeof value_internal === 'boolean'"
2020-03-11 00:15:53 +01:00
class="json-value-boolean"
@click="toggleBoolean"
>
2020-03-11 23:12:18 +01:00
<template v-if="value_internal">true</template>
2020-03-11 00:15:53 +01:00
<template v-else>false</template>
</div>
<div v-else
class="flex flex-row inline-edit"
:class="{
2020-03-11 23:12:18 +01:00
'json-value-number': typeof value_internal === 'number',
'json-value-string': typeof value_internal === 'string'
2020-03-11 00:15:53 +01:00
}"
>
<template v-if="editing">
<div ref="element-edit-area"
:contenteditable="editing"
>
2020-03-11 23:12:18 +01:00
{{value_internal}}
2020-03-11 00:15:53 +01:00
</div>
<div class="btn" @click="changeValue">
</div>
</template>
<template v-else>
2020-03-11 23:12:18 +01:00
<template v-if="typeof value_internal === 'string'">
"{{value_internal}}"
2020-03-11 00:15:53 +01:00
</template>
<template v-else>
2020-03-11 23:12:18 +01:00
{{value_internal}}
</template>,
2020-03-11 00:15:53 +01:00
</template>
</div>
</div>
</template>
<script>
export default {
name: 'json-element',
props: [
'value',
2020-03-11 23:12:18 +01:00
'label',
2020-03-11 00:15:53 +01:00
],
data() {
return {
2020-03-11 23:12:18 +01:00
value_internal: undefined,
2020-03-11 00:15:53 +01:00
editing: false,
}
},
2020-03-11 23:12:18 +01:00
created() {
this.value_internal = this.value;
},
2020-03-11 00:15:53 +01:00
watch: {
value: function(val) {
2020-03-11 23:12:18 +01:00
this.value_internal = val;
2020-03-11 00:15:53 +01:00
}
},
methods: {
toggleBoolean() {
2020-03-11 23:12:18 +01:00
this.value_internal = !this.value_internal;
this.$emit('change', this.value_internal);
2020-03-11 00:15:53 +01:00
},
changeValue() {
const newValue = this.$refs['element-edit-area'].textContent;
if (isNaN(newValue)) {
2020-03-11 23:12:18 +01:00
this.value_internal = newValue;
2020-03-11 00:15:53 +01:00
this.$emit('change', newValue);
} else {
2020-03-11 23:12:18 +01:00
this.value_internal.value = +newValue;
2020-03-11 00:15:53 +01:00
this.$emit('change', +newValue);
}
this.editing = false;
}
}
}
2020-03-11 23:12:18 +01:00
</script>
<style lang="scss" scoped>
@import url('./json.scss');
</style>