diff --git a/src/nub.ts b/src/nub.ts index 9edf01b5b6ebe210b803087d6f27e3332e380dca..a926e99843a7f83d169dd6441f6cbd0151a2c0ff 100644 --- a/src/nub.ts +++ b/src/nub.ts @@ -795,7 +795,7 @@ export abstract class Nub extends ComputeNode implements IObservable { } } // 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) { this.calculatedParam = calculatedParam; } @@ -944,6 +944,18 @@ export abstract class Nub extends ComputeNode implements IObservable { 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 */ diff --git a/src/pab/pab.ts b/src/pab/pab.ts index 2d9997c4d0ad4e0318e6cd0608bd4db1828b99be..12085da568b5f382ade55f74fa2015fd1bb923a0 100644 --- a/src/pab/pab.ts +++ b/src/pab/pab.ts @@ -44,6 +44,29 @@ export class Pab extends Nub { 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 */ @@ -52,4 +75,4 @@ export class Pab extends Nub { this.prms.Z2.calculability = ParamCalculability.DICHO; this.prms.Q.calculability = ParamCalculability.DICHO; } -} \ No newline at end of file +} diff --git a/src/pab/pab_cloisons.ts b/src/pab/pab_cloisons.ts index 2e0edc24ae26f38614025679af6ca1ff41ce7e06..43ba681359439f497547b8194745a5ceb9e5a583 100644 --- a/src/pab/pab_cloisons.ts +++ b/src/pab/pab_cloisons.ts @@ -1,7 +1,7 @@ import { ParamCalculability, ParamDefinition, ParamFamily} from "../param/param-definition"; import { ParamDomainValue } from "../param/param-domain"; -import { CalculatorType } from "../index"; +import { CalculatorType, Session } from "../index"; import { Cloisons, CloisonsParams } from "../structure/cloisons"; import { Pab } from "./pab"; @@ -77,6 +77,19 @@ export class PabCloisons extends Cloisons { 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 */ diff --git a/src/session.ts b/src/session.ts index aaf42b71aae175dc45ae104676a9cda04faae402..2432c89c05e7158ae02df8d33e67d3e24ee041d1 100644 --- a/src/session.ts +++ b/src/session.ts @@ -150,6 +150,9 @@ export class Session { // second pass for links this.fixLinks(serialised, uids); + // second pass for PABs + this.fixPAB(serialised, uids); + return newNubs; } @@ -382,6 +385,7 @@ export class Session { const modeleCloisons: string = params.getPropValue("modeleCloisons"); if (modeleCloisons) { 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); } else { nub = new PabCloisons(undefined); // don't forget to init with a Cloisons model afterwards ! @@ -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); + } + }); + } + } }