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

Finish the JS implementation for fields

Klaus Zandersfd88ce4d47ed
diff --git a/app/components/queries/sort_by_component.html.erb b/app/components/queries/sort_by_component.html.erb
index f871b760c85..87738f51732 100644
--- a/app/components/queries/sort_by_component.html.erb
+++ b/app/components/queries/sort_by_component.html.erb
@@ -1,6 +1,6 @@
<div data-controller="sort-by-config" data-application-target="dynamic">
- <%= text_field_tag :sortBy, current_orders, data: { "sort-by-config-target" => "sortByField"} %>
- <%= render(Primer::OpenProject::FlexLayout.new) do |layout| %>
+ <%= text_field_tag :sortBy, current_orders, data: { "sort-by-config-target" => "sortByField" } %>
+ <%= render(Primer::OpenProject::FlexLayout.new(data: { "sort-by-config-target" => "inputRowContainer" })) do |layout| %>
<% order_limit.times do |i| %>
<% layout.with_row do %>
<%= render(Queries::SortByFieldComponent.new(order: query.orders[i], available_orders:, index: i)) %>
diff --git a/app/components/queries/sort_by_field_component.html.erb b/app/components/queries/sort_by_field_component.html.erb
index 5fe3cf8a58d..e9593db3d23 100644
--- a/app/components/queries/sort_by_field_component.html.erb
+++ b/app/components/queries/sort_by_field_component.html.erb
@@ -1,4 +1,4 @@
-<%= render(Primer::OpenProject::GridLayout.new(css_class: "op-configure-query-sort-form", tag: :div, data: { "sort-by-config-target" => "inputRow"})) do |grid| %>
+<%= render(Primer::OpenProject::GridLayout.new(css_class: "op-configure-query-sort-form", tag: :div, data: { "sort-by-config-target" => "inputRow", "index" => index })) do |grid| %>
<% grid.with_area("field") do %>
<%= select_tag 'sort_field', select_options, include_blank: true, form: "none", data: { action: "change->sort-by-config#fieldChanged"} %>
<% end %>
diff --git a/frontend/src/stimulus/controllers/dynamic/sort-by-config.controller.ts b/frontend/src/stimulus/controllers/dynamic/sort-by-config.controller.ts
index a471a1fef2c..0133f64512b 100644
--- a/frontend/src/stimulus/controllers/dynamic/sort-by-config.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/sort-by-config.controller.ts
@@ -35,51 +35,88 @@ export default class SortByConfigController extends Controller {
static targets = [
'sortByField',
'inputRow',
+ 'inputRowContainer',
];
declare readonly sortByFieldTarget:HTMLInputElement;
declare readonly inputRowTargets:HTMLElement[];
+ declare readonly inputRowContainerTarget:HTMLElement;
- connect():void { }
+ connect():void {
+ this.inputRowTargets.forEach((row, index) => {
+ const selectedField = this.getSelectedField(row);
+ const selectedDirection = this.getSelectedDirection(row);
-buildSortJson():string {
- const filters = this.inputRowTargets.map((row) => {
- const field = this.getSelectedField(row);
- if (field) { return [field, this.getSelectedDirection(row)]; }
- return null;
-});
+ if (!selectedField) {
+ this.hideRow(row);
+ } else if (!selectedDirection) {
+ this.setDirection(row, 'asc');
+ }
- return JSON.stringify(compact(filters));
-}
+ this.displayNewFieldSelectorIfNeeded();
+ this.disableOptions();
+ });
+ }
-fieldChanged(event:Event):void {
- const target = event.target as HTMLElement;
- const row = target.closest('.op-configure-query-sort-form') as HTMLElement;
+ buildSortJson():string {
+ const filters = this.inputRowTargets.map((row) => {
+ const field = this.getSelectedField(row);
+ if (field) { return [field, this.getSelectedDirection(row)]; }
+ return null;
+ });
- const selectedField = this.getSelectedField(row);
- const getSelectedDirection = this.getSelectedDirection(row);
+ return JSON.stringify(compact(filters));
+ }
- if (!selectedField) {
- // we have deselected the field, so we need to unset the direction, remove the row and move it to the end of the list
- // const lastRow = this.inputRowTargets[this.inputRowTargets.length - 1];
+ fieldChanged(event:Event):void {
+ const target = event.target as HTMLElement;
+ const row = target.closest('.op-configure-query-sort-form') as HTMLElement;
- this.unsetDirection(row);
+ const selectedField = this.getSelectedField(row);
+ const getSelectedDirection = this.getSelectedDirection(row);
+
+ if (!selectedField) {
+ // we have deselected the field, so we need to unset the direction, remove the row and move it to the end of the list
+ this.moveRowToBottom(row);
+ this.unsetDirection(row);
- // Unless we are the first row, hide the rows
- if (row !== this.inputRowTargets[0]) { this.hideRow(row); }
- } else if (!getSelectedDirection) {
+ if (this.visibleFieldCount() > 1) {
+ this.hideRow(row);
+ }
+ } else if (!getSelectedDirection) {
// if we have selected a field but no direction, we default to ascending
this.setDirection(row, 'asc');
}
- // this.disableOptionInOtherSelects(row);
- this.sortByFieldTarget.value = this.buildSortJson();
-}
+ this.displayNewFieldSelectorIfNeeded();
+
+ this.disableOptions();
+ this.sortByFieldTarget.value = this.buildSortJson();
+ console.log({ visible: this.visibleFieldCount(), order: this.inputRowTargets.map((iRow) => iRow.dataset.index) });
+ }
+
+ displayNewFieldSelectorIfNeeded():void {
+ // figure out if we need to display a new input field
+ const nextHiddenRow = this.firstHiddenRow();
+
+ // we have not reached the maximum number of visible fields, and there is no visible empty field, display a new one
+ if (nextHiddenRow && !this.anyRowVisibleWithoutSelectedField()) {
+ this.showRow(nextHiddenRow);
+ }
+ }
visibleFieldCount():number {
return this.inputRowTargets.filter((row) => row.style.display !== 'none').length;
}
+ firstHiddenRow():HTMLElement|null {
+ return this.inputRowTargets.find((row) => row.style.display === 'none') || null;
+ }
+
+ anyRowVisibleWithoutSelectedField():boolean {
+ return this.inputRowTargets.some((row) => row.style.display !== 'none' && !this.getSelectedField(row));
+ }
+
getSelectedField(row:HTMLElement):string|null {
const selectedField = row.querySelector('select[name="sort_field"]') as HTMLSelectElement;
return selectedField?.value || null;
@@ -122,16 +159,28 @@ fieldChanged(event:Event):void {
row.style.display = 'none';
}
- disableOptionInOtherSelects(row:HTMLElement):void {
- const selectedField = this.getSelectedField(row);
-
- this.inputRowTargets.forEach((otherRow) => {
- if (otherRow !== row) {
- const otherSelect = otherRow.querySelector('select[name="sort_field"]') as HTMLSelectElement;
- otherSelect.querySelectorAll('option').forEach((option) => {
- option.disabled = option.value === selectedField;
- });
+ getAllSelectedFields(...excludedRows:HTMLElement[]):string[] {
+ return compact(this.inputRowTargets.map((row) => {
+ if (!excludedRows.includes(row)) {
+ return this.getSelectedField(row);
}
+ return null;
+ }));
+ }
+
+ moveRowToBottom(row:HTMLElement):void {
+ const surroundingDiv = row.parentElement as HTMLElement;
+ surroundingDiv.remove();
+ this.inputRowContainerTarget.append(surroundingDiv);
+ }
+
+ disableOptions():void {
+ this.inputRowTargets.forEach((row) => {
+ const selectedFieldsInOtherRows = this.getAllSelectedFields(row);
+ const otherSelect = row.querySelector('select[name="sort_field"]') as HTMLSelectElement;
+ otherSelect.querySelectorAll('option').forEach((option) => {
+ option.disabled = selectedFieldsInOtherRows.includes(option.value);
+ });
});
}
}