summaryrefslogtreecommitdiffstats
path: root/src/components/EditMonitorCondition.vue
blob: ac1b02dd211c88c2372f7cc73c38082dfe602b4b (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
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
<template>
    <div class="monitor-condition mb-3" data-testid="condition">
        <button
            v-if="!isInGroup || !isFirst || !isLast"
            class="btn btn-outline-danger remove-button"
            type="button"
            :aria-label="$t('conditionDelete')"
            data-testid="remove-condition"
            @click="remove"
        >
            <font-awesome-icon icon="trash" />
        </button>

        <select v-if="!isFirst" v-model="model.andOr" class="form-select and-or-select" data-testid="condition-and-or">
            <option value="and">{{ $t("and") }}</option>
            <option value="or">{{ $t("or") }}</option>
        </select>

        <select v-model="model.variable" class="form-select" data-testid="condition-variable">
            <option
                v-for="variable in conditionVariables"
                :key="variable.id"
                :value="variable.id"
            >
                {{ $t(variable.id) }}
            </option>
        </select>

        <select v-model="model.operator" class="form-select" data-testid="condition-operator">
            <option
                v-for="operator in getVariableOperators(model.variable)"
                :key="operator.id"
                :value="operator.id"
            >
                {{ $t(operator.caption) }}
            </option>
        </select>

        <input
            v-model="model.value"
            type="text"
            class="form-control"
            :aria-label="$t('conditionValuePlaceholder')"
            data-testid="condition-value"
            required
        />
    </div>
</template>

<script>
export default {
    name: "EditMonitorCondition",

    props: {
        /**
         * The monitor condition
         */
        modelValue: {
            type: Object,
            required: true,
        },

        /**
         * Whether this is the first condition
         */
        isFirst: {
            type: Boolean,
            required: true,
        },

        /**
         * Whether this is the last condition
         */
        isLast: {
            type: Boolean,
            required: true,
        },

        /**
         * Whether this condition is in a group
         */
        isInGroup: {
            type: Boolean,
            required: false,
            default: false,
        },

        /**
         * Variable choices
         */
        conditionVariables: {
            type: Array,
            required: true,
        },
    },

    emits: [ "update:modelValue", "remove" ],

    computed: {
        model: {
            get() {
                return this.modelValue;
            },
            set(value) {
                this.$emit("update:modelValue", value);
            }
        }
    },

    methods: {
        remove() {
            this.$emit("remove", this.model);
        },

        getVariableOperators(variableId) {
            return this.conditionVariables.find(v => v.id === variableId)?.operators ?? [];
        },
    },
};
</script>

<style lang="scss" scoped>
@import "../assets/vars.scss";

.monitor-condition {
    display: flex;
    flex-wrap: wrap;
}

.remove-button {
    justify-self: flex-end;
    margin-bottom: 12px;
    margin-left: auto;
}

@container (min-width: 500px) {
    .monitor-condition {
        display: flex;
        flex-wrap: nowrap;
    }

    .remove-button {
        margin-bottom: 0;
        margin-left: 10px;
        order: 100;
    }

    .and-or-select {
        width: auto;
    }
}
</style>