@media-quest/builder 0.0.1 → 0.0.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.
Files changed (49) hide show
  1. package/dist/public-api.d.mts +652 -0
  2. package/dist/public-api.d.ts +652 -0
  3. package/dist/public-api.js +2187 -0
  4. package/dist/public-api.mjs +2136 -0
  5. package/package.json +11 -3
  6. package/src/Builder-option.ts +67 -0
  7. package/src/Builder-page.spec.ts +313 -0
  8. package/src/Builder-page.ts +249 -0
  9. package/src/Builder-question.spec.ts +68 -0
  10. package/src/Builder-question.ts +101 -0
  11. package/src/Builder-schema.spec.ts +302 -0
  12. package/src/Builder-schema.ts +250 -0
  13. package/src/Builder-text.spec.ts +24 -0
  14. package/src/Builder-text.ts +57 -0
  15. package/src/BuilderMainImageDto.ts +7 -0
  16. package/src/BuilderMainText.ts +81 -0
  17. package/src/BuilderMainVideoDto.ts +10 -0
  18. package/src/BuilderObject.ts +69 -0
  19. package/src/BuilderTag.ts +97 -0
  20. package/src/media-files.ts +28 -0
  21. package/src/public-api.ts +13 -6
  22. package/src/rulebuilder/Builder-rule.spec.ts +207 -0
  23. package/src/rulebuilder/Builder-rule.ts +134 -0
  24. package/src/rulebuilder/RuleAction.ts +20 -0
  25. package/src/rulebuilder/RuleBuilder-test-utils.ts +254 -0
  26. package/src/rulebuilder/RuleInput.ts +44 -0
  27. package/src/rulebuilder/RuleVariable.ts +39 -0
  28. package/src/rulebuilder/SingleSelectItem.ts +135 -0
  29. package/src/rulebuilder/condition/Builder-condition-group.spec.ts +47 -0
  30. package/src/rulebuilder/condition/Builder-condition-group.ts +91 -0
  31. package/src/rulebuilder/condition/Builder-condition.spec.ts +169 -0
  32. package/src/rulebuilder/condition/Builder-condition.ts +186 -0
  33. package/src/rulebuilder/condition/Builder-operator.spec.ts +9 -0
  34. package/src/rulebuilder/condition/Builder-operator.ts +31 -0
  35. package/src/rulebuilder/index.ts +22 -0
  36. package/src/rulebuilder/jump-to-action-manager.ts +33 -0
  37. package/src/rulebuilder/multi-select-item.ts +70 -0
  38. package/src/rulebuilder/page-action-manager.ts +20 -0
  39. package/src/rulebuilder/tag-action-manager.spec.ts +44 -0
  40. package/src/rulebuilder/tag-action-manager.ts +18 -0
  41. package/src/theme/AbstractThemeCompiler.ts +7 -0
  42. package/src/theme/IDefaultTheme.ts +178 -0
  43. package/src/theme/css-theme.ts +7 -0
  44. package/src/theme/default-theme-compiler.ts +395 -0
  45. package/src/theme/icon-urls.ts +29 -0
  46. package/src/theme/standard-props.ts +113 -0
  47. package/src/theme/theme-utils.ts +110 -0
  48. package/src/theme/theme1.spec.ts +52 -0
  49. package/tsconfig.json +0 -2
