summaryrefslogtreecommitdiffstats
path: root/src/components/EditMonitorConditionGroup.vue
blob: 910b4150839262c4098c5a487d35416440acd774 (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
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
<template>
    <div class="condition-group mb-3" data-testid="condition-group">
        <div class="d-flex">
            <select v-if="!isFirst" v-model="model.andOr" class="form-select" style="width: auto;" data-testid="condition-group-and-or">
                <option value="and">{{ $t("and") }}</option>
                <option value="or">{{ $t("or") }}</option>
            </select>
        </div>

        <div class="condition-group-inner mt-2 pa-2">
            <div class="condition-group-conditions">
                <template v-for="(child, childIndex) in model.children" :key="childIndex">
                    <EditMonitorConditionGroup
                        v-if="child.type === 'group'"
                        v-model="model.children[childIndex]"
                        :is-first="childIndex === 0"
                        :get-new-group="getNewGroup"
                        :get-new-condition="getNewCondition"
                        :condition-variables="conditionVariables"
                        @remove="removeChild"
                    />
                    <EditMonitorCondition
                        v-else
                        v-model="model.children[childIndex]"
                        :is-first="childIndex === 0"
                        :is-last="childIndex === model.children.length - 1"
                        :is-in-group="true"
                        :condition-variables="conditionVariables"
                        @remove="removeChild"
                    />
                </template>
            </div>

            <div class="condition-group-actions mt-3">
                <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>
                <button
                    class="btn btn-outline-danger"
                    type="button"
                    :aria-label="$t('conditionDeleteGroup')"
                    data-testid="remove-condition-group"
                    @click="remove"
                >
                    <font-awesome-icon icon="trash" />
                </button>
            </div>
        </div>
    </div>
</template>

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

export default {
    name: "EditMonitorConditionGroup",

    components: {
        EditMonitorCondition,
    },

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

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

        /**
         * Function to generate a new group model
         */
        getNewGroup: {
            type: Function,
            required: true,
        },

        /**
         * Function to generate a new condition model
         */
        getNewCondition: {
            type: Function,
            required: true,
        },

        /**
         * 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: {
        addGroup() {
            const conditions = [ ...this.model.children ];
            conditions.push(this.getNewGroup());
            this.model.children = conditions;
        },

        addCondition() {
            const conditions = [ ...this.model.children ];
            conditions.push(this.getNewCondition());
            this.model.children = conditions;
        },

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

        removeChild(child) {
            const idx = this.model.children.indexOf(child);
            if (idx !== -1) {
                this.model.children.splice(idx, 1);
            }
        },
    },
};
</script>

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

.condition-group-inner {
    background: rgba(0, 0, 0, 0.05);
    padding: 20px;
}

.dark .condition-group-inner {
    background: rgba(255, 255, 255, 0.05);
}

.condition-group-conditions {
    container-type: inline-size;
}

.condition-group-actions {
    display: grid;
    gap: 10px;
}

// Delete button
.condition-group-actions > :last-child {
    margin-left: auto;
    margin-top: 14px;
}

@container (min-width: 400px) {
    .condition-group-actions {
        display: flex;
    }

    // Delete button
    .condition-group-actions > :last-child {
        margin-left: auto;
        margin-top: 0;
    }

    .btn-delete-group {
        margin-left: auto;
    }
}
</style>