Skip to content
Snippets Groups Projects
Commit 7b3f6531 authored by francois.grand's avatar francois.grand
Browse files

#45 implémentation de la (dé)sérialisation du formulaire

parent 8d8339c3
No related branches found
No related tags found
1 merge request!20Resolve "Ouvrir / Enregistrer une session / une calculette"
......@@ -228,18 +228,21 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
// passage de la liste, récupération d'une Promise pour traiter le résultat
const prom: Promise<any[]> = compRef.instance.run(list);
prom.then(list => {
this.doSaveForm(list)
this.saveSession(list)
});
}
private doSaveForm(calcList: any[]) {
console.log("---");
private saveSession(calcList: any[]) {
let elems = [];
for (const c of calcList)
if (c.selected) {
console.log(c.title);
const form: FormulaireDefinition = this.formulaireService.getFormulaireFromId(c.uid);
console.log(JSON.stringify(form.JSONserialise()));
elems.push(form.JSONserialise());
}
let session = { "session": { "elements": elems } };
console.log("---");
console.log(JSON.stringify(session));
}
private closeCalculator(form: FormulaireDefinition) {
......
......@@ -435,6 +435,72 @@ export abstract class FormulaireDefinition extends FormulaireNode implements Obs
return new TopFormulaireElementIterator(this);
}
/**
* sérialisation en JSON
*/
public JSONserialise(): {} {
let res = {};
res["id"] = this.calculatorName;
res["props"] = this._currentSessionNub.properties.props;
res["elements"] = this.serialiseKids();
return { "form": res };
}
private deserialiseFieldset(elements: {}): FieldSet {
const res: FieldSet = this.getFormulaireNodeById(elements["id"]) as FieldSet;
res.deserialiseJSON(elements);
return res;
}
private deserialiseFieldsetContainer(elements: {}): FieldsetContainer {
const res: FieldsetContainer = this.getFormulaireNodeById(elements["id"]) as FieldsetContainer;
res.deserialiseJSON(elements);
return res;
}
private deserialiseElement(element: {}) {
const keys = Object.keys(element);
if (keys.length !== 1)
throw new Error(`session file : invalid form object '${element}'`);
switch (keys[0]) {
case "fieldset":
this.deserialiseFieldset(element[keys[0]]);
break;
case "fieldset_container":
this.deserialiseFieldsetContainer(element[keys[0]]);
break;
default:
throw new Error(`session file : invalid key '${keys[0]}' in form object`);
}
}
/**
* désérialisation depuis JSON
*/
public deserialiseJSON(elements: {}) {
for (const k in elements) {
switch (k) {
case "id":
this._calculatorName = elements[k];
break;
case "props":
break;
case "elements":
for (const e of elements[k])
this.deserialiseElement(e);
break;
default:
throw new Error(`session file : invalid key '${k}' in form object`);
}
}
}
// interface Observer
public update(sender: any, data: any) {
......
......@@ -105,4 +105,61 @@ export class FieldsetContainer extends FormulaireElement {
super.updateLocalisation(loc);
}
/**
* sérialisation en JSON
*/
public JSONserialise(): {} {
let res = {};
res["id"] = this.id;
res["elements"] = this.serialiseKids();
return { "fieldset_container": res };
}
private deserialiseFieldset(elements: {}): FieldSet {
const ind = this.getTemplateIndex(elements["id"]);
const res: FieldSet = this.addFromTemplate(ind);
const props = elements["props"];
for (const k in props)
if (k !== "calcType" && k !== "nodeType")
res.setPropValue(k, props[k]);
res.deserialiseJSON(elements);
return res;
}
private deserialiseElement(element: {}) {
const keys = Object.keys(element);
if (keys.length !== 1)
throw new Error(`session file : invalid fieldset object '${element}'`);
switch (keys[0]) {
case "fieldset":
this.deserialiseFieldset(element[keys[0]]);
break;
default:
throw new Error(`session file : invalid key '${keys[0]}' in fieldset object`);
}
}
/**
* désérialisation depuis JSON
*/
public deserialiseJSON(elements: {}) {
for (const k in elements) {
switch (k) {
case "id":
this._confId = elements[k];
break;
case "elements":
for (const e of elements[k])
this.deserialiseElement(e);
break;
default:
throw new Error(`session file : invalid key '${k}' in form object`);
}
}
}
}
......@@ -402,4 +402,70 @@ export class FieldSet extends FormulaireElement implements Observer {
break;
}
}
/**
* sérialisation en JSON
*/
public JSONserialise(): {} {
let res = {};
res["id"] = this.id;
res["props"] = this._props.props;
res["elements"] = this.serialiseKids();
return { "fieldset": res };
}
private deserialiseParam(elements: {}): NgParameter {
const res: NgParameter = this.getFormulaireNodeById(elements["id"]) as NgParameter;
res.deserialiseJSON(elements);
return res;
}
private deserialiseSelect(elements: {}): SelectField {
const res: SelectField = this.getFormulaireNodeById(elements["id"]) as SelectField;
res.deserialiseJSON(elements);
return res;
}
private deserialiseElement(element: {}) {
const keys = Object.keys(element);
if (keys.length !== 1)
throw new Error(`session file : invalid fieldset object '${element}'`);
switch (keys[0]) {
case "param":
this.deserialiseParam(element[keys[0]]);
break;
case "select":
this.deserialiseSelect(element[keys[0]]);
break;
default:
throw new Error(`session file : invalid key '${keys[0]}' in fieldset object`);
}
}
/**
* désérialisation depuis JSON
*/
public deserialiseJSON(elements: {}) {
for (const k in elements) {
switch (k) {
case "id":
break;
case "props":
this._props = new Props(elements[k]);
break;
case "elements":
for (const e of elements[k])
this.deserialiseElement(e);
break;
default:
throw new Error(`session file : invalid key '${k}' in form object`);
}
}
}
}
......@@ -144,17 +144,17 @@ export abstract class FormulaireNode implements IObservable {
this._observable.notifyObservers(data, sender);
}
protected toJSON(): any {
return {};
protected serialiseKids() {
const elems = [];
for (const k of this.kids)
elems.push(k.JSONserialise());
return elems;
}
/**
* sérialisation en JSON
*/
public JSONserialise(): any {
let res = this.toJSON();
for (const k of this._kids)
res[k.id] = k.JSONserialise();
return res;
public JSONserialise(): {} {
return {};
}
}
......@@ -320,4 +320,78 @@ export class NgParameter extends InputField {
super.updateLocalisation(loc, key);
}
}
private paramValuesJSON(): any {
let res = {};
res["mode"] = ParamValueMode[this._paramValues.valueMode];
switch (this._paramValues.valueMode) {
case ParamValueMode.SINGLE:
res["value"] = this._paramValues.singleValue;
break;
case ParamValueMode.MINMAX:
res["min"] = this._paramValues.min;
res["max"] = this._paramValues.max;
res["step"] = this._paramValues.step;
break;
case ParamValueMode.LISTE:
res["values"] = this._paramValues.valueList;
break;
}
return res;
}
/**
* sérialisation en JSON
*/
public JSONserialise(): {} {
let res = {};
res["id"] = this.id;
res["values"] = this.paramValuesJSON();
return { "param": res };
}
private deserialiseValues(json: {}) {
const m: ParamValueMode = (<any>ParamValueMode)[json["mode"]];
switch (m) {
case ParamValueMode.SINGLE:
this._paramValues.setValues(+json["value"]);
break;
case ParamValueMode.MINMAX:
this._paramValues.setValues(+json["min"], +json["max"], +json["step"]);
break;
case ParamValueMode.LISTE:
this._paramValues.setValues(json["values"]);
break;
case ParamValueMode.CALCUL:
this._paramValues.valueMode = ParamValueMode.CALCUL;
break;
default:
throw new Error(`session file : invalid value mode '${json["mode"]}' in param object`);
}
}
/**
* désérialisation depuis JSON
*/
public deserialiseJSON(elements: {}) {
for (const k in elements) {
switch (k) {
case "id":
break;
case "values":
this.deserialiseValues(elements[k]);
break;
default:
throw new Error(`session file : invalid key '${k}' in param object`);
}
}
}
}
......@@ -27,4 +27,14 @@ export class SelectEntry {
get value(): any {
return this._value;
}
/**
* sérialisation en JSON
*/
public JSONserialise() {
let res = {};
res["id"] = this.id;
res["value"] = this._value;
return res;
}
}
......@@ -117,4 +117,39 @@ export class SelectField extends Field {
this.addEntry(e);
}
}
/**
* sérialisation en JSON
*/
public JSONserialise(): {} {
let res = {};
res["id"] = this.id;
res["selected_id"] = this._selectedEntry.id;
return { "select": res };
}
/**
* désérialisation depuis JSON
*/
public deserialiseJSON(elements: {}) {
for (const k in elements) {
switch (k) {
case "id":
this._confId = elements[k];
break;
case "selected_id":
const sel = elements[k];
for (const e of this._entries)
if (e.id === sel) {
this._selectedEntry = e;
break;
}
break;
default:
throw new Error(`session file : invalid key '${k}' in select object`);
}
}
}
}
......@@ -337,4 +337,38 @@ export class FormulaireService extends Observable {
"form": f
});
}
private deserialiseForm(elements: {}) {
const props = elements["props"];
const ct: CalculatorType = props["calcType"];
this.createFormulaire(ct, elements);
}
private deserialiseSessionElement(element: {}) {
const keys = Object.keys(element);
if (keys.length !== 1)
throw new Error(`session file : invalid session object '${element}'`);
switch (keys[0]) {
case "form":
this.deserialiseForm(element[keys[0]]);
break;
default:
throw new Error(`session file : invalid key '${keys[0]}' in session object`);
}
}
private deserialiseSession(elements: {}) {
for (const ks in elements)
switch (ks) {
case "elements":
for (const e of elements[ks])
this.deserialiseSessionElement(e);
break;
default:
throw new Error(`session file : invalid key '${ks}' in session object`);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment