forked from openWB/openwb-ui-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenwbBaseTextarea.vue
149 lines (145 loc) · 3.4 KB
/
OpenwbBaseTextarea.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<template>
<div class="form-row mb-1">
<label v-on:click="toggleHelp" class="col-md-4 col-form-label">
{{ title }}
<font-awesome-icon
v-if="$slots.help"
:icon="
showHelp
? ['fas', 'question-circle']
: ['far', 'question-circle']
"
:class="showHelp ? 'text-info' : ''"
/>
</label>
<div class="col-md-8">
<div class="form-row">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<font-awesome-icon
fixed-width
v-if="subtype == 'text'"
:icon="['fas', 'keyboard']"
/>
<font-awesome-icon
fixed-width
v-if="subtype == 'json'"
:icon="['fas', 'code']"
/>
</div>
</div>
<textarea
ref="jsonInput"
v-if="subtype === 'json'"
class="form-control"
v-model.lazy="value"
v-bind="$attrs"
></textarea>
<textarea
v-else
class="form-control"
v-model="value"
v-bind="$attrs"
></textarea>
<div v-if="$attrs.maxlength" class="input-group-append">
<div class="input-group-text">
<small class="form-text text-muted text-right">
{{ length }} / {{ $attrs.maxlength }}
</small>
</div>
</div>
</div>
</div>
<span v-if="showHelp" class="form-row alert alert-info my-1 small">
<slot name="help"></slot>
</span>
</div>
</div>
</template>
<script>
import { library } from "@fortawesome/fontawesome-svg-core";
import { faQuestionCircle as fasQuestionCircle } from "@fortawesome/free-solid-svg-icons";
import { faQuestionCircle as farQuestionCircle } from "@fortawesome/free-regular-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
library.add(fasQuestionCircle, farQuestionCircle);
export default {
name: "OpenwbTextareaInput",
inheritAttrs: false,
props: {
title: String,
modelValue: { String, Object },
subtype: {
validator: function (value) {
return ["text", "json"].indexOf(value) !== -1;
},
default: "text",
},
},
emits: ["update:modelValue"],
data() {
return {
showHelp: false,
inputInvalid: false,
tempValue: this.modelValue,
};
},
computed: {
value: {
get() {
if (this.subtype == "json") {
if (this.inputInvalid) {
console.debug("returning invalid String");
return this.tempValue;
} else {
return JSON.stringify(this.tempValue, undefined, 2);
}
}
return this.modelValue;
},
set(newValue) {
if (this.subtype == "json") {
try {
let myNewJsonValue = JSON.parse(newValue);
this.inputInvalid = false;
this.$refs.jsonInput.setCustomValidity("");
this.tempValue = myNewJsonValue;
this.$emit("update:modelValue", myNewJsonValue);
} catch (e) {
console.warn("parsing JSON failed: " + newValue);
this.inputInvalid = true;
this.$refs.jsonInput.setCustomValidity(
"Ungültiger JSON Ausdruck!",
);
this.tempValue = newValue;
}
} else {
this.$emit("update:modelValue", newValue);
}
},
},
length: {
get() {
if (this.value !== undefined) {
return this.value.length;
} else {
return 0;
}
},
},
},
methods: {
toggleHelp() {
this.showHelp = !this.showHelp && this.$slots.help !== undefined;
},
},
components: {
FontAwesomeIcon,
},
};
</script>
<style scoped>
textarea:invalid {
background-color: pink;
}
</style>