@@ -0,0 +1,68 @@
1
+ import type { BuilderQuestionDto } from "./Builder-question";
2
+ import { BuilderQuestion } from "./Builder-question";
3
+ import type { BuilderObjectId } from "./BuilderObject";
4
+
5
+ const question1: BuilderQuestionDto = {
6
+ id: "q1" as BuilderObjectId.QuestionID,
7
+ prefix: "q1",
8
+ text: "sadf",
9
+ options: [],
10
+ _type: "select-one",
11
+ };
12
+
13
+ const question2: BuilderQuestionDto = {
14
+ id: "q3" as BuilderObjectId.QuestionID,
15
+ prefix: "a",
16
+ text: "har du..",
17
+ _type: "select-one",
18
+ // options: []
19
+ options: [
20
+ {
21
+ id: "q3-opt1" as BuilderObjectId.QuestionOptionID,
22
+ value: 0,
23
+ label: "Nei",
24
+ },
25
+ {
26
+ id: "q3-opt2" as BuilderObjectId.QuestionOptionID,
27
+ value: 1,
28
+ label: "Ja",
29
+ },
30
+ {
31
+ id: "q3-opt3" as BuilderObjectId.QuestionOptionID,
32
+ value: 9,
33
+ label: "Vet ikke",
34
+ },
35
+ ],
36
+ };
37
+
38
+ describe("Builder Question", () => {
39
+ test("Can create question from json", () => {
40
+ const q1 = BuilderQuestion.fromJson(question1);
41
+ const q1AsJson = q1.toJson();
42
+ expect(question1).toStrictEqual(q1AsJson);
43
+ const q2Instance = BuilderQuestion.fromJson(question2);
44
+ const q2AsJson = q2Instance.toJson();
45
+ expect(question2).toStrictEqual(q2AsJson);
46
+ });
47
+ test("Can add options", () => {
48
+ const q1 = BuilderQuestion.fromJson(question1);
49
+ expect(q1.options.length).toBe(0);
50
+ q1.addOption("Ja", 1);
51
+ expect(q1.options.length).toBe(1);
52
+ const nei = q1.addOption("Nei", 0, 0);
53
+ expect(q1.options.length).toBe(2);
54
+ expect(q1.options[0]).toBe(nei);
55
+ const vetIkke = q1.addOption("Vet-ikke", 9);
56
+ expect(q1.options.length).toBe(3);
57
+ expect(q1.options[2]).toBe(vetIkke);
58
+ });
59
+ test("Can delete options", () => {
60
+ const q = BuilderQuestion.fromJson(question2);
61
+ expect(q.options.length).toBe(3);
62
+ const o1 = q.options[0];
63
+ const o2 = q.options[1];
64
+ const o3 = q.options[2];
65
+ const didDelete = q.deleteOption(o1);
66
+ expect(q.options.length).toBe(2);
67
+ });
68
+ });
@@ -0,0 +1,101 @@
1
+ import type { BuilderOptionDto } from "./Builder-option";
2
+ import { BuilderOption } from "./Builder-option";
3
+ import { BuilderObject, BuilderObjectId } from "./BuilderObject";
4
+
5
+ export type BuilderQuestionType =
6
+ | "select-one"
7
+ | "select-many"
8
+ | "text"
9
+ | "color"
10
+ | "radio"
11
+ | "email"
12
+ | "time"
13
+ | "checkbox"
14
+ | "textarea"
15
+ | "date"
16
+ | "numeric-range"
17
+ | "duration";
18
+
19
+ export interface BuilderQuestionDto {
20
+ readonly id: BuilderObjectId.QuestionID;
21
+ _type: BuilderQuestionType;
22
+ text: string;
23
+ options: ReadonlyArray<BuilderOptionDto>;
24
+ prefix: string;
25
+ }
26
+
27
+ export class BuilderQuestion extends BuilderObject<"builder-question", BuilderQuestionDto> {
28
+ readonly objectType = "builder-question";
29
+ id: BuilderObjectId.QuestionID;
30
+ type: BuilderQuestionType;
31
+ questionText = "";
32
+ options: BuilderOption[] = [];
33
+ prefix = "";
34
+
35
+ static create = (type: BuilderQuestionType) => {
36
+ const id = BuilderObjectId.questionId();
37
+
38
+ return new BuilderQuestion({
39
+ id,
40
+ _type: type,
41
+ text: "",
42
+ options: [],
43
+ prefix: "",
44
+ });
45
+ };
46
+
47
+ public static fromJson(dto: BuilderQuestionDto): BuilderQuestion {
48
+ const question = new BuilderQuestion(dto);
49
+ return question;
50
+ }
51
+
52
+ private constructor(dto: BuilderQuestionDto) {
53
+ super(dto);
54
+ this.id = dto.id as BuilderObjectId.QuestionID;
55
+ this.type = dto._type;
56
+ this.questionText = dto.text;
57
+ this.prefix = dto.prefix;
58
+ this.options = dto.options.map((o) => BuilderOption.fromJson(o));
59
+ }
60
+
61
+ addOption(label: string, value: number, atIndex = -1) {
62
+ const option = BuilderOption.create(value, label);
63
+ if (atIndex >= 0 && atIndex < this.options.length) {
64
+ this.options.splice(atIndex, 0, option);
65
+ } else {
66
+ this.options.push(option);
67
+ }
68
+ return option;
69
+ }
70
+
71
+ deleteOption(option: BuilderOption): boolean {
72
+ const filtered = this.options.filter((o) => o.id !== option.id);
73
+ const didDelete = filtered.length === this.options.length - 1;
74
+ this.options = filtered;
75
+ return didDelete;
76
+ }
77
+
78
+ toJson() {
79
+ const optionsJson = this.options.map((o) => o.toJson());
80
+ const dto: BuilderQuestionDto = {
81
+ id: this.id,
82
+ prefix: this.prefix,
83
+ _type: this.type,
84
+ text: this.questionText,
85
+ options: optionsJson,
86
+ };
87
+ return dto;
88
+ }
89
+
90
+ clone(): BuilderQuestionDto {
91
+ const cloneId = BuilderObjectId.questionId();
92
+ const dto = this.toJson();
93
+ const optionsClone = this.options.map((o) => o.clone());
94
+ const clonedDto: BuilderQuestionDto = {
95
+ ...dto,
96
+ id: cloneId,
97
+ options: optionsClone,
98
+ };
99
+ return clonedDto;
100
+ }
101
+ }
@@ -0,0 +1,302 @@
1
+ import type { BuilderSchemaDto } from "./Builder-schema";
2
+ import { BuilderSchema } from "./Builder-schema";
3
+ import { BuilderPage } from "./Builder-page";
4
+ import type { BuilderTagDto } from "./BuilderTag";
5
+ import { BuilderTag } from "./BuilderTag";
6
+ import type { BuilderObjectId } from "./BuilderObject";
7
+
8
+ const tag1: BuilderTagDto = BuilderTag.create("tag1", "This tag is defined in schemaDto1").toJson();
9
+
10
+ const tag2: BuilderTagDto = BuilderTag.create("tag2", "This tag is defined in schemaDto1").toJson();
11
+ const tag3: BuilderTagDto = BuilderTag.create("tag3", "This tag is defined in schemaDto1").toJson();
12
+
13
+ const schemaDto1: BuilderSchemaDto = {
14
+ backgroundColor: "gray",
15
+ baseHeight: 1200,
16
+ baseWidth: 1022,
17
+ mainImage: false,
18
+ id: "dto1",
19
+ name: "dto1-name",
20
+ tags: [tag1, tag2, tag3],
21
+ pages: [
22
+ {
23
+ id: "page1" as BuilderObjectId.PageID,
24
+ _type: "info-page",
25
+ prefix: "p1",
26
+ mainText: {
27
+ text: "hello from test",
28
+ autoplay: false,
29
+ autoplayDelay: 0,
30
+ audioFile: false,
31
+ },
32
+
33
+ nextButton: {
34
+ id: "next-button-text-id" as BuilderObjectId.QuestionOptionID,
35
+ label: "Neste",
36
+ value: -1,
37
+ },
38
+ defaultQuestion: {
39
+ id: "q1" as BuilderObjectId.QuestionID,
40
+ prefix: "one-prefix",
41
+ _type: "select-one",
42
+ text: "q1-text",
43
+ options: [
44
+ {
45
+ id: "opt-nei" as BuilderObjectId.QuestionOptionID,
46
+ value: 0,
47
+ label: "Nei",
48
+ },
49
+ {
50
+ id: "opt-ja" as BuilderObjectId.QuestionOptionID,
51
+ value: 1,
52
+ label: "Ja",
53
+ },
54
+ {
55
+ id: "opt-vet-ikke" as BuilderObjectId.QuestionOptionID,
56
+ value: 9,
57
+ label: "Vet ikke",
58
+ },
59
+ ],
60
+ },
61
+ tags: [
62
+ tag1.tag,
63
+ tag2.tag,
64
+ // { tag: 'can_read', description: 'The patient can read' },
65
+ // { tag: 'is grown up', description: 'Is grownUp.' }
66
+ ],
67
+ questions: [],
68
+ autoplaySequence: [],
69
+ },
70
+ {
71
+ id: "page2" as BuilderObjectId.PageID,
72
+ _type: "multi-select",
73
+ prefix: "page2-prefix",
74
+ tags: [tag3.tag],
75
+ mainText: {
76
+ text: "hello from test",
77
+ autoplay: false,
78
+ autoplayDelay: 0,
79
+ audioFile: false,
80
+ },
81
+ nextButton: {
82
+ id: "next-button-id-page2" as BuilderObjectId.QuestionOptionID,
83
+ label: "Neste",
84
+ value: -1,
85
+ },
86
+ defaultQuestion: {
87
+ id: "default-q" as BuilderObjectId.QuestionID,
88
+ prefix: "one-prefix",
89
+ _type: "select-one",
90
+ text: "q1",
91
+ options: [],
92
+ },
93
+ questions: [
94
+ {
95
+ id: "q1" as BuilderObjectId.QuestionID,
96
+ prefix: "one-prefix",
97
+ _type: "select-one",
98
+ text: "q1-text",
99
+ options: [
100
+ {
101
+ id: "opt-nei" as BuilderObjectId.QuestionOptionID,
102
+ value: 0,
103
+ label: "Nei",
104
+ },
105
+ {
106
+ id: "opt-ja" as BuilderObjectId.QuestionOptionID,
107
+ value: 1,
108
+ label: "Ja",
109
+ },
110
+ {
111
+ id: "opt-vet-ikke" as BuilderObjectId.QuestionOptionID,
112
+ value: 9,
113
+ label: "Vet ikke",
114
+ },
115
+ ],
116
+ },
117
+ ],
118
+ autoplaySequence: [],
119
+ },
120
+ ],
121
+ rules: [],
122
+ prefix: "d1",
123
+ };
124
+
125
+ let builderSchema = BuilderSchema.create("a-id", "test", "a");
126
+
127
+ beforeEach(() => {
128
+ builderSchema = BuilderSchema.create("a-id", "test-name", "a");
129
+ });
130
+
131
+ describe("Builder schema", () => {
132
+ test("Can add pages.", () => {
133
+ builderSchema.addPage("question");
134
+ builderSchema.addPage("info-page");
135
+ expect(builderSchema.pages.length).toBe(2);
136
+ const dto = builderSchema.compile().schema;
137
+ expect(dto.pages.length).toBe(2);
138
+ expect(dto.id).toBe(builderSchema.id);
139
+ expect(dto.prefix).toBe(builderSchema.prefix);
140
+ expect(dto.baseHeight).toBe(builderSchema.baseHeight);
141
+ expect(dto.baseWidth).toBe(builderSchema.baseWidth);
142
+ expect(dto.backgroundColor).toBe(builderSchema.backgroundColor);
143
+ });
144
+
145
+ test("Can delete page by passing reference", () => {
146
+ const p1 = builderSchema.addPage("question");
147
+ const p2 = builderSchema.addPage("question");
148
+ const p3 = builderSchema.addPage("info-page");
149
+ expect(builderSchema.pages.length).toBe(3);
150
+ const result = builderSchema.deletePage(p1);
151
+ expect(builderSchema.pages.length).toBe(2);
152
+ expect(result).toBe(true);
153
+ const result2 = builderSchema.deletePage(p2);
154
+ expect(result2).toBe(true);
155
+ expect(builderSchema.pages.length).toBe(1);
156
+ expect(builderSchema.pages[0]).toBe(p3);
157
+ const result3 = builderSchema.deletePage(p2);
158
+ expect(result3).toBe(false);
159
+ const result4 = builderSchema.deletePage(p3);
160
+ expect(result4).toBe(true);
161
+ });
162
+
163
+ test("Can create Schema from dto", () => {
164
+ const s = BuilderSchema.fromJson(schemaDto1);
165
+ expect(s.id).toBe(schemaDto1.id);
166
+ expect(s.prefix).toBe(schemaDto1.prefix);
167
+ expect(s.baseHeight).toBe(schemaDto1.baseHeight);
168
+ expect(s.baseWidth).toBe(schemaDto1.baseWidth);
169
+ expect(s.backgroundColor).toBe(schemaDto1.backgroundColor);
170
+ expect(s.pages.length).toBe(schemaDto1.pages.length);
171
+ const p1Dto = schemaDto1.pages[0];
172
+ expect(s.pages[0].id).toBe(p1Dto.id);
173
+ const defaultQuestion = s.pages[0].defaultQuestion;
174
+ expect(defaultQuestion).toBeDefined();
175
+ expect(defaultQuestion.id).toBe(p1Dto.defaultQuestion.id);
176
+ expect(defaultQuestion.options.length).toBe(p1Dto.defaultQuestion.options.length);
177
+ const options = defaultQuestion.options;
178
+ const nei = options[0];
179
+ const ja = options[1];
180
+ const vetIkke = options[2];
181
+ expect(nei.label).toBe("Nei");
182
+ expect(ja.label).toBe("Ja");
183
+ expect(vetIkke.label).toBe("Vet ikke");
184
+ expect(vetIkke.id).toBe(p1Dto.defaultQuestion.options[2].id);
185
+ });
186
+
187
+ test("fromJson === toJson", () => {
188
+ const s = BuilderSchema.fromJson(schemaDto1);
189
+ const json = s.toJson();
190
+ expect(schemaDto1).toStrictEqual(json);
191
+ });
192
+ test("Can add page at concrete index", () => {
193
+ const p1 = builderSchema.addPage("form");
194
+ expect(p1.pageType).toBe("form");
195
+ const p2 = builderSchema.addPage("multi-select", 0);
196
+ expect(p2.pageType).toBe("multi-select");
197
+ const pages = builderSchema.pages;
198
+ expect(pages[0].id).toBe(p2.id);
199
+ builderSchema.addPage("form");
200
+ builderSchema.addPage("form");
201
+ const last = builderSchema.addPage("form");
202
+ expect(builderSchema.pages[builderSchema.pages.length - 1]).toBe(last);
203
+ const p3 = builderSchema.addPage("info-page", 4);
204
+ expect(pages[4].id).toBe(p3.id);
205
+ });
206
+ test("Can move page up and down", () => {
207
+ const p1 = builderSchema.addPage("form");
208
+ const p2 = builderSchema.addPage("form");
209
+ const p3 = builderSchema.addPage("form");
210
+ const p4 = builderSchema.addPage("multi-select");
211
+ const last = builderSchema.addPage("form");
212
+ const pages = builderSchema.pages;
213
+ builderSchema.movePage(p2, 0);
214
+ expect(pages[0]).toBe(p2);
215
+ expect(pages[1]).toBe(p1);
216
+ expect(builderSchema.movePage(p4, 4)).toBeTruthy();
217
+ expect(builderSchema.movePage(p4, 5)).toBeFalsy();
218
+ expect(builderSchema.movePage(p4, 10)).toBeFalsy();
219
+
220
+ // expect(pages[0].id).toBe(p4.id);
221
+ });
222
+ test("Can clone a page and insert at index", () => {
223
+ const p1 = builderSchema.addPage("form");
224
+ const p2 = builderSchema.addPage("form");
225
+ const p3 = builderSchema.addPage("form");
226
+ const mainTextContent = "Hello from test";
227
+ const p4 = builderSchema.addPage("multi-select");
228
+ p3.mainText.text = mainTextContent;
229
+ const p1Clone = BuilderPage.fromJson(p1.clone());
230
+ const beforeLen = builderSchema.pages.length;
231
+ builderSchema.insertPage(p1Clone, 2);
232
+ const pages = builderSchema.pages;
233
+ expect(beforeLen + 1).toBe(pages.length);
234
+ expect(pages[2]).toBe(p1Clone);
235
+ });
236
+ test("Will not insert a page that is already in array.", () => {
237
+ const p1 = builderSchema.addPage("form");
238
+ const p2 = builderSchema.addPage("form");
239
+ const p3 = builderSchema.addPage("form");
240
+ const beforeLen = builderSchema.pages.length;
241
+ const result = builderSchema.insertPage(p1, 0);
242
+ const pages = builderSchema.pages;
243
+ expect(beforeLen).toBe(pages.length);
244
+ expect(result).toBe(false);
245
+ });
246
+
247
+ test("Can generate a info-page", () => {
248
+ const p1 = builderSchema.addPage("info-page");
249
+ const p2 = builderSchema.addPage("question");
250
+ p2.defaultQuestion.addOption("Ja", 1, 0);
251
+ p2.defaultQuestion.addOption("Nei", 0);
252
+ p1.nextButton.label = "Neste";
253
+ builderSchema.backgroundColor = "red";
254
+ builderSchema.baseHeight = 600;
255
+ builderSchema.baseWidth = 500;
256
+ builderSchema.prefix = "as";
257
+ builderSchema.name = "depressed";
258
+ const schema = builderSchema.compile().schema;
259
+ expect(schema.backgroundColor).toBe("red");
260
+ expect(schema.baseHeight).toBe(600);
261
+ expect(schema.baseWidth).toBe(500);
262
+ expect(schema.prefix).toBe("as");
263
+ expect(schema.pages.length).toBe(2);
264
+ const schemaP1 = schema.pages[0];
265
+ const schemaP2 = schema.pages[1];
266
+ expect(schemaP1.id).toBe(p1.id);
267
+ expect(schemaP2.id).toBe(p2.id);
268
+ expect(schemaP1.elements.length).toBe(2);
269
+
270
+ // Has Buttons
271
+ const options = p2.defaultQuestion.options;
272
+ expect(schemaP2.elements.length).toBe(options.length + 1);
273
+ });
274
+ test("Can get ruleInput!", () => {
275
+ const p0 = builderSchema.addPage("info-page");
276
+ p0.prefix = "info_page_prefix_";
277
+ const p1 = builderSchema.addPage("question");
278
+ p1.prefix = "p1_prefix_";
279
+ const p2 = builderSchema.addPage("question");
280
+ p2.prefix = "p2_prefix_";
281
+ p1.defaultQuestion.addOption("Ja", 1, 0);
282
+ p1.defaultQuestion.addOption("Nei", 0, 0);
283
+ p2.defaultQuestion.addOption("Ja", 1, 0);
284
+ p2.defaultQuestion.addOption("Nei", 0);
285
+ const ruleInput = builderSchema.getRuleInput();
286
+ expect(ruleInput.questionVars.length).toBe(2);
287
+ expect(ruleInput.jumpToPageActions.length).toBe(3);
288
+ // TODO add TAGS!!
289
+ expect(ruleInput.excludeByTagActions.length).toBe(0);
290
+ expect(ruleInput.excludeByPageIdActions.length).toBe(3);
291
+ const allPrefixes = new Set(ruleInput.jumpToPageActions.map((a) => a.pageId));
292
+ expect(allPrefixes.has(p0.prefix)).toBe(true);
293
+ expect(allPrefixes.has(p1.prefix)).toBe(true);
294
+ expect(allPrefixes.has(p2.prefix)).toBe(true);
295
+ ruleInput.questionVars.forEach((v) => {
296
+ expect(allPrefixes.has(v.varId)).toBe(true);
297
+ });
298
+ ruleInput.jumpToPageActions.forEach((v) => {
299
+ expect(allPrefixes.has(v.pageId)).toBe(true);
300
+ });
301
+ });
302
+ });