All files / src/components/navigation/annotation AnnotationForm.vue

89.79% Statements 44/49
72.22% Branches 13/18
91.66% Functions 22/24
89.79% Lines 44/49

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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282                                                        8x                           1x                   1x             1x                           8x                     14x                                           8x     7x               8x       7x                                     1x     198x                                     14x   14x   14x   16x     1x       14x   16x     1x       14x   1x     1x       14x     16x     1x 1x 1x       14x   16x     1x       14x     15x 1x             1x             1x 1x 1x         1x 1x 1x   1x                   1x     14x                                                  
<template>
  <v-form density="compact">
    <v-container class="pa-0">
      <v-row>
        <v-col cols="3">
          <v-hover #default="{ props, isHovering }">
            <div
              v-bind="props"
              class="upload position-relative d-flex rounded-lg justify-center align-center w-100 h-100"
            >
              <AnnotationIcon
                v-if="modelValue!.Icon2d"
                :annotation="modelValue!"
                size="64"
              />
              <v-icon
                v-else
                color="black"
                icon="image"
                size="64"
                style="opacity: 0.1"
              />
              <v-overlay
                :model-value="!!isHovering"
                class="align-center justify-center rounded-lg"
                contained
                @click="upload"
              >
                <div class="d-flex flex-column justify-center align-center">
                  <v-icon
                    color="white"
                    icon="upload"
                    size="32"
                  />
                  <span class="text-white">.png</span>
                </div>
              </v-overlay>
            </div>
          </v-hover>
        </v-col>
        <v-col>
          <sh-text-field
            v-model="title"
            hide-details
            :label="t('fields.title.label')"
            :placeholder="t('fields.title.placeholder')"
            variant="outlined"
          />
        </v-col>
      </v-row>
      <v-row>
        <v-col>
          <ColourPalette v-model="colour" />
        </v-col>
      </v-row>
      <v-row>
        <v-col>
          <sh-textarea
            no-resize
            v-model="description"
            counter="400"
            hide-details
            :label="t('fields.description.label')"
            :placeholder="t('fields.description.placeholder')"
            variant="outlined"
          />
        </v-col>
      </v-row>
    </v-container>
    <v-divider class="my-4" />
    <v-container class="pa-0">
      <v-row>
        <v-col>
          <div class="d-flex text-h5 align-center">
            <span>{{ t('texts.callToActionHelp') }}</span>
            <v-spacer />
            <v-btn
              color="minsk_900"
              variant="text"
              @click="addAction"
            >
              <v-icon
                class="minsk-500 pr-2"
                icon="add"
              />
              <span>{{ t('labels.addAction') }}</span>
            </v-btn>
          </div>
        </v-col>
      </v-row>
      <v-row>
        <v-col>
          <v-table>
            <thead>
              <tr>
                <th>
                  <span>{{ t('fields.description.label') }}</span>
                </th>
                <th>
                  <span>{{ t('fields.url.label') }}</span>
                </th>
                <th style="width: 64px"></th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="action in actions">
                <td class="new-style">
                  <v-text-field
                    data-testid="actiondata-description"
                    v-model="action.ActionData.Description"
                    density="compact"
                    :placeholder="t('fields.description.placeholder')"
                    flat
                    hide-details
                    variant="outlined"
                  />
                </td>
                <td class="new-style">
                  <v-text-field
                    variant="outlined"
                    data-testid="actiondata-Url"
                    v-model="action.ActionData.Url"
                    density="compact"
                    :placeholder="t('fields.url.placeholder')"
                    flat
                    hide-details
                  />
                </td>
                <td>
                  <v-tooltip
                    location="bottom"
                    open-delay="1000"
                  >
                    <template #activator>
                      <v-btn
                        class="shark-400"
                        data-testid="actiondata-remove"
                        density="comfortable"
                        icon="delete"
                        variant="text"
                        @click="removeAction(action)"
                      />
                    </template>
                    <span>{{ t('labels.removeAction') }}</span>
                  </v-tooltip>
                </td>
              </tr>
            </tbody>
          </v-table>
        </v-col>
      </v-row>
    </v-container>
  </v-form>
</template>
 
<script setup lang="ts">
import { computed, watch } from 'vue';
import { fromColourData, toColourData } from '@/functions/colours';
import { Buffer } from 'buffer';
import { useI18n } from 'vue-i18n';
import { Action, DataOverlayAnnotation } from '@3cr/viewer-types-ts';
 
const { t } = useI18n();
 
const modelValue = defineModel<DataOverlayAnnotation>();
 
const title = computed({
  get(): string {
    return modelValue.value!.Title;
  },
  set(value: string): void {
    modelValue.value!.Title = value;
  },
});
 
const description = computed({
  get(): string {
    return modelValue.value!.Description;
  },
  set(value: string): void {
    modelValue.value!.Description = value;
  },
});
 
const icon = computed({
  get(): string {
    return modelValue.value!.Icon2d;
  },
  set(value: string): void {
    modelValue.value!.Icon2d = value;
  },
});
 
const colour = computed({
  get(): string {
    // We will use colour 2d as the source of truth
    return fromColourData(modelValue.value!.Colour2d);
  },
  set(value: string): void {
    const colourData = toColourData(value);
    modelValue.value!.Colour2d = colourData;
    modelValue.value!.Colour3d = colourData;
  },
});
 
const actions = computed({
  get(): Action[] {
    return modelValue.value!.CallToAction!.Actions;
  },
  set(value: Action[]) {
    modelValue.value!.CallToAction!.Actions = value;
  },
});
 
watch(
  modelValue,
  () => {
    if (modelValue.value!.CallToAction === null) {
      modelValue.value!.CallToAction = { Actions: [] };
    }
  },
  { immediate: true },
);
 
function addAction(): void {
  actions.value.push({
    ActionType: 1,
    ActionData: { Description: '', Url: '' },
  });
}
 
function removeAction(item: Action): void {
  const index = actions.value.indexOf(item);
  Eif (index !== -1) {
    actions.value.splice(index, 1);
  }
}
 
function upload(): void {
  const input = document.createElement('input');
  input.type = 'file';
  input.accept = 'image/png';
  /* v8 ignore start */
  input.onchange = (_) => {
    const files = Array.from(input.files!!);
    if (files.length > 0) {
      const file = files[0];
      file.arrayBuffer().then((arr) => {
        icon.value = Buffer.from(arr).toString('base64');
      });
    }
  };
  /* v8 ignore stop */
  input.click();
}
 
defineExpose({ addAction, actions, icon, upload });
</script>
 
<style scoped lang="scss">
@use 'sass:map';
@use '@/assets/palette' as palette;
 
thead {
  font-weight: 600 !important;
  user-select: none;
  text-align: start;
  font-size: 12px !important;
  background-color: #f7f7f8;
  border-color: transparent;
 
  th {
    font-weight: 600 !important;
    line-height: 6px;
  }
}
 
.upload {
  border: 1px map.get(palette.$shark, 300) dashed;
}
</style>