diff --git a/package-lock.json b/package-lock.json index 7bebfb0..eae5336 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@gisce/ooui", - "version": "2.24.0", + "version": "2.25.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@gisce/ooui", - "version": "2.24.0", + "version": "2.25.0", "dependencies": { "@gisce/conscheck": "1.0.9", "html-entities": "^2.3.3", diff --git a/package.json b/package.json index 307dc3c..f32f698 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gisce/ooui", - "version": "2.24.0", + "version": "2.25.0", "engines": { "node": "20.5.0" }, diff --git a/src/Carousel.ts b/src/Carousel.ts new file mode 100644 index 0000000..a7d0601 --- /dev/null +++ b/src/Carousel.ts @@ -0,0 +1,30 @@ +import ContainerWidget from "./ContainerWidget"; +import Group from "./Group"; +import { parseBoolAttribute } from "./helpers/nodeParser"; + +class Carousel extends ContainerWidget { + _autoPlay = true; + + get autoPlay(): boolean { + return this._autoPlay; + } + + set autoPlay(value: boolean) { + this._autoPlay = value; + } + + get items(): Group[] { + return this._container.rows.flat().filter((g) => !g.invisible) as Group[]; + } + + constructor(props?: any) { + super(props); + if (props) { + if ("auto_play" in props) { + this._autoPlay = parseBoolAttribute(props.auto_play); + } + } + } +} + +export default Carousel; diff --git a/src/WidgetFactory.ts b/src/WidgetFactory.ts index 10ac0c8..73bfa34 100644 --- a/src/WidgetFactory.ts +++ b/src/WidgetFactory.ts @@ -41,6 +41,7 @@ import Comments from "./Comments"; import JSONField from "./JSONField"; import Email from "./Email"; import Spinner from "./Spinner"; +import Carousel from "./Carousel"; class WidgetFactory { /** @@ -184,6 +185,9 @@ class WidgetFactory { case "spinner": this._widgetClass = Spinner; break; + case "carousel": + this._widgetClass = Carousel; + break; default: break; } diff --git a/src/index.ts b/src/index.ts index b92d49b..23041e0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,6 +53,7 @@ import JSONField from "./JSONField"; import Comments from "./Comments"; import Email from "./Email"; import Spinner from "./Spinner"; +import Carousel from "./Carousel"; import { Graph, @@ -142,4 +143,5 @@ export { JSONField, Email, Spinner, + Carousel, }; diff --git a/src/spec/Carousel.spec.ts b/src/spec/Carousel.spec.ts new file mode 100644 index 0000000..d0cb8b1 --- /dev/null +++ b/src/spec/Carousel.spec.ts @@ -0,0 +1,75 @@ +import WidgetFactory from "../WidgetFactory"; +import Form from "../Form"; +import Carousel from "../Carousel"; +import { it, expect, describe } from "vitest"; + +describe("A Carousel", () => { + it("should have an id corresponding to field name", () => { + const widgetFactory = new WidgetFactory(); + const props = { + name: "carousel", + }; + + const widget = widgetFactory.createWidget("carousel", props); + expect(widget).toBeInstanceOf(Carousel); + }); + it("should have autoPlay as true by default", () => { + const widgetFactory = new WidgetFactory(); + const props = { + name: "carousel", + }; + const widget = widgetFactory.createWidget("carousel", props); + expect(widget.autoPlay).toBe(true); + }); + it("should allow autoPlay to be set", () => { + const widgetFactory = new WidgetFactory(); + const props = { + name: "carousel", + auto_play: false, + }; + const widget = widgetFactory.createWidget("carousel", props); + expect(widget.autoPlay).toBe(false); + }); + it("should have items with the first childs group items", () => { + const xml = ` +
+ `; + const fields = { + field1: { + string: "Field 1", + type: "char", + size: 10, + }, + field2: { + string: "Field 2", + type: "char", + size: 10, + }, + field3: { + string: "Field 3", + type: "char", + size: 10, + }, + }; + + const form = new Form(fields); + form.parse(xml); + const carousel = form.findById("carousel") as Carousel; + expect(carousel).toBeInstanceOf(Carousel); + expect(carousel.items.length).toBe(2); + expect(carousel.items[0].label).toBe("Group 1"); + expect(carousel.items[1].label).toBe("Group 2"); + }); +});