@gisce/ooui 2.23.0-alpha.1 → 2.23.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gisce/ooui",
3
- "version": "2.23.0-alpha.1",
3
+ "version": "2.23.0-alpha.3",
4
4
  "engines": {
5
5
  "node": "20.5.0"
6
6
  },
@@ -0,0 +1,30 @@
1
+ import ContainerWidget from "./ContainerWidget";
2
+ import Group from "./Group";
3
+ import { parseBoolAttribute } from "./helpers/nodeParser";
4
+
5
+ class Carousel extends ContainerWidget {
6
+ _autoPlay = true;
7
+
8
+ get autoPlay(): boolean {
9
+ return this._autoPlay;
10
+ }
11
+
12
+ set autoPlay(value: boolean) {
13
+ this._autoPlay = value;
14
+ }
15
+
16
+ get items(): Group[] {
17
+ return this._container.rows.flat().filter((g) => !g.invisible) as Group[];
18
+ }
19
+
20
+ constructor(props?: any) {
21
+ super(props);
22
+ if (props) {
23
+ if ("auto_play" in props) {
24
+ this._autoPlay = parseBoolAttribute(props.auto_play);
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ export default Carousel;
package/src/Field.ts CHANGED
@@ -106,6 +106,14 @@ class Field extends Widget {
106
106
  this._sum = value;
107
107
  }
108
108
 
109
+ get suffix(): string {
110
+ return this._parsedWidgetProps.suffix || "";
111
+ }
112
+
113
+ get prefix(): string {
114
+ return this._parsedWidgetProps.prefix || "";
115
+ }
116
+
109
117
  /**
110
118
  * Values and keys
111
119
  */
package/src/Form.ts CHANGED
@@ -3,7 +3,11 @@ import Container from "./Container";
3
3
  import ContainerWidget from "./ContainerWidget";
4
4
  import Widget from "./Widget";
5
5
  import { ParsedNode } from "./helpers/nodeParser";
6
- import { evaluateAttributes, replaceEntities } from "./helpers/attributeParser";
6
+ import {
7
+ evaluateAttributes,
8
+ replaceEntities,
9
+ parseWidgetProps,
10
+ } from "./helpers/attributeParser";
7
11
  import { evaluateStates, evaluateButtonStates } from "./helpers/stateParser";
8
12
  import { parseContext } from "./helpers/contextParser";
9
13
  import { parseOnChange } from "./helpers/onChangeParser";
@@ -208,6 +212,11 @@ class Form {
208
212
  );
209
213
  }
210
214
  widgetType = this._fields[name].type;
215
+ // Merge _fields[widget_props] with attributes[widget_props]
216
+ attributes.widget_props = {
217
+ ...parseWidgetProps(attributes.widget_props),
218
+ ...(this._fields[name].widget_props || {}),
219
+ };
211
220
  }
212
221
  tagAttributes = {
213
222
  ...this._fields[name],
package/src/Widget.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { replaceEntities } from "./helpers/attributeParser";
1
+ import { replaceEntities, parseWidgetProps } from "./helpers/attributeParser";
2
2
  import { parseBoolAttribute } from "./helpers/nodeParser";
3
3
 
4
4
  abstract class Widget {
@@ -161,17 +161,7 @@ abstract class Widget {
161
161
  this._fieldType = props.fieldsWidgetType;
162
162
  }
163
163
  if (props.widget_props) {
164
- if (typeof props.widget_props === "string") {
165
- try {
166
- this._parsedWidgetProps = JSON.parse(
167
- props.widget_props.replace(/'/g, '"'),
168
- );
169
- } catch (err) {
170
- console.error("Error parsing widget_props");
171
- }
172
- } else {
173
- this._parsedWidgetProps = props.widget_props;
174
- }
164
+ this._parsedWidgetProps = parseWidgetProps(props.widget_props);
175
165
  }
176
166
  if (props.key) {
177
167
  this._key = props.key;
@@ -41,6 +41,7 @@ import Comments from "./Comments";
41
41
  import JSONField from "./JSONField";
42
42
  import Email from "./Email";
43
43
  import Spinner from "./Spinner";
44
+ import Carousel from "./Carousel";
44
45
 
45
46
  class WidgetFactory {
46
47
  /**
@@ -184,6 +185,9 @@ class WidgetFactory {
184
185
  case "spinner":
185
186
  this._widgetClass = Spinner;
186
187
  break;
188
+ case "carousel":
189
+ this._widgetClass = Carousel;
190
+ break;
187
191
  default:
188
192
  break;
189
193
  }
@@ -184,6 +184,22 @@ const parseAttributes = ({
184
184
  return newAttributes;
185
185
  };
186
186
 
187
+ export const parseWidgetProps = (widget_props: string | object): object => {
188
+ if (widget_props === undefined) {
189
+ return {};
190
+ }
191
+ if (typeof widget_props === "string") {
192
+ try {
193
+ return JSON.parse(widget_props.replace(/'/g, '"'));
194
+ } catch (err) {
195
+ console.error("Error parsing widget_props");
196
+ return {};
197
+ }
198
+ } else {
199
+ return widget_props;
200
+ }
201
+ };
202
+
187
203
  export const parseJsonAttributes = ({
188
204
  attrs,
189
205
  values,
package/src/index.ts CHANGED
@@ -53,6 +53,7 @@ import JSONField from "./JSONField";
53
53
  import Comments from "./Comments";
54
54
  import Email from "./Email";
55
55
  import Spinner from "./Spinner";
56
+ import Carousel from "./Carousel";
56
57
 
57
58
  import {
58
59
  Graph,
@@ -142,4 +143,5 @@ export {
142
143
  JSONField,
143
144
  Email,
144
145
  Spinner,
146
+ Carousel,
145
147
  };
@@ -0,0 +1,75 @@
1
+ import WidgetFactory from "../WidgetFactory";
2
+ import Form from "../Form";
3
+ import Carousel from "../Carousel";
4
+ import { it, expect, describe } from "vitest";
5
+
6
+ describe("A Carousel", () => {
7
+ it("should have an id corresponding to field name", () => {
8
+ const widgetFactory = new WidgetFactory();
9
+ const props = {
10
+ name: "carousel",
11
+ };
12
+
13
+ const widget = widgetFactory.createWidget("carousel", props);
14
+ expect(widget).toBeInstanceOf(Carousel);
15
+ });
16
+ it("should have autoPlay as true by default", () => {
17
+ const widgetFactory = new WidgetFactory();
18
+ const props = {
19
+ name: "carousel",
20
+ };
21
+ const widget = widgetFactory.createWidget("carousel", props);
22
+ expect(widget.autoPlay).toBe(true);
23
+ });
24
+ it("should allow autoPlay to be set", () => {
25
+ const widgetFactory = new WidgetFactory();
26
+ const props = {
27
+ name: "carousel",
28
+ auto_play: false,
29
+ };
30
+ const widget = widgetFactory.createWidget("carousel", props);
31
+ expect(widget.autoPlay).toBe(false);
32
+ });
33
+ it("should have items with the first childs group items", () => {
34
+ const xml = `
35
+ <form>
36
+ <carousel name="carousel">
37
+ <group string="Group 1">
38
+ <field name="field1" string="Field 1" />
39
+ </group>
40
+ <group string="Group 2">
41
+ <field name="field2" string="Field 2" />
42
+ <group string="Group 3">
43
+ <field name="field3" string="Field 3" />
44
+ </group>
45
+ </group>
46
+ </carousel>
47
+ </form>
48
+ `;
49
+ const fields = {
50
+ field1: {
51
+ string: "Field 1",
52
+ type: "char",
53
+ size: 10,
54
+ },
55
+ field2: {
56
+ string: "Field 2",
57
+ type: "char",
58
+ size: 10,
59
+ },
60
+ field3: {
61
+ string: "Field 3",
62
+ type: "char",
63
+ size: 10,
64
+ },
65
+ };
66
+
67
+ const form = new Form(fields);
68
+ form.parse(xml);
69
+ const carousel = form.findById("carousel") as Carousel;
70
+ expect(carousel).toBeInstanceOf(Carousel);
71
+ expect(carousel.items.length).toBe(2);
72
+ expect(carousel.items[0].label).toBe("Group 1");
73
+ expect(carousel.items[1].label).toBe("Group 2");
74
+ });
75
+ });
@@ -6065,4 +6065,34 @@ describe("A Form", () => {
6065
6065
  expect(form.autorefreshableFields.length).toBe(1);
6066
6066
  expect(form.autorefreshableFields[0]).toBe("field_char");
6067
6067
  });
6068
+ describe("If the field has widget_props", () => {
6069
+ it("should merge widget_props from fields definition and xml", () => {
6070
+ const fields = {
6071
+ field_integer: {
6072
+ readonly: 1,
6073
+ string: "Power",
6074
+ type: "integer",
6075
+ widget_props: {
6076
+ suffix: "kW",
6077
+ },
6078
+ },
6079
+ };
6080
+
6081
+ const xmlViewForm = `<?xml version="1.0"?>
6082
+ <form string="Form1">
6083
+ <field name="field_integer" widget_props="{'prefix': 'Wow'}" />
6084
+ </form>`;
6085
+
6086
+ const form = new Form(fields);
6087
+ form.parse(xmlViewForm, {
6088
+ values: {
6089
+ field_integer: 10,
6090
+ },
6091
+ });
6092
+ const field = form.findById("field_integer") as Field;
6093
+ expect(field).toBeDefined();
6094
+ expect(field.suffix).toBe("kW");
6095
+ expect(field.prefix).toBe("Wow");
6096
+ });
6097
+ });
6068
6098
  });