-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathsettings.component.ts
More file actions
370 lines (318 loc) · 12.2 KB
/
settings.component.ts
File metadata and controls
370 lines (318 loc) · 12.2 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray, AbstractControl } from '@angular/forms';
import { SettingsService } from '../../service/settings/settings.service';
import { GithubService, GithubReleaseInfo } from 'src/app/service/settings/github.service';
import { LoaderService } from 'src/app/service/loader/data-loader.service';
import { DataStore } from 'src/app/model/data-store';
import { ProgressDefinitions } from 'src/app/model/types';
import {
DialogInfo,
ModalMessageComponent,
} from 'src/app/component/modal-message/modal-message.component';
import { dateStr, deepCopy } from 'src/app/util/util';
import { MetaStore } from 'src/app/model/meta-store';
import { ProgressStore } from 'src/app/model/progress-store';
interface RemoteReleaseInfo {
tagName: string;
publishedAt?: Date;
changelogUrl?: string;
downloadUrl?: string;
}
interface RemoteReleaseCheck {
isChecking: boolean;
isNewerAvailable: boolean | null;
latestRelease: RemoteReleaseInfo | null;
latestCheckError: string | null;
}
@Component({
selector: 'app-settings',
templateUrl: './settings.component.html',
styleUrls: ['./settings.component.css'],
})
export class SettingsComponent implements OnInit {
meta!: MetaStore;
progressStore!: ProgressStore;
dataStoreMaxLevel!: number;
selectedMaxLevel!: number;
selectedMaxLevelCaption: String = '';
progressDefinitionsForm!: FormGroup;
tempProgressDefinitions: ProgressDefinitions = {};
editingProgressDefinitions: boolean = false;
remoteReleaseCheck: RemoteReleaseCheck = {
isChecking: false,
isNewerAvailable: null,
latestRelease: null,
latestCheckError: null,
};
private BROWSER_LOCALE = 'BROWSER';
dateFormats = [
{ label: 'Browser default', value: this.BROWSER_LOCALE },
{ value: 'en-GB' },
{ value: 'de' },
{ value: 'nl' },
{ value: 'en-US' },
{ value: 'sv' },
{ value: 'ja' },
{ value: 'hu' },
];
selectedDateFormat: string = this.BROWSER_LOCALE;
customTeamLabel: string = 'Team';
customGroupLabel: string = 'Group';
// GitHub release check state
checkingLatest: boolean = false;
latestReleaseInfo: GithubReleaseInfo | null = null;
latestCheckError: string | null = null;
isNewerAvailable: boolean | null = null;
latestDownloadUrl: string | null = null;
latestReleasePublishedDate: Date | null = null;
constructor(
private loader: LoaderService,
private settings: SettingsService,
private formBuilder: FormBuilder,
public modal: ModalMessageComponent,
private githubService: GithubService
) {}
ngOnInit(): void {
this.initialize();
this.initProgressDefinitionsForm();
this.loader
.load()
.then((dataStore: DataStore) => {
this.setYamlData(dataStore);
this.updateProgressDefinitionsForm();
})
.catch(err => {
this.modal.openDialog(new DialogInfo(err.message, 'An error occurred'));
if (err.hasOwnProperty('stack')) {
console.warn(err);
}
});
}
async checkForLatestRelease(): Promise<void> {
this.remoteReleaseCheck.isChecking = true;
this.remoteReleaseCheck.isNewerAvailable = null;
this.remoteReleaseCheck.latestRelease = null;
this.remoteReleaseCheck.latestCheckError = null;
try {
this.remoteReleaseCheck.latestRelease = await this.githubService.getLatestRelease();
} catch (err: any) {
console.warn('Error checking latest DSOMM release', err);
this.remoteReleaseCheck.latestCheckError = err?.message || 'Failed to check latest release';
return;
} finally {
this.remoteReleaseCheck.isChecking = false;
}
if (!this.remoteReleaseCheck.latestRelease) {
this.remoteReleaseCheck.latestCheckError =
'Error: No release information received from Github';
} else {
const remote = this.remoteReleaseCheck.latestRelease;
const remoteTag = (remote && remote.tagName?.replace(/^v/, '')) || '';
const localTag = this.meta?.activityMeta?.getDsommVersion()?.replace(/^v/, '') || '';
const remoteDate =
remote && remote.publishedAt && new Date(remote.publishedAt.toDateString());
const localDate = this.meta?.activityMeta?.getDsommReleaseDate();
// Prefer version tag comparison, fallback to published date comparison
let newer = false;
if (remoteTag && localTag && remoteDate && localDate) {
newer = remoteTag !== localTag || remoteDate > localDate;
} else {
newer = true; // Show download link if we cannot compare
// Build error message
let tmp: string[] = [];
if (!remoteTag) tmp.push('DSOMM model version');
if (!localTag) tmp.push('local model version');
if (!remoteDate) tmp.push('DSOMM model date');
if (!localDate) tmp.push('local model date');
this.remoteReleaseCheck.latestCheckError = `Could not determine ${tmp.join(', ')}`; // eslint-disable-line
console.warn('ERROR: ' + this.remoteReleaseCheck.latestCheckError);
}
this.remoteReleaseCheck.isNewerAvailable = newer;
}
}
initialize(): void {
this.selectedDateFormat = this.settings.getDateFormat() || this.BROWSER_LOCALE;
this.customTeamLabel = this.settings.getTeamLabel();
this.customGroupLabel = this.settings.getGroupLabel();
// Init dates
let date: Date = new Date();
date = new Date(date.getFullYear(), 0, 31); // 31 Jan current year
for (let format of this.dateFormats) {
if (format.value === this.BROWSER_LOCALE) {
format.label += ` (${dateStr(date)})`;
} else {
if (!format.label) format.label = dateStr(date, format.value);
}
}
}
setYamlData(dataStore: DataStore): void {
this.dataStoreMaxLevel = dataStore.getMaxLevel();
this.selectedMaxLevel = this.settings.getMaxLevel() || this.dataStoreMaxLevel;
this.updateMaxLevelCaption();
if (dataStore.progressStore) {
this.progressStore = dataStore.progressStore;
}
// Load progress definitions
if (dataStore.meta) {
this.meta = dataStore.meta;
this.tempProgressDefinitions = deepCopy(this.meta.progressDefinition);
}
}
onDateFormatChange(): void {
let value: any = this.selectedDateFormat == 'null' ? null : this.selectedDateFormat;
this.settings.setDateFormat(value);
}
onTeamLabelChange(): void {
this.settings.setTeamLabel(this.customTeamLabel);
}
onGroupLabelChange(): void {
this.settings.setGroupLabel(this.customGroupLabel);
}
onMaxLevelChange(value: number | null): void {
if (value == null) value = this.dataStoreMaxLevel;
if (value == this.dataStoreMaxLevel) {
this.settings.setMaxLevel(null);
} else {
this.settings.setMaxLevel(value);
}
this.selectedMaxLevel = value;
this.updateMaxLevelCaption();
}
// === Max Level ===
updateMaxLevelCaption(): void {
if (this.selectedMaxLevel == this.dataStoreMaxLevel) {
this.selectedMaxLevelCaption = 'All maturity levels';
} else {
if (this.selectedMaxLevel == 1) this.selectedMaxLevelCaption = `Maturity level 1 only`;
else this.selectedMaxLevelCaption = `Maturity levels 1-${this.selectedMaxLevel} only`;
}
}
// === Progress Definitions ===
private initProgressDefinitionsForm(): void {
this.progressDefinitionsForm = this.formBuilder.group({
definitions: this.formBuilder.array([]),
});
}
get definitionsFormArray(): FormArray {
return this.progressDefinitionsForm.get('definitions') as FormArray;
}
// Return the FormGroup for a specific index in the definitions FormArray.
getDefinitionGroup(index: number): FormGroup {
return this.definitionsFormArray.at(index) as FormGroup;
}
private updateProgressDefinitionsForm(): void {
this.definitionsFormArray.clear();
Object.entries(this.tempProgressDefinitions).forEach(([key, progDef], index) => {
this.definitionsFormArray.push(
this.formBuilder.group({
pid: [index],
key: [key],
score: [progDef.score * 100],
definition: [progDef.definition],
mandatory: progDef.score == 1 || progDef.score == 0,
})
);
});
}
addProgressDefinition(): void {
let index: number = this.definitionsFormArray.length - 1;
let score: number = this.getFormGroupValue(this.definitionsFormArray.at(index - 1), 'score');
score = Math.trunc((score + 100) / 2);
this.definitionsFormArray.insert(
index,
this.formBuilder.group({
pid: [-1], // -1 indicates a new item
key: [''],
score: [score],
definition: [''],
})
);
}
removeProgressDefinition(index: number): void {
this.definitionsFormArray.removeAt(index);
}
saveProgressDefinitions(): void {
// Validate form
if (this.progressDefinitionsForm.invalid) {
this.progressDefinitionsForm.markAllAsTouched();
this.modal.openDialog(
new DialogInfo(
'All definitions must have a name, and the score must be between 0% and 100%'
)
);
return;
}
// Get the original keys in order
const originalKeys = Object.keys(this.tempProgressDefinitions);
// Build new progress definitions from form data and identify changes
const newProgressDefinitions: ProgressDefinitions = {};
const renamedItems: Array<{ originalKey: string; newKey: string; pid: number }> = [];
this.definitionsFormArray.controls.forEach(control => {
const formGroup = control as FormGroup;
const pid = formGroup.get('pid')?.value;
const key = formGroup.get('key')?.value;
const score = formGroup.get('score')?.value / 100; // Convert from percentage back to decimal
const definition = formGroup.get('definition')?.value;
if (key && key.trim()) {
// Only add if key is not empty
newProgressDefinitions[key] = {
score: score,
definition: definition,
};
// Check if this is a renamed item
if (pid >= 0 && pid < originalKeys.length) {
const originalKey = originalKeys[pid];
if (originalKey !== key) {
renamedItems.push({
originalKey: originalKey,
newKey: key,
pid: pid,
});
}
}
}
});
// Log renamed items for debugging/tracking
if (renamedItems.length > 0) {
console.log('Renamed progress definitions:', renamedItems);
for (const item of renamedItems) {
console.log(`- PID ${item.pid}: "${item.originalKey}" renamed to "${item.newKey}"`);
this.progressStore.renameProgressTitle(item.originalKey, item.newKey);
}
}
// Sort the definitions by score in ascending order
this.tempProgressDefinitions = this.sortObjectByScore(newProgressDefinitions);
// Save the new progress definitions to MetaStore and localStorage
this.meta.saveProgressDefinition(this.tempProgressDefinitions);
// Reinitialize the ProgressStore with the new definitions
this.progressStore.init(this.tempProgressDefinitions);
// Save progress data to localStorage
this.progressStore.saveToLocalStorage();
this.editingProgressDefinitions = false;
this.updateProgressDefinitionsForm();
}
resetProgressDefinitions(): void {
this.tempProgressDefinitions = deepCopy(this.meta.progressDefinition);
this.editingProgressDefinitions = false;
this.updateProgressDefinitionsForm();
}
toggleProgressDefinitionsEdit(): void {
this.editingProgressDefinitions = !this.editingProgressDefinitions;
}
getFormGroupValue(control: AbstractControl, field: string): any {
return (control as FormGroup).get(field)?.value;
}
/**
* Sorts an object by the 'score' attribute of its values and returns a new object with the sorted order
* @param obj The object to sort (where values have a 'score' property)
* @returns A new object with entries sorted by score in ascending order
*/
sortObjectByScore<T extends { score: number }>(obj: { [key: string]: T }): { [key: string]: T } {
const sortedEntries = Object.entries(obj).sort(([, a], [, b]) => a.score - b.score);
// Convert back to object
return Object.fromEntries(sortedEntries);
}
dateFormat(date: Date | null | undefined): string {
return dateStr(date, this.settings?.getDateFormat());
}
}