-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathUIDropdown.vue
More file actions
202 lines (192 loc) · 7.27 KB
/
UIDropdown.vue
File metadata and controls
202 lines (192 loc) · 7.27 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
<template>
<v-combobox
v-if="typeIsComboBox === true" v-model="value" :disabled="!state.enabled" :class="className"
:multiple="multiple" :chips="chips" :clearable="clearable" :items="options" item-title="label"
item-value="value" variant="outlined" hide-details="auto" auto-select-first
:error-messages="options?.length ? '' : 'No options available'" @update:model-value="onChange"
>
<template #label>
<!-- eslint-disable-next-line vue/no-v-html -->
<span v-html="label" />
</template>
</v-combobox>
<v-select
v-else v-model="value" :disabled="!state.enabled" :class="className" :multiple="multiple"
:chips="chips" :clearable="clearable" :items="options" item-title="label" item-value="value" variant="outlined"
hide-details="auto" :error-messages="options?.length ? '' : 'No options available'"
@update:model-value="onChange"
>
<template #label>
<!-- eslint-disable-next-line vue/no-v-html -->
<span v-html="label" />
</template>
</v-select>
</template>
<script>
import DOMPurify from 'dompurify'
import { mapState } from 'vuex'
export default {
name: 'DBUIDropdown',
inject: ['$socket', '$dataTracker'],
props: {
id: { type: String, required: true },
props: { type: Object, default: () => ({}) },
state: { type: Object, default: () => ({}) }
},
data () {
return {
value: null,
items: null
}
},
computed: {
...mapState('data', ['messages']),
options: {
get () {
const items = this.items || this.getProperty('options')
return items.map((item) => {
if (typeof item !== 'object') {
return {
label: item,
value: item
}
} else if (!('label' in item) || item.label === '') {
return {
label: item.value,
value: item.value
}
} else {
return item
}
})
},
set (value) {
this.items = value
}
},
multiple: function () {
return this.getProperty('multiple')
},
chips: function () {
return this.getProperty('chips')
},
clearable: function () {
return this.getProperty('clearable')
},
label: function () {
// Sanetize the html to avoid XSS attacks
return DOMPurify.sanitize(this.getProperty('label'))
},
typeIsComboBox: function () {
return this.props.typeIsComboBox ?? true
}
},
created () {
// can't do this in setup as we are using custom onInput function that needs access to 'this'
this.$dataTracker(this.id, null, this.onLoad, this.onDynamicProperties, this.onSync)
// let Node-RED know that this widget has loaded
this.$socket.emit('widget-load', this.id)
},
methods: {
// given the last received msg into this node, load the state
onLoad (msg) {
if (msg) {
// update vuex store to reflect server-state
this.$store.commit('data/bind', {
widgetId: this.id,
msg
})
this.select(this.messages[this.id]?.payload)
}
},
onDynamicProperties (msg) {
// When a msg comes in from Node-RED, we need support 2 operations:
// 1. add/replace the dropdown options (to support dynamic options e.g: nested dropdowns populated from a database)
// 2. update the selected value(s)
// keep options out for backward compatibility
// Check for booth possible methods to setup the options
const options = msg.options || msg.ui_update?.options
if (options) {
// 1. add/replace the dropdown options
if (Array.isArray(options)) {
this.items = options
}
}
const payload = msg.payload
if (payload !== undefined) {
// 2. update the selected value(s)
this.select(payload)
}
// update the UI with any other changes
const updates = msg.ui_update
if (updates) {
this.updateDynamicProperty('label', updates.label)
this.updateDynamicProperty('multiple', updates.multiple)
this.updateDynamicProperty('chips', updates.chips)
this.updateDynamicProperty('clearable', updates.clearable)
}
},
onSync (msg) {
// update the UI with any changes
this.value = msg.payload
},
onChange () {
// ensure our data binding with vuex store is updated
const msg = this.messages[this.id] || {}
if (this.multiple) {
// return an array
msg.payload = this.value.map((option) => {
if (this.props.typeIsComboBox === false) {
return option
}
return option.value
})
} else if (this.value) {
// return a single value
if (this.props.typeIsComboBox === false) {
msg.payload = this.value
} else {
msg.payload = this.value.value
}
} else {
// return null
msg.payload = null
}
this.$store.commit('data/bind', {
widgetId: this.id,
msg
})
this.$socket.emit('widget-change', this.id, msg.payload)
},
select (value) {
if (value !== undefined) {
// first, if we have a single value, we need to convert it to an array
if (!Array.isArray(value)) {
value = [value]
}
// value [] is used to clear the current selection
if (value.length > 0) {
// now if this is a single selection, we just need to find the option with the matching value
if (!this.props.multiple) {
value = this.options.find((o) => {
return o.value === value[0]
})
} else {
// this is a multi selection, we need to find all the options with matching values
value = this.options.filter((o) => {
return value.includes(o.value)
})
}
// if we didn't find any matching options, we stop here (unless value is [])
if (!value) {
return
}
}
// ensure we set our local "value" to match
this.value = value
}
}
}
}
</script>
<style scoped></style>