@media-quest/builder 0.0.26 → 0.0.27

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.
@@ -0,0 +1,209 @@
1
+ import { BuilderTag, BuilderTagDto } from "../tag/BuilderTag";
2
+ import { PagePrefix } from "../primitives/page-prefix";
3
+ import { BuilderPageCollection } from "./Builder-page-collection";
4
+ import { OptionID, PageID, QuestionID } from "../primitives/ID";
5
+ import { BuilderPage, BuilderPageDto } from "./Builder-page";
6
+ import { SumScoreVariable } from "../sum-score/sum-score-variable";
7
+ import { SumScoreVariableCollection } from "../sum-score/sum-score-variable-collection";
8
+
9
+ const tag1: BuilderTagDto = BuilderTag.create("tag1", "This tag is defined in schemaDto1").toJson();
10
+
11
+ const tag2: BuilderTagDto = BuilderTag.create("tag2", "This tag is defined in schemaDto1").toJson();
12
+ const tag3: BuilderTagDto = BuilderTag.create("tag3", "This tag is defined in schemaDto1").toJson();
13
+
14
+ const sumScoreVariable1 = SumScoreVariable.create({
15
+ name: "ss1",
16
+ useAvg: true,
17
+ description: "ss1 description",
18
+ });
19
+ const sumScoreVariable2 = SumScoreVariable.create({
20
+ name: "ss2",
21
+ useAvg: true,
22
+ description: "ss2 description",
23
+ });
24
+ const sumScoreVariable3 = SumScoreVariable.create({
25
+ name: "ss3",
26
+ useAvg: true,
27
+ description: "ss3 description",
28
+ });
29
+
30
+ const pages: BuilderPageDto[] = [
31
+ {
32
+ id: PageID.validateOrCreate("a".repeat(24)),
33
+ _type: "info-page",
34
+ prefix: PagePrefix.fromStringOrThrow("p1"),
35
+ mainText: {
36
+ text: "hello from test",
37
+ autoplay: false,
38
+ autoplayDelay: 0,
39
+ audioFile: false,
40
+ },
41
+ includedInSumScores: [{ sumScoreVariableId: sumScoreVariable1.id, weight: 1 }],
42
+
43
+ nextButton: {
44
+ id: OptionID.create(),
45
+ label: "Neste",
46
+ value: -1,
47
+ },
48
+ defaultQuestion: {
49
+ id: QuestionID.create(),
50
+ prefix: "one-prefix",
51
+ _type: "select-one",
52
+ text: "q1-text",
53
+ options: [
54
+ {
55
+ id: "opt-nei" as OptionID,
56
+ value: 0,
57
+ label: "Nei",
58
+ },
59
+ {
60
+ id: "opt-ja" as OptionID,
61
+ value: 1,
62
+ label: "Ja",
63
+ },
64
+ {
65
+ id: "opt-vet-ikke" as OptionID,
66
+ value: 9,
67
+ label: "Vet ikke",
68
+ },
69
+ ],
70
+ },
71
+ tags: [
72
+ tag1.tag,
73
+ tag2.tag,
74
+ // { tag: 'can_read', description: 'The patient can read' },
75
+ // { tag: 'is grown up', description: 'Is grownUp.' }
76
+ ],
77
+ autoplaySequence: [],
78
+ },
79
+ {
80
+ id: PageID.dummy.b,
81
+ _type: "question",
82
+ prefix: PagePrefix.fromStringOrThrow("page2-prefix"),
83
+ tags: [tag3.tag],
84
+ includedInSumScores: [],
85
+ mainText: {
86
+ text: "hello from test",
87
+ autoplay: false,
88
+ autoplayDelay: 0,
89
+ audioFile: false,
90
+ },
91
+ nextButton: {
92
+ id: "next-button-id-page2" as OptionID,
93
+ label: "Neste",
94
+ value: -1,
95
+ },
96
+ defaultQuestion: {
97
+ id: QuestionID.validateOrCreate("default-question-id"),
98
+ prefix: "one-prefix",
99
+ _type: "select-one",
100
+ text: "q1",
101
+ options: [],
102
+ },
103
+ autoplaySequence: [],
104
+ },
105
+ ];
106
+
107
+ let sumScoreVariableCollection = SumScoreVariableCollection.create([
108
+ sumScoreVariable1,
109
+ sumScoreVariable2,
110
+ sumScoreVariable3,
111
+ ]);
112
+ let empty = BuilderPageCollection.create([]);
113
+ beforeEach(() => {
114
+ sumScoreVariableCollection = SumScoreVariableCollection.create([
115
+ sumScoreVariable1,
116
+ sumScoreVariable2,
117
+ sumScoreVariable3,
118
+ ]);
119
+ empty = BuilderPageCollection.create([]);
120
+ });
121
+
122
+ describe("Builder page collection", () => {
123
+ test("Can add pages.", () => {
124
+ const p1 = empty.add("question");
125
+ const p2 = empty.add("info-page");
126
+ expect(empty.size).toBe(2);
127
+ });
128
+
129
+ test("Can delete page by id,", () => {
130
+ const p1 = empty.add("question");
131
+ const p2 = empty.add("question");
132
+ const p3 = empty.add("info-page");
133
+ expect(empty.size).toBe(3);
134
+ const result = empty.deleteById(p1.id);
135
+ expect(empty.size).toBe(2);
136
+ expect(result).toBe(true);
137
+ const result2 = empty.deleteById(p2.id);
138
+ expect(result2).toBe(true);
139
+ expect(empty.size).toBe(1);
140
+ expect(empty.pages[0]).toBe(p3);
141
+ const result3 = empty.deleteById(PageID.create());
142
+ expect(result3).toBe(false);
143
+ });
144
+
145
+ test("fromJson === toJson", () => {
146
+ const col = BuilderPageCollection.create(pages);
147
+ const json = col.toJson();
148
+ expect(pages).toStrictEqual(json);
149
+ });
150
+ //
151
+
152
+ test("Can add page at concrete index", () => {
153
+ const p1 = empty.add("question");
154
+ expect(p1.pageType).toBe("question");
155
+ const p2 = empty.add("info-page", 0);
156
+ expect(p2.pageType).toBe("info-page");
157
+
158
+ expect(empty.pages[0].id).toBe(p2.id);
159
+ empty.add("question");
160
+ empty.add("question");
161
+ const last = empty.add("question");
162
+ expect(empty.pages[empty.pages.length - 1]).toBe(last);
163
+ const p3 = empty.add("info-page", 4);
164
+ expect(empty.pages[4].id).toBe(p3.id);
165
+ });
166
+ //
167
+ test("Can move page up and down", () => {
168
+ const p1 = empty.add("question");
169
+ const p2 = empty.add("question");
170
+ const p3 = empty.add("question");
171
+ const p4 = empty.add("info-page");
172
+ const last = empty.add("question");
173
+ empty.movePage(p2, 0);
174
+ expect(empty.pages[0]).toBe(p2);
175
+ expect(empty.pages[1]).toBe(p1);
176
+ expect(empty.movePage(p4, 4)).toBeTruthy();
177
+ expect(empty.movePage(p4, 5)).toBeFalsy();
178
+ expect(empty.movePage(p4, 10)).toBeFalsy();
179
+
180
+ expect(empty.pages[4].id).toBe(p4.id);
181
+ expect(empty.pages[3].id).toBe(last.id);
182
+ });
183
+ //
184
+ test("Can clone a page and insert at index", () => {
185
+ const p1 = empty.add("question");
186
+ const p2 = empty.add("question");
187
+ const p3 = empty.add("question");
188
+ const mainTextContent = "Hello from test";
189
+ const p4 = empty.add("info-page");
190
+ p3.mainText.text = mainTextContent;
191
+ const p1Clone = BuilderPage.fromJson(p1.clone());
192
+ const beforSize = empty.size;
193
+ empty.insertPage(p1Clone, 2);
194
+ // const pages = s1.pages;
195
+ expect(beforSize + 1).toBe(empty.size);
196
+ expect(empty.pages[2]).toBe(p1Clone);
197
+ });
198
+
199
+ test("Will not insert a page that is already in array.", () => {
200
+ const p1 = empty.add("question");
201
+ const p2 = empty.add("question");
202
+ const p3 = empty.add("question");
203
+ const beforeSize = empty.size;
204
+ const result = empty.insertPage(p1, 0);
205
+ const pages = empty.pages;
206
+ expect(beforeSize).toBe(empty.size);
207
+ expect(result).toBe(false);
208
+ });
209
+ });
@@ -0,0 +1,113 @@
1
+ import { DUtil } from "@media-quest/engine";
2
+ import { BuilderPage, BuilderPageDto } from "./Builder-page";
3
+ import { PageID, PagePrefix, SumScoreVariableID } from "../public-api";
4
+ import { SumScoreVariableCollection } from "../sum-score/sum-score-variable-collection";
5
+ import { SumScoreVariable } from "../sum-score/sum-score-variable";
6
+
7
+ const U = DUtil;
8
+ export type BuilderPageType = "info-page" | "question";
9
+
10
+ export class BuilderPageCollection implements Iterable<BuilderPage> {
11
+ private _all: Array<BuilderPage> = [];
12
+
13
+ public static create(pages: BuilderPageDto[]) {
14
+ const page = new BuilderPageCollection(pages);
15
+ page._all = pages.map((p) => BuilderPage.fromJson(p));
16
+ return page;
17
+ }
18
+ private constructor(initialPages: BuilderPageDto[]) {}
19
+
20
+ /** @internal - used by Schema*/
21
+ _init(pages: BuilderPageDto[]) {
22
+ this._all = pages.map(BuilderPage.fromJson);
23
+ }
24
+
25
+ add(type: BuilderPageType, atIndex = -1): BuilderPage {
26
+ const pagePrefix = PagePrefix.create();
27
+ const newPage = BuilderPage.create(type, pagePrefix.value);
28
+ if (atIndex >= 0 && atIndex < this._all.length) {
29
+ this._all.splice(atIndex, 0, newPage);
30
+ } else {
31
+ this._all.push(newPage);
32
+ }
33
+ return newPage;
34
+ }
35
+
36
+ deleteById(id: PageID) {
37
+ const index = this._all.findIndex((p) => p.id === id);
38
+ if (index !== -1) {
39
+ this._all.splice(index, 1);
40
+ return true;
41
+ } else {
42
+ return false;
43
+ }
44
+ }
45
+
46
+ private getPageById(pageId: PageID) {
47
+ const maybePage = this._all.find((p) => p.id === pageId);
48
+ return maybePage ?? false;
49
+ }
50
+ get size() {
51
+ return this._all.length;
52
+ }
53
+ get pages(): ReadonlyArray<BuilderPage> {
54
+ return [...this._all];
55
+ }
56
+ toJson(): ReadonlyArray<BuilderPageDto> {
57
+ return this._all.map((p) => p.toJson());
58
+ }
59
+
60
+ [Symbol.iterator]() {
61
+ const list = [...this._all];
62
+ return list[Symbol.iterator]();
63
+ }
64
+
65
+ movePage(page: BuilderPage, toIndex: number) {
66
+ const index = this._all.indexOf(page);
67
+ if (index < 0) {
68
+ return false;
69
+ }
70
+ const isValidIndex = U.isInRange(0, this._all.length - 1);
71
+ if (!isValidIndex(toIndex)) {
72
+ return false;
73
+ }
74
+ // console.log('Moving from :' + index + ' to: ' + toIndex);
75
+ this._all.splice(index, 1);
76
+ this._all.splice(toIndex, 0, page);
77
+ return true;
78
+ }
79
+
80
+ insertPage(page: BuilderPage, atIndex: number) {
81
+ const isValidIndex = U.isInRange(0, this._all.length - 1);
82
+ if (!isValidIndex(atIndex)) {
83
+ return false;
84
+ }
85
+ const exists = !!this._all.find((p) => p.id === page.id);
86
+ if (exists) {
87
+ return false;
88
+ }
89
+ this._all.splice(atIndex, 0, page);
90
+ return true;
91
+ }
92
+
93
+ addSumScoreVariable(sumScoreVariable: SumScoreVariable, pageId: PageID, weight: number) {
94
+ const maybePage = this.getPageById(pageId);
95
+ if (!maybePage) return false;
96
+ maybePage.sumScoreVariableSet(sumScoreVariable, weight);
97
+
98
+ // maybePage.addSumScoreVariable(maybeVariable, weight);
99
+ return true;
100
+ }
101
+
102
+ updateAllData(context: { sumScoreVariables: ReadonlyArray<SumScoreVariable> }) {
103
+ this._all.forEach((p) => {
104
+ p.sumScoreVariableUpdateData(context.sumScoreVariables);
105
+ });
106
+ }
107
+
108
+ sumScoreVariableDelete(sumScoreVariableID: SumScoreVariableID) {
109
+ this._all.forEach((p) => {
110
+ p.sumScoreVariableDelete(sumScoreVariableID);
111
+ });
112
+ }
113
+ }
@@ -1,11 +1,10 @@
1
1
  import { BuilderPage } from "./Builder-page";
