All files / src/components ConditionGroup.vue

100% Statements 18/18
90.9% Branches 20/22
100% Functions 7/7
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  36x 6x           2x       6x 6x 6x             1x                     1x   31x 6x       6x                                                 6x           1x                 1x                     1x                                         6x                       26x 26x                          
<template>
  <div class="condition-group" :class="{ nested: depth > 1 }" data-testid="condition-group">
    <div class="d-flex align-items-center gap-2 mb-2">
      <select
        :value="group.mode"
        class="form-select form-select-sm w-auto"
        data-testid="condition-group-mode"
        name="condition-group-mode"
        @change="
          actions.setMode(group.key, ($event.target as HTMLSelectElement).value as GroupMode)
        "
      >
        <option value="all">{{ t('workflow.groupAll') }}</option>
        <option value="any">{{ t('workflow.groupAny') }}</option>
        <option value="none">{{ t('workflow.groupNone') }}</option>
      </select>
      <button
        v-if="depth > 1"
        type="button"
        class="btn btn-sm btn-outline-danger ms-auto"
        :title="t('workflow.removeGroup')"
        @click="actions.remove(group.key)"
      >
        <font-awesome-icon icon="fa-solid fa-trash" />
      </button>
    </div>
 
    <draggable
      :model-value="group.children"
      item-key="key"
      handle=".condition-drag-handle"
      :group="{ name: group.key }"
      @update:model-value="(children: NodeModel[]) => actions.reorder(group.key, children)"
    >
      <template #item="{ element }">
        <div class="d-flex align-items-start gap-2 mb-2">
          <span class="condition-drag-handle text-muted pt-1" :title="t('workflow.dragToReorder')">
            <font-awesome-icon icon="fa-solid fa-grip-vertical" />
          </span>
          <div class="flex-grow-1">
            <ConditionGroup
              v-if="element.kind === 'group'"
              :group="element"
              :depth="depth + 1"
              :fields="fields"
              :translate="translate"
              :linked-entities="linkedEntities"
            />
            <ConditionRow
              v-else
              :row="element"
              :fields="fields"
              :translate="translate"
              :linked-entities="linkedEntities"
            />
          </div>
        </div>
      </template>
    </draggable>
 
    <div v-if="group.children.length === 0" class="text-muted small mb-2">
      {{ depth === 1 ? t('workflow.noConditionAlwaysTrue') : t('workflow.emptyGroup') }}
    </div>
 
    <div class="d-flex flex-wrap gap-2">
      <button
        type="button"
        class="btn btn-sm btn-outline-primary"
        :disabled="fields.length === 0"
        data-testid="condition-add-row"
        @click="actions.addRow(group.key)"
      >
        <font-awesome-icon icon="fa-solid fa-plus" class="me-1" />
        {{ t('workflow.addCondition') }}
      </button>
      <button
        type="button"
        class="btn btn-sm btn-outline-primary"
        data-testid="condition-add-formula"
        @click="actions.addFormula(group.key)"
      >
        <font-awesome-icon icon="fa-solid fa-plus" class="me-1" />
        {{ t('workflow.addFormula') }}
      </button>
      <button
        v-if="depth < MAX_NESTED_GROUP_DEPTH"
        type="button"
        class="btn btn-sm btn-outline-secondary"
        :disabled="fields.length === 0"
        data-testid="condition-add-group"
        @click="actions.addGroup(group.key)"
      >
        <font-awesome-icon icon="fa-solid fa-layer-group" class="me-1" />
        {{ t('workflow.addGroup') }}
      </button>
    </div>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-WORKFLOW-007.1
import { inject } from 'vue'
import draggable from 'vuedraggable'
import ConditionRow from './ConditionRow.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { CONDITION_ACTIONS, type ConditionActions } from './conditionActions'
import type { GroupMode, GroupModel, NodeModel } from '@/utils/conditionModel'
import type { ConditionField, LinkedEntities } from '@/utils/workflowConditions'
 
// A "None of" group takes two levels of the API's depth limit (not + any):
// three nested groups always stay within it.
const MAX_NESTED_GROUP_DEPTH = 3
 
defineOptions({ name: 'ConditionGroup' })
 
defineProps<{
  group: GroupModel
  depth: number
  fields: ConditionField[]
  translate: (value: string | Record<string, string> | undefined) => string
  linkedEntities?: LinkedEntities
}>()
 
const { t } = useUILanguage()
const actions = inject(CONDITION_ACTIONS) as ConditionActions
</script>
 
<style scoped>
.condition-group.nested {
  border-left: 3px solid var(--bw-border);
  padding-left: 10px;
}
 
.condition-drag-handle {
  cursor: grab;
}
</style>