summaryrefslogtreecommitdiffstats
path: root/src/components/EditMonitorConditions.vue
blob: 60f7c658993cae446e0c2235bc3ab9089f3a7b2b (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
<template>
    <div class="monitor-conditions">
        <label class="form-label">{{ $t("Conditions") }}</label>
        <div class="monitor-conditions-conditions">
            <template v-for="(condition, conditionIndex) in model" :key="conditionIndex">
                <EditMonitorConditionGroup
                    v-if="condition.type === 'group'"
                    v-model="model[conditionIndex]"
                    :is-first="conditionIndex === 0"
                    :get-new-group="getNewGroup"
                    :get-new-condition="getNewCondition"
                    :condition-variables="conditionVariables"
                    @remove="removeCondition"
                />
                <EditMonitorCondition
                    v-else
                    v-model="model[conditionIndex]"
                    :is-first="conditionIndex === 0"
                    :is-last="conditionIndex === model.length - 1"
                    :condition-variables="conditionVariables"
                    @remove="removeCondition"
                />
            </template>
        </div>
        <div class="monitor-conditions-buttons">
            <button class="btn btn-outline-secondary me-2" type="button" data-testid="add-condition-button" @click="addCondition">
                {{ $t("conditionAdd") }}
            </button>
            <button class="btn btn-outline-secondary me-2" type="button" data-testid="add-group-button" @click="addGroup">
                {{ $t("conditionAddGroup") }}
            </button>
        </div>
    </div>
</template>

<script>
import EditMonitorConditionGroup from "./EditMonitorConditionGroup.vue";
import EditMonitorCondition from "./EditMonitorCondition.vue";

export default {
    name: "EditMonitorConditions",

    components: {
        EditMonitorConditionGroup,
        EditMonitorCondition,
    },

    props: {
        /**
         * The monitor conditions
         */
        modelValue: {
            type: Array,
            required: true,
        },

        conditionVariables: {
            type: Array,
            required: true,
        },
    },

    emits: [ "update:modelValue" ],

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

    created() {
        if (this.model.length === 0) {
            this.addCondition();
        }
    },

    methods: {
        getNewGroup() {
            return {
                type: "group",
                children: [ this.getNewCondition() ],
                andOr: "and",
            };
        },

        getNewCondition() {
            const firstVariable = this.conditionVariables[0]?.id || null;
            const firstOperator = this.getVariableOperators(firstVariable)[0] || null;
            return {
                type: "expression",
                variable: firstVariable,
                operator: firstOperator?.id || null,
                value: "",
                andOr: "and",
            };
        },

        addGroup() {
            const conditions = [ ...this.model ];
            conditions.push(this.getNewGroup());
            this.$emit("update:modelValue", conditions);
        },

        addCondition() {
            const conditions = [ ...this.model ];
            conditions.push(this.getNewCondition());
            this.$emit("update:modelValue", conditions);
        },

        removeCondition(condition) {
            const conditions = [ ...this.model ];
            const idx = conditions.indexOf(condition);
            if (idx !== -1) {
                conditions.splice(idx, 1);
                this.$emit("update:modelValue", conditions);
            }
        },

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

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

.monitor-conditions,
.monitor-conditions-conditions {
    container-type: inline-size;
}

.monitor-conditions-buttons {
    display: grid;
    gap: 10px;
}

@container (min-width: 400px) {
    .monitor-conditions-buttons {
        display: flex;
    }
}
</style>