mirror of
https://github.com/nasa/openmct.git
synced 2024-12-25 07:41:06 +00:00
[Duplicate Action] Fix Display Layout unwanted duplication issue (#3591)
* WIP: refactoring legacy dulicate action * WIP: debugging duplicate duplicates... * WIP: fixed duplicate duplicates issue * added unit tests * removing old legacy copyaction and renaming duplicate action * removing fdescribe * trying to see if a done callback fixes testing issues * fixed tests * testing autoflow tests on server * tweaked autoflow tests to stop failing * minor updates for new 3 dot menu * WIP bug fixing * WIP debugging * WIP more debuggin * WIP using parent namespace for all duped objs * WIP * WIP ; ; * cleaning up debugging code * fixed linting issues * fixed layout configuration items issue Co-authored-by: Deep Tailor <deep.j.tailor@nasa.gov>
This commit is contained in:
parent
ce8c31cfa4
commit
5eb6c15959
@ -79,8 +79,9 @@ export default class DuplicateTask {
|
|||||||
*/
|
*/
|
||||||
async buildDuplicationPlan() {
|
async buildDuplicationPlan() {
|
||||||
let domainObjectClone = await this.duplicateObject(this.domainObject);
|
let domainObjectClone = await this.duplicateObject(this.domainObject);
|
||||||
|
|
||||||
if (domainObjectClone !== this.domainObject) {
|
if (domainObjectClone !== this.domainObject) {
|
||||||
domainObjectClone.location = this.getId(this.parent);
|
domainObjectClone.location = this.getKeyString(this.parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.firstClone = domainObjectClone;
|
this.firstClone = domainObjectClone;
|
||||||
@ -159,36 +160,6 @@ export default class DuplicateTask {
|
|||||||
return originalObject;
|
return originalObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Update identifiers in a cloned object model (or part of
|
|
||||||
* a cloned object model) to reflect new identifiers after
|
|
||||||
* duplicating.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
rewriteIdentifiers(obj, idMap) {
|
|
||||||
function lookupValue(value) {
|
|
||||||
return (typeof value === 'string' && idMap[value]) || value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(obj)) {
|
|
||||||
obj.forEach((value, index) => {
|
|
||||||
obj[index] = lookupValue(value);
|
|
||||||
this.rewriteIdentifiers(obj[index], idMap);
|
|
||||||
});
|
|
||||||
} else if (obj && typeof obj === 'object') {
|
|
||||||
Object.keys(obj).forEach((key) => {
|
|
||||||
let value = obj[key];
|
|
||||||
obj[key] = lookupValue(value);
|
|
||||||
if (idMap[key]) {
|
|
||||||
delete obj[key];
|
|
||||||
obj[idMap[key]] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.rewriteIdentifiers(value, idMap);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given an array of objects composed by a parent, clone them, then
|
* Given an array of objects composed by a parent, clone them, then
|
||||||
* add them to the parent.
|
* add them to the parent.
|
||||||
@ -196,31 +167,67 @@ export default class DuplicateTask {
|
|||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
async duplicateComposees(clonedParent, composees = []) {
|
async duplicateComposees(clonedParent, composees = []) {
|
||||||
let idMap = {};
|
let idMappings = [];
|
||||||
let allComposeesDuplicated = composees.reduce(async (previousPromise, nextComposee) => {
|
let allComposeesDuplicated = composees.reduce(async (previousPromise, nextComposee) => {
|
||||||
await previousPromise;
|
await previousPromise;
|
||||||
|
|
||||||
let clonedComposee = await this.duplicateObject(nextComposee);
|
let clonedComposee = await this.duplicateObject(nextComposee);
|
||||||
idMap[this.getId(nextComposee)] = this.getId(clonedComposee);
|
|
||||||
this.composeChild(clonedComposee, clonedParent, clonedComposee !== nextComposee);
|
if (clonedComposee) {
|
||||||
|
idMappings.push({
|
||||||
|
newId: clonedComposee.identifier,
|
||||||
|
oldId: nextComposee.identifier
|
||||||
|
});
|
||||||
|
this.composeChild(clonedComposee, clonedParent, clonedComposee !== nextComposee);
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}, Promise.resolve());
|
}, Promise.resolve());
|
||||||
|
|
||||||
await allComposeesDuplicated;
|
await allComposeesDuplicated;
|
||||||
|
|
||||||
this.rewriteIdentifiers(clonedParent, idMap);
|
clonedParent = this.rewriteIdentifiers(clonedParent, idMappings);
|
||||||
this.clones.push(clonedParent);
|
this.clones.push(clonedParent);
|
||||||
|
|
||||||
return clonedParent;
|
return clonedParent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update identifiers in a cloned object model (or part of
|
||||||
|
* a cloned object model) to reflect new identifiers after
|
||||||
|
* duplicating.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
rewriteIdentifiers(clonedParent, childIdMappings) {
|
||||||
|
for (let { newId, oldId } of childIdMappings) {
|
||||||
|
let newIdKeyString = this.openmct.objects.makeKeyString(newId);
|
||||||
|
let oldIdKeyString = this.openmct.objects.makeKeyString(oldId);
|
||||||
|
|
||||||
|
// regex replace keystrings
|
||||||
|
clonedParent = JSON.stringify(clonedParent).replace(new RegExp(oldIdKeyString, 'g'), newIdKeyString);
|
||||||
|
|
||||||
|
// parse reviver to replace identifiers
|
||||||
|
clonedParent = JSON.parse(clonedParent, (key, value) => {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(value, 'key')
|
||||||
|
&& Object.prototype.hasOwnProperty.call(value, 'namespace')
|
||||||
|
&& value.key === oldId.key
|
||||||
|
&& value.namespace === oldId.namespace) {
|
||||||
|
return newId;
|
||||||
|
} else {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return clonedParent;
|
||||||
|
}
|
||||||
|
|
||||||
composeChild(child, parent, setLocation) {
|
composeChild(child, parent, setLocation) {
|
||||||
let childKeyString = this.openmct.objects.makeKeyString(child.identifier);
|
parent.composition.push(child.identifier);
|
||||||
parent.composition.push(childKeyString);
|
|
||||||
|
|
||||||
//If a location is not specified, set it.
|
//If a location is not specified, set it.
|
||||||
if (setLocation && child.location === undefined) {
|
if (setLocation && child.location === undefined) {
|
||||||
let parentKeyString = this.getId(parent);
|
let parentKeyString = this.getKeyString(parent);
|
||||||
child.location = parentKeyString;
|
child.location = parentKeyString;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -256,7 +263,7 @@ export default class DuplicateTask {
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
getId(domainObject) {
|
getKeyString(domainObject) {
|
||||||
return this.openmct.objects.makeKeyString(domainObject.identifier);
|
return this.openmct.objects.makeKeyString(domainObject.identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user