2
2
  import type { BuilderPageDto } from "./Builder-page";
3
- import type { BuilderQuestionDto } from "./Builder-question";
4
- import { BuilderQuestion } from "./Builder-question";
3
+ import type { BuilderQuestionDto } from "../Builder-question";
5
4
  import { DUtil } from "@media-quest/engine";
6
- import { PagePrefix } from "./primitives/page-prefix";
7
- import { SchemaPrefix } from "./primitives/schema-prefix";
8
- import { OptionID, PageID, QuestionID } from "./primitives/ID";
5
+ import { PagePrefix } from "../primitives/page-prefix";
6
+ import { SchemaPrefix } from "../primitives/schema-prefix";
7
+ import { OptionID, PageID, QuestionID } from "../primitives/ID";
9
8
 
10
9
  const U = DUtil;
11
10
  const deleteIdsFromPage = (page: BuilderPageDto) => {
@@ -24,6 +23,7 @@ const pxx = PagePrefix.fromStringOrThrow("pxx");
24
23
  const questionPageDto: BuilderPageDto = {
25
24
  _type: "question",
26
25
  autoplaySequence: [],
26
+ includedInSumScores: [],
27
27
  mainText: {
28
28
  text: "Velkommen til denne undersøkelsen.",
29
29
  audioFile: false,
@@ -66,7 +66,8 @@ const questionPageDto: BuilderPageDto = {
66
66
  };
67
67
 
68
68
  const multiQuestionPageDto: BuilderPageDto = {
69
- _type: "multi-select",
69
+ _type: "question",
70
+ includedInSumScores: [],
70
71
  mainText: {
71
72
  text: "Velkommen til denne undersøkelsen.",
72
73
  audioFile: false,
@@ -1,21 +1,22 @@
1
- import type { BuilderQuestionDto, BuilderQuestionType } from "./Builder-question";
2
- import { BuilderQuestion } from "./Builder-question";
3
- import { BuilderObject } from "./BuilderObject";
4
- import type { BuilderOptionDto } from "./Builder-option";
5
- import { BuilderOption } from "./Builder-option";
6
- import type { BuilderMainVideoDto } from "./BuilderMainVideoDto";
7
- import type { BuilderMainImageDto } from "./BuilderMainImageDto";
8
- import type { BuilderMainTextDto } from "./BuilderMainText";
9
- import { BuilderMainText } from "./BuilderMainText";
10
- import { RuleVariableOption, RuleQuestionVariable } from "./rulebuilder";
1
+ import type { BuilderQuestionDto, BuilderQuestionType } from "../Builder-question";
2
+ import { BuilderQuestion } from "../Builder-question";
3
+ import { BuilderObject } from "../BuilderObject";
4
+ import type { BuilderOptionDto } from "../Builder-option";
5
+ import { BuilderOption } from "../Builder-option";
6
+ import type { BuilderMainVideoDto } from "../BuilderMainVideoDto";
7
+ import type { BuilderMainImageDto } from "../BuilderMainImageDto";
8
+ import type { BuilderMainTextDto } from "../BuilderMainText";
9
+ import { BuilderMainText } from "../BuilderMainText";
10
+ import { RuleVariableOption, RuleQuestionVariable } from "../rulebuilder";
11
11
  import { DUtil } from "@media-quest/engine";
12
- import { PagePrefix, PagePrefixValue } from "./primitives/page-prefix";
13
- import { VarID } from "./primitives/varID";
14
- import { SchemaPrefix } from "./primitives/schema-prefix";
15
- import { PageID } from "./primitives/ID";
12
+ import { PagePrefix, PagePrefixValue } from "../primitives/page-prefix";
13
+ import { VarID } from "../primitives/varID";
14
+ import { SchemaPrefix } from "../primitives/schema-prefix";
15
+ import { PageID, SumScoreVariableID } from "../primitives/ID";
16
+ import { SumScoreVariable } from "../sum-score/sum-score-variable";
16
17
 
17
18
  const U = DUtil;
18
- export type BuilderPageType = "info-page" | "question" | "multi-select" | "form";
19
+ export type BuilderPageType = "info-page" | "question";
19
20
 
20
21
  export interface BuilderPageDto {
21
22
  readonly id: PageID;
@@ -25,6 +26,7 @@ export interface BuilderPageDto {
25
26
  nextButton: BuilderOptionDto;
26
27
  defaultQuestion: BuilderQuestionDto;
27
28
  mainMedia?: BuilderMainImageDto | BuilderMainVideoDto;
29
+ includedInSumScores: Array<{ sumScoreVariableId: SumScoreVariableID; weight: number }>;
28
30
  autoplaySequence: Array<string>;
29
31
  tags: ReadonlyArray<string>;
30
32
  }
@@ -36,6 +38,16 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
36
38
  private _prefix: PagePrefix;
37
39
  private readonly _tags: Set<string>;
38
40
  private _backgroundColor = "#FFFFFF";
41
+ private _includedInSumScores: Map<
42
+ SumScoreVariableID,
43
+ {
44
+ sumScoreVariableId: SumScoreVariableID;
45
+ weight: number;
46
+ name: string;
47
+ description: string;
48
+ }
49
+ > = new Map();
50
+
39
51
  mainMedia: BuilderMainVideoDto | BuilderMainImageDto | false = false;
40
52
  defaultQuestion: BuilderQuestion;
41
53
  mainText: BuilderMainText;
@@ -60,7 +72,7 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
60
72
  nextButton: nextButtonDto,
61
73
  mainText: mainTextDto,
62
74
  prefix: _prefix,
63
- // questions: [],
75
+ includedInSumScores: [],
64
76
  tags: [],
65
77
  };
66
78
 
@@ -82,6 +94,15 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
82
94
  this.nextButton = BuilderOption.fromJson(dto.nextButton);
83
95
  this.defaultQuestion = BuilderQuestion.fromJson(dto.defaultQuestion);
84
96
  const tagList: string[] = Array.isArray(dto.tags) ? dto.tags : [];
97
+ const sumScores = Array.isArray(dto.includedInSumScores) ? dto.includedInSumScores : [];
98
+ sumScores.forEach((item) => {
99
+ this._includedInSumScores.set(item.sumScoreVariableId, {
100
+ sumScoreVariableId: item.sumScoreVariableId,
101
+ weight: item.weight,
102
+ name: "",
103
+ description: "",
104
+ });
105
+ });
85
106
  this._tags = new Set(tagList);
86
107
  if (dto.mainMedia) {
87
108
  this.mainMedia = dto.mainMedia;
@@ -99,6 +120,10 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
99
120
  this._pageType = value;
100
121
  }
101
122
 
123
+ get includedInSumScores() {
124
+ return [...this._includedInSumScores.values()];
125
+ }
126
+
102
127
  getQuestionVariables(
103
128
  modulePrefix: SchemaPrefix,
104
129
  pageNumber: number,
@@ -135,10 +160,36 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
135
160
  this._prefix.value = value;
136
161
  }
137
162
 
163
+ sumScoreVariableSet(sumScoreVariable: SumScoreVariable, weight: number) {
164
+ const { id, name, description } = sumScoreVariable;
165
+
166
+ this._includedInSumScores.set(sumScoreVariable.id, {
167
+ sumScoreVariableId: id,
168
+ weight,
169
+ name,
170
+ description,
171
+ });
172
+
173
+ return true;
174
+ }
175
+
176
+ sumScoreVariableUpdateData(variables: ReadonlyArray<SumScoreVariable>) {
177
+ variables.forEach((v) => {
178
+ const sumScoreEntry = this._includedInSumScores.get(v.id);
179
+ if (sumScoreEntry) {
180
+ this.sumScoreVariableSet(v, sumScoreEntry.weight);
181
+ }
182
+ });
183
+ }
184
+
138
185
  toJson(): BuilderPageDto {
139
186
  const mainText = this.mainText.toJson();
140
187
  const nextButton = this.nextButton.toJson();
141
188
  const mainMedia = this.mainMedia;
189
+ const includedInSumScores = this.includedInSumScores.map(({ sumScoreVariableId, weight }) => ({
190
+ sumScoreVariableId,
191
+ weight,
192
+ }));
142
193
  const dto: BuilderPageDto = {
143
194
  _type: this.pageType,
144
195
  mainText,
@@ -146,6 +197,7 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
146
197
  nextButton,
147
198
  id: this.id,
148
199
  tags: [...this.tags],
200
+ includedInSumScores,
149
201
  prefix: this._prefix.value,
150
202
  defaultQuestion: this.defaultQuestion.toJson(),
151
203
  };
@@ -179,4 +231,8 @@ export class BuilderPage extends BuilderObject<"builder-page", BuilderPageDto> {
179
231
  this._backgroundColor = color;
180
232
  }
181
233
  }
234
+
235
+ sumScoreVariableDelete(sumScoreVariableID: SumScoreVariableID) {
236
+ this._includedInSumScores.delete(sumScoreVariableID);
237
+ }
182
238
  }