Merge branch 'Aufinal/multientry' into 'master'

Simplifie la gestion des formulaires

Closes #17

See merge request klub-dev-ens/annuaire!12
This commit is contained in:
Martin Pepin 2021-01-08 13:49:13 +01:00
commit e17d768ded
2 changed files with 27 additions and 84 deletions

View file

@ -53,31 +53,10 @@ class SubEntry {
this.display();
}
reindex(newIndex) {
// Replace -<old index>- by -<new index>- in the values of the
// id and name attributes of every child of this sub-entry's element
const attributesToUpdate = ["id", "name"];
for (let childElement of this.subEntryElement.childNodes) {
for (let attribute of attributesToUpdate) {
if (!childElement.hasAttribute(attribute)) {
continue;
}
const newValue = childElement
.getAttribute(attribute)
.replace(`-${this.index}-`, `-${newIndex}-`);
childElement.setAttribute(attribute, newValue);
}
}
this.index = newIndex;
}
startHandlingSubEntryRemoveButtonClicks() {
this.removeButtonElement.addEventListener("click", event => {
event.preventDefault();
this.parentFormEntry.removeSubEntry(this);
this.markForDeletionAndHide();;
});
}
@ -125,6 +104,10 @@ class MultiEntryFormEntry {
}
get nbSubEntries() {
return this.subEntries.length;
}
get nbActiveSubEntries() {
return this.subEntries
.filter(subEntry =>
!subEntry.subEntryElement.classList.contains("hidden")
@ -132,13 +115,6 @@ class MultiEntryFormEntry {
.length;
}
get hasHiddenSubEntries() {
return this.subEntries
.findIndex(subEntry =>
subEntry.subEntryElement.classList.contains("hidden")
) >= 0;
}
createInitialSubEntries() {
const subEntryElements = this.formEntryElement.querySelectorAll(".form-sub-entry");
this.initialNbSubEntries = subEntryElements.length;
@ -153,64 +129,31 @@ class MultiEntryFormEntry {
}
createNewSubEntry() {
if (this.nbSubEntries === this.maxNbSubEntries) {
if (this.nbActiveSubEntries === this.maxNbSubEntries) {
console.log(`The max. number of sub-entries has been reached (${this.maxNbSubEntries}).`);
return;
}
// If there are hidden sub-entries,
// it means one of the initial sub-entry elements should be reset and displayed
if (this.hasHiddenSubEntries) {
// Reset and display the first hidden sub-entry
const existingSubEntry = this.subEntries.find(
subEntry => subEntry.subEntryElement.classList.contains("hidden")
);
existingSubEntry.resetAndDisplay();
}
// Otherwise, it means a new sub-entry (element) must be created
// and appended to the form entry element
else {
const newSubEntryIndex = this.nbSubEntries;
const newSubEntry = SubEntry.fromTemplate(
this.subEntryTemplateElement,
newSubEntryIndex,
this
);
this.subEntries.push(newSubEntry);
this.formEntryElement.insertBefore(
newSubEntry.subEntryElement,
this.newSubEntryButtonElement
);
this.subEntryTemplateElement,
newSubEntryIndex,
this
);
// Increment Django's TOTAL_FORMS hidden form input
this.totalFormsElement.value = (parseInt(this.totalFormsElement.value) + 1).toString();
}
this.subEntries.push(newSubEntry);
this.formEntryElement.insertBefore(
newSubEntry.subEntryElement,
this.newSubEntryButtonElement
);
// Increment Django's TOTAL_FORMS hidden form input
this.totalFormsElement.value = (parseInt(this.totalFormsElement.value) + 1).toString();
}
removeSubEntry(subEntry) {
// If the index of the sub-entry to remove is below the initial number of sub-entries,
// it means one of the initial sub entry elements should be marked for deletion and hidden
const removedSubEntryIndex = subEntry.index;
if (removedSubEntryIndex < this.initialNbSubEntries) {
subEntry.markForDeletionAndHide();
}
// Otherwise, delete the sub-entry (and remove its element from the DOM)
// and reindex other user-created sub-entries if need be
else {
subEntry.subEntryElement.remove();
this.subEntries.splice(subEntry.index, 1);
for (let index = this.removeSubEntryIndex; index < this.subEntries.length; index++) {
this.subEntries[index].reindex(index);
}
// Decrement Django's TOTAL_FORMS hidden form input
this.totalFormsElement.value = (parseInt(this.totalFormsElement.value) - 1).toString();
}
}
startHandlingNewSubEntryButtonClicks() {

View file

@ -1,4 +1,4 @@
{{ formset.non_field_errors }}
{{ formset.non_field_errors }}
{{ formset.management_form }}
{% for form in formset %}
{{ form.non_field_errors }}
@ -10,16 +10,16 @@
{% for field in form.hidden_fields %}
{{field}}
{% endfor %}
<button type=button" class="remove-button"></button>
<button type=button class="remove-button"></button>
</div>
{% endfor %}
<div class="form-sub-entry-template">
{% for field in form.visible_fields %}
{{field}}
{% endfor %}
{% for field in form.hidden_fields %}
{{field}}
{% endfor %}
{% for field in formset.empty_form.visible_fields %}
{{field}}
{% endfor %}
{% for field in formset.empty_form.hidden_fields %}
{{field}}
{% endfor %}
<button type="button" class="remove-button"></button>
</div>
<button type="button" class="add-sub-entry-button">{{ new_entry_text }}</button>