-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathUITextInput.vue
More file actions
222 lines (219 loc) · 8.11 KB
/
UITextInput.vue
File metadata and controls
222 lines (219 loc) · 8.11 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<template>
<v-tooltip :text="tooltip" :disabled="!tooltip?.length" location="bottom">
<!-- eslint-disable-next-line vue/no-template-shadow -->
<template #activator="{ props: activatorProps }">
<v-text-field
v-if="type !== 'textarea'" v-model="value"
v-bind="activatorProps"
:disabled="!state.enabled" class="nrdb-ui-text-field"
:type="type" :rules="validation" :clearable="clearable" variant="outlined" hide-details="auto"
:prepend-icon="prependIcon" :append-icon="appendIcon" :append-inner-icon="appendInnerIcon" :prepend-inner-icon="prependInnerIcon" @update:model-value="onChange"
@keyup.enter="onEnter" @blur="onBlur" @click:clear="onClear"
>
<template #label>
<!-- eslint-disable-next-line vue/no-v-html -->
<span v-html="label" />
</template>
</v-text-field>
<v-textarea
v-else
v-bind="activatorProps"
v-model="value" :disabled="!state.enabled" class="nrdb-ui-text-field nrdb-ui-textarea" :style="{ 'grid-row-end': `span ${props.height}` }"
:prepend-icon="prependIcon" :append-icon="appendIcon" :append-inner-icon="appendInnerIcon" :prepend-inner-icon="prependInnerIcon"
:clearable="clearable" variant="outlined" hide-details="auto" @update:model-value="onChange" @blur="send"
@click:clear="onClear"
>
<template #label>
<!-- eslint-disable-next-line vue/no-v-html -->
<span v-html="label" />
</template>
</v-textarea>
</template>
</v-tooltip>
</template>
<script>
import DOMPurify from 'dompurify'
import { mapState } from 'vuex' // eslint-disable-line import/order
export default {
name: 'DBUITextInput',
inject: ['$socket', '$dataTracker'],
props: {
id: { type: String, required: true },
props: { type: Object, default: () => ({}) },
state: { type: Object, default: () => ({}) }
},
data () {
return {
delayTimer: null,
textValue: null
}
},
computed: {
...mapState('data', ['messages']),
label: function () {
// Sanetize the html to avoid XSS attacks
return DOMPurify.sanitize(this.getProperty('label'))
},
type: function () {
return this.getProperty('mode') || 'text'
},
tooltip: function () {
return this.props.tooltip
},
clearable: function () {
return this.getProperty('clearable')
},
prependIcon () {
const icon = this.getProperty('icon')
if (!icon) {
return undefined
}
const mdiIcon = this.makeMdiIcon(icon)
return icon && this.iconPosition === 'left' && this.iconInnerPosition === 'outside' ? mdiIcon : undefined
},
appendIcon () {
const icon = this.getProperty('icon')
if (!icon) {
return undefined
}
const mdiIcon = this.makeMdiIcon(icon)
return icon && this.iconPosition === 'right' && this.iconInnerPosition === 'outside' ? mdiIcon : undefined
},
prependInnerIcon () {
const icon = this.getProperty('icon')
if (!icon) {
return undefined
}
const mdiIcon = this.makeMdiIcon(icon)
return icon && this.iconPosition === 'left' && this.iconInnerPosition === 'inside' ? mdiIcon : undefined
},
appendInnerIcon () {
const icon = this.getProperty('icon')
if (!icon) {
return undefined
}
const mdiIcon = this.makeMdiIcon(icon)
return icon && this.iconPosition === 'right' && this.iconInnerPosition === 'inside' ? mdiIcon : undefined
},
iconPosition () {
return this.getProperty('iconPosition')
},
iconInnerPosition () {
return this.getProperty('iconInnerPosition')
},
value: {
get () {
return this.textValue
},
set (val) {
if (this.value === val) {
return // no change
}
const msg = this.messages[this.id] || {}
this.textValue = val
msg.payload = val
this.messages[this.id] = msg
}
},
validation: function () {
if (this.type === 'email') {
return [v => !v || /^[^\s@]+@[^\s@]+$/.test(v) || 'E-mail must be valid']
} else {
return []
}
}
},
created () {
// can't do this in setup as we are using custom onInput function that needs access to 'this'
this.$dataTracker(this.id, this.onInput, this.onLoad, this.onDynamicProperties, this.onSync)
},
methods: {
onInput (msg) {
// update our vuex store with the value retrieved from Node-RED
this.$store.commit('data/bind', {
widgetId: this.id,
msg
})
// make sure our v-model is updated to reflect the value from Node-RED
if (msg.payload !== undefined) {
this.textValue = msg.payload
}
},
onLoad (msg) {
if (msg) {
// update vuex store to reflect server-state
this.$store.commit('data/bind', {
widgetId: this.id,
msg
})
// make sure we've got the relevant option selected on load of the page
if (msg.payload !== undefined) {
this.textValue = msg.payload
}
}
},
onSync (msg) {
this.textValue = msg.payload
},
send: function () {
this.$socket.emit('widget-change', this.id, this.value)
},
onChange: function () {
if (this.props.sendOnDelay) {
// is send on delay enabled, if so, set a timeout to send the message
if (this.delayTimer) {
// reset the timer to count from the latest change
clearTimeout(this.delayTimer)
}
this.delayTimer = setTimeout(this.send, this.props.delay)
}
},
onBlur: function () {
if (this.props.sendOnBlur) {
// don't compare previous value, if user has clicked away they want it submitted
this.send()
}
},
onEnter: function () {
if (this.props.sendOnEnter) {
// don't compare previous value, if user has pressed <enter> they want it submitted
this.send()
}
},
onClear: function () {
if (this.props.sendOnClear) {
// don't compare previous value, if user has cleared the field they want it submitted
this.send()
}
},
makeMdiIcon (icon) {
return 'mdi-' + icon.replace(/^mdi-/, '')
},
onDynamicProperties (msg) {
const updates = msg.ui_update
if (!updates) {
return
}
this.updateDynamicProperty('label', updates.label)
this.updateDynamicProperty('mode', updates.mode)
this.updateDynamicProperty('clearable', updates.clearable)
this.updateDynamicProperty('icon', updates.icon)
this.updateDynamicProperty('iconPosition', updates.iconPosition)
this.updateDynamicProperty('iconInnerPosition', updates.iconInnerPosition)
}
}
}
</script>
<style lang="scss">
.nrdb-ui-textarea {
height: 100%; /* Ensure the textarea takes full height */
&.v-input--horizontal{
&:has(textarea) {
grid-template-rows: auto 0;
}
}
textarea.v-field__input {
height:100%;
}
}
</style>