forked from enviPath/enviPy
[Feature] Dynamic additional information rendering in frontend (#282)
This implements a version of #274, relying on Pydantics built in JSON schema and JSON rendering. Requires additional UI tagging in the ai model repo but will remove HTML tags. Example scenario with filled information: 5882df9c-dae1-4d80-a40e-db4724271456/scenario/3a4d395a-6a6d-4154-8ce3-ced667fceec0 Reviewed-on: enviPath/enviPy#282 Co-authored-by: Tobias O <tobias.olenyi@envipath.com> Co-committed-by: Tobias O <tobias.olenyi@envipath.com>
This commit is contained in:
@ -1,7 +1,15 @@
|
||||
{% extends "collections/paginated_base.html" %}
|
||||
{% load static %}
|
||||
|
||||
{% block page_title %}Scenarios{% endblock %}
|
||||
|
||||
{% block action_modals %}
|
||||
{# Load required scripts before modal #}
|
||||
<script src="{% static 'js/alpine/components/widgets.js' %}"></script>
|
||||
<script src="{% static 'js/api/additional-information.js' %}"></script>
|
||||
{% include "modals/collections/new_scenario_modal.html" %}
|
||||
{% endblock action_modals %}
|
||||
|
||||
{% block action_button %}
|
||||
{% if meta.can_edit %}
|
||||
<button
|
||||
@ -14,10 +22,6 @@
|
||||
{% endif %}
|
||||
{% endblock action_button %}
|
||||
|
||||
{% block action_modals %}
|
||||
{% include "modals/collections/new_scenario_modal.html" %}
|
||||
{% endblock action_modals %}
|
||||
|
||||
{% block description %}
|
||||
<p>
|
||||
A scenario contains meta-information that can be attached to other data
|
||||
|
||||
5
templates/components/modals/error_state.html
Normal file
5
templates/components/modals/error_state.html
Normal file
@ -0,0 +1,5 @@
|
||||
<template x-if="{{ error_var }}">
|
||||
<div class="alert alert-error mb-4">
|
||||
<span x-text="{{ error_var }}"></span>
|
||||
</div>
|
||||
</template>
|
||||
5
templates/components/modals/loading_state.html
Normal file
5
templates/components/modals/loading_state.html
Normal file
@ -0,0 +1,5 @@
|
||||
<template x-if="{{ loading_var }}">
|
||||
<div class="flex justify-center items-center p-4">
|
||||
<span class="loading loading-spinner loading-md"></span>
|
||||
</div>
|
||||
</template>
|
||||
135
templates/components/schema_form.html
Normal file
135
templates/components/schema_form.html
Normal file
@ -0,0 +1,135 @@
|
||||
{% load static %}
|
||||
|
||||
<div>
|
||||
<!-- Loading state -->
|
||||
<template x-if="loading">
|
||||
<div class="flex items-center justify-center p-4">
|
||||
<span class="loading loading-spinner loading-md"></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Error state -->
|
||||
<template x-if="error">
|
||||
<div class="alert alert-error mb-4">
|
||||
<span x-text="error"></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Schema form -->
|
||||
<template x-if="schema && !loading">
|
||||
<div class="space-y-4">
|
||||
<!-- Title from schema -->
|
||||
<template x-if="schema['x-title'] || schema.title">
|
||||
<h4
|
||||
class="text-lg font-semibold"
|
||||
x-text="schema['x-title'] || schema.title"
|
||||
></h4>
|
||||
</template>
|
||||
|
||||
<!-- Render each field (ordered by ui:order) -->
|
||||
<template x-for="fieldName in getFieldOrder()" :key="fieldName">
|
||||
<div>
|
||||
<!-- Text widget -->
|
||||
<template
|
||||
x-if="getWidget(fieldName, schema.properties[fieldName]) === 'text'"
|
||||
>
|
||||
<div
|
||||
x-data="textWidget(fieldName, data, schema, uiSchema, fieldErrors, mode)"
|
||||
>
|
||||
{% include "components/widgets/text_widget.html" %}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Textarea widget -->
|
||||
<template
|
||||
x-if="getWidget(fieldName, schema.properties[fieldName]) === 'textarea'"
|
||||
>
|
||||
<div
|
||||
x-data="textareaWidget(fieldName, data, schema, uiSchema, fieldErrors, mode)"
|
||||
>
|
||||
{% include "components/widgets/textarea_widget.html" %}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Number widget -->
|
||||
<template
|
||||
x-if="getWidget(fieldName, schema.properties[fieldName]) === 'number'"
|
||||
>
|
||||
<div
|
||||
x-data="numberWidget(fieldName, data, schema, uiSchema, fieldErrors, mode)"
|
||||
>
|
||||
{% include "components/widgets/number_widget.html" %}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Select widget -->
|
||||
<template
|
||||
x-if="getWidget(fieldName, schema.properties[fieldName]) === 'select'"
|
||||
>
|
||||
<div
|
||||
x-data="selectWidget(fieldName, data, schema, uiSchema, fieldErrors, mode)"
|
||||
>
|
||||
{% include "components/widgets/select_widget.html" %}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Checkbox widget -->
|
||||
<template
|
||||
x-if="getWidget(fieldName, schema.properties[fieldName]) === 'checkbox'"
|
||||
>
|
||||
<div
|
||||
x-data="checkboxWidget(fieldName, data, schema, uiSchema, fieldErrors, mode)"
|
||||
>
|
||||
{% include "components/widgets/checkbox_widget.html" %}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Interval widget -->
|
||||
<template
|
||||
x-if="getWidget(fieldName, schema.properties[fieldName]) === 'interval'"
|
||||
>
|
||||
<div
|
||||
x-data="intervalWidget(fieldName, data, schema, uiSchema, fieldErrors, mode)"
|
||||
>
|
||||
{% include "components/widgets/interval_widget.html" %}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- PubMed link widget -->
|
||||
<template
|
||||
x-if="getWidget(fieldName, schema.properties[fieldName]) === 'pubmed-link'"
|
||||
>
|
||||
<div
|
||||
x-data="pubmedWidget(fieldName, data, schema, uiSchema, fieldErrors, mode)"
|
||||
>
|
||||
{% include "components/widgets/pubmed_link_widget.html" %}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Compound link widget -->
|
||||
<template
|
||||
x-if="getWidget(fieldName, schema.properties[fieldName]) === 'compound-link'"
|
||||
>
|
||||
<div
|
||||
x-data="compoundWidget(fieldName, data, schema, uiSchema, fieldErrors, mode)"
|
||||
>
|
||||
{% include "components/widgets/compound_link_widget.html" %}
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Submit button (only in edit mode with endpoint) -->
|
||||
<template x-if="mode === 'edit' && endpoint">
|
||||
<div class="form-control mt-4">
|
||||
<button class="btn btn-primary" @click="submit()" :disabled="loading">
|
||||
<template x-if="loading">
|
||||
<span class="loading loading-spinner loading-sm"></span>
|
||||
</template>
|
||||
<span x-text="loading ? 'Submitting...' : 'Submit'"></span>
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
50
templates/components/widgets/checkbox_widget.html
Normal file
50
templates/components/widgets/checkbox_widget.html
Normal file
@ -0,0 +1,50 @@
|
||||
{# Checkbox widget - pure HTML template #}
|
||||
<div class="form-control">
|
||||
<div class="flex flex-col gap-2 sm:flex-row sm:items-baseline">
|
||||
<!-- Label -->
|
||||
<label class="label sm:w-48 sm:shrink-0">
|
||||
<span
|
||||
class="label-text"
|
||||
:class="{
|
||||
'text-error': hasError,
|
||||
'text-sm text-base-content/60': isViewMode
|
||||
}"
|
||||
x-text="label"
|
||||
></span>
|
||||
</label>
|
||||
|
||||
<!-- Input column -->
|
||||
<div class="flex-1">
|
||||
<!-- Help text -->
|
||||
<template x-if="helpText">
|
||||
<div class="label">
|
||||
<span
|
||||
class="label-text-alt text-base-content/60"
|
||||
x-text="helpText"
|
||||
></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- View mode -->
|
||||
<template x-if="isViewMode">
|
||||
<div class="mt-1">
|
||||
<span class="text-base" x-text="checked ? 'Yes' : 'No'"></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Edit mode -->
|
||||
<template x-if="isEditMode">
|
||||
<input type="checkbox" class="checkbox" x-model="checked" />
|
||||
</template>
|
||||
|
||||
<!-- Errors -->
|
||||
<template x-if="hasError">
|
||||
<div class="label">
|
||||
<template x-for="errMsg in errors" :key="errMsg">
|
||||
<span class="label-text-alt text-error" x-text="errMsg"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
66
templates/components/widgets/compound_link_widget.html
Normal file
66
templates/components/widgets/compound_link_widget.html
Normal file
@ -0,0 +1,66 @@
|
||||
{# Compound link widget - pure HTML template #}
|
||||
<div class="form-control">
|
||||
<div class="flex flex-col gap-2 sm:flex-row sm:items-baseline">
|
||||
<!-- Label -->
|
||||
<label class="label sm:w-48 sm:shrink-0">
|
||||
<span
|
||||
class="label-text"
|
||||
:class="{
|
||||
'text-error': hasError,
|
||||
'text-sm text-base-content/60': isViewMode
|
||||
}"
|
||||
x-text="label"
|
||||
></span>
|
||||
</label>
|
||||
|
||||
<!-- Input column -->
|
||||
<div class="flex-1">
|
||||
<!-- Help text -->
|
||||
<template x-if="helpText">
|
||||
<div class="label">
|
||||
<span
|
||||
class="label-text-alt text-base-content/60"
|
||||
x-text="helpText"
|
||||
></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- View mode: display as link -->
|
||||
<template x-if="isViewMode">
|
||||
<div class="mt-1">
|
||||
<template x-if="value">
|
||||
<a
|
||||
:href="value"
|
||||
class="link link-primary break-all"
|
||||
target="_blank"
|
||||
x-text="value"
|
||||
></a>
|
||||
</template>
|
||||
<template x-if="!value">
|
||||
<span class="text-base-content/50">—</span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Edit mode -->
|
||||
<template x-if="isEditMode">
|
||||
<input
|
||||
type="url"
|
||||
class="input input-bordered w-full"
|
||||
:class="{ 'input-error': hasError }"
|
||||
placeholder="Compound URL"
|
||||
x-model="value"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<!-- Errors -->
|
||||
<template x-if="hasError">
|
||||
<div class="label">
|
||||
<template x-for="errMsg in errors" :key="errMsg">
|
||||
<span class="label-text-alt text-error" x-text="errMsg"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
76
templates/components/widgets/interval_widget.html
Normal file
76
templates/components/widgets/interval_widget.html
Normal file
@ -0,0 +1,76 @@
|
||||
{# Interval widget for range inputs - pure HTML template #}
|
||||
<div class="form-control">
|
||||
<div class="flex flex-col gap-2 sm:flex-row sm:items-baseline">
|
||||
<!-- Label -->
|
||||
<label class="label sm:w-48 sm:shrink-0">
|
||||
<span
|
||||
class="label-text"
|
||||
:class="{
|
||||
'text-error': hasError,
|
||||
'text-sm text-base-content/60': isViewMode
|
||||
}"
|
||||
x-text="label"
|
||||
></span>
|
||||
</label>
|
||||
|
||||
<!-- Input column -->
|
||||
<div class="flex-1">
|
||||
<!-- Help text -->
|
||||
<template x-if="helpText">
|
||||
<div class="label">
|
||||
<span
|
||||
class="label-text-alt text-base-content/60"
|
||||
x-text="helpText"
|
||||
></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- View mode: formatted range with unit -->
|
||||
<template x-if="isViewMode">
|
||||
<div class="mt-1">
|
||||
<span class="text-base" x-text="start"></span>
|
||||
<span class="text-base-content/60 text-xs" x-show="!isSameValue"
|
||||
>to</span
|
||||
>
|
||||
<span class="text-base" x-text="end" x-show="!isSameValue"></span>
|
||||
<template x-if="start && end && unit">
|
||||
<span class="text-xs" x-text="unit"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Edit mode: two inputs with shared unit badge -->
|
||||
<template x-if="isEditMode">
|
||||
<div class="flex gap-2 items-center">
|
||||
<input
|
||||
type="number"
|
||||
class="input input-bordered flex-1"
|
||||
:class="{ 'input-error': hasError }"
|
||||
placeholder="Min"
|
||||
x-model="start"
|
||||
/>
|
||||
<span class="text-base-content/60">to</span>
|
||||
<input
|
||||
type="number"
|
||||
class="input input-bordered flex-1"
|
||||
:class="{ 'input-error': hasError }"
|
||||
placeholder="Max"
|
||||
x-model="end"
|
||||
/>
|
||||
<template x-if="unit">
|
||||
<span class="badge badge-ghost badge-lg" x-text="unit"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Errors -->
|
||||
<template x-if="hasError">
|
||||
<div class="label">
|
||||
<template x-for="errMsg in errors" :key="errMsg">
|
||||
<span class="label-text-alt text-error" x-text="errMsg"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
66
templates/components/widgets/number_widget.html
Normal file
66
templates/components/widgets/number_widget.html
Normal file
@ -0,0 +1,66 @@
|
||||
{# Number input widget - pure HTML template #}
|
||||
<div class="form-control">
|
||||
<div class="flex flex-col gap-2 sm:flex-row sm:items-baseline">
|
||||
<!-- Label -->
|
||||
<label class="label sm:w-48 sm:shrink-0">
|
||||
<span
|
||||
class="label-text"
|
||||
:class="{
|
||||
'text-error': hasError,
|
||||
'text-sm text-base-content/60': isViewMode
|
||||
}"
|
||||
x-text="label"
|
||||
></span>
|
||||
</label>
|
||||
|
||||
<!-- Input column -->
|
||||
<div class="flex-1">
|
||||
<!-- Help text -->
|
||||
<template x-if="helpText">
|
||||
<div class="label">
|
||||
<span
|
||||
class="label-text-alt text-base-content/60"
|
||||
x-text="helpText"
|
||||
></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- View mode: show value with unit -->
|
||||
<template x-if="isViewMode">
|
||||
<div class="mt-1">
|
||||
<span class="text-base" x-text="value"></span>
|
||||
<template x-if="value && unit">
|
||||
<span class="text-xs" x-text="unit"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Edit mode: input with unit suffix -->
|
||||
<template x-if="isEditMode">
|
||||
<div :class="unit ? 'join w-full' : ''">
|
||||
<input
|
||||
type="number"
|
||||
:class="unit ? 'input input-bordered join-item flex-1' : 'input input-bordered w-full'"
|
||||
class:input-error="hasError"
|
||||
x-model="value"
|
||||
/>
|
||||
<template x-if="unit">
|
||||
<span
|
||||
class="btn btn-ghost join-item no-animation pointer-events-none"
|
||||
x-text="unit"
|
||||
></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Errors -->
|
||||
<template x-if="hasError">
|
||||
<div class="label">
|
||||
<template x-for="errMsg in errors" :key="errMsg">
|
||||
<span class="label-text-alt text-error" x-text="errMsg"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
66
templates/components/widgets/pubmed_link_widget.html
Normal file
66
templates/components/widgets/pubmed_link_widget.html
Normal file
@ -0,0 +1,66 @@
|
||||
{# PubMed link widget - pure HTML template #}
|
||||
<div class="form-control">
|
||||
<div class="flex flex-col gap-2 sm:flex-row sm:items-baseline">
|
||||
<!-- Label -->
|
||||
<label class="label sm:w-48 sm:shrink-0">
|
||||
<span
|
||||
class="label-text"
|
||||
:class="{
|
||||
'text-error': hasError,
|
||||
'text-sm text-base-content/60': isViewMode
|
||||
}"
|
||||
x-text="label"
|
||||
></span>
|
||||
</label>
|
||||
|
||||
<!-- Input column -->
|
||||
<div class="flex-1">
|
||||
<!-- Help text -->
|
||||
<template x-if="helpText">
|
||||
<div class="label">
|
||||
<span
|
||||
class="label-text-alt text-base-content/60"
|
||||
x-text="helpText"
|
||||
></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- View mode: display as link -->
|
||||
<template x-if="isViewMode">
|
||||
<div class="mt-1">
|
||||
<template x-if="value && pubmedUrl">
|
||||
<a
|
||||
:href="pubmedUrl"
|
||||
class="link link-primary"
|
||||
target="_blank"
|
||||
x-text="value"
|
||||
></a>
|
||||
</template>
|
||||
<template x-if="!value">
|
||||
<span class="text-base-content/50">—</span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Edit mode -->
|
||||
<template x-if="isEditMode">
|
||||
<input
|
||||
type="text"
|
||||
class="input input-bordered w-full"
|
||||
:class="{ 'input-error': hasError }"
|
||||
placeholder="PubMed ID"
|
||||
x-model="value"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<!-- Errors -->
|
||||
<template x-if="hasError">
|
||||
<div class="label">
|
||||
<template x-for="errMsg in errors" :key="errMsg">
|
||||
<span class="label-text-alt text-error" x-text="errMsg"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
68
templates/components/widgets/select_widget.html
Normal file
68
templates/components/widgets/select_widget.html
Normal file
@ -0,0 +1,68 @@
|
||||
{# Select dropdown widget - pure HTML template #}
|
||||
<div class="form-control">
|
||||
<div class="flex flex-col gap-2 sm:flex-row sm:items-baseline">
|
||||
<!-- Label -->
|
||||
<label class="label sm:w-48 sm:shrink-0">
|
||||
<span
|
||||
class="label-text"
|
||||
:class="{
|
||||
'text-error': hasError,
|
||||
'text-sm text-base-content/60': isViewMode
|
||||
}"
|
||||
x-text="label"
|
||||
></span>
|
||||
</label>
|
||||
|
||||
<!-- Input column -->
|
||||
<div class="flex-1">
|
||||
<!-- Help text -->
|
||||
<template x-if="helpText">
|
||||
<div class="label">
|
||||
<span
|
||||
class="label-text-alt text-base-content/60"
|
||||
x-text="helpText"
|
||||
></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- View mode -->
|
||||
<template x-if="isViewMode">
|
||||
<div class="mt-1">
|
||||
<template x-if="value">
|
||||
<span class="text-base" x-text="value"></span>
|
||||
</template>
|
||||
<template x-if="!value">
|
||||
<span class="text-base-content/50">—</span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Edit mode -->
|
||||
<template x-if="isEditMode">
|
||||
<select
|
||||
class="select select-bordered w-full"
|
||||
:class="{ 'select-error': hasError }"
|
||||
x-model="value"
|
||||
>
|
||||
<option value="" :selected="!value">Select...</option>
|
||||
<template x-for="opt in options" :key="opt">
|
||||
<option
|
||||
:value="opt"
|
||||
:selected="value === opt"
|
||||
x-text="opt"
|
||||
></option>
|
||||
</template>
|
||||
</select>
|
||||
</template>
|
||||
|
||||
<!-- Errors -->
|
||||
<template x-if="hasError">
|
||||
<div class="label">
|
||||
<template x-for="errMsg in errors" :key="errMsg">
|
||||
<span class="label-text-alt text-error" x-text="errMsg"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
60
templates/components/widgets/text_widget.html
Normal file
60
templates/components/widgets/text_widget.html
Normal file
@ -0,0 +1,60 @@
|
||||
{# Text input widget - pure HTML template #}
|
||||
<div class="form-control">
|
||||
<div class="flex flex-col gap-2 sm:flex-row sm:items-baseline">
|
||||
<!-- Label -->
|
||||
<label class="label sm:w-48 sm:shrink-0">
|
||||
<span
|
||||
class="label-text"
|
||||
:class="{
|
||||
'text-error': hasError,
|
||||
'text-sm text-base-content/60': isViewMode
|
||||
}"
|
||||
x-text="label"
|
||||
></span>
|
||||
</label>
|
||||
|
||||
<!-- Input column -->
|
||||
<div class="flex-1">
|
||||
<!-- Help text -->
|
||||
<template x-if="helpText">
|
||||
<div class="label">
|
||||
<span
|
||||
class="label-text-alt text-base-content/60"
|
||||
x-text="helpText"
|
||||
></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- View mode -->
|
||||
<template x-if="isViewMode">
|
||||
<div class="mt-1">
|
||||
<template x-if="value">
|
||||
<span class="text-base" x-text="value"></span>
|
||||
</template>
|
||||
<template x-if="!value">
|
||||
<span class="text-base-content/50">—</span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Edit mode -->
|
||||
<template x-if="isEditMode">
|
||||
<input
|
||||
type="text"
|
||||
class="input input-bordered w-full"
|
||||
:class="{ 'input-error': hasError }"
|
||||
x-model="value"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<!-- Errors -->
|
||||
<template x-if="hasError">
|
||||
<div class="label">
|
||||
<template x-for="errMsg in errors" :key="errMsg">
|
||||
<span class="label-text-alt text-error" x-text="errMsg"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
59
templates/components/widgets/textarea_widget.html
Normal file
59
templates/components/widgets/textarea_widget.html
Normal file
@ -0,0 +1,59 @@
|
||||
{# Textarea widget - pure HTML template #}
|
||||
<div class="form-control">
|
||||
<div class="flex flex-col gap-2 sm:flex-row sm:items-baseline">
|
||||
<!-- Label -->
|
||||
<label class="label sm:w-48 sm:shrink-0">
|
||||
<span
|
||||
class="label-text"
|
||||
:class="{
|
||||
'text-error': hasError,
|
||||
'text-sm text-base-content/60': isViewMode
|
||||
}"
|
||||
x-text="label"
|
||||
></span>
|
||||
</label>
|
||||
|
||||
<!-- Input column -->
|
||||
<div class="flex-1">
|
||||
<!-- Help text -->
|
||||
<template x-if="helpText">
|
||||
<div class="label">
|
||||
<span
|
||||
class="label-text-alt text-base-content/60"
|
||||
x-text="helpText"
|
||||
></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- View mode -->
|
||||
<template x-if="isViewMode">
|
||||
<div class="mt-1">
|
||||
<template x-if="value">
|
||||
<p class="text-base whitespace-pre-wrap" x-text="value"></p>
|
||||
</template>
|
||||
<template x-if="!value">
|
||||
<span class="text-base-content/50">—</span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Edit mode -->
|
||||
<template x-if="isEditMode">
|
||||
<textarea
|
||||
class="textarea textarea-bordered w-full"
|
||||
:class="{ 'textarea-error': hasError }"
|
||||
x-model="value"
|
||||
></textarea>
|
||||
</template>
|
||||
|
||||
<!-- Errors -->
|
||||
<template x-if="hasError">
|
||||
<div class="label">
|
||||
<template x-for="errMsg in errors" :key="errMsg">
|
||||
<span class="label-text-alt text-error" x-text="errMsg"></span>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -30,6 +30,7 @@
|
||||
<script src="{% static 'js/alpine/search.js' %}"></script>
|
||||
<script src="{% static 'js/alpine/pagination.js' %}"></script>
|
||||
<script src="{% static 'js/alpine/pathway.js' %}"></script>
|
||||
<script src="{% static 'js/alpine/components/schema-form.js' %}"></script>
|
||||
|
||||
{# Font Awesome #}
|
||||
<link
|
||||
|
||||
@ -4,17 +4,152 @@
|
||||
id="new_scenario_modal"
|
||||
class="modal"
|
||||
x-data="{
|
||||
...modalForm(),
|
||||
isSubmitting: false,
|
||||
error: null,
|
||||
scenarioType: 'empty',
|
||||
schemas: {},
|
||||
groupSchemas: {},
|
||||
loadingSchemas: false,
|
||||
formData: {
|
||||
name: '',
|
||||
description: '',
|
||||
dateYear: '',
|
||||
dateMonth: '',
|
||||
dateDay: '',
|
||||
scenarioType: 'empty',
|
||||
additionalInformation: {}
|
||||
},
|
||||
// Track form data from each schema renderer
|
||||
schemaFormData: {},
|
||||
|
||||
validateYear(el) {
|
||||
if (el.value && el.value.length < 4) {
|
||||
el.value = new Date().getFullYear();
|
||||
}
|
||||
},
|
||||
|
||||
async init() {
|
||||
try {
|
||||
this.loadingSchemas = true;
|
||||
// Ensure API client is available
|
||||
if (!window.AdditionalInformationApi) {
|
||||
throw new Error('Additional Information API client not loaded. Please refresh the page.');
|
||||
}
|
||||
|
||||
// Use single API call to load schemas organized by groups
|
||||
const { schemas, groupSchemas } =
|
||||
await window.AdditionalInformationApi.loadSchemasWithGroups(['soil', 'sludge', 'sediment']);
|
||||
|
||||
this.schemas = schemas;
|
||||
this.groupSchemas = groupSchemas;
|
||||
} catch (err) {
|
||||
this.error = err.message;
|
||||
console.error('Error loading schemas:', err);
|
||||
} finally {
|
||||
this.loadingSchemas = false;
|
||||
}
|
||||
},
|
||||
|
||||
reset() {
|
||||
this.isSubmitting = false;
|
||||
this.error = null;
|
||||
this.scenarioType = 'empty';
|
||||
this.formData = {
|
||||
name: '',
|
||||
description: '',
|
||||
dateYear: '',
|
||||
dateMonth: '',
|
||||
dateDay: '',
|
||||
scenarioType: 'empty',
|
||||
additionalInformation: {}
|
||||
};
|
||||
this.schemaFormData = {};
|
||||
},
|
||||
|
||||
setSchemaFormData(schemaName, data) {
|
||||
this.schemaFormData[schemaName] = data;
|
||||
},
|
||||
|
||||
async submit() {
|
||||
if (!this.formData.name || this.formData.name.trim() === '') {
|
||||
this.error = 'Please enter a scenario name';
|
||||
return;
|
||||
}
|
||||
|
||||
this.isSubmitting = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
// Build scenario date
|
||||
let scenarioDate = this.formData.dateYear || '';
|
||||
if (this.formData.dateMonth && this.formData.dateMonth.trim() !== '') {
|
||||
scenarioDate += `-${parseInt(this.formData.dateMonth).toString().padStart(2, '0')}`;
|
||||
if (this.formData.dateDay && this.formData.dateDay.trim() !== '') {
|
||||
scenarioDate += `-${parseInt(this.formData.dateDay).toString().padStart(2, '0')}`;
|
||||
}
|
||||
}
|
||||
|
||||
if (!scenarioDate || scenarioDate.trim() === '') {
|
||||
scenarioDate = 'No date';
|
||||
}
|
||||
|
||||
// Collect additional information from schema forms
|
||||
const additionalInformation = [];
|
||||
const currentGroupSchemas = this.groupSchemas[this.scenarioType] || {};
|
||||
|
||||
for (const schemaName in this.schemaFormData) {
|
||||
const data = this.schemaFormData[schemaName];
|
||||
// Only include if schema belongs to current group and has data
|
||||
if (currentGroupSchemas[schemaName] && data && Object.keys(data).length > 0) {
|
||||
// Check if data has any non-null/non-empty values
|
||||
const hasData = Object.values(data).some(val => {
|
||||
if (val === null || val === undefined || val === '') return false;
|
||||
if (typeof val === 'object' && val !== null) {
|
||||
// For interval objects, check if start or end has value
|
||||
if (val.start !== null && val.start !== undefined && val.start !== '') return true;
|
||||
if (val.end !== null && val.end !== undefined && val.end !== '') return true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
if (hasData) {
|
||||
additionalInformation.push({
|
||||
type: schemaName,
|
||||
data: data
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build payload
|
||||
const payload = {
|
||||
name: this.formData.name.trim(),
|
||||
description: this.formData.description ? this.formData.description.trim() : '',
|
||||
scenario_date: scenarioDate,
|
||||
scenario_type: this.scenarioType === 'empty' ? 'Not specified' : this.scenarioType,
|
||||
additional_information: additionalInformation
|
||||
};
|
||||
|
||||
const packageUuid = '{{ meta.current_package.uuid }}';
|
||||
|
||||
// Use API client for scenario creation
|
||||
const result = await window.AdditionalInformationApi.createScenario(packageUuid, payload);
|
||||
|
||||
// Close modal and redirect to new scenario
|
||||
document.getElementById('new_scenario_modal').close();
|
||||
window.location.href = result.url || `{{ meta.current_package.url }}/scenario/${result.uuid}`;
|
||||
} catch (err) {
|
||||
this.error = err.message;
|
||||
} finally {
|
||||
this.isSubmitting = false;
|
||||
}
|
||||
}
|
||||
}"
|
||||
@close="reset()"
|
||||
@schema-form-data-changed.window="setSchemaFormData($event.detail.schemaName, $event.detail.data)"
|
||||
>
|
||||
<div class="modal-box max-w-3xl">
|
||||
<div class="modal-box max-w-3xl max-h-[90vh] overflow-y-auto">
|
||||
<!-- Header -->
|
||||
<h3 class="text-lg font-bold">New Scenario</h3>
|
||||
|
||||
@ -30,135 +165,212 @@
|
||||
|
||||
<!-- Body -->
|
||||
<div class="py-4">
|
||||
<form
|
||||
id="new-scenario-modal-form"
|
||||
accept-charset="UTF-8"
|
||||
action="{{ meta.current_package.url }}/scenario"
|
||||
method="post"
|
||||
>
|
||||
{% csrf_token %}
|
||||
<div class="alert alert-info mb-4">
|
||||
<span>
|
||||
Please enter name, description, and date of scenario. Date should be
|
||||
associated to the data, not the current date. For example, this could
|
||||
reflect the publishing date of a study. You can leave all fields but
|
||||
the name empty and fill them in later.
|
||||
<a
|
||||
target="_blank"
|
||||
href="https://wiki.envipath.org/index.php/scenario"
|
||||
class="link"
|
||||
>wiki >></a
|
||||
>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-info mb-4">
|
||||
<span>
|
||||
Please enter name, description, and date of scenario. Date should be
|
||||
associated to the data, not the current date. For example, this
|
||||
could reflect the publishing date of a study. You can leave all
|
||||
fields but the name empty and fill them in later.
|
||||
<a
|
||||
target="_blank"
|
||||
href="https://wiki.envipath.org/index.php/scenario"
|
||||
class="link"
|
||||
>wiki >></a
|
||||
>
|
||||
</span>
|
||||
<!-- Error state -->
|
||||
<template x-if="error">
|
||||
<div class="alert alert-error mb-4">
|
||||
<span x-text="error"></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="form-control mb-3">
|
||||
<label class="label" for="scenario-name">
|
||||
<span class="label-text">Name</span>
|
||||
</label>
|
||||
<input
|
||||
id="scenario-name"
|
||||
name="scenario-name"
|
||||
class="input input-bordered w-full"
|
||||
placeholder="Name"
|
||||
required
|
||||
/>
|
||||
<!-- Loading state -->
|
||||
<template x-if="loadingSchemas">
|
||||
<div class="flex items-center justify-center p-4">
|
||||
<span class="loading loading-spinner loading-md"></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="form-control mb-3">
|
||||
<label class="label" for="scenario-description">
|
||||
<span class="label-text">Description</span>
|
||||
</label>
|
||||
<input
|
||||
id="scenario-description"
|
||||
name="scenario-description"
|
||||
class="input input-bordered w-full"
|
||||
placeholder="Description"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-control mb-3">
|
||||
<label class="label">
|
||||
<span class="label-text">Date</span>
|
||||
</label>
|
||||
<div class="flex gap-2">
|
||||
<!-- Form fields -->
|
||||
<template x-if="!loadingSchemas">
|
||||
<div>
|
||||
<div class="form-control mb-3">
|
||||
<label class="label" for="scenario-name">
|
||||
<span class="label-text">Name</span>
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
id="dateYear"
|
||||
name="scenario-date-year"
|
||||
class="input input-bordered w-24"
|
||||
placeholder="YYYY"
|
||||
max="{% now 'Y' %}"
|
||||
@blur="validateYear($el)"
|
||||
/>
|
||||
<input
|
||||
type="number"
|
||||
id="dateMonth"
|
||||
name="scenario-date-month"
|
||||
min="1"
|
||||
max="12"
|
||||
class="input input-bordered w-20"
|
||||
placeholder="MM"
|
||||
/>
|
||||
<input
|
||||
type="number"
|
||||
id="dateDay"
|
||||
name="scenario-date-day"
|
||||
min="1"
|
||||
max="31"
|
||||
class="input input-bordered w-20"
|
||||
placeholder="DD"
|
||||
id="scenario-name"
|
||||
type="text"
|
||||
class="input input-bordered w-full"
|
||||
placeholder="Name"
|
||||
x-model="formData.name"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-control mb-3">
|
||||
<label class="label">
|
||||
<span class="label-text">Scenario Type</span>
|
||||
</label>
|
||||
<div role="tablist" class="tabs tabs-border">
|
||||
<button
|
||||
type="button"
|
||||
role="tab"
|
||||
class="tab"
|
||||
:class="{ 'tab-active': scenarioType === 'empty' }"
|
||||
@click="scenarioType = 'empty'"
|
||||
>
|
||||
Empty Scenario
|
||||
</button>
|
||||
{% for k, v in scenario_types.items %}
|
||||
<div class="form-control mb-3">
|
||||
<label class="label" for="scenario-description">
|
||||
<span class="label-text">Description</span>
|
||||
</label>
|
||||
<input
|
||||
id="scenario-description"
|
||||
type="text"
|
||||
class="input input-bordered w-full"
|
||||
placeholder="Description"
|
||||
x-model="formData.description"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-control mb-3">
|
||||
<label class="label">
|
||||
<span class="label-text">Date</span>
|
||||
</label>
|
||||
<div class="flex gap-2">
|
||||
<input
|
||||
type="number"
|
||||
class="input input-bordered w-24"
|
||||
placeholder="YYYY"
|
||||
max="{% now 'Y' %}"
|
||||
x-model="formData.dateYear"
|
||||
@blur="validateYear($el)"
|
||||
/>
|
||||
<input
|
||||
type="number"
|
||||
min="1"
|
||||
max="12"
|
||||
class="input input-bordered w-20"
|
||||
placeholder="MM"
|
||||
x-model="formData.dateMonth"
|
||||
/>
|
||||
<input
|
||||
type="number"
|
||||
min="1"
|
||||
max="31"
|
||||
class="input input-bordered w-20"
|
||||
placeholder="DD"
|
||||
x-model="formData.dateDay"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-control mb-3">
|
||||
<label class="label">
|
||||
<span class="label-text">Scenario Type</span>
|
||||
</label>
|
||||
<div role="tablist" class="tabs tabs-border">
|
||||
<button
|
||||
type="button"
|
||||
role="tab"
|
||||
class="tab"
|
||||
:class="{ 'tab-active': scenarioType === '{{ v.name }}' }"
|
||||
@click="scenarioType = '{{ v.name }}'"
|
||||
:class="{ 'tab-active': scenarioType === 'empty' }"
|
||||
@click="scenarioType = 'empty'"
|
||||
>
|
||||
{{ k }}
|
||||
Empty Scenario
|
||||
</button>
|
||||
{% endfor %}
|
||||
<button
|
||||
type="button"
|
||||
role="tab"
|
||||
class="tab"
|
||||
:class="{ 'tab-active': scenarioType === 'soil' }"
|
||||
@click="scenarioType = 'soil'"
|
||||
>
|
||||
Soil Data
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
role="tab"
|
||||
class="tab"
|
||||
:class="{ 'tab-active': scenarioType === 'sludge' }"
|
||||
@click="scenarioType = 'sludge'"
|
||||
>
|
||||
Sludge Data
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
role="tab"
|
||||
class="tab"
|
||||
:class="{ 'tab-active': scenarioType === 'sediment' }"
|
||||
@click="scenarioType = 'sediment'"
|
||||
>
|
||||
Water-Sediment System Data
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<input
|
||||
type="hidden"
|
||||
id="scenario-type"
|
||||
name="scenario-type"
|
||||
x-model="scenarioType"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{% for type in scenario_types.values %}
|
||||
<div
|
||||
id="{{ type.name }}-specific-inputs"
|
||||
x-show="scenarioType === '{{ type.name }}'"
|
||||
x-cloak
|
||||
>
|
||||
{% for widget in type.widgets %}
|
||||
{{ widget|safe }}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</form>
|
||||
<!-- Schema forms for each scenario type -->
|
||||
<template x-if="scenarioType === 'soil'">
|
||||
<div class="space-y-4 mt-4">
|
||||
<template
|
||||
x-for="(rjsf, schemaName) in groupSchemas.soil"
|
||||
:key="schemaName"
|
||||
>
|
||||
<div
|
||||
x-data="schemaRenderer({
|
||||
rjsf: rjsf,
|
||||
mode: 'edit'
|
||||
})"
|
||||
x-init="await init();
|
||||
const currentSchemaName = schemaName;
|
||||
$watch('data', (value) => {
|
||||
$dispatch('schema-form-data-changed', { schemaName: currentSchemaName, data: value });
|
||||
}, { deep: true })"
|
||||
>
|
||||
{% include "components/schema_form.html" %}
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template x-if="scenarioType === 'sludge'">
|
||||
<div class="space-y-4 mt-4">
|
||||
<template
|
||||
x-for="(rjsf, schemaName) in groupSchemas.sludge"
|
||||
:key="schemaName"
|
||||
>
|
||||
<div
|
||||
x-data="schemaRenderer({
|
||||
rjsf: rjsf,
|
||||
mode: 'edit'
|
||||
})"
|
||||
x-init="await init();
|
||||
const currentSchemaName = schemaName;
|
||||
$watch('data', (value) => {
|
||||
$dispatch('schema-form-data-changed', { schemaName: currentSchemaName, data: value });
|
||||
}, { deep: true })"
|
||||
>
|
||||
{% include "components/schema_form.html" %}
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template x-if="scenarioType === 'sediment'">
|
||||
<div class="space-y-4 mt-4">
|
||||
<template
|
||||
x-for="(rjsf, schemaName) in groupSchemas.sediment"
|
||||
:key="schemaName"
|
||||
>
|
||||
<div
|
||||
x-data="schemaRenderer({
|
||||
rjsf: rjsf,
|
||||
mode: 'edit'
|
||||
})"
|
||||
x-init="await init();
|
||||
const currentSchemaName = schemaName;
|
||||
$watch('data', (value) => {
|
||||
$dispatch('schema-form-data-changed', { schemaName: currentSchemaName, data: value });
|
||||
}, { deep: true })"
|
||||
>
|
||||
{% include "components/schema_form.html" %}
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<!-- Footer -->
|
||||
@ -174,8 +386,8 @@
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
@click="submit('new-scenario-modal-form')"
|
||||
:disabled="isSubmitting"
|
||||
@click="submit()"
|
||||
:disabled="isSubmitting || loadingSchemas"
|
||||
>
|
||||
<span x-show="!isSubmitting">Submit</span>
|
||||
<span
|
||||
|
||||
@ -6,27 +6,110 @@
|
||||
x-data="{
|
||||
isSubmitting: false,
|
||||
selectedType: '',
|
||||
schemas: {},
|
||||
loadingSchemas: false,
|
||||
error: null,
|
||||
formData: null, // Store reference to form data
|
||||
formRenderKey: 0, // Counter to force form re-render
|
||||
existingTypes: [], // Track existing additional information types
|
||||
|
||||
// Get sorted unique schema names for dropdown, excluding already-added types
|
||||
get sortedSchemaNames() {
|
||||
const names = Object.keys(this.schemas);
|
||||
// Remove duplicates, exclude existing types, and sort alphabetically by display title
|
||||
const unique = [...new Set(names)];
|
||||
const available = unique.filter(name => !this.existingTypes.includes(name));
|
||||
return available.sort((a, b) => {
|
||||
const titleA = (this.schemas[a]?.schema?.['x-title'] || a).toLowerCase();
|
||||
const titleB = (this.schemas[b]?.schema?.['x-title'] || b).toLowerCase();
|
||||
return titleA.localeCompare(titleB);
|
||||
});
|
||||
},
|
||||
|
||||
async init() {
|
||||
// Watch for selectedType changes
|
||||
this.$watch('selectedType', (value) => {
|
||||
// Reset formData when type changes and increment key to force re-render
|
||||
this.formData = null;
|
||||
this.formRenderKey++;
|
||||
});
|
||||
|
||||
// Load schemas and existing items
|
||||
try {
|
||||
this.loadingSchemas = true;
|
||||
const scenarioUuid = '{{ scenario.uuid }}';
|
||||
const [schemasRes, itemsRes] = await Promise.all([
|
||||
fetch('/api/v1/information/schema/'),
|
||||
fetch(`/api/v1/scenario/${scenarioUuid}/information/`)
|
||||
]);
|
||||
|
||||
if (!schemasRes.ok) throw new Error('Failed to load schemas');
|
||||
if (!itemsRes.ok) throw new Error('Failed to load existing items');
|
||||
|
||||
this.schemas = await schemasRes.json();
|
||||
const items = await itemsRes.json();
|
||||
|
||||
// Get unique existing types (normalize to lowercase)
|
||||
this.existingTypes = [...new Set(items.map(item => item.type.toLowerCase()))];
|
||||
} catch (err) {
|
||||
this.error = err.message;
|
||||
} finally {
|
||||
this.loadingSchemas = false;
|
||||
}
|
||||
},
|
||||
|
||||
reset() {
|
||||
this.isSubmitting = false;
|
||||
this.selectedType = '';
|
||||
this.error = null;
|
||||
this.formData = null;
|
||||
},
|
||||
|
||||
submit() {
|
||||
setFormData(data) {
|
||||
this.formData = data;
|
||||
},
|
||||
|
||||
async submit() {
|
||||
if (!this.selectedType) return;
|
||||
|
||||
const form = document.getElementById('add_' + this.selectedType + '_add-additional-information-modal-form');
|
||||
if (form && form.checkValidity()) {
|
||||
this.isSubmitting = true;
|
||||
form.submit();
|
||||
} else if (form) {
|
||||
form.reportValidity();
|
||||
const payload = window.AdditionalInformationApi.sanitizePayload(this.formData);
|
||||
|
||||
// Validate that form has data
|
||||
if (!payload || Object.keys(payload).length === 0) {
|
||||
this.error = 'Please fill in at least one field';
|
||||
return;
|
||||
}
|
||||
|
||||
this.isSubmitting = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
const scenarioUuid = '{{ scenario.uuid }}';
|
||||
await window.AdditionalInformationApi.createItem(
|
||||
scenarioUuid,
|
||||
this.selectedType,
|
||||
payload
|
||||
);
|
||||
|
||||
// Close modal and reload page to show new item
|
||||
document.getElementById('add_additional_information_modal').close();
|
||||
window.location.reload();
|
||||
} catch (err) {
|
||||
if (err.isValidationError && err.fieldErrors) {
|
||||
window.dispatchEvent(new CustomEvent('set-field-errors', {
|
||||
detail: err.fieldErrors
|
||||
}));
|
||||
}
|
||||
this.error = err.message;
|
||||
} finally {
|
||||
this.isSubmitting = false;
|
||||
}
|
||||
}
|
||||
}"
|
||||
@close="reset()"
|
||||
@form-data-ready="formData = $event.detail"
|
||||
>
|
||||
<div class="modal-box">
|
||||
<div class="modal-box max-w-2xl">
|
||||
<!-- Header -->
|
||||
<h3 class="text-lg font-bold">Add Additional Information</h3>
|
||||
|
||||
@ -42,46 +125,59 @@
|
||||
|
||||
<!-- Body -->
|
||||
<div class="py-4">
|
||||
<div class="form-control">
|
||||
<label class="label" for="select-additional-information-type">
|
||||
<span class="label-text">Select the type to add</span>
|
||||
</label>
|
||||
<select
|
||||
id="select-additional-information-type"
|
||||
class="select select-bordered w-full"
|
||||
x-model="selectedType"
|
||||
>
|
||||
<option value="" selected disabled>Select the type to add</option>
|
||||
{% for add_inf in available_additional_information %}
|
||||
<option value="{{ add_inf.name }}">
|
||||
{{ add_inf.display_name }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{% for add_inf in available_additional_information %}
|
||||
<div
|
||||
class="mt-4"
|
||||
x-show="selectedType === '{{ add_inf.name }}'"
|
||||
x-cloak
|
||||
>
|
||||
<form
|
||||
id="add_{{ add_inf.name }}_add-additional-information-modal-form"
|
||||
accept-charset="UTF-8"
|
||||
action=""
|
||||
method="post"
|
||||
>
|
||||
{% csrf_token %}
|
||||
{{ add_inf.widget|safe }}
|
||||
<input
|
||||
type="hidden"
|
||||
name="hidden"
|
||||
value="add-additional-information"
|
||||
/>
|
||||
</form>
|
||||
<!-- Loading state -->
|
||||
<template x-if="loadingSchemas">
|
||||
<div class="flex items-center justify-center p-4">
|
||||
<span class="loading loading-spinner loading-md"></span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</template>
|
||||
|
||||
<!-- Error state -->
|
||||
<template x-if="error">
|
||||
<div class="alert alert-error mb-4">
|
||||
<span x-text="error"></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Schema selection -->
|
||||
<template x-if="!loadingSchemas">
|
||||
<div>
|
||||
<div class="form-control mb-4">
|
||||
<label class="label" for="select-additional-information-type">
|
||||
<span class="label-text">Select the type to add</span>
|
||||
</label>
|
||||
<select
|
||||
id="select-additional-information-type"
|
||||
class="select select-bordered w-full"
|
||||
x-model="selectedType"
|
||||
>
|
||||
<option value="" selected disabled>Select the type to add</option>
|
||||
<template x-for="name in sortedSchemaNames" :key="name">
|
||||
<option
|
||||
:value="name"
|
||||
x-text="(schemas[name].schema && schemas[name].schema['x-title']) || name"
|
||||
></option>
|
||||
</template>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- Form renderer for selected type -->
|
||||
<!-- Use unique key per type to force re-render -->
|
||||
<template x-for="renderKey in [formRenderKey]" :key="renderKey">
|
||||
<div x-show="selectedType && schemas[selectedType]">
|
||||
<div
|
||||
x-data="schemaRenderer({
|
||||
rjsf: schemas[selectedType],
|
||||
mode: 'edit'
|
||||
})"
|
||||
x-init="await init(); $dispatch('form-data-ready', data)"
|
||||
>
|
||||
{% include "components/schema_form.html" %}
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<!-- Footer -->
|
||||
@ -98,7 +194,7 @@
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
@click="submit()"
|
||||
:disabled="isSubmitting || !selectedType"
|
||||
:disabled="isSubmitting || !selectedType || loadingSchemas"
|
||||
>
|
||||
<span x-show="!isSubmitting">Add</span>
|
||||
<span
|
||||
|
||||
@ -3,10 +3,99 @@
|
||||
<dialog
|
||||
id="update_scenario_additional_information_modal"
|
||||
class="modal"
|
||||
x-data="modalForm()"
|
||||
x-data="{
|
||||
isSubmitting: false,
|
||||
items: [],
|
||||
schemas: {},
|
||||
loading: false,
|
||||
error: null,
|
||||
originalItems: [], // Store original data to detect changes
|
||||
modifiedUuids: new Set(), // Track which items were modified
|
||||
|
||||
async init() {
|
||||
try {
|
||||
this.loading = true;
|
||||
const scenarioUuid = '{{ scenario.uuid }}';
|
||||
const { items, schemas } =
|
||||
await window.AdditionalInformationApi.loadSchemasAndItems(scenarioUuid);
|
||||
this.items = items;
|
||||
this.schemas = schemas;
|
||||
// Store deep copy of original items for comparison
|
||||
this.originalItems = JSON.parse(JSON.stringify(items));
|
||||
} catch (err) {
|
||||
this.error = err.message;
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
reset() {
|
||||
this.isSubmitting = false;
|
||||
this.error = null;
|
||||
this.modifiedUuids.clear();
|
||||
},
|
||||
|
||||
updateItemData(uuid, data) {
|
||||
// Update the item's data in the items array
|
||||
const item = this.items.find(i => i.uuid === uuid);
|
||||
if (item) {
|
||||
item.data = data;
|
||||
// Mark this item as modified
|
||||
this.modifiedUuids.add(uuid);
|
||||
}
|
||||
},
|
||||
|
||||
async submit() {
|
||||
if (this.items.length === 0) {
|
||||
this.error = 'No data to update';
|
||||
return;
|
||||
}
|
||||
|
||||
// Filter to only items that were actually modified
|
||||
const modifiedItems = this.items.filter(item => this.modifiedUuids.has(item.uuid));
|
||||
|
||||
if (modifiedItems.length === 0) {
|
||||
this.error = 'No changes to save';
|
||||
return;
|
||||
}
|
||||
|
||||
this.isSubmitting = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
const scenarioUuid = '{{ scenario.uuid }}';
|
||||
|
||||
// Use the unified API client for sequential, safe updates - only modified items
|
||||
await window.AdditionalInformationApi.updateItems(scenarioUuid, modifiedItems);
|
||||
|
||||
// Close modal and reload page
|
||||
document.getElementById('update_scenario_additional_information_modal').close();
|
||||
window.location.reload();
|
||||
} catch (err) {
|
||||
// Handle validation errors with field-level details
|
||||
if (err.isValidationError && err.fieldErrors) {
|
||||
this.error = err.message;
|
||||
// Dispatch event to set field errors in the specific form
|
||||
if (err.itemUuid) {
|
||||
window.dispatchEvent(new CustomEvent('set-field-errors-for-item', {
|
||||
detail: {
|
||||
uuid: err.itemUuid,
|
||||
fieldErrors: err.fieldErrors
|
||||
}
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
this.error = err.message;
|
||||
}
|
||||
} finally {
|
||||
this.isSubmitting = false;
|
||||
}
|
||||
}
|
||||
}"
|
||||
@close="reset()"
|
||||
@update-item-data.window="updateItemData($event.detail.uuid, $event.detail.data)"
|
||||
>
|
||||
<div class="modal-box">
|
||||
<div class="modal-box max-h-[90vh] max-w-4xl overflow-y-auto">
|
||||
<!-- Header -->
|
||||
<h3 class="text-lg font-bold">Update Additional Information</h3>
|
||||
|
||||
@ -22,18 +111,39 @@
|
||||
|
||||
<!-- Body -->
|
||||
<div class="py-4">
|
||||
<form
|
||||
id="edit-scenario-additional-information-modal-form"
|
||||
accept-charset="UTF-8"
|
||||
action=""
|
||||
method="post"
|
||||
>
|
||||
{% csrf_token %}
|
||||
{% for widget in update_widgets %}
|
||||
{{ widget|safe }}
|
||||
{% endfor %}
|
||||
<input type="hidden" name="hidden" value="set-additional-information" />
|
||||
</form>
|
||||
<!-- Loading state -->
|
||||
{% include "components/modals/loading_state.html" with loading_var="loading" %}
|
||||
|
||||
<!-- Error state -->
|
||||
{% include "components/modals/error_state.html" with error_var="error" %}
|
||||
|
||||
<!-- Items list -->
|
||||
<template x-if="!loading">
|
||||
<div class="space-y-4">
|
||||
<template x-if="items.length === 0">
|
||||
<p class="text-base-content/60">
|
||||
No additional information to update.
|
||||
</p>
|
||||
</template>
|
||||
|
||||
<template x-for="(item, index) in items" :key="item.uuid">
|
||||
<div class="card bg-base-200 shadow-sm">
|
||||
<div class="card-body p-4">
|
||||
<div
|
||||
x-data="schemaRenderer({
|
||||
rjsf: schemas[item.type.toLowerCase()],
|
||||
data: item.data,
|
||||
mode: 'edit'
|
||||
})"
|
||||
x-init="await init(); $watch('data', (value) => { $dispatch('update-item-data', { uuid: item.uuid, data: value }) }, { deep: true })"
|
||||
>
|
||||
{% include "components/schema_form.html" %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<!-- Footer -->
|
||||
@ -49,10 +159,17 @@
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
@click="submit('edit-scenario-additional-information-modal-form')"
|
||||
:disabled="isSubmitting"
|
||||
@click="submit()"
|
||||
:disabled="isSubmitting || loading || items.length === 0 || modifiedUuids.size === 0"
|
||||
>
|
||||
<span x-show="!isSubmitting">Update</span>
|
||||
<span x-show="!isSubmitting">
|
||||
<template x-if="modifiedUuids.size > 0">
|
||||
<span x-text="`Update (${modifiedUuids.size})`"></span>
|
||||
</template>
|
||||
<template x-if="modifiedUuids.size === 0">
|
||||
<span>No Changes</span>
|
||||
</template>
|
||||
</span>
|
||||
<span
|
||||
x-show="isSubmitting"
|
||||
class="loading loading-spinner loading-sm"
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
{% extends "framework_modern.html" %}
|
||||
{% load static %}
|
||||
|
||||
{% block content %}
|
||||
<script src="{% static 'js/alpine/components/widgets.js' %}"></script>
|
||||
<script src="{% static 'js/api/additional-information.js' %}"></script>
|
||||
|
||||
{% block action_modals %}
|
||||
{% include "modals/objects/edit_scenario_modal.html" %}
|
||||
@ -58,46 +61,88 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Additional Information Table -->
|
||||
<!-- Additional Information -->
|
||||
<div class="card bg-base-100">
|
||||
<div class="card-body">
|
||||
<h3 class="card-title mb-4 text-lg">Additional Information</h3>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="table-zebra table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Property</th>
|
||||
<th>Value</th>
|
||||
<th>Unit</th>
|
||||
{% if meta.can_edit %}
|
||||
<th>Remove</th>
|
||||
{% endif %}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for ai in scenario.get_additional_information %}
|
||||
<tr>
|
||||
<td>{{ ai.property_name|safe }}</td>
|
||||
<td>{{ ai.property_data|safe }}</td>
|
||||
<td>{{ ai.property_unit|safe }}</td>
|
||||
{% if meta.can_edit %}
|
||||
<td>
|
||||
<form
|
||||
action="{% url 'package scenario detail' scenario.package.uuid scenario.uuid %}"
|
||||
method="post"
|
||||
<div
|
||||
x-data="{
|
||||
items: [],
|
||||
schemas: {},
|
||||
loading: true,
|
||||
error: null,
|
||||
async init() {
|
||||
try {
|
||||
// Use the unified API client for loading data
|
||||
const { items, schemas } = await window.AdditionalInformationApi.loadSchemasAndItems('{{ scenario.uuid }}');
|
||||
this.items = items;
|
||||
this.schemas = schemas;
|
||||
} catch (err) {
|
||||
this.error = err.message;
|
||||
console.error('Error loading additional information:', err);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
async deleteItem(uuid) {
|
||||
if (!confirm('Are you sure you want to delete this item?')) return;
|
||||
|
||||
try {
|
||||
// Use the unified API client for delete operations
|
||||
await window.AdditionalInformationApi.deleteItem('{{ scenario.uuid }}', uuid);
|
||||
|
||||
// Remove from items array
|
||||
this.items = this.items.filter(item => item.uuid !== uuid);
|
||||
} catch (err) {
|
||||
alert('Error deleting item: ' + err.message);
|
||||
console.error('Error deleting item:', err);
|
||||
}
|
||||
}
|
||||
}"
|
||||
>
|
||||
<!-- Loading state -->
|
||||
<template x-if="loading">
|
||||
<div class="flex items-center justify-center p-4">
|
||||
<span class="loading loading-spinner loading-md"></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Error state -->
|
||||
<template x-if="error">
|
||||
<div class="alert alert-error mb-4">
|
||||
<span x-text="error"></span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Items list -->
|
||||
<template x-if="!loading && !error">
|
||||
<div class="space-y-4">
|
||||
<template x-if="items.length === 0">
|
||||
<p class="text-base-content/60">
|
||||
No additional information available.
|
||||
</p>
|
||||
</template>
|
||||
|
||||
<template x-for="item in items" :key="item.uuid">
|
||||
<div class="card bg-base-200 shadow-sm">
|
||||
<div class="card-body p-4">
|
||||
<div class="flex items-start justify-between">
|
||||
<div
|
||||
class="flex-1"
|
||||
x-data="schemaRenderer({
|
||||
rjsf: schemas[item.type.toLowerCase()],
|
||||
data: item.data,
|
||||
mode: 'view'
|
||||
})"
|
||||
x-init="init()"
|
||||
>
|
||||
{% csrf_token %}
|
||||
<input
|
||||
type="hidden"
|
||||
name="uuid"
|
||||
value="{{ ai.uuid }}"
|
||||
/>
|
||||
<input
|
||||
type="hidden"
|
||||
name="hidden"
|
||||
value="delete-additional-information"
|
||||
/>
|
||||
<button type="submit" class="btn btn-sm btn-ghost">
|
||||
{% include "components/schema_form.html" %}
|
||||
</div>
|
||||
{% if meta.can_edit %}
|
||||
<button
|
||||
class="btn btn-sm btn-ghost ml-2"
|
||||
@click="deleteItem(item.uuid)"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="16"
|
||||
@ -108,56 +153,20 @@
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
class="lucide lucide-minus"
|
||||
class="lucide lucide-trash"
|
||||
>
|
||||
<path d="M5 12h14" />
|
||||
<path d="M3 6h18" />
|
||||
<path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6" />
|
||||
<path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2" />
|
||||
</svg>
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
{% endif %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% if meta.can_edit %}
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>Delete all</td>
|
||||
<td>
|
||||
<form
|
||||
action="{% url 'package scenario detail' scenario.package.uuid scenario.uuid %}"
|
||||
method="post"
|
||||
>
|
||||
{% csrf_token %}
|
||||
<input
|
||||
type="hidden"
|
||||
name="hidden"
|
||||
value="delete-all-additional-information"
|
||||
/>
|
||||
<button type="submit" class="btn btn-sm btn-ghost">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="16"
|
||||
height="16"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
class="lucide lucide-trash"
|
||||
>
|
||||
<path d="M3 6h18" />
|
||||
<path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6" />
|
||||
<path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2" />
|
||||
</svg>
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user