/
editable-attribute-field.component.ts
234 lines (194 loc) · 7.48 KB
/
editable-attribute-field.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// -- copyright
// OpenProject is an open source project management software.
// Copyright (C) 2012-2024 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 {
HalResourceEditingService,
} from 'core-app/shared/components/fields/edit/services/hal-resource-editing.service';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Injector,
Input,
OnDestroy,
OnInit,
Optional,
ViewChild,
} from '@angular/core';
import { OPContextMenuService } from 'core-app/shared/components/op-context-menu/op-context-menu.service';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { getPosition } from 'core-app/shared/helpers/set-click-position/set-click-position';
import { EditFormComponent } from 'core-app/shared/components/fields/edit/edit-form/edit-form.component';
import { HalResource } from 'core-app/features/hal/resources/hal-resource';
import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin';
import { SchemaCacheService } from 'core-app/core/schemas/schema-cache.service';
import {
displayClassName,
DisplayFieldRenderer, displayTriggerLink,
editFieldContainerClass,
} from 'core-app/shared/components/fields/display/display-field-renderer';
import { States } from 'core-app/core/states/states.service';
import { debugLog } from '../../../../helpers/debug_output';
import { hasSelectionWithin } from '../../../../helpers/selection-helpers';
import { EditFieldHandler } from 'core-app/shared/components/fields/edit/editing-portal/edit-field-handler';
import { SchemaResource } from 'core-app/features/hal/resources/schema-resource';
@Component({
selector: 'op-editable-attribute-field',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './editable-attribute-field.component.html',
})
export class EditableAttributeFieldComponent extends UntilDestroyedMixin implements OnInit, OnDestroy {
@Input() public fieldName:string;
@Input() public resource:HalResource;
@Input() public wrapperClasses?:string;
@Input() public displayFieldOptions:{ [key:string]:unknown } = {};
@Input() public isDropTarget?:boolean = false;
@ViewChild('displayContainer', { static: true }) readonly displayContainer:ElementRef<HTMLElement>;
@ViewChild('editContainer', { static: true }) readonly editContainer:ElementRef<HTMLElement>;
public fieldRenderer:DisplayFieldRenderer;
public editFieldContainerClass = editFieldContainerClass;
public active = false;
private $element:JQuery;
public destroyed = false;
constructor(
protected states:States,
protected injector:Injector,
protected elementRef:ElementRef,
protected opContextMenu:OPContextMenuService,
protected halEditing:HalResourceEditingService,
protected schemaCache:SchemaCacheService,
// Get parent field group from injector if we're in a form
@Optional() protected editForm:EditFormComponent,
protected cdRef:ChangeDetectorRef,
protected I18n:I18nService,
) {
super();
}
public setActive(active = true):void {
this.active = active;
if (!this.componentDestroyed) {
this.cdRef.detectChanges();
}
}
public ngOnInit():void {
this.fieldRenderer = new DisplayFieldRenderer(this.injector, 'single-view', this.displayFieldOptions);
this.$element = jQuery<HTMLElement>(this.elementRef.nativeElement);
// Register on the form if we're in an editable context
this.editForm?.register(this);
this.halEditing
.temporaryEditResource(this.resource)
.values$()
.pipe(
this.untilDestroyed(),
)
.subscribe((resource) => {
this.resource = resource;
this.render();
});
}
// Open the field when its closed and relay drag & drop events to it.
public startDragActivation(event:DragEvent):void {
if (!this.isDropTarget || !this.isEditable || this.active) {
return;
}
this.handleUserActivate(null);
event.preventDefault();
}
public render():void {
const el = this.fieldRenderer.render(this.resource, this.fieldName, null);
this.displayContainer.nativeElement.innerHTML = '';
this.displayContainer.nativeElement.appendChild(el);
}
public deactivate(focus = false):void {
this.editContainer.nativeElement.innerHTML = '';
this.editContainer.nativeElement.hidden = true;
this.setActive(false);
if (focus) {
setTimeout(() => this.$element.find(`.${displayClassName}`).focus(), 20);
}
}
public get isEditable():boolean {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
return !!(this.editForm && this.schema.isAttributeEditable(this.fieldName));
}
public activateIfEditable(event:MouseEvent|KeyboardEvent):boolean {
// Ignore selections
if (hasSelectionWithin(event.target as HTMLElement)) {
debugLog(`Not activating ${this.fieldName} because of active selection within`);
return true;
}
// Skip activation if the user clicked on a link or within a macro
const target = jQuery(event.target as HTMLElement);
if (target.closest(`a:not(.${displayTriggerLink}),macro`, this.displayContainer.nativeElement).length > 0) {
return true;
}
this.handleUserActivate(event);
this.opContextMenu.close();
event.preventDefault();
event.stopImmediatePropagation();
return false;
}
public activateOnForm(noWarnings = false):Promise<void|EditFieldHandler> {
// Activate the field
this.setActive(true);
return this.editForm
.activate(this.fieldName, noWarnings)
.catch(() => this.deactivate(true));
}
public handleUserActivate(evt:MouseEvent|KeyboardEvent|null):boolean {
if (!this.isEditable) {
return false;
}
let positionOffset = 0;
// This can be both a direct click as well as a "click" via keyboard, e.g. the <Enter> key.
if (evt?.type === 'click') {
// Get the position where the user clicked.
positionOffset = getPosition(evt);
}
void this.activateOnForm()
.then((handler) => {
if (!handler) {
return;
}
handler.$onUserActivate.next();
handler.focus(positionOffset);
});
return false;
}
public reset():void {
this.render();
this.deactivate();
}
private get schema() {
if (this.halEditing.typedState(this.resource).hasValue()) {
const val = this.halEditing.typedState(this.resource).value as { schema:SchemaResource };
return val.schema;
}
return this.schemaCache.of(this.resource);
}
}