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

replace global function by stimulus controller

ulferts5023ce9923b8
diff --git a/app/views/work_packages/moves/new.html.erb b/app/views/work_packages/moves/new.html.erb
index cfdccb9f6e3..49475c237a8 100644
--- a/app/views/work_packages/moves/new.html.erb
+++ b/app/views/work_packages/moves/new.html.erb
@@ -44,7 +44,11 @@ See COPYRIGHT and LICENSE files for more details.
<%= styled_form_tag({action: 'create'},
id: 'move_form',
- data: { 'refresh-url': url_for(action: 'new'), 'input-selector': '[name="new_project_id"],[name="new_type_id"]' },
+ data: {
+ 'controller': 'refresh-on-form-changes',
+ 'refresh-on-form-changes-target': 'form',
+ 'refresh-on-form-changes-refresh-url-value': url_for(action: 'new')
+ },
class: '-wide-labels augment--refresh-on-form-changes') do %>
<% @work_packages.each do |wp| %>
<%= hidden_field_tag 'ids[]', wp.id %>
@@ -64,6 +68,7 @@ See COPYRIGHT and LICENSE files for more details.
name: 'new_project_id',
value: @target_project,
appendTo: 'body',
+ hiddenFieldAction: 'change->refresh-on-form-changes#triggerReload'
},
id: 'new_project_id',
class: 'remote-field--input',
@@ -78,7 +83,10 @@ See COPYRIGHT and LICENSE files for more details.
<div class="form--field-container">
<%= styled_select_tag("new_type_id",
content_tag('option', t(:label_no_change_option), value: '') +
- options_from_collection_for_select(@types, "id", "name", @target_type&.id)) %>
+ options_from_collection_for_select(@types, "id", "name", @target_type&.id),
+ data: {
+ 'action': 'change->refresh-on-form-changes#triggerReload'
+ }) %>
</div>
</div>
<div class="form--field">
diff --git a/frontend/src/app/core/setup/globals/global-listeners.ts b/frontend/src/app/core/setup/globals/global-listeners.ts
index 38d794b9d56..f2acbf8f325 100644
--- a/frontend/src/app/core/setup/globals/global-listeners.ts
+++ b/frontend/src/app/core/setup/globals/global-listeners.ts
@@ -26,7 +26,6 @@
// See COPYRIGHT and LICENSE files for more details.
//++
-import { refreshOnFormChanges } from 'core-app/core/setup/globals/global-listeners/refresh-on-form-changes';
import { registerRequestForConfirmation } from 'core-app/core/setup/globals/global-listeners/request-for-confirmation';
import { DeviceService } from 'core-app/core/browser/device.service';
import { scrollHeaderOnMobile } from 'core-app/core/setup/globals/global-listeners/top-menu-scroll';
@@ -101,8 +100,6 @@ export function initializeGlobalListeners():void {
return false;
});
- refreshOnFormChanges();
-
// Allow forms with [request-for-confirmation]
// to show the password confirmation dialog
registerRequestForConfirmation(jQuery);
diff --git a/frontend/src/app/core/setup/globals/global-listeners/refresh-on-form-changes.ts b/frontend/src/app/core/setup/globals/global-listeners/refresh-on-form-changes.ts
deleted file mode 100644
index d52ed0f6f00..00000000000
--- a/frontend/src/app/core/setup/globals/global-listeners/refresh-on-form-changes.ts
+++ /dev/null
@@ -1,55 +0,0 @@
-// -- copyright
-// OpenProject is an open source project management software.
-// Copyright (C) 2012-2023 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.
-//++
-
-export function refreshOnFormChanges() {
- const matches = document.querySelectorAll('.augment--refresh-on-form-changes');
-
- for (let i = 0; i < matches.length; i++) {
- const element = matches[i];
- const form = jQuery(element);
- const url = form.data('refreshUrl');
- const inputId = form.data('inputSelector');
-
- // TODO: Not all elements are available when we run here. The angular dynamic
- // components have to be instantiated first. This race condition should be removed
- // by changing how we refresh on form changes altogether.
- setTimeout(() => {
- form
- .find(inputId)
- .on('change', (e:Event) => {
- // The project selector also fires a change event when the
- // value is empty, but we don't want that here.
- const input = e.currentTarget as HTMLInputElement;
- if (input.name === 'new_project_id' && input.value === '') {
- return;
- }
- window.location.href = `${url}?${form.serialize()}`;
- });
- }, 100);
- }
-}
diff --git a/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.html b/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.html
index 1f21a397f63..43688e74934 100644
--- a/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.html
+++ b/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.html
@@ -47,5 +47,6 @@
#hiddenInput
[name]="name"
type="hidden"
+ [attr.data-action]="hiddenFieldAction"
[value]="plainValue"
/>
diff --git a/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.ts b/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.ts
index f35ef9712e2..efb988f0135 100644
--- a/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.ts
+++ b/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.ts
@@ -119,6 +119,8 @@ export class ProjectAutocompleterComponent implements ControlValueAccessor {
@Input() public isInlineContext = false;
+ @Input() public hiddenFieldAction = '';
+
dataLoaded = false;
projects:IProjectAutocompleteItem[];
diff --git a/frontend/src/stimulus/controllers/refresh-on-from-changes.controller.ts b/frontend/src/stimulus/controllers/refresh-on-from-changes.controller.ts
new file mode 100644
index 00000000000..922968ad4f4
--- /dev/null
+++ b/frontend/src/stimulus/controllers/refresh-on-from-changes.controller.ts
@@ -0,0 +1,53 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) 2023 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.
+ * ++
+ */
+
+import { ApplicationController } from 'stimulus-use';
+
+export default class RefreshOnFromChangesController extends ApplicationController {
+ static targets = [
+ 'form',
+ ];
+
+ static values = {
+ refreshUrl: String,
+ };
+
+ declare readonly formTarget:HTMLFormElement;
+
+ declare refreshUrlValue:string;
+
+ triggerReload():void {
+ // without the cast to undefined, the URLSearchParams constructor will
+ // not accept the FormData object.
+ const formData = new FormData(this.formTarget) as unknown as undefined;
+ const serializedFormData = new URLSearchParams(formData).toString();
+ window.location.href = `${this.refreshUrlValue}?${serializedFormData}`;
+ }
+}
diff --git a/frontend/src/stimulus/setup.ts b/frontend/src/stimulus/setup.ts
index 12d16fc6ff9..a85174aa279 100644
--- a/frontend/src/stimulus/setup.ts
+++ b/frontend/src/stimulus/setup.ts
@@ -3,6 +3,7 @@ import { environment } from '../environments/environment';
import { OpApplicationController } from './controllers/op-application.controller';
import OpDisableWhenCheckedController from './controllers/disable-when-checked.controller';
import PrintController from './controllers/print.controller';
+import RefreshOnFromChangesController from './controllers/refresh-on-from-changes.controller';
declare global {
interface Window {
@@ -21,3 +22,4 @@ instance.handleError = (error, message, detail) => {
instance.register('application', OpApplicationController);
instance.register('disable-when-checked', OpDisableWhenCheckedController);
instance.register('print', PrintController);
+instance.register('refresh-on-form-changes', RefreshOnFromChangesController);