@media-quest/engine 0.0.30 → 0.0.32

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/src/page/Page.ts CHANGED
@@ -1,182 +1,190 @@
1
- import { DElement } from "../Delement/DElement";
2
- import { TaskManager } from "./task-manager";
3
- import { createDElement } from "../Delement/element-factory";
4
- import { ScaleService } from "../engine/scale";
5
- import { Task } from "./task";
6
- import { PStyle } from "../Delement/DStyle";
7
- import { ButtonClickAction } from "../Delement/button-click-action";
8
- import { PageComponent, PageComponentDto } from "./page-component";
9
- import { DElementDto } from "../Delement/DElement.dto";
10
- import { Fact } from "../rules/fact";
11
- import { DTimestamp } from "../common/DTimestamp";
12
- import { PageResult } from "./page-result";
13
- import { TaskState, TaskStateDiff } from "./task-state";
14
- import {
15
- MqEvent,
16
- MqEventPageEnter,
17
- MqEventPageLeave,
18
- MqEventUserClicked,
19
- } from "../events/mq-events";
20
-
21
- export interface VideoPlayerDto {
22
- playUrl: string;
23
- style?: PStyle;
24
- }
25
-
26
- export interface PageDto {
27
- readonly id: string;
28
- readonly prefix: string;
29
- readonly tags: string[];
30
- staticElements: Array<DElementDto>;
31
- background: string;
32
- components: Array<PageComponentDto>;
33
- videoPlayer?: VideoPlayerDto;
34
- initialTasks: Array<Task>;
35
- }
36
-
37
- export const PageDto = {
38
- createDummy: (id: number): PageDto => {
39
- return {
40
- id: "id" + id,
41
- prefix: "prefix" + id,
42
- tags: [],
43
- staticElements: [],
44
- background: "white",
45
- // videoList: [],
46
- components: [
47
- {
48
- el: {
49
- _tag: "div",
50
- style: { x: 10, y: 0, w: 40, h: 20, backgroundColor: "red" },
51
- children: [],
52
- innerText: "Next btn " + id,
53
- },
54
- },
55
- ],
56
-
57
- initialTasks: [],
58
- };
59
- },
60
- };
61
-
62
- export class Page {
63
- private readonly TAG = "[ DPage ]: ";
64
- private staticElements: DElement<HTMLElement>[] = [];
65
- private components: PageComponent[] = [];
66
- private pageEntered: DTimestamp = DTimestamp.now();
67
- private previousState: TaskState | false = false;
68
- private eventLog = new Array<MqEvent>();
69
-
70
- constructor(
71
- private readonly dto: PageDto,
72
- private readonly taskManager: TaskManager,
73
- private readonly scaleService: ScaleService,
74
- private readonly onCompleted: (result: PageResult) => void,
75
- ) {
76
- this.components = dto.components.map((el) => {
77
- const component = new PageComponent(el, scaleService);
78
- component.onClick = (action) => {
79
- this.handleButtonAction(action);
80
- };
81
- this.components.push(component);
82
-
83
- return component;
84
- });
85
- dto.staticElements.forEach((el) => {
86
- const element = createDElement(el, scaleService);
87
- this.staticElements.push(element);
88
- });
89
-
90
- if (dto.videoPlayer) {
91
- this.taskManager.loadVideo(dto.videoPlayer.playUrl);
92
- if (dto.videoPlayer.style) {
93
- this.taskManager.setVideoStyles(dto.videoPlayer.style);
94
- }
95
- }
96
-
97
- if (dto.initialTasks.length) {
98
- this.taskManager.autoPlaySequence(dto.initialTasks);
99
- }
100
- }
101
-
102
- private createPageResult(facts: Fact[]): PageResult {
103
- const pageExited = DTimestamp.now();
104
- const pageTime = DTimestamp.diff(this.pageEntered, pageExited);
105
- const pageExit = MqEvent.pageLeave(this.dto.id, this.dto.prefix);
106
- this.eventLog.push(pageExit);
107
- const eventLog = [...this.eventLog];
108
- return {
109
- pagePrefix: this.dto.prefix,
110
- pageId: this.dto.id,
111
- eventLog,
112
- pageTime,
113
- collectedFacts: facts,
114
- };
115
- }
116
- private handleButtonAction(a: ButtonClickAction) {
117
- const event = MqEvent.userClicked({
118
- pageId: this.dto.id,
119
- pagePrefix: this.dto.prefix,
120
- action: a.kind,
121
- descriptions: ButtonClickAction.describe(a),
122
- });
123
- this.eventLog.push(event);
124
-
125
- switch (a.kind) {
126
- case "next-page":
127
- const nextPageResult = this.createPageResult([]);
128
- this.onCompleted(nextPageResult);
129
- break;
130
- case "play-video":
131
- this.taskManager.execute(a.task);
132
- break;
133
- case "play-audio":
134
- this.taskManager.execute(a.task);
135
- break;
136
- case "pause-video":
137
- this.taskManager.pauseVideo();
138
- break;
139
- case "pause-audio":
140
- this.taskManager.pauseAudio();
141
- break;
142
- case "submit-fact":
143
- const submitFactResult = this.createPageResult([a.fact]);
144
- this.onCompleted(submitFactResult);
145
- break;
146
- case "submit-form":
147
- // TODO IMPLEMENT Collection of form-data // LOOP OVER ALL INPUTS
148
- const submitFormResult = this.createPageResult([]);
149
- this.onCompleted(submitFormResult);
150
- break;
151
- default:
152
- const _exhaustiveCheck: never = a;
153
- console.log(_exhaustiveCheck);
154
- }
155
- }
156
-
157
- appendYourself(parent: HTMLElement) {
158
- const pageEnterEvent = MqEvent.pageEnter(this.dto.id, this.dto.prefix);
159
- this.pageEntered = DTimestamp.now();
160
- this.eventLog.push(pageEnterEvent);
161
- this.staticElements.forEach((el) => {
162
- el.appendYourself(parent);
163
- });
164
-
165
- this.components.forEach((comp) => {
166
- comp.appendToParent(parent);
167
- });
168
- }
169
-
170
- destroy() {
171
- this.taskManager.clear();
172
- }
173
-
174
- tick() {
175
- const prev = this.previousState;
176
- const curr = this.taskManager.getState();
177
- const diff = TaskState.getDiff(curr, prev);
178
- this.components.forEach((comp) => {
179
- comp.updateState(diff);
180
- });
181
- }
182
- }
1
+ import { DElement } from "../Delement/DElement";
2
+ import { TaskManager } from "./task-manager";
3
+ import { createDElement } from "../Delement/element-factory";
4
+ import { ScaleService } from "../engine/scale";
5
+ import { Task } from "./task";
6
+ import { PStyle } from "../Delement/DStyle";
7
+ import { ButtonClickAction } from "../Delement/button-click-action";
8
+ import { DElementDto } from "../Delement/DElement.dto";
9
+ import { Fact } from "../rules/fact";
10
+ import { DTimestamp } from "../common/DTimestamp";
11
+ import { PageResult } from "./page-result";
12
+ import { TaskState } from "./task-state";
13
+ import { MqEvent } from "../events/mq-events";
14
+
15
+ export interface VideoPlayerDto {
16
+ playUrl: string;
17
+ style?: PStyle;
18
+ }
19
+
20
+ export interface PageDto {
21
+ readonly id: string;
22
+ readonly prefix: string;
23
+ readonly tags: string[];
24
+ background: string;
25
+ elements: Array<DElementDto>;
26
+ videoPlayer?: VideoPlayerDto;
27
+ initialTasks: Array<Task>;
28
+ }
29
+
30
+ export const PageDto = {
31
+ createDummy: (id: number): PageDto => {
32
+ return {
33
+ id: "id" + id,
34
+ prefix: "prefix" + id,
35
+ tags: [],
36
+ background: "white",
37
+ elements: [
38
+ {
39
+ _tag: "div",
40
+ style: { x: 10, y: 0, w: 40, h: 20, backgroundColor: "red" },
41
+ children: [],
42
+ innerText: "Next btn " + id,
43
+ },
44
+ ],
45
+
46
+ initialTasks: [],
47
+ };
48
+ },
49
+ };
50
+
51
+ export class Page {
52
+ private readonly TAG = "[ DPage ]: ";
53
+ private elements: DElement<HTMLElement>[] = [];
54
+ // private elements: PageComponent[] = [];
55
+ // private layoutComponents: PageLayoutComponent[] = [];
56
+ private pageEntered: DTimestamp = DTimestamp.now();
57
+ private previousState: TaskState | false = false;
58
+ private eventLog = new Array<MqEvent>();
59
+
60
+ constructor(
61
+ private readonly dto: PageDto,
62
+ private readonly taskManager: TaskManager,
63
+ private readonly scaleService: ScaleService,
64
+ private readonly onCompleted: (result: PageResult) => void,
65
+ ) {
66
+ dto.elements.forEach((el) => {
67
+ const element = createDElement(el, scaleService);
68
+ // if (element instanceof DDiv) {
69
+ // }
70
+ element.registerClickHandler((action) => {
71
+ this.handleButtonAction(action);
72
+ });
73
+
74
+ this.elements.push(element);
75
+
76
+ // if(element.)
77
+ // element= (action) => {
78
+ // console.log("TODO ONCLICK ");
79
+ // this.handleButtonAction(action);
80
+ // };
81
+ // this.elements.push(element);
82
+ });
83
+
84
+ if (dto.videoPlayer) {
85
+ this.taskManager.loadVideo(dto.videoPlayer.playUrl);
86
+ if (dto.videoPlayer.style) {
87
+ this.taskManager.setVideoStyles(dto.videoPlayer.style);
88
+ }
89
+ }
90
+
91
+ if (dto.initialTasks.length) {
92
+ this.taskManager.autoPlaySequence(dto.initialTasks);
93
+ }
94
+ }
95
+
96
+ private createPageResult(facts: Fact[]): PageResult {
97
+ const pageExited = DTimestamp.now();
98
+ const pageTime = DTimestamp.diff(this.pageEntered, pageExited);
99
+ const pageExit = MqEvent.pageLeave(this.dto.id, this.dto.prefix);
100
+ this.eventLog.push(pageExit);
101
+ const eventLog = [...this.eventLog];
102
+ return {
103
+ pagePrefix: this.dto.prefix,
104
+ pageId: this.dto.id,
105
+ eventLog,
106
+ pageTime,
107
+ collectedFacts: facts,
108
+ };
109
+ }
110
+ private handleButtonAction(a: ButtonClickAction) {
111
+ const event = MqEvent.userClicked({
112
+ pageId: this.dto.id,
113
+ pagePrefix: this.dto.prefix,
114
+ action: a.kind,
115
+ descriptions: ButtonClickAction.describe(a),
116
+ });
117
+ this.eventLog.push(event);
118
+ navigator.vibrate(200);
119
+ const { vibrateMs } = a;
120
+ if (vibrateMs) {
121
+ navigator.vibrate(vibrateMs);
122
+ }
123
+
124
+ switch (a.kind) {
125
+ case "next-page":
126
+ const nextPageResult = this.createPageResult([]);
127
+ this.onCompleted(nextPageResult);
128
+ break;
129
+ case "play-video":
130
+ this.taskManager.execute(a.task);
131
+ break;
132
+ case "play-audio":
133
+ this.taskManager.execute(a.task);
134
+ break;
135
+ case "pause-video":
136
+ this.taskManager.pauseVideo();
137
+ break;
138
+ case "pause-audio":
139
+ this.taskManager.pauseAudio();
140
+ break;
141
+ case "mute-video":
142
+ this.taskManager.muteVideo();
143
+ break;
144
+ case "un-mute-video":
145
+ this.taskManager.unMuteVideo();
146
+ break;
147
+ case "submit-fact":
148
+ const submitFactResult = this.createPageResult([a.fact]);
149
+ this.onCompleted(submitFactResult);
150
+ break;
151
+ case "submit-form":
152
+ // TODO IMPLEMENT Collection of form-data // LOOP OVER ALL INPUTS
153
+ const submitFormResult = this.createPageResult([]);
154
+ this.onCompleted(submitFormResult);
155
+ break;
156
+ default:
157
+ const _exhaustiveCheck: never = a;
158
+ console.log(_exhaustiveCheck);
159
+ }
160
+ }
161
+
162
+ appendYourself(parent: HTMLElement) {
163
+ const pageEnterEvent = MqEvent.pageEnter(this.dto.id, this.dto.prefix);
164
+ this.pageEntered = DTimestamp.now();
165
+ this.eventLog.push(pageEnterEvent);
166
+ this.elements.forEach((el) => {
167
+ el.appendYourself(parent);
168
+ });
169
+
170
+ // this.components.forEach((comp) => {
171
+ // comp.appendToParent(parent);
172
+ // });
173
+ // this.layoutComponents.forEach((comp) => {
174
+ // comp.appendToParent(parent);
175
+ // });
176
+ }
177
+
178
+ destroy() {
179
+ this.taskManager.clear();
180
+ }
181
+
182
+ tick() {
183
+ const prev = this.previousState;
184
+ const curr = this.taskManager.getState();
185
+ const diff = TaskState.getDiff(curr, prev);
186
+ this.elements.forEach((element) => {
187
+ element.updateState(diff);
188
+ });
189
+ }
190
+ }