Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Auto-register event listeners at the stimulus controller level

Aaron Contreras30d7c96d2bdc
diff --git a/app/views/filters/_autocomplete.html.erb b/app/views/filters/_autocomplete.html.erb
index c33e527e4d3..e10bec695b7 100644
--- a/app/views/filters/_autocomplete.html.erb
+++ b/app/views/filters/_autocomplete.html.erb
@@ -4,7 +4,13 @@
inputName: 'value',
multiple: true,
multipleAsSeparateInputs: false,
- inputValue: filter.values
+ inputValue: filter.values,
+ hiddenFieldAction: "change->filter--filters-form#sendForm"
+ # Set on the template as there is a timing issue where the
+ # angular component is not yet ready in the DOM and hence
+ # the action can't be registered on the input field at the time
+ # of the #connect lifecycle hook of the filter--filters-form
+ # Stimulus controller.
}.merge(autocomplete_options.except(:component)),
class: 'form--field',
data: {
diff --git a/frontend/src/stimulus/controllers/dynamic/filter/filters-form.controller.ts b/frontend/src/stimulus/controllers/dynamic/filter/filters-form.controller.ts
index 4339650433e..23c7ae1e68d 100644
--- a/frontend/src/stimulus/controllers/dynamic/filter/filters-form.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/filter/filters-form.controller.ts
@@ -78,6 +78,56 @@ export default class FiltersFormController extends Controller {
connect() {
const urlParams = new URLSearchParams(window.location.search);
this.displayFiltersValue = urlParams.has('filters');
+
+ this.simpleValueTargets.forEach((simpleValue) => {
+ simpleValue.addEventListener('change', this.sendForm.bind(this));
+ });
+
+ this.operatorTargets.forEach((operator) => {
+ operator.addEventListener('change', this.sendForm.bind(this));
+ });
+
+ this.filterValueSelectTargets.forEach((select) => {
+ select.addEventListener('change', this.sendForm.bind(this));
+ });
+
+ this.filterValueContainerTargets.forEach((container) => {
+ container.addEventListener('change', this.sendForm.bind(this));
+ });
+
+ this.singleDayTargets.forEach((singleDay) => {
+ singleDay.addEventListener('change', this.sendForm.bind(this));
+ });
+
+ this.daysTargets.forEach((days) => {
+ days.addEventListener('change', this.sendForm.bind(this));
+ });
+ }
+
+ disconnect() {
+ this.simpleValueTargets.forEach((simpleValue) => {
+ simpleValue.removeEventListener('change', this.sendForm.bind(this));
+ });
+
+ this.operatorTargets.forEach((operator) => {
+ operator.removeEventListener('change', this.sendForm.bind(this));
+ });
+
+ this.filterValueSelectTargets.forEach((select) => {
+ select.removeEventListener('change', this.sendForm.bind(this));
+ });
+
+ this.filterValueContainerTargets.forEach((container) => {
+ container.removeEventListener('change', this.sendForm.bind(this));
+ });
+
+ this.singleDayTargets.forEach((singleDay) => {
+ singleDay.removeEventListener('change', this.sendForm.bind(this));
+ });
+
+ this.daysTargets.forEach((days) => {
+ days.removeEventListener('change', this.sendForm.bind(this));
+ });
}
toggleDisplayFilters() {
@@ -136,6 +186,8 @@ export default class FiltersFormController extends Controller {
this.disableSelection();
this.reselectPlaceholderOption();
this.setSpacerVisibility();
+
+ this.sendForm();
}
private disableSelection() {
@@ -154,6 +206,8 @@ export default class FiltersFormController extends Controller {
const removedFilterOption = selectOptions.find((option) => option.value === filterName);
removedFilterOption?.removeAttribute('disabled');
this.setSpacerVisibility();
+
+ this.sendForm();
}
private setSpacerVisibility() {