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

allow integer arguments to be inserted for Date (spent on) filter

ulferts4e7164a454aa
diff --git a/frontend/src/app/features/reporting/reporting-page/functionality/reporting_engine/filters.js b/frontend/src/app/features/reporting/reporting-page/functionality/reporting_engine/filters.js
index c80dca10810..14a731127b1 100644
--- a/frontend/src/app/features/reporting/reporting-page/functionality/reporting_engine/filters.js
+++ b/frontend/src/app/features/reporting/reporting-page/functionality/reporting_engine/filters.js
@@ -172,28 +172,14 @@ Reporting.Filters = function($){
};
var operator_changed = function (field, select) {
- var option_tag, arity, first;
+ var option_tag, arity, forcedType;
if (select === null) {
return;
}
- first = false;
- if (!select.attr("data-first")) {
- first = true;
- $(select).attr("data-first", "false");
- }
option_tag = select.find('option[value="' + select.val() + '"]');
arity = parseInt(option_tag.attr("data-arity"));
- change_argument_visibility(field, arity);
- if (option_tag.attr("data-forced")) {
- force_type(option_tag, first);
- }
- };
-
- // Overwrite to customize input enforcements.
- // option: 'option' HTMLElement
- // first: Boolean indicating whether the operator changed for the first time
- var force_type = function (option, first) {
- return true;
+ forcedType = option_tag.attr("data-forced");
+ change_argument_visibility(field, arity, forcedType);
};
var value_changed = function (field) {
@@ -210,20 +196,37 @@ Reporting.Filters = function($){
}
};
- var change_argument_visibility = function (field, arg_nr) {
- var params, i;
- params = [$('#' + field + '_arg_1'), $('#' + field + '_arg_2')];
-
- for (i = 0; i < 2; i += 1) {
- if (params[i] !== null) {
- if (arg_nr >= (i + 1) || arg_nr <= (-1 - i)) {
- params[i].show();
- params[i].children().show();
- } else {
- params[i].hide();
- params[i].children().hide();
- }
- }
+ // 'integers' currently seems to be the only forced typed.
+ // See lib/report/operator.rb
+ var knownForcedTypes = ['integers'];
+
+ // Hides/Displays the various inputs for the filter arguments.
+ // Those fields are already completely rendered and are then displayed/hidden
+ // depending on the arity of the operator.
+ // argNr (the arity) apparently can be 0, 1, 2 and -1. -1 seems to be used for an arity of 1.
+ var change_argument_visibility = function (fieldName, argNr, forcedType) {
+ const fields = [
+ ['#' + fieldName + '_arg_1', [1, 2, -1].includes(argNr)],
+ ['#' + fieldName + '_arg_2', argNr === 2]
+ ];
+
+ for (const [fieldSelector, active] of fields) {
+ set_active_state(fieldSelector, active && !forcedType);
+ knownForcedTypes.forEach(function (knownForcedType) {
+ set_active_state(fieldSelector + '_' + knownForcedType, active && forcedType === knownForcedType);
+ });
+ }
+ };
+
+ var set_active_state = function(selector, active) {
+ const input = document.querySelector(selector);
+
+ if (!input) { return; }
+
+ input.hidden = !active;
+ for (const child of input.children) {
+ child.hidden = !active;
+ child.disabled = !active;
}
};
diff --git a/modules/reporting/lib/widget/filters/date.rb b/modules/reporting/lib/widget/filters/date.rb
index 1b283203c42..f22350b3bfd 100644
--- a/modules/reporting/lib/widget/filters/date.rb
+++ b/modules/reporting/lib/widget/filters/date.rb
@@ -30,20 +30,18 @@ class Widget::Filters::Date < Widget::Filters::Base
include AngularHelper
def render # rubocop:disable Metrics/AbcSize
- @calendar_headers_tags_included = true
-
name = "values[#{filter_class.underscore_name}][]"
id_prefix = "#{filter_class.underscore_name}_"
write(content_tag(:span, class: "advanced-filters--filter-value -binary") do
label1 = label_tag "#{id_prefix}arg_1_val",
- h(filter_class.label) + " " + I18n.t(:label_filter_value),
+ value_label,
class: "hidden-for-sighted"
arg1 = content_tag :span, id: "#{id_prefix}arg_1" do
text1 = angular_component_tag "opce-basic-single-date-picker",
inputs: {
- value: filter.values.first.to_s,
+ value: filter.operator.forced == :integers ? "" : filter.values.first.to_s,
inputId: "#{id_prefix}arg_1_val",
name:
}
@@ -51,20 +49,39 @@ class Widget::Filters::Date < Widget::Filters::Base
end
label2 = label_tag "#{id_prefix}arg_2_val",
- h(filter_class.label) + " " + I18n.t(:label_filter_value),
+ value_label,
class: "hidden-for-sighted"
arg2 = content_tag :span, id: "#{id_prefix}arg_2", class: "advanced-filters--filter-value2" do
text2 = angular_component_tag "opce-basic-single-date-picker",
inputs: {
- value: filter.values.second.to_s,
+ value: filter.operator.forced == :integers ? "" : filter.values.second.to_s,
inputId: "#{id_prefix}arg_2_val",
name: name.to_s
}
label2 + text2
end
- arg1 + arg2
+ arg3 = content_tag :span, id: "#{id_prefix}arg_1_integers", class: "advanced-filters--integer" do
+ label3 = label_tag "#{id_prefix}arg_1_integers_val",
+ value_label,
+ class: "hidden-for-sighted"
+
+ label3 + text_field_tag(name,
+ filter.operator.forced == :integers ? filter.values.first.to_s : "",
+ type: "number",
+ min: 0,
+ steps: 1,
+ class: "advanced-filters--text-field -slim",
+ id: "#{id_prefix}arg_1_integers_val",
+ "data-filter-name": filter_class.underscore_name)
+ end
+
+ arg1 + arg2 + arg3
end)
end
+
+ def value_label
+ "#{filter_class.label} #{I18n.t(:label_filter_value)}"
+ end
end
diff --git a/modules/reporting/spec/features/date_filter_spec.rb b/modules/reporting/spec/features/date_filter_spec.rb
new file mode 100644
index 00000000000..01153f918ba
--- /dev/null
+++ b/modules/reporting/spec/features/date_filter_spec.rb
@@ -0,0 +1,99 @@
+# frozen_string_literal: true
+
+#
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+require "spec_helper"
+
+RSpec.describe "Cost report date filter", :js do
+ let(:project) { create(:project) }
+
+ let!(:today_time_entry) { create(:time_entry, project: project, hours: 5) }
+ let!(:five_days_ago_time_entry) { create(:time_entry, project: project, hours: 10, spent_on: 5.days.ago) }
+ let!(:five_days_from_now_time_entry) { create(:time_entry, project: project, hours: 15, spent_on: 5.days.from_now) }
+
+ current_user { create(:admin) }
+
+ before do
+ visit cost_reports_path(project)
+ end
+
+ it "filters the time entries" do
+ # Remove filters not tested here but added by default
+ find_by_id("rm_box_user_id").click
+ find_by_id("rm_box_project_id").click
+
+ # 'Date (spent on)' filter is also selected by default
+ select "today", from: "operators[spent_on]"
+ click_link "Apply"
+
+ expect(page).to have_content(today_time_entry.hours)
+ expect(page).to have_content(today_time_entry.work_package.subject)
+ expect(page).to have_no_content(five_days_ago_time_entry.work_package.subject)
+ expect(page).to have_no_content(five_days_from_now_time_entry.work_package.subject)
+
+ select "<=", from: "operators[spent_on]"
+ fill_in "spent_on_arg_1_val", with: 4.days.ago.iso8601
+ click_link "Apply"
+
+ expect(page).to have_content(five_days_ago_time_entry.hours)
+ expect(page).to have_content(five_days_ago_time_entry.work_package.subject)
+ expect(page).to have_no_content(today_time_entry.work_package.subject)
+ expect(page).to have_no_content(five_days_from_now_time_entry.work_package.subject)
+
+ select "during the last days", from: "operators[spent_on]"
+ fill_in "spent_on_arg_1_integers_val", with: "5"
+ click_link "Apply"
+
+ expect(page).to have_content(five_days_ago_time_entry.hours)
+ expect(page).to have_content(five_days_ago_time_entry.work_package.subject)
+ expect(page).to have_content(today_time_entry.hours)
+ expect(page).to have_content(today_time_entry.work_package.subject)
+ expect(page).to have_no_content(five_days_from_now_time_entry.work_package.subject)
+
+ select ">=", from: "operators[spent_on]"
+ fill_in "spent_on_arg_1_val", with: 4.days.from_now.iso8601
+ click_link "Apply"
+
+ expect(page).to have_content(five_days_from_now_time_entry.hours)
+ expect(page).to have_content(five_days_from_now_time_entry.work_package.subject)
+ expect(page).to have_no_content(five_days_ago_time_entry.work_package.subject)
+ expect(page).to have_no_content(today_time_entry.work_package.subject)
+
+ select "between", from: "operators[spent_on]"
+ fill_in "spent_on_arg_1_val", with: 4.days.ago.iso8601
+ fill_in "spent_on_arg_2_val", with: 4.days.from_now.iso8601
+ click_link "Apply"
+
+ expect(page).to have_content(today_time_entry.hours)
+ expect(page).to have_content(today_time_entry.work_package.subject)
+ expect(page).to have_no_content(five_days_ago_time_entry.work_package.subject)
+ expect(page).to have_no_content(five_days_from_now_time_entry.work_package.subject)
+ end
+end