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

Implement JS for SortByConfigController

Klaus Zanders91e5ae3c1db0
diff --git a/app/components/queries/sort_by_component.html.erb b/app/components/queries/sort_by_component.html.erb
index 4499461d779..f871b760c85 100644
--- a/app/components/queries/sort_by_component.html.erb
+++ b/app/components/queries/sort_by_component.html.erb
@@ -1,5 +1,5 @@
<div data-controller="sort-by-config" data-application-target="dynamic">
- <%= hidden_field_tag :sortBy, current_orders, data: { "sort-by-config-target" => "sortByField"} %>
+ <%= text_field_tag :sortBy, current_orders, data: { "sort-by-config-target" => "sortByField"} %>
<%= render(Primer::OpenProject::FlexLayout.new) do |layout| %>
<% order_limit.times do |i| %>
<% layout.with_row do %>
diff --git a/app/components/queries/sort_by_field_component.html.erb b/app/components/queries/sort_by_field_component.html.erb
index 46292f564b2..30c76006684 100644
--- a/app/components/queries/sort_by_field_component.html.erb
+++ b/app/components/queries/sort_by_field_component.html.erb
@@ -1,11 +1,11 @@
<%= render(Primer::OpenProject::GridLayout.new(css_class: "op-configure-query-sort-form", tag: :div, data: { "sort-by-config-target" => "inputRow"}, style: (display? ? "" : "display:none;"))) do |grid| %>
<% grid.with_area("field") do %>
- <%= select_tag 'sort_field', select_options, include_blank: true, form: "none" %>
+ <%= select_tag 'sort_field', select_options, include_blank: true, form: "none", data: { action: "change->sort-by-config#fieldChanged"} %>
<% end %>
<% grid.with_area("direction") do %>
<%= render(Primer::Alpha::SegmentedControl.new("aria-label": "Sort order", hide_labels: true)) do |sort_buttons| %>
- <% sort_buttons.with_item(icon: "sort-asc", label: "sort ascending", selected: order_asc?) %>
- <% sort_buttons.with_item(icon: "sort-desc", label: "sort descending", selected: order_desc?) %>
+ <% sort_buttons.with_item(icon: "sort-asc", label: "sort ascending", selected: order_asc? , data: { direction: 'asc', action: "click:segmented-control#select click->sort-by-config#fieldChanged"}) %>
+ <% sort_buttons.with_item(icon: "sort-desc", label: "sort descending", selected: order_desc?, data: { direction: 'desc', action: "click:segmented-control#select click->sort-by-config#fieldChanged"}) %>
<% end %>
<% end %>
<% 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 2db6be6e045..04433d80a61 100644
--- a/frontend/src/stimulus/controllers/dynamic/sort-by-config.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/sort-by-config.controller.ts
@@ -29,19 +29,79 @@
*/
import { Controller } from '@hotwired/stimulus';
+import { compact } from 'lodash';
export default class SortByConfigController extends Controller {
static targets = [
'sortByField',
- 'inputRow'
+ 'inputRow',
];
declare readonly sortByFieldTarget:HTMLInputElement;
declare readonly inputRowTargets:HTMLElement[];
- connect(): void {
- console.log("Hello, we are connected!")
- console.log(`Current value of the field: ${this.sortByFieldTarget.value}`)
- console.log(this.inputRowTargets)
+ connect():void {
+ console.log('Hello, we are connected!');
+ console.log(`Current value of the field: ${this.sortByFieldTarget.value}`);
+ console.log(`Visible rows: ${this.visibleFieldCount()}`);
+ console.log(this.inputRowTargets);
+ console.log(this.buildSortJson());
+ this.setDirection(this.inputRowTargets[0], 'desc');
+}
+
+buildSortJson():string {
+ const filters = this.inputRowTargets.map((row) => {
+ const field = this.getSelectedField(row);
+ if (field) {
+ return [field, this.getSelectedDirection(row)];
+ }
+});
+
+ return JSON.stringify(compact(filters));
+}
+
+fieldChanged(event:Event):void {
+ const target = event.target as HTMLElement;
+ const row = target.closest('.op-configure-query-sort-form') as HTMLElement;
+
+ this.sortByFieldTarget.value = this.buildSortJson();
+}
+
+ visibleFieldCount():number {
+ return this.inputRowTargets.filter((row) => row.style.display !== 'none').length;
+ }
+
+ getSelectedField(row:HTMLElement):string|null {
+ const selectedField = row.querySelector('select[name="sort_field"]') as HTMLSelectElement;
+ return selectedField?.value || null;
+ }
+
+ getSelectedDirection(row:HTMLElement):string|null {
+ const selectedSegment = row.querySelector('li.SegmentedControl-item--selected > button');
+ return selectedSegment?.getAttribute('data-direction') || null;
+ }
+
+ unsetDirection(row:HTMLElement):void {
+ const segmentControls = row.querySelectorAll('li.SegmentedControl-item');
+ segmentControls.forEach((control) => {
+ control.classList.remove('SegmentedControl-item--selected');
+ control.querySelector('button')?.setAttribute('aria-current', 'false');
+ });
+ }
+
+ setDirection(row:HTMLElement, direction:string):void {
+ const segmentControls = row.querySelectorAll('li.SegmentedControl-item');
+
+ segmentControls.forEach((control) => {
+ const button = control.querySelector('button') as HTMLButtonElement;
+
+ if (button.getAttribute('data-direction') === direction) {
+ control.classList.add('SegmentedControl-item--selected');
+ button.setAttribute('aria-current', 'true');
+ } else {
+ control.classList.remove('SegmentedControl-item--selected');
+ button.setAttribute('aria-current', 'false');
+ }
+ });
}
}