Implement radio flag type

This commit is contained in:
nemunaire 2021-11-13 00:58:03 +01:00
parent 41565729fd
commit 49664c3dfe
2 changed files with 64 additions and 44 deletions

View file

@ -145,7 +145,7 @@ func buildKeyFlag(exercice fic.Exercice, flag ExerciceFlag, flagline int, defaul
}) })
f = &fl f = &fl
if len(flag.Choice) > 0 || flag.Type == "ucq" { if len(flag.Choice) > 0 || (flag.Type == "ucq" || flag.Type == "radio") {
// Import choices // Import choices
hasOne := false hasOne := false
@ -241,10 +241,12 @@ func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nli
flag.Type = "vector" flag.Type = "vector"
case "ucq": case "ucq":
flag.Type = "ucq" flag.Type = "ucq"
case "radio":
flag.Type = "radio"
case "mcq": case "mcq":
flag.Type = "mcq" flag.Type = "mcq"
default: default:
errs = append(errs, fmt.Sprintf("%q: flag #%d: invalid type of flag: should be 'key', 'number', 'text', 'mcq', 'ucq' or 'vector'.", path.Base(exercice.Path), nline+1)) errs = append(errs, fmt.Sprintf("%q: flag #%d: invalid type of flag: should be 'key', 'number', 'text', 'mcq', 'ucq', 'radio' or 'vector'.", path.Base(exercice.Path), nline+1))
return return
} }
@ -258,7 +260,7 @@ func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nli
} }
} }
if flag.Type == "key" || strings.HasPrefix(flag.Type, "number") || flag.Type == "text" || flag.Type == "ucq" || flag.Type == "vector" { if flag.Type == "key" || strings.HasPrefix(flag.Type, "number") || flag.Type == "text" || flag.Type == "ucq" || flag.Type == "radio" || flag.Type == "vector" {
addedFlag, choices, berrs := buildKeyFlag(exercice, flag, nline+1, "Flag") addedFlag, choices, berrs := buildKeyFlag(exercice, flag, nline+1, "Flag")
if len(berrs) > 0 { if len(berrs) > 0 {
errs = append(errs, berrs...) errs = append(errs, berrs...)

View file

@ -101,8 +101,8 @@
{/if} {/if}
{#if !flag.found} {#if !flag.found}
{#each values as v, index} {#each values as v, index}
<div class="input-group" class:mt-1={index != 0}> {#if !flag.choices}
{#if !flag.choices} <div class="input-group" class:mt-1={index != 0}>
{#if flag.type == 'number'} {#if flag.type == 'number'}
<input <input
type="number" type="number"
@ -136,45 +136,63 @@
title="{flag.placeholder}" title="{flag.placeholder}"
></textarea> ></textarea>
{/if} {/if}
{:else} {#if flag.unit}
<select <span class="input-group-text">{flag.unit}</span>
class="form-select" {/if}
id="sol_{flag.type}{flag.id}_{index}" {#if flag.choices_cost > 0}
bind:value={values[index]} <Button
> color="success"
{#each Object.keys(flag.choices) as l} type="button"
<option value={l}>{flag.choices[l]}</option> on:click={wantchoices}
{/each} disabled={wcsubmitted}
</select> title="Cliquez pour échanger ce champ de texte par une liste de choix. L'opération vous coûtera {flag.choices_cost * $settings.wchoiceCurrentCoefficient} points."
{/if} >
{#if flag.choices_cost > 0} {#if wcsubmitted}
<Button <Spinner size="sm" class="me-2" />
color="success" {/if}
type="button" <Icon name="tasks" />
on:click={wantchoices} Liste de propositions ({flag.choices_cost * $settings.wchoiceCurrentCoefficient} {flag.choices_cost * $settings.wchoiceCurrentCoefficient===1?"point":"points"})
disabled={wcsubmitted} </Button>
title="Cliquez pour échanger ce champ de texte par une liste de choix. L'opération vous coûtera {flag.choices_cost * $settings.wchoiceCurrentCoefficient} points." {:else if flag.separator && !flag.nb_lines && index == values.length - 1}
> <Button
{#if wcsubmitted} color="success"
<Spinner size="sm" class="me-2" /> type="button"
{/if} title="Ajouter un élément."
<Icon name="tasks" /> on:click={addItem}
Liste de propositions ({flag.choices_cost * $settings.wchoiceCurrentCoefficient} {flag.choices_cost * $settings.wchoiceCurrentCoefficient===1?"point":"points"}) >
</Button> <Icon name="plus" />
{:else if flag.separator && !flag.nb_lines && index == values.length - 1} </Button>
<Button {/if}
color="success" </div>
type="button" {:else if flag.type == 'radio'}
title="Ajouter un élément." {#each Object.keys(flag.choices) as l, i}
on:click={addItem} <div class="form-check">
> <input
<Icon name="plus" /> id="sol_{flag.type}{flag.id}_{index}_{i}"
</Button> type="radio"
{/if} value={l}
{#if flag.unit} bind:group={values[index]}
<span class="input-group-text">{flag.unit}</span> class="form-check-input"
{/if} >
</div> <label
class="form-check-label"
for="sol_{flag.type}{flag.id}_{index}_{i}"
>
{flag.choices[l]}
</label>
</div>
{/each}
{:else}
<select
class="form-select"
id="sol_{flag.type}{flag.id}_{index}"
bind:value={values[index]}
>
{#each Object.keys(flag.choices) as l}
<option value={l}>{flag.choices[l]}</option>
{/each}
</select>
{/if}
{/each} {/each}
{#if flag.help} {#if flag.help}
<small class="form-text text-muted">{flag.help}</small> <small class="form-text text-muted">{flag.help}</small>