@media-quest/builder 0.0.38 → 0.0.40
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/dist/public-api.d.ts +82 -99
- package/dist/public-api.js +36 -101
- package/dist/public-api.js.map +1 -1
- package/package.json +29 -29
- package/src/{theme → ARKIV}/button-bar/button-text-utils.ts +233 -233
- package/src/{theme → ARKIV}/button-bar/text-utils.spec.ts +105 -105
- package/src/Builder-option.ts +78 -62
- package/src/Builder-question.ts +98 -98
- package/src/Builder-schema.spec.ts +348 -348
- package/src/Builder-schema.ts +308 -306
- package/src/builder-compiler.ts +14 -20
- package/src/code-book/codebook-variable.ts +27 -27
- package/src/code-book/codebook.ts +89 -89
- package/src/media-files.ts +28 -32
- package/src/page/Builder-page-collection.spec.ts +219 -219
- package/src/page/Builder-page-collection.ts +129 -129
- package/src/page/Builder-page.spec.ts +177 -177
- package/src/page/Builder-page.ts +250 -250
- package/src/primitives/ID.ts +135 -135
- package/src/public-api.ts +29 -30
- package/src/rulebuilder/RuleAction.ts +105 -105
- package/src/schema-config.ts +25 -26
- package/src/sum-score/sum-score-variable-collection.spec.ts +68 -68
- package/src/sum-score/sum-score-variable-collection.ts +101 -101
- package/src/sum-score/sum-score-variable.ts +0 -1
- package/src/sum-score/sum-score.ts +166 -167
- package/src/tag/BuilderTag.ts +45 -45
- package/src/tag/Tag-Collection.ts +53 -53
- package/src/theme/Default-theme.ts +173 -188
- package/src/theme/IDefault-theme.ts +125 -125
- package/src/theme/ThemeCompiler.ts +10 -11
- package/src/theme/default-theme-compiler.spec.ts +31 -31
- package/src/theme/default-theme-compiler.ts +655 -652
- package/src/theme/icon-urls.ts +29 -29
- package/src/theme/icons.ts +117 -117
- package/src/theme/theme-utils.spec.ts +52 -52
- package/src/theme/theme-utils.ts +56 -56
- package/src/theme/theme2.ts +388 -386
- package/tsconfig.json +19 -19
- package/src/Builder-schema-dto.spec.ts +0 -155
- package/src/Builder-schema-dto.ts +0 -86
|
@@ -1,129 +1,129 @@
|
|
|
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
|
-
updateRelationShips(context: { sumScoreVariables: ReadonlyArray<SumScoreVariable> }) {
|
|
103
|
-
const { sumScoreVariables } = context;
|
|
104
|
-
|
|
105
|
-
// Update all relationships in pages.
|
|
106
|
-
this._all.forEach((p) => {
|
|
107
|
-
p.updateRelationShips(context.sumScoreVariables);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
// Set the used in pages array on every variable.
|
|
111
|
-
sumScoreVariables.forEach((v) => {
|
|
112
|
-
const usedInPages = this._all.filter((p) => p._isIncludedInSumScore(v.id));
|
|
113
|
-
v._setUsedInPages(usedInPages);
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
sumScoreVariableDelete(sumScoreVariableID: SumScoreVariableID) {
|
|
118
|
-
this._all.forEach((p) => {
|
|
119
|
-
p.sumScoreVariableDelete(sumScoreVariableID);
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
sumScoreVariableDeleteFromPage(pageId: PageID, sumScoreVariableId: SumScoreVariableID) {
|
|
124
|
-
const maybePage = this.getPageById(pageId);
|
|
125
|
-
if (!maybePage) return false;
|
|
126
|
-
maybePage.sumScoreVariableDelete(sumScoreVariableId);
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
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
|
+
updateRelationShips(context: { sumScoreVariables: ReadonlyArray<SumScoreVariable> }) {
|
|
103
|
+
const { sumScoreVariables } = context;
|
|
104
|
+
|
|
105
|
+
// Update all relationships in pages.
|
|
106
|
+
this._all.forEach((p) => {
|
|
107
|
+
p.updateRelationShips(context.sumScoreVariables);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Set the used in pages array on every variable.
|
|
111
|
+
sumScoreVariables.forEach((v) => {
|
|
112
|
+
const usedInPages = this._all.filter((p) => p._isIncludedInSumScore(v.id));
|
|
113
|
+
v._setUsedInPages(usedInPages);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
sumScoreVariableDelete(sumScoreVariableID: SumScoreVariableID) {
|
|
118
|
+
this._all.forEach((p) => {
|
|
119
|
+
p.sumScoreVariableDelete(sumScoreVariableID);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
sumScoreVariableDeleteFromPage(pageId: PageID, sumScoreVariableId: SumScoreVariableID) {
|
|
124
|
+
const maybePage = this.getPageById(pageId);
|
|
125
|
+
if (!maybePage) return false;
|
|
126
|
+
maybePage.sumScoreVariableDelete(sumScoreVariableId);
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -1,177 +1,177 @@
|
|
|
1
|
-
import { BuilderPage } from "./Builder-page";
|
|
2
|
-
import type { BuilderPageDto } from "./Builder-page";
|
|
3
|
-
import type { BuilderQuestionDto } from "../Builder-question";
|
|
4
|
-
import { DUtil } from "@media-quest/engine";
|
|
5
|
-
import { PagePrefix } from "../primitives/page-prefix";
|
|
6
|
-
import { SchemaPrefix } from "../primitives/schema-prefix";
|
|
7
|
-
import { OptionID, PageID, QuestionID, SumScoreVariableID } from "../primitives/ID";
|
|
8
|
-
import { SumScoreVariable } from "../sum-score/sum-score-variable";
|
|
9
|
-
|
|
10
|
-
const U = DUtil;
|
|
11
|
-
const deleteIdsFromPage = (page: BuilderPageDto) => {
|
|
12
|
-
const deleteIdsFromQuestion = (q: BuilderQuestionDto) => {
|
|
13
|
-
U.deleteProp(q, "id");
|
|
14
|
-
q.options.forEach((o) => {
|
|
15
|
-
U.deleteProp(o, "id");
|
|
16
|
-
});
|
|
17
|
-
};
|
|
18
|
-
U.deleteProp(page, "id");
|
|
19
|
-
deleteIdsFromQuestion(page.defaultQuestion);
|
|
20
|
-
return page;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const pxx = PagePrefix.fromStringOrThrow("pxx");
|
|
24
|
-
const questionPageDto: BuilderPageDto = {
|
|
25
|
-
_type: "question",
|
|
26
|
-
autoplaySequence: [],
|
|
27
|
-
includedInSumScores: [],
|
|
28
|
-
mainText: {
|
|
29
|
-
text: "Velkommen til denne undersøkelsen.",
|
|
30
|
-
audioFile: false,
|
|
31
|
-
autoplayDelay: 0,
|
|
32
|
-
autoplay: false,
|
|
33
|
-
},
|
|
34
|
-
tags: [],
|
|
35
|
-
|
|
36
|
-
nextButton: {
|
|
37
|
-
id: OptionID.dummy.a,
|
|
38
|
-
label: "Neste",
|
|
39
|
-
value: -1,
|
|
40
|
-
},
|
|
41
|
-
defaultQuestion: {
|
|
42
|
-
id: QuestionID.validateOrThrow("default-question-id"),
|
|
43
|
-
prefix: "q1",
|
|
44
|
-
text: "sadf",
|
|
45
|
-
options: [
|
|
46
|
-
{
|
|
47
|
-
id: "q1-opt1" as OptionID,
|
|
48
|
-
value: 0,
|
|
49
|
-
label: "Nei",
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
id: "q1-opt2" as OptionID,
|
|
53
|
-
value: 1,
|
|
54
|
-
label: "Ja",
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
id: "q1-opt3" as OptionID,
|
|
58
|
-
value: 9,
|
|
59
|
-
label: "Vet ikke",
|
|
60
|
-
},
|
|
61
|
-
],
|
|
62
|
-
_type: "select-one",
|
|
63
|
-
},
|
|
64
|
-
// id: "p1" as BuilderObjectId.PageID,
|
|
65
|
-
id: PageID.create(),
|
|
66
|
-
prefix: PagePrefix.fromStringOrThrow("pxx"),
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const multiQuestionPageDto: BuilderPageDto = {
|
|
70
|
-
_type: "question",
|
|
71
|
-
includedInSumScores: [],
|
|
72
|
-
mainText: {
|
|
73
|
-
text: "Velkommen til denne undersøkelsen.",
|
|
74
|
-
audioFile: false,
|
|
75
|
-
autoplay: false,
|
|
76
|
-
autoplayDelay: 0,
|
|
77
|
-
},
|
|
78
|
-
autoplaySequence: [],
|
|
79
|
-
defaultQuestion: {
|
|
80
|
-
id: QuestionID.create(),
|
|
81
|
-
prefix: "",
|
|
82
|
-
text: "",
|
|
83
|
-
options: [],
|
|
84
|
-
_type: "select-one",
|
|
85
|
-
},
|
|
86
|
-
tags: [],
|
|
87
|
-
nextButton: {
|
|
88
|
-
id: "next-button-id-3" as OptionID,
|
|
89
|
-
label: "Neste",
|
|
90
|
-
value: -1,
|
|
91
|
-
},
|
|
92
|
-
|
|
93
|
-
id: PageID.create(),
|
|
94
|
-
prefix: PagePrefix.fromStringOrThrow("pxx"),
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
let page = BuilderPage.create("info-page", PagePrefix.fromStringOrThrow("a"));
|
|
98
|
-
|
|
99
|
-
beforeEach(() => {
|
|
100
|
-
page = BuilderPage.create("info-page", PagePrefix.fromStringOrThrow("a"));
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
describe("Builder Page", () => {
|
|
104
|
-
test("Can create page, and convert to json, and back", () => {
|
|
105
|
-
const page1 = BuilderPage.fromJson(questionPageDto);
|
|
106
|
-
const asJson = page1.toJson();
|
|
107
|
-
expect(questionPageDto).toStrictEqual(asJson);
|
|
108
|
-
const page2 = BuilderPage.fromJson(multiQuestionPageDto);
|
|
109
|
-
const asJson2 = page2.toJson();
|
|
110
|
-
expect(multiQuestionPageDto).toStrictEqual(asJson2);
|
|
111
|
-
// expect(page1.prefix).toBe(pageDto.prefix);
|
|
112
|
-
});
|
|
113
|
-
test("Can clone a page and everything except id is equal ", () => {
|
|
114
|
-
const page1 = BuilderPage.create("info-page", PagePrefix.fromStringOrThrow("as"));
|
|
115
|
-
const clone1 = BuilderPage.fromJson(page1.clone());
|
|
116
|
-
const page1Json = deleteIdsFromPage(page1.clone());
|
|
117
|
-
const clone1Json = deleteIdsFromPage(page1.clone());
|
|
118
|
-
expect(page1Json).toStrictEqual(clone1Json);
|
|
119
|
-
|
|
120
|
-
const page2 = BuilderPage.fromJson(questionPageDto);
|
|
121
|
-
const clone2 = BuilderPage.fromJson(page2.clone());
|
|
122
|
-
const page2Json = deleteIdsFromPage(page2.toJson());
|
|
123
|
-
const clone2Json = deleteIdsFromPage(clone2.toJson());
|
|
124
|
-
expect(page2Json).toStrictEqual(clone2Json);
|
|
125
|
-
|
|
126
|
-
const page3 = BuilderPage.fromJson(multiQuestionPageDto);
|
|
127
|
-
const clone3 = BuilderPage.fromJson(page3.clone());
|
|
128
|
-
const page3Json = deleteIdsFromPage(page3.toJson());
|
|
129
|
-
const clone3Json = deleteIdsFromPage(clone3.toJson());
|
|
130
|
-
expect(page3Json).toStrictEqual(clone3Json);
|
|
131
|
-
});
|
|
132
|
-
test("Can clone a page and no id is equal", () => {
|
|
133
|
-
const page1 = BuilderPage.create("info-page", pxx);
|
|
134
|
-
const clone1 = BuilderPage.fromJson(page1.clone());
|
|
135
|
-
expect(page1.id).not.toBe(clone1.id);
|
|
136
|
-
|
|
137
|
-
const testDtoIds = (dto: BuilderPageDto) => {
|
|
138
|
-
const instance = BuilderPage.fromJson(dto);
|
|
139
|
-
const clone = BuilderPage.fromJson(instance.clone());
|
|
140
|
-
|
|
141
|
-
testDtoIds(questionPageDto);
|
|
142
|
-
testDtoIds(multiQuestionPageDto);
|
|
143
|
-
};
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
test("Can get all rule-variables.", () => {
|
|
147
|
-
const prefix = PagePrefix.fromStringOrThrow("as1");
|
|
148
|
-
const page = BuilderPage.create("question", prefix);
|
|
149
|
-
page.mainText.text = "Hva heter du?";
|
|
150
|
-
page.defaultQuestion.addOption("Ja", 1);
|
|
151
|
-
page.defaultQuestion.addOption("Nei", 0);
|
|
152
|
-
const shemaPrefix = SchemaPrefix.fromValueOrThrow("ax");
|
|
153
|
-
const variables = page.getQuestionVariables(shemaPrefix, 1);
|
|
154
|
-
const first = variables[0];
|
|
155
|
-
expect(variables.length).toBe(1);
|
|
156
|
-
expect(first.options.length).toBe(2);
|
|
157
|
-
expect(first.options[0].value).toBe(1);
|
|
158
|
-
expect(first.kind === "question-variable").toBe(true);
|
|
159
|
-
// const newQuestion = BuilderQuestion.create('select-one');
|
|
160
|
-
// const m1 = page1.moveQuestion(newQuestion, 0);
|
|
161
|
-
// expect(page1.questions.length).toBe(3);
|
|
162
|
-
// expect(m1).toBe(false);
|
|
163
|
-
});
|
|
164
|
-
test("Can check if page is included in sum-score-variable", () => {
|
|
165
|
-
const prefix = PagePrefix.fromStringOrThrow("as1");
|
|
166
|
-
const page = BuilderPage.create("question", prefix);
|
|
167
|
-
const ss1 = SumScoreVariable.create({ name: "ss1", description: "ss1_desc", useAvg: true });
|
|
168
|
-
const ss2 = SumScoreVariable.create({ name: "ss1", description: "ss1_desc", useAvg: true });
|
|
169
|
-
const success1 = page.sumScoreVariableSet(ss1, 1);
|
|
170
|
-
const success2 = page.sumScoreVariableSet(ss2, 1);
|
|
171
|
-
expect(success1).toBe(true);
|
|
172
|
-
expect(success2).toBe(true);
|
|
173
|
-
expect(page._isIncludedInSumScore(ss1.id)).toBe(true);
|
|
174
|
-
expect(page._isIncludedInSumScore(ss2.id)).toBe(true);
|
|
175
|
-
expect(page._isIncludedInSumScore(SumScoreVariableID.dummy.a)).toBe(false);
|
|
176
|
-
});
|
|
177
|
-
});
|
|
1
|
+
import { BuilderPage } from "./Builder-page";
|
|
2
|
+
import type { BuilderPageDto } from "./Builder-page";
|
|
3
|
+
import type { BuilderQuestionDto } from "../Builder-question";
|
|
4
|
+
import { DUtil } from "@media-quest/engine";
|
|
5
|
+
import { PagePrefix } from "../primitives/page-prefix";
|
|
6
|
+
import { SchemaPrefix } from "../primitives/schema-prefix";
|
|
7
|
+
import { OptionID, PageID, QuestionID, SumScoreVariableID } from "../primitives/ID";
|
|
8
|
+
import { SumScoreVariable } from "../sum-score/sum-score-variable";
|
|
9
|
+
|
|
10
|
+
const U = DUtil;
|
|
11
|
+
const deleteIdsFromPage = (page: BuilderPageDto) => {
|
|
12
|
+
const deleteIdsFromQuestion = (q: BuilderQuestionDto) => {
|
|
13
|
+
U.deleteProp(q, "id");
|
|
14
|
+
q.options.forEach((o) => {
|
|
15
|
+
U.deleteProp(o, "id");
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
U.deleteProp(page, "id");
|
|
19
|
+
deleteIdsFromQuestion(page.defaultQuestion);
|
|
20
|
+
return page;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const pxx = PagePrefix.fromStringOrThrow("pxx");
|
|
24
|
+
const questionPageDto: BuilderPageDto = {
|
|
25
|
+
_type: "question",
|
|
26
|
+
autoplaySequence: [],
|
|
27
|
+
includedInSumScores: [],
|
|
28
|
+
mainText: {
|
|
29
|
+
text: "Velkommen til denne undersøkelsen.",
|
|
30
|
+
audioFile: false,
|
|
31
|
+
autoplayDelay: 0,
|
|
32
|
+
autoplay: false,
|
|
33
|
+
},
|
|
34
|
+
tags: [],
|
|
35
|
+
|
|
36
|
+
nextButton: {
|
|
37
|
+
id: OptionID.dummy.a,
|
|
38
|
+
label: "Neste",
|
|
39
|
+
value: -1,
|
|
40
|
+
},
|
|
41
|
+
defaultQuestion: {
|
|
42
|
+
id: QuestionID.validateOrThrow("default-question-id"),
|
|
43
|
+
prefix: "q1",
|
|
44
|
+
text: "sadf",
|
|
45
|
+
options: [
|
|
46
|
+
{
|
|
47
|
+
id: "q1-opt1" as OptionID,
|
|
48
|
+
value: 0,
|
|
49
|
+
label: "Nei",
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: "q1-opt2" as OptionID,
|
|
53
|
+
value: 1,
|
|
54
|
+
label: "Ja",
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: "q1-opt3" as OptionID,
|
|
58
|
+
value: 9,
|
|
59
|
+
label: "Vet ikke",
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
_type: "select-one",
|
|
63
|
+
},
|
|
64
|
+
// id: "p1" as BuilderObjectId.PageID,
|
|
65
|
+
id: PageID.create(),
|
|
66
|
+
prefix: PagePrefix.fromStringOrThrow("pxx"),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const multiQuestionPageDto: BuilderPageDto = {
|
|
70
|
+
_type: "question",
|
|
71
|
+
includedInSumScores: [],
|
|
72
|
+
mainText: {
|
|
73
|
+
text: "Velkommen til denne undersøkelsen.",
|
|
74
|
+
audioFile: false,
|
|
75
|
+
autoplay: false,
|
|
76
|
+
autoplayDelay: 0,
|
|
77
|
+
},
|
|
78
|
+
autoplaySequence: [],
|
|
79
|
+
defaultQuestion: {
|
|
80
|
+
id: QuestionID.create(),
|
|
81
|
+
prefix: "",
|
|
82
|
+
text: "",
|
|
83
|
+
options: [],
|
|
84
|
+
_type: "select-one",
|
|
85
|
+
},
|
|
86
|
+
tags: [],
|
|
87
|
+
nextButton: {
|
|
88
|
+
id: "next-button-id-3" as OptionID,
|
|
89
|
+
label: "Neste",
|
|
90
|
+
value: -1,
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
id: PageID.create(),
|
|
94
|
+
prefix: PagePrefix.fromStringOrThrow("pxx"),
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
let page = BuilderPage.create("info-page", PagePrefix.fromStringOrThrow("a"));
|
|
98
|
+
|
|
99
|
+
beforeEach(() => {
|
|
100
|
+
page = BuilderPage.create("info-page", PagePrefix.fromStringOrThrow("a"));
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("Builder Page", () => {
|
|
104
|
+
test("Can create page, and convert to json, and back", () => {
|
|
105
|
+
const page1 = BuilderPage.fromJson(questionPageDto);
|
|
106
|
+
const asJson = page1.toJson();
|
|
107
|
+
expect(questionPageDto).toStrictEqual(asJson);
|
|
108
|
+
const page2 = BuilderPage.fromJson(multiQuestionPageDto);
|
|
109
|
+
const asJson2 = page2.toJson();
|
|
110
|
+
expect(multiQuestionPageDto).toStrictEqual(asJson2);
|
|
111
|
+
// expect(page1.prefix).toBe(pageDto.prefix);
|
|
112
|
+
});
|
|
113
|
+
test("Can clone a page and everything except id is equal ", () => {
|
|
114
|
+
const page1 = BuilderPage.create("info-page", PagePrefix.fromStringOrThrow("as"));
|
|
115
|
+
const clone1 = BuilderPage.fromJson(page1.clone());
|
|
116
|
+
const page1Json = deleteIdsFromPage(page1.clone());
|
|
117
|
+
const clone1Json = deleteIdsFromPage(page1.clone());
|
|
118
|
+
expect(page1Json).toStrictEqual(clone1Json);
|
|
119
|
+
|
|
120
|
+
const page2 = BuilderPage.fromJson(questionPageDto);
|
|
121
|
+
const clone2 = BuilderPage.fromJson(page2.clone());
|
|
122
|
+
const page2Json = deleteIdsFromPage(page2.toJson());
|
|
123
|
+
const clone2Json = deleteIdsFromPage(clone2.toJson());
|
|
124
|
+
expect(page2Json).toStrictEqual(clone2Json);
|
|
125
|
+
|
|
126
|
+
const page3 = BuilderPage.fromJson(multiQuestionPageDto);
|
|
127
|
+
const clone3 = BuilderPage.fromJson(page3.clone());
|
|
128
|
+
const page3Json = deleteIdsFromPage(page3.toJson());
|
|
129
|
+
const clone3Json = deleteIdsFromPage(clone3.toJson());
|
|
130
|
+
expect(page3Json).toStrictEqual(clone3Json);
|
|
131
|
+
});
|
|
132
|
+
test("Can clone a page and no id is equal", () => {
|
|
133
|
+
const page1 = BuilderPage.create("info-page", pxx);
|
|
134
|
+
const clone1 = BuilderPage.fromJson(page1.clone());
|
|
135
|
+
expect(page1.id).not.toBe(clone1.id);
|
|
136
|
+
|
|
137
|
+
const testDtoIds = (dto: BuilderPageDto) => {
|
|
138
|
+
const instance = BuilderPage.fromJson(dto);
|
|
139
|
+
const clone = BuilderPage.fromJson(instance.clone());
|
|
140
|
+
|
|
141
|
+
testDtoIds(questionPageDto);
|
|
142
|
+
testDtoIds(multiQuestionPageDto);
|
|
143
|
+
};
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("Can get all rule-variables.", () => {
|
|
147
|
+
const prefix = PagePrefix.fromStringOrThrow("as1");
|
|
148
|
+
const page = BuilderPage.create("question", prefix);
|
|
149
|
+
page.mainText.text = "Hva heter du?";
|
|
150
|
+
page.defaultQuestion.addOption("Ja", 1);
|
|
151
|
+
page.defaultQuestion.addOption("Nei", 0);
|
|
152
|
+
const shemaPrefix = SchemaPrefix.fromValueOrThrow("ax");
|
|
153
|
+
const variables = page.getQuestionVariables(shemaPrefix, 1);
|
|
154
|
+
const first = variables[0];
|
|
155
|
+
expect(variables.length).toBe(1);
|
|
156
|
+
expect(first.options.length).toBe(2);
|
|
157
|
+
expect(first.options[0].value).toBe(1);
|
|
158
|
+
expect(first.kind === "question-variable").toBe(true);
|
|
159
|
+
// const newQuestion = BuilderQuestion.create('select-one');
|
|
160
|
+
// const m1 = page1.moveQuestion(newQuestion, 0);
|
|
161
|
+
// expect(page1.questions.length).toBe(3);
|
|
162
|
+
// expect(m1).toBe(false);
|
|
163
|
+
});
|
|
164
|
+
test("Can check if page is included in sum-score-variable", () => {
|
|
165
|
+
const prefix = PagePrefix.fromStringOrThrow("as1");
|
|
166
|
+
const page = BuilderPage.create("question", prefix);
|
|
167
|
+
const ss1 = SumScoreVariable.create({ name: "ss1", description: "ss1_desc", useAvg: true });
|
|
168
|
+
const ss2 = SumScoreVariable.create({ name: "ss1", description: "ss1_desc", useAvg: true });
|
|
169
|
+
const success1 = page.sumScoreVariableSet(ss1, 1);
|
|
170
|
+
const success2 = page.sumScoreVariableSet(ss2, 1);
|
|
171
|
+
expect(success1).toBe(true);
|
|
172
|
+
expect(success2).toBe(true);
|
|
173
|
+
expect(page._isIncludedInSumScore(ss1.id)).toBe(true);
|
|
174
|
+
expect(page._isIncludedInSumScore(ss2.id)).toBe(true);
|
|
175
|
+
expect(page._isIncludedInSumScore(SumScoreVariableID.dummy.a)).toBe(false);
|
|
176
|
+
});
|
|
177
|
+
});
|