forked from openhab/openhab-webui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththing-picker.vue
71 lines (70 loc) · 2.34 KB
/
thing-picker.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
<template>
<ul>
<f7-list-item :title="title || 'Thing'" smart-select :smart-select-params="smartSelectParams" ref="smartSelect" v-if="ready">
<select :name="name" :multiple="multiple" @change="select" :required="required">
<option v-if="!multiple" value="" />
<option v-for="thing in things" :value="thing.UID" :key="thing.UID" :selected="(multiple) ? value.indexOf(thing.UID) >= 0 : value === thing.UID">
{{ thing.label ? thing.label + ' (' + thing.UID + ')' : thing.UID }}
</option>
</select>
</f7-list-item>
<!-- for placeholder purposes before items are loaded -->
<f7-list-item link v-show="!ready" :title="title" />
</ul>
</template>
<script>
export default {
props: ['title', 'name', 'value', 'multiple', 'required', 'filterType', 'filterUid', 'openOnReady'],
data () {
return {
ready: false,
things: [],
icons: {},
smartSelectParams: {
view: this.$f7.view.main,
openIn: 'popup',
searchbar: true,
searchbarPlaceholder: this.$t('dialogs.search.things'),
virtualList: true,
virtualListHeight: (this.$theme.aurora) ? 32 : undefined
}
}
},
created () {
this.smartSelectParams.closeOnSelect = !(this.multiple)
this.$oh.api.get('/rest/things?staticDataOnly=true').then((data) => {
this.things = data.sort((a, b) => {
const labelA = a.label || a.UID
const labelB = b.label || b.UID
return labelA.localeCompare(labelB)
})
if (this.filterType) {
this.things = this.things.filter((i) => this.filterType.indexOf(i.thingTypeUID) >= 0)
if (this.things.length < 5) {
this.smartSelectParams.openIn = 'sheet'
this.smartSelectParams.searchbar = false
}
}
if (this.filterUid && this.filterUid.length) {
this.things = this.things.filter((t) => this.filterUid.indexOf(t.UID) >= 0)
}
this.ready = true
if (this.openOnReady) {
this.$nextTick(() => {
this.$refs.smartSelect.f7SmartSelect.open()
})
}
})
},
methods: {
open () {
this.$refs.smartSelect.f7SmartSelect.open()
},
select (e) {
this.$f7.input.validateInputs(this.$refs.smartSelect.$el)
this.$emit('input', e.target.value)
this.$f7.emit('thingPicked', e.target.value)
}
}
}
</script>