Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure MultipleChooserPanel selecting unique objects #11855

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions client/src/components/InlinePanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@
return forms.length;
}

getActiveFormsIndex() {
const choiceIds = [];
const forms = $('> [data-inline-panel-child]', this.formsElt).not(

Check warning on line 186 in client/src/components/InlinePanel/index.js

View check run for this annotation

Codecov / codecov/patch

client/src/components/InlinePanel/index.js#L184-L186

Added lines #L184 - L186 were not covered by tests
'.deleted',
);
// eslint-disable-next-line func-names
forms.each(function () {
const id = $(this).attr('id');
const index = id.match(/\d+$/)[0];

Check warning on line 192 in client/src/components/InlinePanel/index.js

View check run for this annotation

Codecov / codecov/patch

client/src/components/InlinePanel/index.js#L190-L192

Added lines #L190 - L192 were not covered by tests

if (index !== undefined && typeof parseInt(index, 10) === 'number') {
choiceIds.push(index);

Check warning on line 195 in client/src/components/InlinePanel/index.js

View check run for this annotation

Codecov / codecov/patch

client/src/components/InlinePanel/index.js#L195

Added line #L195 was not covered by tests
}
});
return choiceIds;

Check warning on line 198 in client/src/components/InlinePanel/index.js

View check run for this annotation

Codecov / codecov/patch

client/src/components/InlinePanel/index.js#L198

Added line #L198 was not covered by tests
}

updateAddButtonState() {
if (this.opts.maxForms) {
const addButton = $('#' + this.opts.formsetPrefix + '-ADD');
Expand Down
27 changes: 27 additions & 0 deletions client/src/components/MultipleChooserPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@
),
);

const getChoiceSelectIds = () => {
const chooserIds = [];
const formsIndexes = this.getActiveFormsIndex();

Check warning on line 16 in client/src/components/MultipleChooserPanel/index.js

View check run for this annotation

Codecov / codecov/patch

client/src/components/MultipleChooserPanel/index.js#L14-L16

Added lines #L14 - L16 were not covered by tests

for (const formIndex of formsIndexes) {
const formPrefix = `${opts.formsetPrefix}-${formIndex}`;
const chooserFieldId = `${formPrefix}-${opts.chooserFieldName}`;
const chooserWidget = this.chooserWidgetFactory.getById(chooserFieldId);

Check warning on line 21 in client/src/components/MultipleChooserPanel/index.js

View check run for this annotation

Codecov / codecov/patch

client/src/components/MultipleChooserPanel/index.js#L18-L21

Added lines #L18 - L21 were not covered by tests

if (
chooserWidget !== null &&
chooserWidget.state !== null &&
chooserWidget.state.id !== null &&
typeof parseInt(chooserWidget.state.id, 10) === 'number'

Check warning on line 27 in client/src/components/MultipleChooserPanel/index.js

View check run for this annotation

Codecov / codecov/patch

client/src/components/MultipleChooserPanel/index.js#L24-L27

Added lines #L24 - L27 were not covered by tests
) {
chooserIds.push(chooserWidget.state.id);

Check warning on line 29 in client/src/components/MultipleChooserPanel/index.js

View check run for this annotation

Codecov / codecov/patch

client/src/components/MultipleChooserPanel/index.js#L29

Added line #L29 was not covered by tests
}
}
return chooserIds;

Check warning on line 32 in client/src/components/MultipleChooserPanel/index.js

View check run for this annotation

Codecov / codecov/patch

client/src/components/MultipleChooserPanel/index.js#L32

Added line #L32 was not covered by tests
};

const openModalButton = document.getElementById(
`${opts.formsetPrefix}-OPEN_MODAL`,
);
Expand All @@ -30,6 +51,12 @@
},
{ multiple: true },
);
if (opts.allowDuplicates === 'True') {
openModalButton.setAttribute(

Check warning on line 55 in client/src/components/MultipleChooserPanel/index.js

View check run for this annotation

Codecov / codecov/patch

client/src/components/MultipleChooserPanel/index.js#L55

Added line #L55 was not covered by tests
'chooserids',
getChoiceSelectIds().join(','),
);
}
});
}

Expand Down
24 changes: 24 additions & 0 deletions client/src/includes/chooserModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@
$('[data-multiple-choice-select]', containerElement).on('change', () => {
this.updateMultipleChoiceSubmitEnabledState(modal);
});

this.disabledDuplicateCheckboxes(modal);

Check warning on line 238 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L238

Added line #L238 was not covered by tests
}

updateMultipleChoiceSubmitEnabledState(modal) {
Expand All @@ -246,6 +248,28 @@
}
}

disabledDuplicateCheckboxes(modal) {
const openModalButton = modal.triggerElement;

Check warning on line 252 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L251-L252

Added lines #L251 - L252 were not covered by tests

if (openModalButton.hasAttribute('chooserids')) {
const selectedObjectIds = openModalButton

Check warning on line 255 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L255

Added line #L255 was not covered by tests
.getAttribute('chooserids')
.split(',')
.map(Number);

// eslint-disable-next-line func-names
$('[data-multiple-choice-select]', modal.body).each(function () {
const value = $(this).val();

Check warning on line 262 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L261-L262

Added lines #L261 - L262 were not covered by tests

if (selectedObjectIds.includes(parseInt(value, 10))) {
$(this).prop('disabled', true);
} else {
$(this).prop('disabled', false);

Check warning on line 267 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L265-L267

Added lines #L265 - L267 were not covered by tests
}
});
}
}

modalHasTabs(modal) {
return $('[data-tabs]', modal.body).length;
}
Expand Down
7 changes: 6 additions & 1 deletion wagtail/admin/panels/multiple_chooser_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@


class MultipleChooserPanel(InlinePanel):
def __init__(self, relation_name, chooser_field_name=None, **kwargs):
def __init__(
self, relation_name, chooser_field_name=None, allow_duplicates=True, **kwargs
):
if chooser_field_name is None:
raise ImproperlyConfigured(
"MultipleChooserPanel must specify a chooser_field_name argument"
)

self.chooser_field_name = chooser_field_name
self.allow_duplicates = allow_duplicates
super().__init__(relation_name, **kwargs)

def clone_kwargs(self):
kwargs = super().clone_kwargs()
kwargs["chooser_field_name"] = self.chooser_field_name
kwargs["allow_duplicates"] = self.allow_duplicates
return kwargs

class BoundPanel(InlinePanel.BoundPanel):
Expand All @@ -38,6 +42,7 @@ def __init__(self, *args, **kwargs):
def get_context_data(self, parent_context=None):
context = super().get_context_data(parent_context)
context["chooser_field_name"] = self.panel.chooser_field_name
context["allow_duplicates"] = self.panel.allow_duplicates
context[
"chooser_widget_definition"
] = self.chooser_widget_telepath_definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
canOrder: {% if can_order %}true{% else %}false{% endif %},
maxForms: {{ self.formset.max_num|unlocalize }},
chooserFieldName: "{{ chooser_field_name }}",
allowDuplicates: "{{ allow_duplicates }}" ,
});
})();
</script>
Expand Down