summaryrefslogtreecommitdiffstats
path: root/apps/workflowengine/src/components/Check.vue
blob: c8c7c46aa8790a84d201efa742bd434157813a7d (plain)
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
<template>
	<div class="check" @click="showDelete" v-click-outside="hideDelete">
		<Multiselect v-model="currentOption" :options="options" label="name"
					 track-by="class" :allow-empty="false" :placeholder="t('workflowengine', 'Select a filter')"
					 @input="updateCheck" ref="checkSelector"></Multiselect>
		<Multiselect v-if="currentOption" v-model="currentOperator" @input="updateCheck"
					 :options="operators" label="name" track-by="operator"
					 :allow-empty="false" :placeholder="t('workflowengine', 'Select a comparator')"></Multiselect>
		<component v-if="currentOperator && currentComponent" :is="currentOption.component()" v-model="check.value" />
		<input v-else-if="currentOperator" type="text" v-model="check.value" @input="updateCheck" />
		<Actions>
			<ActionButton icon="icon-delete" v-if="deleteVisible || !currentOption" @click="$emit('remove')" />
		</Actions>
	</div>
</template>

<script>
	import { Multiselect, Actions, ActionButton } from 'nextcloud-vue'
	import ClickOutside from 'vue-click-outside';

	export default {
		name: 'Check',
		components: {
			ActionButton,
			Actions,
			Multiselect
		},
		directives: {
			ClickOutside
		},
		props: {
			check: {
				type: Object,
				required: true
			}
		},
		data() {
			return {
				deleteVisible: false,
				currentOption: null,
				currentOperator: null,
				options: [],
			}
		},
		mounted() {
			this.options = Object.values(OCA.WorkflowEngine.Plugins).map((plugin) => {
				if (plugin.component) {
					return {...plugin.getCheck(), component: plugin.component}
				}
				return plugin.getCheck()
			})
			this.currentOption = this.options.find((option) => option.class === this.check.class)
			this.currentOperator = this.operators.find((operator) => operator.operator === this.check.operator)
			this.$nextTick(() => {
				this.$refs.checkSelector.$el.focus()
			})
		},
		computed: {
			operators() {
				if (!this.currentOption)
					return []
				return this.options.find((item) => item.class === this.currentOption.class).operators
			},
			currentComponent() {
				if (!this.currentOption)
					return []
				let currentComponent = this.options.find((item) => item.class === this.currentOption.class).component
				return currentComponent && currentComponent()
			}
		},
		methods: {
			showDelete() {
				this.deleteVisible = true
			},
			hideDelete() {
				this.deleteVisible = false
			},
			updateCheck() {
				if (this.check.class !== this.currentOption.class) {
					this.currentOperator = this.operators[0]
				}
				this.check.class = this.currentOption.class
				this.check.operator = this.currentOperator.operator
				this.$emit('update', this.check)
			}
		}
	}
</script>

<style scoped lang="scss">
	.check {
		display: flex;
		& > .multiselect,
		& > input[type=text] {
			margin-right: 5px;
		}
	}
	input[type=text] {
		margin: 0;
	}
	::placeholder {
		font-size: 10px;
	}
</style>