Files
TieMeasureFlow/client/templates/maker/recipe_editor.html
T
Adriano d262ef68af fix: version badge rendering, Jinja2 scoping, navbar i18n, missing GET task endpoint
- Fix version badge showing [object Object] or Python dict dump in
  select_recipe, task_list, and task_complete templates by accessing
  current_version.version_number instead of the whole object
- Fix recipe_editor.html Internal Server Error caused by Jinja2 block
  scoping: {% set %} variables from block content were invisible in
  block extra_js, replaced with direct recipe.* references
- Fix task_editor.html SyntaxError from Italian apostrophe in
  nell'eliminazione breaking JS string literals, switched to |tojson
- Add i18n {{ _() }} wrappers to all hardcoded navbar strings (desktop
  and mobile menus) so language toggle works correctly
- Add app title text "TieMeasureFlow" next to logo in navbar
- Add missing GET /api/tasks/{task_id} endpoint on server that caused
  405 Method Not Allowed when starting measurements

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-07 20:44:33 +01:00

827 lines
32 KiB
HTML

{% extends "base.html" %}
{% block title %}
{% if recipe %}
{{ recipe.code }} — {{ _('Modifica Ricetta') }}
{% else %}
{{ _('Nuova Ricetta') }}
{% endif %}
— TieMeasureFlow
{% endblock %}
{% block extra_head %}
<style>
/* Drag-and-drop overlay */
.drop-zone {
border: 2px dashed var(--border-color);
border-radius: 0.75rem;
transition: border-color 0.2s ease, background-color 0.2s ease;
}
.drop-zone.drag-over {
border-color: var(--color-primary);
background-color: rgba(37, 99, 235, 0.05);
}
.dark .drop-zone.drag-over {
background-color: rgba(59, 130, 246, 0.08);
}
/* Annotation toolbar button */
.anno-btn {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0.375rem 0.625rem;
font-size: 0.75rem;
font-weight: 500;
border-radius: 0.375rem;
border: 1px solid var(--border-color);
background: var(--bg-card);
color: var(--text-secondary);
cursor: pointer;
transition: all 0.15s ease;
gap: 0.25rem;
}
.anno-btn:hover {
background: var(--bg-secondary);
color: var(--text-primary);
border-color: var(--text-muted);
}
.anno-btn.active {
background: var(--color-primary);
color: #fff;
border-color: var(--color-primary);
}
.anno-btn:disabled {
opacity: 0.4;
cursor: not-allowed;
}
/* Canvas container */
.canvas-container {
position: relative;
background: var(--bg-secondary);
border: 1px solid var(--border-color);
border-radius: 0.5rem;
overflow: hidden;
min-height: 400px;
}
.canvas-container canvas {
display: block;
}
</style>
{% endblock %}
{% block content %}
{# ——— Determine mode flags ——— #}
{% set is_new = (recipe is none) %}
{% set recipe_id = recipe.id if recipe else None %}
{% set current_version = recipe.current_version if recipe and recipe.current_version else None %}
{% set versions = recipe.versions if recipe and recipe.versions else [] %}
<div class="container mx-auto px-4 sm:px-6 lg:px-8 py-8 max-w-4xl"
x-data="recipeEditor()"
x-cloak>
<!-- ============================================================
Breadcrumb
============================================================ -->
<nav class="mb-6" aria-label="Breadcrumb">
<ol class="flex items-center gap-2 text-sm text-[var(--text-secondary)]">
<li>
<a href="{{ url_for('maker.recipe_list') }}"
class="hover:text-primary transition-colors inline-flex items-center gap-1">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="1.75" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
</svg>
{{ _('Ricette') }}
</a>
</li>
<li>
<svg class="w-4 h-4 text-[var(--text-muted)]" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7"/>
</svg>
</li>
<li class="font-medium text-[var(--text-primary)]">
{% if is_new %}
{{ _('Nuova Ricetta') }}
{% else %}
{{ recipe.code }}
{% endif %}
</li>
</ol>
</nav>
<!-- ============================================================
Header + Action Buttons
============================================================ -->
<div class="mb-8">
<div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-3">
<div>
<h1 class="text-3xl font-bold text-[var(--text-primary)] mb-1">
{% if is_new %}
{{ _('Nuova Ricetta') }}
{% else %}
<span class="font-mono text-primary">{{ recipe.code }}</span>
<span class="text-[var(--text-muted)] text-lg font-normal ml-2">
(v<span x-text="currentVersion"></span>)
</span>
{% endif %}
</h1>
{% if not is_new %}
<p class="text-sm text-[var(--text-secondary)]" x-text="name"></p>
{% else %}
<p class="text-sm text-[var(--text-secondary)]">
{{ _('Compila i dati della ricetta e carica il disegno tecnico') }}
</p>
{% endif %}
</div>
<!-- Top action buttons -->
<div class="flex items-center gap-2 shrink-0 flex-wrap">
<button @click="saveRecipe()"
:disabled="saving || !name.trim() || (isNew && !code.trim())"
class="btn btn-primary gap-1.5">
<svg x-show="!saving" class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M5 13l4 4L19 7"/>
</svg>
<svg x-show="saving" class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"/>
</svg>
<span x-text="saving ? '{{ _('Salvataggio...') }}' : '{{ _('Salva') }}'"></span>
</button>
{% if not is_new %}
<a href="{{ url_for('maker.recipe_preview', recipe_id=recipe_id) }}"
class="btn btn-secondary gap-1.5">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
<path stroke-linecap="round" stroke-linejoin="round"
d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/>
</svg>
{{ _('Anteprima') }}
</a>
<a href="{{ url_for('maker.task_editor', recipe_id=recipe_id) }}"
class="btn btn-secondary gap-1.5">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"/>
</svg>
{{ _('Task') }}
</a>
{% endif %}
<a href="{{ url_for('maker.recipe_list') }}"
class="btn btn-secondary gap-1.5">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"/>
</svg>
{{ _('Annulla') }}
</a>
</div>
</div>
</div>
<!-- ============================================================
Error Banner
============================================================ -->
<div x-show="errorMessage"
x-transition
class="mb-6 flex items-center gap-3 px-4 py-3 rounded-lg
bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-800
text-red-800 dark:text-red-200 text-sm">
<svg class="w-5 h-5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
<span x-text="errorMessage"></span>
<button @click="errorMessage = ''" class="ml-auto shrink-0 hover:opacity-70">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
</svg>
</button>
</div>
<!-- ============================================================
Success Banner
============================================================ -->
<div x-show="saved"
x-transition
x-init="$watch('saved', v => { if (v) setTimeout(() => saved = false, 4000) })"
class="mb-6 flex items-center gap-3 px-4 py-3 rounded-lg
bg-emerald-50 dark:bg-emerald-900/30 border border-emerald-200 dark:border-emerald-800
text-emerald-800 dark:text-emerald-200 text-sm">
<svg class="w-5 h-5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
<span>{{ _('Ricetta salvata con successo') }}</span>
</div>
<!-- ============================================================
1. Card: Metadati Ricetta
============================================================ -->
<div class="tmf-card mb-6">
<div class="tmf-card-header flex items-center gap-2">
<svg class="w-4 h-4 text-[var(--text-muted)]" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
</svg>
{{ _('Metadati Ricetta') }}
</div>
<div class="tmf-card-body">
<div class="grid grid-cols-1 sm:grid-cols-2 gap-5">
<!-- Codice -->
<div>
<label class="tmf-label" for="recipe-code">
{{ _('Codice') }} <span class="text-red-500">*</span>
</label>
<input id="recipe-code"
type="text"
x-model="code"
class="tmf-input font-mono tracking-wide"
:disabled="!isNew"
:class="{ 'opacity-60 cursor-not-allowed': !isNew }"
placeholder="{{ _('Es. COUPLING-256') }}"
required>
<p x-show="!isNew" class="text-xs text-[var(--text-muted)] mt-1">
{{ _('Il codice non puo essere modificato dopo la creazione') }}
</p>
</div>
<!-- Nome -->
<div>
<label class="tmf-label" for="recipe-name">
{{ _('Nome') }} <span class="text-red-500">*</span>
</label>
<input id="recipe-name"
type="text"
x-model="name"
class="tmf-input"
placeholder="{{ _('Es. Coupling Assembly 256') }}"
required>
</div>
<!-- Descrizione -->
<div class="sm:col-span-2">
<label class="tmf-label" for="recipe-description">
{{ _('Descrizione') }}
</label>
<textarea id="recipe-description"
x-model="description"
class="tmf-input"
rows="3"
placeholder="{{ _('Descrizione opzionale della ricetta...') }}"></textarea>
</div>
</div>
</div>
</div>
<!-- ============================================================
2. Card: Upload / Annotazioni (Canvas Fabric.js)
============================================================ -->
<div class="tmf-card mb-6">
<div class="tmf-card-header flex items-center justify-between">
<div class="flex items-center gap-2">
<svg class="w-4 h-4 text-[var(--text-muted)]" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>
</svg>
{{ _('Disegno Tecnico e Annotazioni') }}
</div>
<!-- File indicator -->
<template x-if="currentFilePath">
<span class="badge badge-pass text-xs">
<svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7"/>
</svg>
{{ _('Immagine caricata') }}
</span>
</template>
</div>
<div class="tmf-card-body space-y-4">
<!-- Annotation Toolbar -->
<div x-show="currentFilePath"
x-transition
class="flex flex-wrap items-center gap-1.5 p-2 rounded-lg bg-[var(--bg-secondary)] border border-[var(--border-color)]">
<!-- Select Tool -->
<button type="button"
class="anno-btn"
:class="{ 'active': annoTool === 'select' }"
@click="setAnnoTool('select')"
title="{{ _('Seleziona') }}">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M15 15l-2 5L9 9l11 4-5 2z"/>
</svg>
<span class="hidden sm:inline">{{ _('Seleziona') }}</span>
</button>
<!-- Marker Tool -->
<button type="button"
class="anno-btn"
:class="{ 'active': annoTool === 'marker' }"
@click="setAnnoTool('marker')"
title="{{ _('Marker numerato') }}">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/>
<path stroke-linecap="round" stroke-linejoin="round" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/>
</svg>
<span class="hidden sm:inline">{{ _('Marker') }} #</span>
</button>
<!-- Arrow Tool -->
<button type="button"
class="anno-btn"
:class="{ 'active': annoTool === 'arrow' }"
@click="setAnnoTool('arrow')"
title="{{ _('Freccia') }}">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M17 8l4 4m0 0l-4 4m4-4H3"/>
</svg>
<span class="hidden sm:inline">{{ _('Freccia') }}</span>
</button>
<!-- Rectangle Tool -->
<button type="button"
class="anno-btn"
:class="{ 'active': annoTool === 'rect' }"
@click="setAnnoTool('rect')"
title="{{ _('Rettangolo') }}">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<rect x="3" y="3" width="18" height="18" rx="2" ry="2"/>
</svg>
<span class="hidden sm:inline">{{ _('Rettangolo') }}</span>
</button>
<!-- Separator -->
<div class="w-px h-6 bg-[var(--border-color)] mx-1"></div>
<!-- Delete -->
<button type="button"
class="anno-btn text-red-500 hover:text-red-600"
@click="deleteAnnotation()"
:disabled="!annoSelected"
title="{{ _('Elimina selezionato') }}">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
</svg>
<span class="hidden sm:inline">{{ _('Elimina') }}</span>
</button>
<!-- Separator -->
<div class="w-px h-6 bg-[var(--border-color)] mx-1"></div>
<!-- Zoom Controls -->
<span class="text-xs text-[var(--text-muted)] hidden sm:inline mr-1">{{ _('Zoom') }}:</span>
<button type="button"
class="anno-btn"
@click="zoomCanvas(-0.1)"
title="{{ _('Zoom indietro') }}">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M20 12H4"/>
</svg>
</button>
<button type="button"
class="anno-btn"
@click="zoomCanvas(0.1)"
title="{{ _('Zoom avanti') }}">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4"/>
</svg>
</button>
<button type="button"
class="anno-btn"
@click="resetZoom()"
title="{{ _('Reset zoom') }}">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
</svg>
</button>
<!-- Separator -->
<div class="w-px h-6 bg-[var(--border-color)] mx-1"></div>
<!-- Pan Mode -->
<button type="button"
class="anno-btn"
:class="{ 'active': annoTool === 'pan' }"
@click="setAnnoTool('pan')"
title="{{ _('Trascina') }}">
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M7 11.5V14m0-2.5v-6a1.5 1.5 0 113 0m-3 6a1.5 1.5 0 00-3 0v2a7.5 7.5 0 0015 0v-5a1.5 1.5 0 00-3 0m-6-2V11m0-5.5v-1a1.5 1.5 0 013 0v1m0 0V11m0-5.5a1.5 1.5 0 013 0v3m0 0V11"/>
</svg>
<span class="hidden sm:inline">{{ _('Pan') }}</span>
</button>
</div>
<!-- Canvas Area (shown when image loaded) -->
<div x-show="currentFilePath"
x-transition
x-data="annotationEditor()"
class="canvas-container"
id="annotationCanvasContainer">
<canvas id="annotationCanvas" x-ref="fabricCanvas"></canvas>
</div>
<!-- Drop Zone (always visible for uploading/replacing) -->
<div class="drop-zone p-6 text-center cursor-pointer relative"
:class="{ 'drag-over': dragOver }"
@dragover.prevent="dragOver = true"
@dragleave.prevent="dragOver = false"
@drop.prevent="handleDrop($event)"
@click="$refs.fileInput.click()">
<input type="file"
x-ref="fileInput"
@change="uploadFile($event.target.files[0]); $event.target.value = ''"
accept="image/*,application/pdf"
class="hidden">
<!-- Uploading spinner -->
<template x-if="uploadingFile">
<div class="flex flex-col items-center gap-3 py-4">
<svg class="w-8 h-8 animate-spin text-primary" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"/>
</svg>
<p class="text-sm text-[var(--text-secondary)]">{{ _('Caricamento in corso...') }}</p>
</div>
</template>
<!-- Upload prompt -->
<template x-if="!uploadingFile">
<div class="flex flex-col items-center gap-3 py-4">
<div class="w-12 h-12 rounded-full bg-primary-50 dark:bg-primary-900/30 flex items-center justify-center">
<svg class="w-6 h-6 text-primary" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5m-13.5-9L12 3m0 0l4.5 4.5M12 3v13.5"/>
</svg>
</div>
<div>
<p class="text-sm font-medium text-[var(--text-primary)]">
<template x-if="!currentFilePath">
<span>{{ _('Trascina qui il disegno tecnico oppure clicca per selezionare') }}</span>
</template>
<template x-if="currentFilePath">
<span>{{ _('Trascina qui per sostituire il disegno tecnico') }}</span>
</template>
</p>
<p class="text-xs text-[var(--text-muted)] mt-1">
{{ _('Formati supportati: PNG, JPG, PDF') }}
</p>
</div>
</div>
</template>
</div>
</div>
</div>
<!-- ============================================================
3. Card: Versioning (solo in modifica)
============================================================ -->
{% if not is_new %}
<div class="tmf-card mb-6">
<div class="tmf-card-header flex items-center gap-2">
<svg class="w-4 h-4 text-[var(--text-muted)]" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
{{ _('Versioning') }}
</div>
<div class="tmf-card-body space-y-4">
<!-- Current version info -->
<div class="flex items-center gap-3 flex-wrap">
<span class="text-sm text-[var(--text-secondary)]">
{{ _('Versione corrente:') }}
</span>
<span class="badge badge-neutral font-mono font-semibold"
x-text="'v' + currentVersion">
</span>
<template x-if="versions.length > 1">
<a href="{{ url_for('maker.version_history', recipe_id=recipe_id) }}"
class="text-xs text-primary hover:underline">
{{ _('Vedi cronologia') }} (<span x-text="versions.length"></span> {{ _('versioni') }})
</a>
</template>
</div>
<!-- Auto-version notice -->
<div class="flex items-start gap-2 p-3 rounded-lg bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800">
<svg class="w-5 h-5 text-blue-500 shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
<p class="text-xs text-blue-700 dark:text-blue-300">
{{ _('Se modifichi questa ricetta verra creata automaticamente la versione') }}
<strong x-text="'v' + (currentVersion + 1)"></strong>.
{{ _('Le misure esistenti resteranno associate alla versione corrente.') }}
</p>
</div>
<!-- Change notes -->
<div>
<label class="tmf-label" for="change-notes">
{{ _('Motivo della modifica') }}
</label>
<input id="change-notes"
type="text"
x-model="changeNotes"
class="tmf-input"
placeholder="{{ _('Es. Aggiornate tolleranze foro centrale...') }}">
<p class="text-xs text-[var(--text-muted)] mt-1">
{{ _('Opzionale. Verra registrato nella cronologia versioni.') }}
</p>
</div>
</div>
</div>
{% endif %}
<!-- ============================================================
4. Footer Action Buttons
============================================================ -->
<div class="flex flex-col sm:flex-row items-center gap-3 pt-4 pb-8">
<button @click="saveRecipe()"
:disabled="saving || !name.trim() || (isNew && !code.trim())"
class="btn btn-primary gap-2 w-full sm:w-auto justify-center">
<svg x-show="!saving" class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round"
d="M5 13l4 4L19 7"/>
</svg>
<svg x-show="saving" class="w-5 h-5 animate-spin" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"/>
</svg>
<span x-text="saving ? '{{ _('Salvataggio...') }}' : '{{ _('Salva Ricetta') }}'"></span>
</button>
<a href="{{ url_for('maker.recipe_list') }}"
class="btn btn-secondary gap-2 w-full sm:w-auto justify-center">
<svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"/>
</svg>
{{ _('Annulla') }}
</a>
</div>
</div>
{% endblock %}
{% block extra_js %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/5.3.1/fabric.min.js"></script>
<script src="{{ url_for('static', filename='js/annotation-editor.js') }}"></script>
<script>
function recipeEditor() {
return {
// ---- Mode ----
isNew: {{ 'true' if recipe is none else 'false' }},
recipeId: {{ recipe.id|tojson if recipe else 'null' }},
// ---- Form data ----
code: {{ (recipe.code if recipe else '')|tojson }},
name: {{ (recipe.name if recipe else '')|tojson }},
description: {{ (recipe.description if recipe and recipe.description else '')|tojson }},
changeNotes: '',
// ---- File upload ----
currentFilePath: {{ (recipe.current_version.tasks[0].file_path if recipe and recipe.current_version and recipe.current_version.tasks and recipe.current_version.tasks|length > 0 else None)|tojson }},
uploadingFile: false,
dragOver: false,
// ---- Annotation state (managed by annotation-editor.js) ----
annotationsJson: {{ (recipe.current_version.tasks[0].annotations_json if recipe and recipe.current_version and recipe.current_version.tasks and recipe.current_version.tasks|length > 0 and recipe.current_version.tasks[0].annotations_json else None)|tojson }},
annoTool: 'select',
annoSelected: false,
// ---- Save state ----
saving: false,
saved: false,
errorMessage: '',
// ---- Versioning ----
versions: {{ (recipe.versions if recipe and recipe.versions else [])|tojson }},
currentVersion: {{ (recipe.current_version.version_number if recipe and recipe.current_version else 0)|tojson }},
// ============================================================
// Init
// ============================================================
init() {
// Listen for annotation selection events from annotation-editor.js
window.addEventListener('annotation-selected', (e) => {
this.annoSelected = e.detail.selected;
});
// Listen for annotation data changes
window.addEventListener('annotations-changed', (e) => {
this.annotationsJson = e.detail.json;
});
},
// ============================================================
// Save Recipe
// ============================================================
async saveRecipe() {
this.saving = true;
this.errorMessage = '';
this.saved = false;
const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content;
const payload = {
code: this.code.trim(),
name: this.name.trim(),
description: this.description.trim(),
};
// Include change notes if editing
if (!this.isNew && this.changeNotes.trim()) {
payload.change_notes = this.changeNotes.trim();
}
// Include annotations if available
if (this.annotationsJson) {
payload.annotations_json = this.annotationsJson;
}
// Include file path if uploaded
if (this.currentFilePath) {
payload.file_path = this.currentFilePath;
}
const url = this.isNew
? '/maker/api/recipes'
: '/maker/api/recipes/' + this.recipeId;
const method = this.isNew ? 'POST' : 'PUT';
try {
const resp = await fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfToken || ''
},
body: JSON.stringify(payload)
});
const data = await resp.json();
if (data.error) {
this.errorMessage = data.detail || data.error || '{{ _("Errore nel salvataggio") }}';
this.saving = false;
return;
}
if (this.isNew) {
// Redirect to task editor after creating a new recipe
window.location.href = '/maker/recipes/' + data.id + '/tasks';
return;
}
// Update local state on successful edit
this.saved = true;
this.changeNotes = '';
// Update version info if server returned it
if (data.current_version) {
this.currentVersion = data.current_version.version_number;
}
// Refresh versions list if available
if (data.versions) {
this.versions = data.versions;
}
} catch (err) {
console.error('Save error:', err);
this.errorMessage = '{{ _("Errore di connessione al server") }}';
}
this.saving = false;
},
// ============================================================
// File Upload
// ============================================================
async uploadFile(file) {
if (!file) return;
// Validate file type
const allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/webp', 'application/pdf'];
if (!allowedTypes.includes(file.type)) {
this.errorMessage = '{{ _("Formato file non supportato. Usa PNG, JPG o PDF.") }}';
return;
}
// Validate file size (max 20MB)
const maxSize = 20 * 1024 * 1024;
if (file.size > maxSize) {
this.errorMessage = '{{ _("File troppo grande. Dimensione massima: 20MB.") }}';
return;
}
this.uploadingFile = true;
this.errorMessage = '';
const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content;
const formData = new FormData();
formData.append('file', file);
if (this.recipeId) {
formData.append('recipe_id', this.recipeId);
}
try {
const resp = await fetch('/maker/api/upload', {
method: 'POST',
headers: {
'X-CSRFToken': csrfToken || ''
},
body: formData
});
const data = await resp.json();
if (data.error) {
this.errorMessage = data.detail || '{{ _("Errore durante il caricamento del file") }}';
} else {
this.currentFilePath = data.file_path;
// Notify annotation-editor to load the new image
window.dispatchEvent(new CustomEvent('image-loaded', {
detail: { path: data.file_path, annotations: null }
}));
}
} catch (err) {
console.error('Upload error:', err);
this.errorMessage = '{{ _("Errore di connessione durante il caricamento") }}';
}
this.uploadingFile = false;
this.dragOver = false;
},
// ============================================================
// Drag & Drop
// ============================================================
handleDrop(event) {
this.dragOver = false;
const files = event.dataTransfer?.files;
if (files && files.length > 0) {
this.uploadFile(files[0]);
}
},
// ============================================================
// Annotation Tools (delegates to annotation-editor.js)
// ============================================================
setAnnoTool(tool) {
this.annoTool = tool;
window.dispatchEvent(new CustomEvent('anno-tool-change', {
detail: { tool }
}));
},
deleteAnnotation() {
window.dispatchEvent(new CustomEvent('anno-delete-selected'));
},
zoomCanvas(delta) {
window.dispatchEvent(new CustomEvent('anno-zoom', {
detail: { delta }
}));
},
resetZoom() {
window.dispatchEvent(new CustomEvent('anno-zoom-reset'));
},
// ============================================================
// Navigation
// ============================================================
goToPreview() {
if (this.recipeId) {
window.location.href = '/maker/recipes/' + this.recipeId + '/preview';
}
},
goToTasks() {
if (this.recipeId) {
window.location.href = '/maker/recipes/' + this.recipeId + '/tasks';
}
}
};
}
</script>
{% endblock %}