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:
commit
e17d768ded
2 changed files with 27 additions and 84 deletions
|
@ -53,31 +53,10 @@ class SubEntry {
|
||||||
this.display();
|
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() {
|
startHandlingSubEntryRemoveButtonClicks() {
|
||||||
this.removeButtonElement.addEventListener("click", event => {
|
this.removeButtonElement.addEventListener("click", event => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
this.parentFormEntry.removeSubEntry(this);
|
this.markForDeletionAndHide();;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,6 +104,10 @@ class MultiEntryFormEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
get nbSubEntries() {
|
get nbSubEntries() {
|
||||||
|
return this.subEntries.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
get nbActiveSubEntries() {
|
||||||
return this.subEntries
|
return this.subEntries
|
||||||
.filter(subEntry =>
|
.filter(subEntry =>
|
||||||
!subEntry.subEntryElement.classList.contains("hidden")
|
!subEntry.subEntryElement.classList.contains("hidden")
|
||||||
|
@ -132,13 +115,6 @@ class MultiEntryFormEntry {
|
||||||
.length;
|
.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
get hasHiddenSubEntries() {
|
|
||||||
return this.subEntries
|
|
||||||
.findIndex(subEntry =>
|
|
||||||
subEntry.subEntryElement.classList.contains("hidden")
|
|
||||||
) >= 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
createInitialSubEntries() {
|
createInitialSubEntries() {
|
||||||
const subEntryElements = this.formEntryElement.querySelectorAll(".form-sub-entry");
|
const subEntryElements = this.formEntryElement.querySelectorAll(".form-sub-entry");
|
||||||
this.initialNbSubEntries = subEntryElements.length;
|
this.initialNbSubEntries = subEntryElements.length;
|
||||||
|
@ -153,25 +129,11 @@ class MultiEntryFormEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
createNewSubEntry() {
|
createNewSubEntry() {
|
||||||
if (this.nbSubEntries === this.maxNbSubEntries) {
|
if (this.nbActiveSubEntries === this.maxNbSubEntries) {
|
||||||
console.log(`The max. number of sub-entries has been reached (${this.maxNbSubEntries}).`);
|
console.log(`The max. number of sub-entries has been reached (${this.maxNbSubEntries}).`);
|
||||||
return;
|
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 newSubEntryIndex = this.nbSubEntries;
|
||||||
const newSubEntry = SubEntry.fromTemplate(
|
const newSubEntry = SubEntry.fromTemplate(
|
||||||
this.subEntryTemplateElement,
|
this.subEntryTemplateElement,
|
||||||
|
@ -188,29 +150,10 @@ class MultiEntryFormEntry {
|
||||||
// Increment Django's TOTAL_FORMS hidden form input
|
// Increment Django's TOTAL_FORMS hidden form input
|
||||||
this.totalFormsElement.value = (parseInt(this.totalFormsElement.value) + 1).toString();
|
this.totalFormsElement.value = (parseInt(this.totalFormsElement.value) + 1).toString();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
removeSubEntry(subEntry) {
|
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() {
|
startHandlingNewSubEntryButtonClicks() {
|
||||||
|
|
|
@ -10,14 +10,14 @@
|
||||||
{% for field in form.hidden_fields %}
|
{% for field in form.hidden_fields %}
|
||||||
{{field}}
|
{{field}}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<button type=button" class="remove-button"></button>
|
<button type=button class="remove-button"></button>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<div class="form-sub-entry-template">
|
<div class="form-sub-entry-template">
|
||||||
{% for field in form.visible_fields %}
|
{% for field in formset.empty_form.visible_fields %}
|
||||||
{{field}}
|
{{field}}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% for field in form.hidden_fields %}
|
{% for field in formset.empty_form.hidden_fields %}
|
||||||
{{field}}
|
{{field}}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<button type="button" class="remove-button"></button>
|
<button type="button" class="remove-button"></button>
|
||||||
|
|
Loading…
Reference in a new issue