-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
rules-list.vue
330 lines (311 loc) · 12.6 KB
/
rules-list.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<template>
<f7-page @page:afterin="onPageAfterIn" @page:afterout="stopEventSource">
<f7-navbar :title="type" back-link="Settings" back-link-url="/settings/" back-link-force>
<f7-nav-right>
<f7-link icon-md="material:done_all" @click="toggleCheck()"
:text="(!$theme.md) ? ((showCheckboxes) ? 'Done' : 'Select') : ''" />
</f7-nav-right>
<f7-subnavbar :inner="false" v-show="initSearchbar">
<f7-searchbar
v-if="initSearchbar"
ref="searchbar"
class="searchbar-rules"
:init="initSearchbar"
search-container=".rules-list"
search-item=".rulelist-item"
search-in=".item-title, .item-text, .item-after, .item-subtitle, .item-header, .item-footer"
:placeholder="searchPlaceholder"
:disable-button="!$theme.aurora" />
</f7-subnavbar>
</f7-navbar>
<f7-toolbar class="contextual-toolbar" :class="{ 'navbar': $theme.md }" v-if="showCheckboxes" bottom-ios bottom-aurora>
<f7-link color="red" v-show="selectedItems.length" v-if="!$theme.md" class="delete" icon-ios="f7:trash" icon-aurora="f7:trash" @click="removeSelected">
Remove {{ selectedItems.length }}
</f7-link>
<f7-link color="orange" v-show="selectedItems.length" v-if="!$theme.md && !showScenes" class="disable" @click="doDisableEnableSelected(false)" icon-ios="f7:pause_circle" icon-aurora="f7:pause_circle">
Disable {{ selectedItems.length }}
</f7-link>
<f7-link color="green" v-show="selectedItems.length" v-if="!$theme.md && !showScenes" class="enable" @click="doDisableEnableSelected(true)" icon-ios="f7:play_circle" icon-aurora="f7:play_circle">
Enable {{ selectedItems.length }}
</f7-link>
<f7-link v-if="$theme.md" icon-md="material:close" icon-color="white" @click="showCheckboxes = false" />
<div class="title" v-if="$theme.md">
{{ selectedItems.length }} selected
</div>
<div class="right" v-if="$theme.md">
<f7-link v-if="!showScenes" v-show="selectedItems.length" tooltip="Disable selected" icon-md="material:pause_circle_outline" icon-color="white" @click="doDisableEnableSelected(false)" />
<f7-link v-if="!showScenes" v-show="selectedItems.length" tooltip="Enable selected" icon-md="material:play_circle_outline" icon-color="white" @click="doDisableEnableSelected(true)" />
<f7-link v-show="selectedItems.length" icon-md="material:delete" icon-color="white" @click="removeSelected" />
</div>
</f7-toolbar>
<f7-list-index
ref="listIndex"
v-if="$refs.rulesList"
v-show="!$device.desktop"
:listEl="$refs.rulesList ? $$($refs.rulesList.$el) : undefined"
:scroll-list="true"
:label="true" />
<f7-list class="searchbar-not-found">
<f7-list-item title="Nothing found" />
</f7-list>
<empty-state-placeholder v-if="noRuleEngine" icon="exclamationmark_triangle" title="rules.missingengine.title" text="rules.missingengine.text" />
<f7-block class="block-narrow" v-show="!noRuleEngine">
<!-- skeleton for not ready -->
<f7-col v-if="!ready">
<f7-block-title> Loading...</f7-block-title>
<f7-list contacts-list class="col rules-list">
<f7-list-group>
<f7-list-item
media-item
v-for="n in 20"
:key="n"
:class="`skeleton-text skeleton-effect-blink`"
title="Title of the rule"
subtitle="Tags, Schedule, Scene..."
after="status badge"
footer="Description of the rule" />
</f7-list-group>
</f7-list>
</f7-col>
<f7-col v-else-if="rules.length > 0">
<f7-block-title class="searchbar-hide-on-search">
{{ rules.length }} {{ type.toLowerCase() }} -
<f7-link external :href="documentationLink" target="_blank" text="Open documentation" color="blue" />
</f7-block-title>
<f7-list
v-show="rules.length > 0"
class="searchbar-found col rules-list"
ref="rulesList"
media-list contacts-list>
<f7-list-group v-for="(rulesWithInitial, initial) in indexedRules" :key="initial">
<f7-list-item v-if="rulesWithInitial.length" :title="initial" group-title />
<f7-list-item
v-for="rule in rulesWithInitial"
:key="rule.uid"
media-item
class="rulelist-item"
:checkbox="showCheckboxes"
:checked="isChecked(rule.uid)"
@click.ctrl="(e) => ctrlClick(e, rule)"
@click.meta="(e) => ctrlClick(e, rule)"
@click.exact="(e) => click(e, rule)"
link=""
:title="rule.name"
:text="rule.uid"
:footer="rule.description"
:badge="showScenes ? '' : ruleStatusBadgeText(rule.status)"
:badge-color="ruleStatusBadgeColor(rule.status)">
<div slot="footer">
<f7-chip v-for="tag in rule.tags.filter((t) => t !== 'Script' && t !== 'Scene')" :key="tag" :text="tag" media-bg-color="blue" style="margin-right: 6px">
<f7-icon slot="media" ios="f7:tag_fill" md="material:label" aurora="f7:tag_fill" />
</f7-chip>
</div>
<!-- <span slot="media" class="item-initial">{{initial}}</span> -->
<f7-icon v-if="rule.editable === false" slot="after-title" f7="lock_fill" size="1rem" color="gray" />
</f7-list-item>
</f7-list-group>
</f7-list>
</f7-col>
</f7-block>
<f7-block v-if="ready && !noRuleEngine && !rules.length" class="service-config block-narrow">
<empty-state-placeholder v-if="showScripts" icon="doc_plaintext" title="scripts.title" text="scripts.text" />
<empty-state-placeholder v-else-if="showScenes" icon="film" title="scenes.title" text="scenes.text" />
<empty-state-placeholder v-else icon="wand_stars" title="rules.title" text="rules.text" />
<f7-row class="display-flex justify-content-center">
<f7-button large fill color="blue" external :href="documentationLink" target="_blank" v-t="'home.overview.button.documentation'" />
</f7-row>
</f7-block>
<f7-fab v-show="ready && !showCheckboxes" position="right-bottom" slot="fixed" color="blue" href="add">
<f7-icon ios="f7:plus" md="material:add" aurora="f7:plus" />
<f7-icon ios="f7:close" md="material:close" aurora="f7:close" />
</f7-fab>
</f7-page>
</template>
<script>
import RuleStatus from '@/components/rule/rule-status-mixin'
export default {
mixins: [RuleStatus],
props: ['showScripts', 'showScenes'],
components: {
'empty-state-placeholder': () => import('@/components/empty-state-placeholder.vue')
},
data () {
return {
ready: false,
loading: false,
noRuleEngine: false,
rules: [],
initSearchbar: false,
selectedItems: [],
showCheckboxes: false,
eventSource: null
}
},
computed: {
type () {
return this.showScripts ? 'Scripts' : (this.showScenes ? 'Scenes' : 'Rules')
},
documentationLink () {
return `https://${this.$store.state.runtimeInfo.buildString === 'Release Build' ? 'www' : 'next'}.openhab.org/link/${this.type.toLowerCase()}`
},
indexedRules () {
return this.rules.reduce((prev, rule, i, rules) => {
const initial = rule.name.substring(0, 1).toUpperCase()
if (!prev[initial]) {
prev[initial] = []
}
prev[initial].push(rule)
return prev
}, {})
},
searchPlaceholder () {
return window.innerWidth >= 1280 ? 'Search (for advanced search, use the developer sidebar (Shift+Alt+D))' : 'Search'
}
},
methods: {
onPageAfterIn () {
this.load()
},
load () {
if (this.loading) return
this.loading = true
this.$set(this, 'selectedItems', [])
this.showCheckboxes = false
let filter = ''
if (this.showScripts) {
filter = '&tags=Script'
}
if (this.showScenes) {
filter = '&tags=Scene'
}
this.$oh.api.get('/rest/rules?summary=true' + filter).then(data => {
this.rules = data.sort((a, b) => {
return a.name.localeCompare(b.name)
})
if (!this.showScripts) {
this.rules = this.rules.filter((r) => !r.tags || r.tags.indexOf('Script') < 0)
}
if (!this.showScenes) {
this.rules = this.rules.filter((r) => !r.tags || r.tags.indexOf('Scene') < 0)
}
this.loading = false
this.ready = true
setTimeout(() => {
this.initSearchbar = true
if (this.$refs.listIndex) this.$refs.listIndex.update()
this.$nextTick(() => {
if (this.$device.desktop && this.$refs.searchbar) {
this.$refs.searchbar.f7Searchbar.$inputEl[0].focus()
}
})
})
if (!this.eventSource) this.startEventSource()
}).catch((err, status) => {
if (err === 'Not Found' || status === 404) {
this.noRuleEngine = true
}
})
},
startEventSource () {
this.eventSource = this.$oh.sse.connect('/rest/events?topics=openhab/rules/*/*', null, (event) => {
const topicParts = event.topic.split('/')
switch (topicParts[3]) {
case 'added':
case 'removed':
case 'updated':
this.load()
break
case 'state':
const rule = this.rules.find((r) => r.uid === topicParts[2])
const newStatus = JSON.parse(event.payload)
if (!rule) break
if (rule.status.status !== newStatus.status) rule.status.status = newStatus.status
if (rule.status.statusDetail !== newStatus.statusDetail) rule.status.statusDetail = newStatus.statusDetail
if (rule.status.description !== newStatus.description) rule.status.description = newStatus.description
}
})
},
stopEventSource () {
this.$oh.sse.close(this.eventSource)
this.eventSource = null
},
toggleCheck () {
this.showCheckboxes = !this.showCheckboxes
},
isChecked (item) {
return this.selectedItems.indexOf(item) >= 0
},
click (event, item) {
if (this.showCheckboxes) {
this.toggleItemCheck(event, item.uid, item)
} else {
this.$f7router.navigate((item.editable) ? item.uid : '/settings/scripts/' + item.uid)
}
},
ctrlClick (event, item) {
this.toggleItemCheck(event, item.uid, item)
if (!this.selectedItems.length) this.showCheckboxes = false
},
toggleItemCheck (event, item) {
if (!this.showCheckboxes) this.showCheckboxes = true
if (this.isChecked(item)) {
this.selectedItems.splice(this.selectedItems.indexOf(item), 1)
} else {
this.selectedItems.push(item)
}
},
removeSelected () {
const vm = this
this.$f7.dialog.confirm(
`Remove ${this.selectedItems.length} selected rules?`,
'Remove Rules',
() => {
vm.doRemoveSelected()
}
)
},
doRemoveSelected () {
if (this.selectedItems.some((i) => this.rules.find((rule) => rule.uid === i).editable === false)) {
this.$f7.dialog.alert('Some of the selected rules are not modifiable because they have been provisioned by files')
return
}
let dialog = this.$f7.dialog.progress('Deleting Rules...')
const promises = this.selectedItems.map((i) => this.$oh.api.delete('/rest/rules/' + i))
Promise.all(promises).then((data) => {
this.$f7.toast.create({
text: 'Rules removed',
destroyOnClose: true,
closeTimeout: 2000
}).open()
this.selectedItems = []
dialog.close()
this.load()
}).catch((err) => {
dialog.close()
this.load()
console.error(err)
this.$f7.dialog.alert('An error occurred while deleting: ' + err)
})
},
doDisableEnableSelected (enable) {
let dialog = this.$f7.dialog.progress('Please Wait...')
const promises = this.selectedItems.map((i) => this.$oh.api.postPlain('/rest/rules/' + i + '/enable', enable.toString()))
Promise.all(promises).then((data) => {
this.$f7.toast.create({
text: (enable) ? 'Rules enabled' : 'Rules disabled',
destroyOnClose: true,
closeTimeout: 2000
}).open()
this.selectedItems = []
dialog.close()
this.load()
}).catch((err) => {
dialog.close()
this.load()
console.error(err)
this.$f7.dialog.alert('An error occurred while enabling/disabling: ' + err)
})
}
}
}
</script>