Skip to content

Commit

Permalink
Merge branch 'v2' into v2-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Nov 8, 2024
2 parents b2b81f3 + 1e196d9 commit 7e0ede2
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 45 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gisce/ooui",
"version": "2.13.0",
"version": "2.14.0",
"engines": {
"node": "20.5.0"
},
Expand Down
6 changes: 3 additions & 3 deletions src/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ class Form {

if (checkIfDomainHasValue(tagAttributes.domain)) {
domain = tagAttributes.domain;
}

if (checkIfDomainHasValue(this._fields[tagAttributes.name]?.domain)) {
} else if (
checkIfDomainHasValue(this._fields[tagAttributes.name]?.domain)
) {
domain = this._fields[tagAttributes.name].domain;
}

Expand Down
7 changes: 7 additions & 0 deletions src/JSONField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import CodeEditor from "./CodeEditor";

class JSONField extends CodeEditor {
_lang = "json";
}

export default JSONField;
9 changes: 8 additions & 1 deletion src/WidgetFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Avatar from "./Avatar";
import Time from "./Time";
import Alert from "./Alert";
import Comments from "./Comments";
import JSONField from "./JSONField";

class WidgetFactory {
/**
Expand Down Expand Up @@ -172,13 +173,19 @@ class WidgetFactory {
case "comments_timeline":
this._widgetClass = Comments;
break;

case "json":
this._widgetClass = JSONField;
break;
case "arrow_steps":
this._widgetClass = JSONField;
break;
default:
break;
}
}

createWidget(type: string, props: any) {
this._widgetClass = undefined;
let finalType = type;

this.setWidgetClass(type);
Expand Down
16 changes: 15 additions & 1 deletion src/helpers/attributeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,12 @@ export const parseJsonAttributes = ({
attrs,
values,
fields,
widgetType,
}: {
attrs: string;
values: any;
fields: any;
widgetType?: string;
}) => {
try {
const attrsWithReplacedEntities = replaceEntities(attrs);
Expand All @@ -200,7 +202,7 @@ export const parseJsonAttributes = ({
) as JsonAttributes;
const finalAttributes: Record<string, boolean> = {};
for (const attrField of Object.keys(jsonAttributes)) {
finalAttributes[attrField] = evaluateConscheckCondition({
const evaluatedEntry = evaluateConscheckCondition({
object: values,
condition: jsonAttributes[attrField],
evaluateFieldComparison: ({
Expand All @@ -216,6 +218,17 @@ export const parseJsonAttributes = ({
});
},
});

if (evaluatedEntry) {
finalAttributes[attrField] = true;
} else if (
attrField === "readonly" &&
!evaluatedEntry &&
widgetType === "button"
) {
// Buttons with readonly false will have to override the default readonly
finalAttributes[attrField] = false;
}
}

return finalAttributes;
Expand Down Expand Up @@ -261,6 +274,7 @@ const evaluateAttributes = ({
attrs: tagAttributes.json_attrs,
values,
fields,
widgetType,
});
} catch (error) {
if (fallbackMode && tagAttributes.attrs) {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import CodeEditor from "./CodeEditor";
import Time from "./Time";
import HTMLPreview from "./HTMLPreview";
import Alert from "./Alert";
import JSONField from "./JSONField";
import Comments from "./Comments";

import {
Expand Down Expand Up @@ -136,4 +137,5 @@ export {
YAxisOpts,
MinMaxValues,
Comments,
JSONField,
};
244 changes: 209 additions & 35 deletions src/spec/Form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5796,39 +5796,213 @@ describe("A Form", () => {
expect(alert1.invisible).toBeFalsy();
expect(alert2.invisible).toBeTruthy();
});
// it("Should not be able to view the Autoconsum tab since it's invisible due to tags and attrs", () => {
// const xml = `
// <form string="polisses">
// <notebook
// colspan="6">
// <page name="page-autoconsum" string="Autoconsum"
// attrs="{'invisible':[('autoconsum_id', '=', False)]}" invisible="1"
// json_attrs="{&quot;invisible&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;=&quot;, &quot;field&quot;: &quot;autoconsum_id&quot;, &quot;value&quot;: false}], &quot;condition&quot;: &quot;AND&quot;}}">
// <field name="autoconsum_id" />
// </page>
// </notebook>
// </form>
// `;
// const form = new Form({
// autoconsum_id: {
// context: "",
// digits: [16, 2],
// domain: [],
// is_function: true,
// readonly: 1,
// relation: "giscedata.autoconsum",
// string: "Autoconsum",
// type: "many2one",
// views: {},
// },
// });
// form.parse(xml, {
// values: {
// autoconsum_id: [1, "ES0318363477145938GEA000"],
// },
// });
// const pageAutoconsum = form.findById("page-autoconsum") as Page;
// expect(pageAutoconsum).toBeDefined();
// expect(pageAutoconsum.invisible).toBeTruthy();
// });
it("Should not be able to view the Autoconsum tab since it's invisible due to tags and attrs", () => {
const xml = `
<form string="polisses">
<notebook
colspan="6">
<page name="page-autoconsum" string="Autoconsum"
attrs="{'invisible':[('autoconsum_id', '=', False)]}" invisible="1"
json_attrs="{&quot;invisible&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;=&quot;, &quot;field&quot;: &quot;autoconsum_id&quot;, &quot;value&quot;: false}], &quot;condition&quot;: &quot;AND&quot;}}">
<field name="autoconsum_id" />
</page>
</notebook>
</form>
`;
const form = new Form({
autoconsum_id: {
context: "",
digits: [16, 2],
domain: [],
is_function: true,
readonly: 1,
relation: "giscedata.autoconsum",
string: "Autoconsum",
type: "many2one",
views: {},
},
});
form.parse(xml, {
values: {
autoconsum_id: [1, "ES0318363477145938GEA000"],
},
});
const pageAutoconsum = form.findById("page-autoconsum") as Page;
expect(pageAutoconsum).toBeDefined();
expect(pageAutoconsum.invisible).toBeTruthy();
});
it("Should be able to view a form with disabled and enabled buttons depending on the attrs", () => {
const xml = `<form string="Generar Casos Gestió ATR">
<label string="Generar Cas Switching de la pólissa o pólisses. Es crearà un procés del tipus indicat amb pas 01 per cada pòlissa"/>
<separator colspan="4"/>
<field name="state" invisible="1"/>
<field name="whereiam" invisible="1"/>
<field name="multipol" invisible="1"/>
<field name="te_esborranys" invisible="1"/>
<field name="te_actius" invisible="1"/>
<field name="te_autolectures" invisible="1"/>
<field name="casos_generats" invisible="1"/>
<field nolabel="1" name="info" height="100" width="640" readonly="1"/>
<group colspan="4" attrs="{'invisible': [('state','!=','init')]}" json_attrs="{&quot;invisible&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;!=&quot;, &quot;field&quot;: &quot;state&quot;, &quot;value&quot;: &quot;init&quot;}], &quot;condition&quot;: &quot;AND&quot;}}">
<separator colspan="4" string="Generar Casos"/>
<!-- Comer cases -->
<group attrs="{'invisible': [('whereiam','!=','comer')]}" json_attrs="{&quot;invisible&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;!=&quot;, &quot;field&quot;: &quot;whereiam&quot;, &quot;value&quot;: &quot;comer&quot;}], &quot;condition&quot;: &quot;AND&quot;}}">
<button name="genera_casos_atr1" string="C1 - Canvi de comercialitzadora" type="object" icon="gtk-execute" attrs="{'readonly': [('te_esborranys','=',0)]}" context="{'proces': 'C1'}" colspan="4" readonly="0" json_attrs="{&quot;readonly&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;=&quot;, &quot;field&quot;: &quot;te_esborranys&quot;, &quot;value&quot;: 0}], &quot;condition&quot;: &quot;AND&quot;}}"/>
<button name="genera_casos_atr2" string="C2 - Canvi de comercialitzadora amb canvis" type="object" icon="gtk-execute" attrs="{'readonly': [('te_esborranys','=',0)]}" context="{'proces': 'C2', 'allow_multipol': False}" colspan="4" readonly="0" json_attrs="{&quot;readonly&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;=&quot;, &quot;field&quot;: &quot;te_esborranys&quot;, &quot;value&quot;: 0}], &quot;condition&quot;: &quot;AND&quot;}}"/>
<button name="genera_casos_atr3" string="M1 - Modificació contractual" type="object" icon="gtk-execute" attrs="{'readonly': [('te_actius','=',0)]}" context="{'proces': 'M1', 'allow_multipol': False}" colspan="4" readonly="0" json_attrs="{&quot;readonly&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;=&quot;, &quot;field&quot;: &quot;te_actius&quot;, &quot;value&quot;: 0}], &quot;condition&quot;: &quot;AND&quot;}}"/>
<button name="genera_casos_atr4" string="A3 - Alta de subministrament" type="object" icon="gtk-execute" attrs="{'readonly': [('te_esborranys','=',0)]}" context="{'proces': 'A3'}" colspan="4" readonly="0" json_attrs="{&quot;readonly&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;=&quot;, &quot;field&quot;: &quot;te_esborranys&quot;, &quot;value&quot;: 0}], &quot;condition&quot;: &quot;AND&quot;}}"/>
<button name="genera_casos_atr5" string="B1 - Baixa" type="object" icon="gtk-execute" attrs="{'readonly': [('te_esborranys','!=',0)]}" context="{'proces': 'B1'}" colspan="4" readonly="0" json_attrs="{&quot;readonly&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;!=&quot;, &quot;field&quot;: &quot;te_esborranys&quot;, &quot;value&quot;: 0}], &quot;condition&quot;: &quot;AND&quot;}}"/>
<button name="genera_casos_atr6" string="R1 - Reclamació" type="object" icon="gtk-execute" context="{'proces': 'R1'}" colspan="4" readonly="0"/>
<button name="genera_casos_atr7" string="W1 - Autolectures" type="object" icon="gtk-execute" attrs="{'readonly': [('te_autolectures','=',0)]}" context="{'proces': 'W1'}" colspan="4" readonly="0" json_attrs="{&quot;readonly&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;=&quot;, &quot;field&quot;: &quot;te_autolectures&quot;, &quot;value&quot;: 0}], &quot;condition&quot;: &quot;AND&quot;}}"/>
<button name="genera_casos_atr8" string="Segons dades del formulari" type="object" icon="gtk-execute" attrs="{'readonly': [('te_esborranys','=',0)]}" context="{'proces': 'Cn'}" colspan="4" readonly="0" json_attrs="{&quot;readonly&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;=&quot;, &quot;field&quot;: &quot;te_esborranys&quot;, &quot;value&quot;: 0}], &quot;condition&quot;: &quot;AND&quot;}}"/>
</group>
<!-- Distri cases -->
<group attrs="{'invisible': [('whereiam','!=','distri')]}" json_attrs="{&quot;invisible&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;!=&quot;, &quot;field&quot;: &quot;whereiam&quot;, &quot;value&quot;: &quot;distri&quot;}], &quot;condition&quot;: &quot;AND&quot;}}">
<button name="genera_casos_atr" string="D1 - Notificació de canvis" type="object" icon="gtk-execute" attrs="{'readonly': [('te_actius','=',0)]}" context="{'proces': 'D1'}" colspan="4" readonly="0" json_attrs="{&quot;readonly&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;=&quot;, &quot;field&quot;: &quot;te_actius&quot;, &quot;value&quot;: 0}], &quot;condition&quot;: &quot;AND&quot;}}"/>
<button name="genera_casos_atr" string="T1 - Traspàs a COR" type="object" icon="gtk-execute" context="{'proces': 'T1'}" colspan="4" readonly="0"/>
<button name="genera_casos_atr" string="B2 - Baixa unidireccional" type="object" icon="gtk-execute" attrs="{'readonly': [('te_esborranys','!=',0)]}" context="{'proces': 'B2'}" colspan="4" readonly="0" json_attrs="{&quot;readonly&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;!=&quot;, &quot;field&quot;: &quot;te_esborranys&quot;, &quot;value&quot;: 0}], &quot;condition&quot;: &quot;AND&quot;}}"/>
</group>
</group>
<group colspan="4">
<button name="action_casos" string="Obrir Casos Generats" type="object" icon="gtk-execute" colspan="4" attrs="{'invisible': [('state','!=','done')]}" readonly="0" json_attrs="{&quot;invisible&quot;: {&quot;rules&quot;: [{&quot;operator&quot;: &quot;!=&quot;, &quot;field&quot;: &quot;state&quot;, &quot;value&quot;: &quot;done&quot;}], &quot;condition&quot;: &quot;AND&quot;}}"/>
<button special="cancel" string="Tancar" type="object" icon="gtk-no" colspan="4" readonly="0"/>
</group>
</form>`;
const fields = {
casos_generats: {
is_function: false,
string: "Casos generats",
type: "text",
views: {},
},
info: {
is_function: false,
string: "Info",
type: "text",
views: {},
},
multipol: {
is_function: false,
string: "Multipol",
type: "boolean",
views: {},
},
state: {
is_function: false,
size: 16,
string: "State",
type: "char",
views: {},
},
te_actius: {
is_function: false,
string: "Te Actius",
type: "boolean",
views: {},
},
te_autolectures: {
is_function: false,
string: "Te Autolectures",
type: "boolean",
views: {},
},
te_esborranys: {
is_function: false,
string: "Te Esborranys",
type: "boolean",
views: {},
},
whereiam: {
is_function: false,
size: 6,
string: "Whereiam",
type: "char",
views: {},
},
};
const values = {
casos_generats: "[]",
info: "Només de les pólisses que:\n * Estan en esborrany per C1/C2/A3\n * Estan actives per M1/B1/R1\n * Esta actives i amb autolectures per W1 encara que tingui casos ATR creats\n * No tenen cap altre cas creat de gestió ATR\n",
multipol: false,
state: "init",
te_actius: false,
te_autolectures: false,
te_esborranys: false,
whereiam: "comer",
active_ids: [null],
uid: 1,
};
const form = new Form(fields);
form.parse(xml, {
values,
});
const but1 = form.findById("genera_casos_atr1") as Button;
const but2 = form.findById("genera_casos_atr2") as Button;
const but3 = form.findById("genera_casos_atr3") as Button;
const but4 = form.findById("genera_casos_atr4") as Button;
const but5 = form.findById("genera_casos_atr5") as Button;
const but6 = form.findById("genera_casos_atr6") as Button;
const but7 = form.findById("genera_casos_atr7") as Button;
const but8 = form.findById("genera_casos_atr8") as Button;

expect(but1.readOnly).toBeTruthy();
expect(but2.readOnly).toBeTruthy();
expect(but3.readOnly).toBeTruthy();
expect(but4.readOnly).toBeTruthy();
expect(but5.readOnly).toBeFalsy();
expect(but6.readOnly).toBeFalsy();
expect(but7.readOnly).toBeTruthy();
expect(but8.readOnly).toBeTruthy();
});
it("a domain defined in the xml should have priority over the domain defined in the fields", () => {
const fields = {
field_char: {
type: "char",
domain: "[('value', '=', 'field')]",
},
};
const xmlViewForm = `<?xml version="1.0"?>
<form string="Form1">
<field name="field_char" colspan="4" nolabel="1" domain="[('value', '=', 'form')]"/>
</form>`;
const form = new Form(fields);
form.parse(xmlViewForm, {
values: {
field_char: "test",
},
});

const field_char = form.findById("field_char");
expect(field_char!.domain!).toBe("[('value', '=', 'form')]");
});
it("a field with no supported type should fallback to field generic widget", () => {
const fields = {
field_char: {
digits: [16, 2],
is_function: true,
readonly: 1,
string: "Etapa",
type: "json",
views: {},
widget: "arrow_steps",
},
};

const xmlViewForm = `<?xml version="1.0"?>
<form string="Form1">
<field name="field_char" widget="arrow_steps" colspan="4" nolabel="1"/>
</form>`;

const form = new Form(fields);
form.parse(xmlViewForm, {
values: {
field_char: "test",
},
});

const field_char = form.findById("field_char") as Field;
expect(field_char).toBeDefined();
expect(field_char?.type).toBe("arrow_steps");
expect(field_char?.id).toBe("field_char");
});
});
Loading

0 comments on commit 7e0ede2

Please sign in to comment.