Commit 1ce59e8e authored by Mathias Chouet's avatar Mathias Chouet :spaghetti:
Browse files

Added lechanism to reinit PabCloisons after loading session

Showing with 75 additions and 3 deletions
+75 -3
...@@ -795,7 +795,7 @@ export abstract class Nub extends ComputeNode implements IObservable { ...@@ -795,7 +795,7 @@ export abstract class Nub extends ComputeNode implements IObservable {
} }
} }
// define calculated param at Nub level // define calculated param at Nub level
// @TODO except if we are a Section or Structure ? // @TODO except if we are a Section / Structure / PabCloisons ?
if (calculatedParam) { if (calculatedParam) {
this.calculatedParam = calculatedParam; this.calculatedParam = calculatedParam;
} }
...@@ -944,6 +944,18 @@ export abstract class Nub extends ComputeNode implements IObservable { ...@@ -944,6 +944,18 @@ export abstract class Nub extends ComputeNode implements IObservable {
return false; return false;
} }
/**
* @return child having the given UID
*/
public getChild(uid: string): Nub {
for (const s of this._children) {
if (s.uid === uid) {
return s;
}
}
return undefined;
}
/** /**
* Moves a child to first position * Moves a child to first position
*/ */
......
...@@ -44,6 +44,29 @@ export class Pab extends Nub { ...@@ -44,6 +44,29 @@ export class Pab extends Nub {
return r; return r;
} }
/**
* Once session is loaded, run a second pass on all PabCloisons to
* reinit their target if needed
*/
public fixPAB(obj: any) {
if (obj.children && Array.isArray(obj.children)) {
for (const c of obj.children) {
if (c.props.calcType === CalculatorType.PabCloisons) { // who knows ?
const childUid = c.uid;
const targetCloisonsUid = c.props.modeleCloisons;
console.log(`==> relinking ${childUid} to ${targetCloisonsUid}`);
// find child PabCloisons to relink
const pabCloisons = (this.getChild(childUid) as PabCloisons);
if (pabCloisons) {
pabCloisons.setModel(targetCloisonsUid);
} else {
console.error("fixPAB : cannot find child PabCloisons");
}
}
}
}
}
/** /**
* paramétrage de la calculabilité des paramètres * paramétrage de la calculabilité des paramètres
*/ */
...@@ -52,4 +75,4 @@ export class Pab extends Nub { ...@@ -52,4 +75,4 @@ export class Pab extends Nub {
this.prms.Z2.calculability = ParamCalculability.DICHO; this.prms.Z2.calculability = ParamCalculability.DICHO;
this.prms.Q.calculability = ParamCalculability.DICHO; this.prms.Q.calculability = ParamCalculability.DICHO;
} }
} }
\ No newline at end of file
import { ParamCalculability, ParamDefinition, ParamFamily} from "../param/param-definition"; import { ParamCalculability, ParamDefinition, ParamFamily} from "../param/param-definition";
import { ParamDomainValue } from "../param/param-domain"; import { ParamDomainValue } from "../param/param-domain";
import { CalculatorType } from "../index"; import { CalculatorType, Session } from "../index";
import { Cloisons, CloisonsParams } from "../structure/cloisons"; import { Cloisons, CloisonsParams } from "../structure/cloisons";
import { Pab } from "./pab"; import { Pab } from "./pab";
...@@ -77,6 +77,19 @@ export class PabCloisons extends Cloisons { ...@@ -77,6 +77,19 @@ export class PabCloisons extends Cloisons {
this._children = modelCloisons ? modelCloisons.structures : []; this._children = modelCloisons ? modelCloisons.structures : [];
} }
/**
* Finds the Cloisons targetted by modelUid and defines it as the current model
*/
public setModel(modelUid: string) {
this.properties.setPropValue("modeleCloisons", modelUid);
const cloisons = (Session.getInstance().findNubByUid(modelUid) as Cloisons);
if (cloisons) {
this.initModelCloisons(cloisons);
} else {
console.error("PabCloisons.setModel : cannot find target Cloisons");
}
}
/** /**
* Returns an object representation of the Nub's current state * Returns an object representation of the Nub's current state
*/ */
......
...@@ -150,6 +150,9 @@ export class Session { ...@@ -150,6 +150,9 @@ export class Session {
// second pass for links // second pass for links
this.fixLinks(serialised, uids); this.fixLinks(serialised, uids);
// second pass for PABs
this.fixPAB(serialised, uids);
return newNubs; return newNubs;
} }
...@@ -382,6 +385,7 @@ export class Session { ...@@ -382,6 +385,7 @@ export class Session {
const modeleCloisons: string = params.getPropValue("modeleCloisons"); const modeleCloisons: string = params.getPropValue("modeleCloisons");
if (modeleCloisons) { if (modeleCloisons) {
const cloisons = (Session.getInstance().findNubByUid(modeleCloisons) as Cloisons); const cloisons = (Session.getInstance().findNubByUid(modeleCloisons) as Cloisons);
// si le module Cloisons ciblé n'existe pas, Session.fixPAB() est censé s'en occuper
nub = new PabCloisons(cloisons); nub = new PabCloisons(cloisons);
} else { } else {
nub = new PabCloisons(undefined); // don't forget to init with a Cloisons model afterwards ! nub = new PabCloisons(undefined); // don't forget to init with a Cloisons model afterwards !
...@@ -571,4 +575,24 @@ export class Session { ...@@ -571,4 +575,24 @@ export class Session {
}); });
} }
} }
/**
* Asks all loaded PAB Nubs to reinit any PabCloisons from their model
*/
private fixPAB(serialised: string, uids?: string[]) {
const data = JSON.parse(serialised);
if (data.session && Array.isArray(data.session)) {
// find each PAB in the session
data.session.forEach((e: any) => {
if (
(! uids || uids.length === 0 || uids.includes(e.uid))
&& (e.props.calcType === CalculatorType.Pab)
) {
const nub = (this.findNubByUid(e.uid) as Pab);
// find linked parameters
nub.fixPAB(e);
}
});
}
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment