All files / src/components RibbonFormSidebar.vue

91.3% Statements 42/46
93.75% Branches 30/32
77.77% Functions 14/18
89.74% Lines 35/39

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 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209  38x                             4x                       4x 4x 4x                                                       4x 132x               1x               4x                             4x     4x 4x                       22x                             20x 20x   20x               20x             60x   20x   20x   29x 20x 28x 28x 20x   24x   20x 20x 20x     1x       1x 1x         20x 21x 29x                                                                            
<template>
  <BaseFormSidebar
    :is-open="isOpen"
    :dirty="dirty"
    storage-key="ribbon-form-sidebar"
    :title="mode === 'create' ? t('ribbon.createNew') : t('ribbon.editRibbon')"
    panel-width="700px"
    :loading="loading"
    :submit-disabled="!isValid"
    :submit-label="mode === 'create' ? t('ribbon.create') : t('common.save')"
    :submit-icon="mode === 'create' ? 'fa-solid fa-plus' : 'fa-solid fa-save'"
    submit-button-class="btn-success"
    @close="handleClose"
    @submit="handleSubmit"
  >
    <form @submit.prevent="handleSubmit">
      <div class="mb-3">
        <MultilingualTextarea
          :model-value="formData.message"
          :label="t('ribbon.message')"
          :placeholder="t('ribbon.messagePlaceholder')"
          :rows="2"
          :enabled-languages="ribbonLanguages"
          required
          @update:model-value="$emit('update:formData', { ...formData, message: $event })"
        />
      </div>
 
      <div class="mb-3">
        <label class="form-label">{{ t('ribbon.color') }} <span class="text-danger">*</span></label>
        <div class="d-flex align-items-center gap-2">
          <input
            :value="formData.color"
            type="color"
            class="form-control form-control-color"
            name="ribbon-color-picker"
            @input="
              $emit('update:formData', {
                ...formData,
                color: ($event.target as HTMLInputElement).value
              })
            "
          />
          <input
            :value="formData.color"
            type="text"
            class="form-control"
            :class="{ 'is-invalid': !isValidColor }"
            placeholder="#dc3545"
            name="ribbon-color"
            @input="
              $emit('update:formData', {
                ...formData,
                color: ($event.target as HTMLInputElement).value
              })
            "
          />
        </div>
        <div class="preset-colors mt-2">
          <button
            v-for="preset in presetColors"
            :key="preset"
            type="button"
            class="preset-swatch"
            :style="{ backgroundColor: preset }"
            :class="{ selected: formData.color.toLowerCase() === preset }"
            :title="preset"
            @click="$emit('update:formData', { ...formData, color: preset })"
          />
        </div>
        <div v-if="!isValidColor" class="invalid-feedback d-block">
          {{ t('ribbon.colorInvalid') }}
        </div>
      </div>
 
      <div class="mb-3 form-check form-switch">
        <input
          id="ribbon-active"
          class="form-check-input"
          type="checkbox"
          role="switch"
          :checked="formData.active"
          @change="
            $emit('update:formData', {
              ...formData,
              active: ($event.target as HTMLInputElement).checked
            })
          "
        />
        <label class="form-check-label" for="ribbon-active">{{ t('ribbon.active') }}</label>
        <div class="form-text">{{ t('ribbon.activeHelp') }}</div>
      </div>
 
      <div class="mb-3">
        <label class="form-label small text-muted mb-1">{{ t('ribbon.preview') }}</label>
        <div
          class="ribbon-preview"
          :style="{ backgroundColor: previewColor, color: previewTextColor }"
        >
          {{ previewText || t('ribbon.previewPlaceholder') }}
        </div>
      </div>
 
      <div v-if="error" class="alert alert-danger">
        {{ error }}
      </div>
    </form>
  </BaseFormSidebar>
</template>
 
<script setup lang="ts">
import { useUnsavedChanges } from '@/composables/useUnsavedChanges'
import { computed } from 'vue'
import BaseFormSidebar from '@/components/BaseFormSidebar.vue'
import MultilingualTextarea from '@/components/MultilingualTextarea.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useLanguage } from '@/composables/useLanguage'
import { getContrastTextColor } from '@/utils/color'
import { UI_LANGUAGES } from '@/utils/languages'
import type { RibbonInput } from '@/stores/ribbons'
 
const { t, uiLanguage } = useUILanguage()
const { getTranslation } = useLanguage()
 
const props = defineProps<{
  isOpen: boolean
  mode: 'create' | 'edit'
  formData: RibbonInput
  loading: boolean
  error: string
}>()
 
const emit = defineEmits<{
  'update:formData': [value: RibbonInput]
  close: []
  submit: []
}>()
 
// Ribbons are displayed in the UI language, so every UI language must be editable
const ribbonLanguages = UI_LANGUAGES.map((lang) => lang.code)
 
const COLOR_PATTERN = /^#[0-9a-fA-F]{6}$/
 
const presetColors = ['#dc3545', '#ffaa00', '#7aa821', '#66c5e5', '#615581', '#46434d']
 
const isValidColor = computed(() => COLOR_PATTERN.test(props.formData.color || ''))
const isValidMessage = computed(() => {
  const message = props.formData.message
  if (typeof message === 'string') return message.trim().length > 0
  return !!message?.en?.trim()
})
const isValid = computed(() => isValidColor.value && isValidMessage.value)
 
const previewColor = computed(() => (isValidColor.value ? props.formData.color : '#6c757d'))
const previewTextColor = computed(() => getContrastTextColor(previewColor.value))
const previewText = computed(() => getTranslation(props.formData.message, uiLanguage.value))
 
function handleClose() {
  emit('close')
}
 
function handleSubmit() {
  Eif (isValid.value) {
    emit('submit')
  }
}
 
// Closing, or leaving the page, asks before dropping the changes (BaseFormSidebar)
const { dirty } = useUnsavedChanges(
  () => props.formData,
  () => props.isOpen
)
</script>
 
<style scoped>
.preset-colors {
  display: flex;
  gap: 0.5rem;
}
 
.preset-swatch {
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}
 
.preset-swatch.selected {
  border-color: var(--bw-text);
}
 
.form-control-color {
  width: 3rem;
  padding: 0.25rem;
  flex-shrink: 0;
}
 
.ribbon-preview {
  padding: 0.5rem 1rem;
  border-radius: 0.25rem;
  text-align: center;
  font-size: 0.9rem;
  font-weight: 500;
  white-space: pre-line;
}
</style>