-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
wp-embedded-table.component.ts
202 lines (168 loc) · 6.62 KB
/
wp-embedded-table.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
import { AfterViewInit, Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
import {
WorkPackageViewTimelineService,
} from 'core-app/features/work-packages/routing/wp-view-base/view-services/wp-view-timeline.service';
import {
WorkPackageViewPaginationService,
} from 'core-app/features/work-packages/routing/wp-view-base/view-services/wp-view-pagination.service';
import { OpTableActionFactory } from 'core-app/features/work-packages/components/wp-table/table-actions/table-action';
import {
OpTableActionsService,
} from 'core-app/features/work-packages/components/wp-table/table-actions/table-actions.service';
import { QueryResource } from 'core-app/features/hal/resources/query-resource';
import {
WpTableConfigurationModalComponent,
} from 'core-app/features/work-packages/components/wp-table/configuration-modal/wp-table-configuration.modal';
import { OpModalService } from 'core-app/shared/components/modal/modal.service';
import {
WorkPackageEmbeddedBaseComponent,
} from 'core-app/features/work-packages/components/wp-table/embedded/wp-embedded-base.component';
import { QueryFormResource } from 'core-app/features/hal/resources/query-form-resource';
import { distinctUntilChanged, map, take, withLatestFrom } from 'rxjs/operators';
import { InjectField } from 'core-app/shared/helpers/angular/inject-field.decorator';
import {
KeepTabService,
} from 'core-app/features/work-packages/components/wp-single-view-tabs/keep-tab/keep-tab.service';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
import { firstValueFrom } from 'rxjs';
import { QueryRequestParams } from 'core-app/features/work-packages/components/wp-query/url-params-helper';
@Component({
selector: 'wp-embedded-table',
templateUrl: './wp-embedded-table.html',
})
export class WorkPackageEmbeddedTableComponent extends WorkPackageEmbeddedBaseComponent implements OnInit, AfterViewInit, OnDestroy {
@Input('queryId') public queryId?:string;
@Input('queryProps') public queryProps:Partial<QueryRequestParams> = {};
@Input() public tableActions:OpTableActionFactory[] = [];
@Input() public externalHeight = false;
/** Inform about loading errors */
@Output() public onError = new EventEmitter<string>();
/** Inform about loaded query */
@Output() public onQueryLoaded = new EventEmitter<QueryResource>();
@InjectField() apiv3Service:ApiV3Service;
@InjectField() opModalService:OpModalService;
@InjectField() tableActionsService:OpTableActionsService;
@InjectField() wpTableTimeline:WorkPackageViewTimelineService;
@InjectField() wpTablePagination:WorkPackageViewPaginationService;
@InjectField() keepTab:KeepTabService;
// Cache the form promise
private formPromise:Promise<QueryFormResource|undefined>|undefined;
// If the query was provided to use via the query space,
// use it to cache first loading
private loadedQuery:QueryResource|undefined;
ngOnInit() {
super.ngOnInit();
this.loadedQuery = this.querySpace.query.value;
}
ngAfterViewInit():void {
super.ngAfterViewInit();
// Provision embedded table actions
if (this.tableActions) {
this.tableActionsService.setActions(...this.tableActions);
}
// Reload results on changes to pagination (Regression #29845)
this.wpTablePagination
.updates$()
.pipe(
map((pagination) => [pagination.page, pagination.perPage]),
distinctUntilChanged(),
this.untilDestroyed(),
withLatestFrom(this.querySpace.query.values$()),
).subscribe(([_, query]) => {
const pagination = this.wpTablePagination.paginationObject;
const params = this.urlParamsHelper.buildV3GetQueryFromQueryResource(query, pagination);
this.loadingIndicator = firstValueFrom(
this
.wpListService
.loadQueryFromExisting(query, params, this.queryProjectScope),
)
.then((query) => this.initializeStates(query));
});
}
public async openConfigurationModal(onUpdated:() => void):Promise<void> {
await this.querySpace.query.valuesPromise();
this.opModalService
.show(WpTableConfigurationModalComponent, this.injector)
// Detach this component when the modal closes and pass along the query data
.subscribe((modal) => modal.onDataUpdated.subscribe(onUpdated));
}
protected initializeStates(query:QueryResource) {
super.initializeStates(query);
this.querySpace
.initialized
.values$()
.pipe(take(1))
.subscribe(() => {
this.showTablePagination = query.results.total > query.results.count;
this.setLoaded();
// Disable compact mode when timeline active
if (this.wpTableTimeline.isVisible) {
this.configuration = { ...this.configuration, compactTableStyle: false };
}
});
}
public loadQuery(visible = true, firstPage = false):Promise<QueryResource> {
if (this.loadedQuery) {
const query = this.loadedQuery;
this.loadedQuery = undefined;
this.initializeStates(query);
return Promise.resolve(query);
}
// HACK: Decrease loading time of queries when results are not needed.
// We should allow the backend to disable results embedding instead.
if (!this.configuration.tableVisible) {
this.queryProps.pageSize = 1;
// Also use a valid subset to ensure we get a valid response.
this.queryProps.valid_subset = true;
}
// Set first page
if (firstPage) {
this.queryProps.page = 1;
}
this.error = null;
const promise = this
.apiv3Service
.queries
.find(
this.queryProps,
this.queryId,
this.queryProjectScope,
)
.toPromise()
.then((query:QueryResource) => {
this.initializeStates(query);
this.onQueryLoaded.emit(query);
return query;
})
.catch((error) => {
this.error = this.I18n.t(
'js.error.embedded_table_loading',
{ message: _.get(error, 'message', error) },
);
this.onError.emit(error);
});
if (visible) {
this.loadingIndicator = promise;
}
return promise as Promise<QueryResource>;
}
handleWorkPackageClicked(event:{ workPackageId:string; double:boolean }) {
if (event.double) {
this.$state.go(
'work-packages.show',
{ workPackageId: event.workPackageId },
);
}
}
openStateLink(event:{ workPackageId:string; requestedState:'show'|'split' }) {
const params = {
workPackageId: event.workPackageId,
focus: true,
};
if (event.requestedState === 'split') {
this.keepTab.goCurrentDetailsState(params);
} else {
this.keepTab.goCurrentShowState(params);
}
}
}