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

Move the project autocompleter for custom action to the autocomplete decoration component in order to keep the logic of adding hidden field values when chosing a different option. Otherwise the project selection would never be saved. Unfortunately, there is some mapping neede as the the project autocompleter and the decorator deal with different types.

Henriette Darge627fe9123aba
diff --git a/app/views/augmented/_autocomplete_select_decoration.html.erb b/app/views/augmented/_autocomplete_select_decoration.html.erb
index 55bfe349705..29ffcb70c4d 100644
--- a/app/views/augmented/_autocomplete_select_decoration.html.erb
+++ b/app/views/augmented/_autocomplete_select_decoration.html.erb
@@ -5,7 +5,8 @@
"multiselect": multiple,
"input-name": input_name,
"input-id": input_id,
- options: JSON.dump(select_options)
+ "key": key,
+ options: JSON.dump(select_options),
} %>
<!-- Mapped values are being input as hidden fields here -->
diff --git a/app/views/custom_actions/_form.html.erb b/app/views/custom_actions/_form.html.erb
index 29c7b878146..48de9f82e7f 100644
--- a/app/views/custom_actions/_form.html.erb
+++ b/app/views/custom_actions/_form.html.erb
@@ -22,22 +22,13 @@
<div class="form--field-container">
<div class="form--select-container -middle">
- <% if condition.key.to_s == 'project' %>
- <%= angular_component_tag 'op-project-autocompleter',
- inputs: {
- multiple: true,
- apiFilters: [],
- name: input_name,
- labelForId: "custom_action_conditions_#{condition.key}"
- }
- %>
- <% else %>
- <%= render partial: 'augmented/autocomplete_select_decoration',
- locals: { input_name: input_name,
- input_id: "custom_action_conditions_#{condition.key}",
- select_options: select_options,
- multiple: true } %>
- <% end %>
+ <%= render partial: 'augmented/autocomplete_select_decoration',
+ locals: { input_name: input_name,
+ input_id: "custom_action_conditions_#{condition.key}",
+ select_options: select_options,
+ multiple: true,
+ key: condition.key.to_s
+ } %>
</div>
</div>
</div>
@@ -61,15 +52,17 @@
<% selected_values = action.values
select_options = action.allowed_values.map { |v| { label: v[:label], value: v[:value], selected: selected_values.include?(v[:value]) } } %>
- <div class="form--field-container">
- <div class="form--select-container -middle">
- <%= render partial: 'augmented/autocomplete_select_decoration',
- locals: { input_name: input_name,
- input_id: "custom_action_actions_#{action.key}",
- select_options: select_options,
- multiple: action.multi_value? } %>
- </div>
+ <div class="form--field-container">
+ <div class="form--select-container -middle">
+ <%= render partial: 'augmented/autocomplete_select_decoration',
+ locals: { input_name: input_name,
+ input_id: "custom_action_actions_#{action.key}",
+ select_options: select_options,
+ multiple: action.multi_value?,
+ key: action.key.to_s
+ } %>
</div>
+ </div>
<% elsif %i(date_property).include?(action.type) %>
<div class="form--field-container">
<% date = action.values.first %>
diff --git a/frontend/src/app/shared/components/autocompleter/autocomplete-select-decoration/autocomplete-select-decoration.component.ts b/frontend/src/app/shared/components/autocompleter/autocomplete-select-decoration/autocomplete-select-decoration.component.ts
index af1ffd98480..158188904a8 100644
--- a/frontend/src/app/shared/components/autocompleter/autocomplete-select-decoration/autocomplete-select-decoration.component.ts
+++ b/frontend/src/app/shared/components/autocompleter/autocomplete-select-decoration/autocomplete-select-decoration.component.ts
@@ -30,6 +30,10 @@ import {
Component, ElementRef, OnInit, ViewChild,
} from '@angular/core';
import { NgSelectComponent } from '@ng-select/ng-select';
+import { I18nService } from 'core-app/core/i18n/i18n.service';
+import { HalResource } from 'core-app/features/hal/resources/hal-resource';
+import { IProjectAutocompleterData } from 'core-app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component';
+import { IProjectAutocompleteItem } from 'core-app/shared/components/autocompleter/project-autocompleter/project-autocomplete-item';
type SelectItem = { label:string, value:string, selected?:boolean };
@@ -37,15 +41,26 @@ export const autocompleteSelectDecorationSelector = 'autocomplete-select-decorat
@Component({
template: `
- <ng-select [items]="options"
- [labelForId]="labelForId"
- bindLabel="label"
- [multiple]="multiselect"
- [virtualScroll]="true"
- [ngModel]="selected"
- appendTo="body"
- placeholder="Please select"
- (ngModelChange)="updateSelection($event)">
+ <op-project-autocompleter
+ *ngIf="isProjectField()"
+ [value]="currentProjectSelection"
+ [multiple]="multiselect"
+ [labelForId]="labelForId"
+ (valueChange)="updateProjectSelection($event)"
+ >
+ </op-project-autocompleter>
+
+ <ng-select
+ *ngIf="!isProjectField()"
+ [items]="options"
+ [labelForId]="labelForId"
+ bindLabel="label"
+ [multiple]="multiselect"
+ [virtualScroll]="true"
+ [ngModel]="selected"
+ appendTo="body"
+ [placeholder]="text.placeholder"
+ (ngModelChange)="updateSelection($event)">
<ng-template ng-option-tmp let-item="item" let-index="index">
{{ item.label }}
</ng-template>
@@ -64,24 +79,38 @@ export class AutocompleteSelectDecorationComponent implements OnInit {
/** Get the selected options */
public selected:SelectItem|SelectItem[];
+ /** Get the selected options especially fot the project autocompleter */
+ public currentProjectSelection:{ id:string, name:string }|{ id:string, name:string }[];
+
/** The input name we're syncing selections to */
private syncInputFieldName:string;
/** The input id used for label */
public labelForId:string;
- constructor(protected elementRef:ElementRef) {
+ /** The field key (e.g. status, type, or project) */
+ public key:string;
+
+ text = {
+ placeholder: this.I18n.t('js.placeholders.selection'),
+ };
+
+ constructor(
+ protected elementRef:ElementRef,
+ readonly I18n:I18nService,
+ ) {
}
- ngOnInit() {
- const element = this.elementRef.nativeElement;
+ ngOnInit():void {
+ const element = this.elementRef.nativeElement as HTMLElement;
// Set options
this.multiselect = element.dataset.multiselect === 'true';
this.labelForId = element.dataset.inputId!;
+ this.key = element.dataset.key!;
// Get the sync target
- this.syncInputFieldName = element.dataset.inputName;
+ this.syncInputFieldName = element.dataset.inputName!;
// Add Rails multiple identifier if multiselect
if (this.multiselect) {
this.syncInputFieldName += '[]';
@@ -89,10 +118,13 @@ export class AutocompleteSelectDecorationComponent implements OnInit {
// Prepare and build the options
// Expected array of objects with id, name, select
- const data:SelectItem[] = JSON.parse(element.dataset.options);
+ const data:SelectItem[] = JSON.parse(element.dataset.options!);
// Set initial selection
this.setInitialSelection(data);
+ if (this.isProjectField()) {
+ this.setInitialProjectSelection();
+ }
if (!this.multiselect) {
this.selected = (this.selected as SelectItem[])[0];
@@ -101,14 +133,14 @@ export class AutocompleteSelectDecorationComponent implements OnInit {
this.options = data;
// Unhide the parent
- element.parentElement.hidden = false;
+ element.parentElement!.hidden = false;
}
- setInitialSelection(data:SelectItem[]) {
+ setInitialSelection(data:SelectItem[]):void {
this.updateSelection(data.filter((element) => element.selected));
}
- updateSelection(items:SelectItem|SelectItem[]) {
+ updateSelection(items:SelectItem|SelectItem[]):void {
this.selected = items;
items = _.castArray(items);
@@ -118,18 +150,54 @@ export class AutocompleteSelectDecorationComponent implements OnInit {
});
}
- createSyncedField(value:string) {
+ createSyncedField(value:string):void {
const element = jQuery(this.elementRef.nativeElement);
element
.parent()
.append(`<input type="hidden" name="${this.syncInputFieldName || ''}" value="${value}" />`);
}
- removeCurrentSyncedFields() {
+ removeCurrentSyncedFields():void {
const element = jQuery(this.elementRef.nativeElement);
element
.parent()
.find(`input[name="${this.syncInputFieldName}"]`)
.remove();
}
+
+ updateProjectSelection(items:IProjectAutocompleteItem|IProjectAutocompleteItem[]):void {
+ items = _.castArray(items);
+ const mappedItems = items.map(item => {
+ const selectedItem:SelectItem = {
+ label: item.name,
+ value: item.id.toString(),
+ selected: true,
+ };
+
+ return selectedItem;
+ });
+
+ this.updateSelection(mappedItems);
+ }
+
+ setInitialProjectSelection():void {
+ const items = _.castArray(this.selected);
+ if (items.length === 0) return;
+
+ if (this.multiselect) {
+ this.currentProjectSelection = items.map((item:SelectItem) => ({
+ id: item.value,
+ name: item.label,
+ }));
+ } else {
+ this.currentProjectSelection = {
+ id: items[0].value,
+ name: items[0].label,
+ };
+ }
+ }
+
+ isProjectField():boolean {
+ return this.key === 'project';
+ }
}