Refactor dashboard state duplicate fix.
This commit is contained in:
parent
fd70520801
commit
260772ce55
@ -258,61 +258,54 @@ export class ManageDashboardStatesDialogComponent
|
|||||||
}
|
}
|
||||||
|
|
||||||
duplicateState($event: Event, state: DashboardStateInfo) {
|
duplicateState($event: Event, state: DashboardStateInfo) {
|
||||||
const originalState = state;
|
const suffix = ` - ${this.translate.instant('action.copy')} `;
|
||||||
const newStateName = this.getNextDuplicatedName(state.name);
|
let counter = 0;
|
||||||
if (newStateName) {
|
const maxAttempts = 1000;
|
||||||
const newStateId = newStateName.toLowerCase().replace(/\W/g, '_');
|
|
||||||
if (this.states[newStateId]) {
|
while (counter++ < maxAttempts) {
|
||||||
this.stateNames.add(newStateName);
|
const candidateName = `${state.name}${suffix}${counter}`;
|
||||||
this.duplicateState(null, state);
|
if (this.stateNames.has(candidateName)) continue;
|
||||||
return;
|
|
||||||
|
const candidateId = candidateName.toLowerCase().replace(/\W/g, '_');
|
||||||
|
if (this.states[candidateId]) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
const duplicatedStates = deepClone(originalState);
|
|
||||||
|
const originalState = state;
|
||||||
|
const duplicatedState = deepClone(originalState);
|
||||||
const duplicatedWidgets = deepClone(this.widgets);
|
const duplicatedWidgets = deepClone(this.widgets);
|
||||||
const mainWidgets = {};
|
const mainWidgets = {};
|
||||||
const rightWidgets = {};
|
const rightWidgets = {};
|
||||||
duplicatedStates.id = newStateId;
|
duplicatedState.id = candidateId;
|
||||||
duplicatedStates.name = newStateName;
|
duplicatedState.name = candidateName;
|
||||||
duplicatedStates.root = false;
|
duplicatedState.root = false;
|
||||||
this.stateNames.add(duplicatedStates.name);
|
this.stateNames.add(duplicatedState.name);
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(duplicatedStates.layouts.main.widgets)) {
|
for (const [key, value] of Object.entries(duplicatedState.layouts.main.widgets)) {
|
||||||
const guid = this.utils.guid();
|
const guid = this.utils.guid();
|
||||||
mainWidgets[guid] = value;
|
mainWidgets[guid] = value;
|
||||||
duplicatedWidgets[guid] = this.widgets[key];
|
duplicatedWidgets[guid] = this.widgets[key];
|
||||||
duplicatedWidgets[guid].id = guid;
|
duplicatedWidgets[guid].id = guid;
|
||||||
}
|
}
|
||||||
duplicatedStates.layouts.main.widgets = mainWidgets;
|
duplicatedState.layouts.main.widgets = mainWidgets;
|
||||||
|
|
||||||
if (isDefined(duplicatedStates.layouts?.right)) {
|
if (isDefined(duplicatedState.layouts?.right)) {
|
||||||
for (const [key, value] of Object.entries(duplicatedStates.layouts.right.widgets)) {
|
for (const [key, value] of Object.entries(duplicatedState.layouts.right.widgets)) {
|
||||||
const guid = this.utils.guid();
|
const guid = this.utils.guid();
|
||||||
rightWidgets[guid] = value;
|
rightWidgets[guid] = value;
|
||||||
duplicatedWidgets[guid] = this.widgets[key];
|
duplicatedWidgets[guid] = this.widgets[key];
|
||||||
duplicatedWidgets[guid].id = guid;
|
duplicatedWidgets[guid].id = guid;
|
||||||
}
|
}
|
||||||
duplicatedStates.layouts.right.widgets = rightWidgets;
|
duplicatedState.layouts.right.widgets = rightWidgets;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.states[duplicatedStates.id] = duplicatedStates;
|
this.states[duplicatedState.id] = duplicatedState;
|
||||||
this.widgets = duplicatedWidgets;
|
this.widgets = duplicatedWidgets;
|
||||||
this.onStatesUpdated();
|
this.onStatesUpdated();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private getNextDuplicatedName(stateName: string): string {
|
|
||||||
const suffix = ` - ${this.translate.instant('action.copy')} `;
|
|
||||||
let counter = 0;
|
|
||||||
while (++counter < Number.MAX_SAFE_INTEGER) {
|
|
||||||
const newName = `${stateName}${suffix}${counter}`;
|
|
||||||
if (!this.stateNames.has(newName)) {
|
|
||||||
return newName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private onStatesUpdated() {
|
private onStatesUpdated() {
|
||||||
this.statesFormGroup.markAsDirty();
|
this.statesFormGroup.markAsDirty();
|
||||||
this.updateData(true);
|
this.updateData(true);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user