@media-quest/engine 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,1533 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/public-api.ts
21
+ var public_api_exports = {};
22
+ __export(public_api_exports, {
23
+ ButtonClickAction: () => ButtonClickAction,
24
+ Condition: () => Condition,
25
+ DCss: () => DCss,
26
+ DDiv: () => DDiv,
27
+ DElement: () => DElement,
28
+ DImg: () => DImg,
29
+ DStyle: () => DStyle,
30
+ DText: () => DText,
31
+ DUtil: () => DUtil,
32
+ MqEvent: () => MqEvent,
33
+ PageComponent: () => PageComponent,
34
+ PageDto: () => PageDto,
35
+ Rule: () => Rule,
36
+ RuleEngine: () => RuleEngine,
37
+ SchemaEngine: () => SchemaEngine,
38
+ Task: () => Task
39
+ });
40
+ module.exports = __toCommonJS(public_api_exports);
41
+
42
+ // src/engine/history-que.ts
43
+ var HistoryQue = class {
44
+ history = [];
45
+ getFacts() {
46
+ const answers = this.history.map((h) => h.collectedFacts).flat(1);
47
+ return answers;
48
+ }
49
+ addToHistory(result) {
50
+ this.history.push(result);
51
+ }
52
+ };
53
+
54
+ // src/utils/DUtil.ts
55
+ var DUtil;
56
+ ((DUtil2) => {
57
+ DUtil2.randomString = (length) => {
58
+ const letters = "abcdefghijklmnopqrstuvxyz";
59
+ const uppercase = letters.toUpperCase();
60
+ const all = letters + uppercase;
61
+ const abs = Math.abs(length);
62
+ let result = "";
63
+ for (let i = 0; i < abs; i++) {
64
+ const char = all.charAt(Math.floor(Math.random() * all.length));
65
+ result += char;
66
+ }
67
+ return result;
68
+ };
69
+ DUtil2.randomObjectId = () => (0, DUtil2.randomString)(32);
70
+ DUtil2.deleteProp = (obj, key) => {
71
+ delete obj[key];
72
+ return obj;
73
+ };
74
+ DUtil2.isInRange = (min, max) => {
75
+ return (value) => value >= min && value <= max;
76
+ };
77
+ DUtil2.isInfinity = (number) => {
78
+ return number === Number.POSITIVE_INFINITY || number === Number.NEGATIVE_INFINITY || number === Infinity;
79
+ };
80
+ DUtil2.isNonEmptyArray = (array) => {
81
+ return array.length > 0;
82
+ };
83
+ DUtil2.neverCheck = (args) => {
84
+ console.log("OOPS: This value slipped through the never-check", args);
85
+ };
86
+ DUtil2.isString = (str) => typeof str === "string";
87
+ DUtil2.hasKey = (obj, key) => {
88
+ if (!(0, DUtil2.isRecord)(obj)) {
89
+ return false;
90
+ }
91
+ return Object.prototype.hasOwnProperty.call(obj, key);
92
+ };
93
+ DUtil2.isRecord = (obj) => {
94
+ if (!obj) {
95
+ return false;
96
+ }
97
+ if (Array.isArray(obj)) {
98
+ return false;
99
+ }
100
+ if (typeof obj !== "object") {
101
+ return false;
102
+ }
103
+ if (obj === null) {
104
+ return false;
105
+ }
106
+ return true;
107
+ };
108
+ DUtil2.isBool = (obj) => typeof obj === "boolean";
109
+ DUtil2.isTrue = (bool) => bool === true;
110
+ DUtil2.isFalse = (bool) => bool === false;
111
+ DUtil2.isDefined = (obj) => {
112
+ const notNull = obj !== null;
113
+ const notUndefined = obj !== void 0;
114
+ return notNull && notUndefined;
115
+ };
116
+ DUtil2.hasKind = (obj) => {
117
+ if (!(0, DUtil2.hasKey)(obj, "kind")) {
118
+ return false;
119
+ }
120
+ if (typeof obj.kind !== "string") {
121
+ return false;
122
+ }
123
+ return obj.kind.length > 0;
124
+ };
125
+ DUtil2.hasValue = (obj) => {
126
+ return (0, DUtil2.hasKey)(obj, "value");
127
+ };
128
+ DUtil2.isNumber = (value) => {
129
+ const isNumber2 = typeof value === "number";
130
+ const notNaN = !Number.isNaN(value);
131
+ return isNumber2 && notNaN;
132
+ };
133
+ DUtil2.maxFn = (upperLimit) => {
134
+ return (value) => {
135
+ return Math.min(value, upperLimit);
136
+ };
137
+ };
138
+ DUtil2.minFn = (lowerLimit) => {
139
+ return (value) => {
140
+ return Math.min(value, lowerLimit);
141
+ };
142
+ };
143
+ })(DUtil || (DUtil = {}));
144
+
145
+ // src/rules/condition.ts
146
+ var Condition;
147
+ ((Condition2) => {
148
+ Condition2.evaluate = (condition, facts) => {
149
+ let result = false;
150
+ switch (condition.kind) {
151
+ case "string-condition":
152
+ result = evaluateSimple(condition, facts);
153
+ break;
154
+ case "numeric-condition":
155
+ result = evaluateSimple(condition, facts);
156
+ break;
157
+ case "complex-condition":
158
+ result = evaluateComplex(condition, facts);
159
+ break;
160
+ default:
161
+ const check = condition;
162
+ }
163
+ return result;
164
+ };
165
+ const evaluateComplex = (condition, facts) => {
166
+ if (condition.some.length === 0 && condition.all.length === 0) {
167
+ return false;
168
+ }
169
+ const allSolved = condition.all.map((condition2) => {
170
+ return evaluateSimple(condition2, facts);
171
+ });
172
+ const someEvaluated = condition.some.map((condition2) => {
173
+ return evaluateSimple(condition2, facts);
174
+ });
175
+ const allResult = allSolved.every(DUtil.isTrue);
176
+ const someResult = someEvaluated.length === 0 || someEvaluated.some(DUtil.isTrue);
177
+ return someResult && allResult;
178
+ };
179
+ const evaluateSimple = (condition, facts) => {
180
+ const fact = facts.find((f) => f.referenceId === condition.referenceId);
181
+ if (!fact) {
182
+ return false;
183
+ }
184
+ let res = false;
185
+ switch (condition.kind) {
186
+ case "numeric-condition":
187
+ if (fact.kind === "numeric-fact") {
188
+ res = evaluateNumeric(condition, fact.value);
189
+ }
190
+ break;
191
+ case "string-condition":
192
+ if (fact.kind === "string-fact") {
193
+ res = evaluateString(condition, fact.value);
194
+ }
195
+ break;
196
+ default:
197
+ const check = condition;
198
+ }
199
+ return res;
200
+ };
201
+ Condition2.isEmpty = (complex) => {
202
+ return complex.all.length === 0 && complex.some.length === 0;
203
+ };
204
+ const evaluateString = (condition, value) => {
205
+ const operator = condition.operator;
206
+ let result = false;
207
+ switch (operator) {
208
+ case "eq":
209
+ result = condition.value === value;
210
+ break;
211
+ case "not-eq":
212
+ result = condition.value !== value;
213
+ break;
214
+ case "shorter-then":
215
+ result = condition.value !== value;
216
+ break;
217
+ case "longer-then":
218
+ result = condition.value !== value;
219
+ break;
220
+ default:
221
+ const check = operator;
222
+ }
223
+ return result;
224
+ };
225
+ const evaluateNumeric = (condition, value) => {
226
+ const op = condition.operator;
227
+ const conditionValue = condition.value;
228
+ let result = false;
229
+ switch (op) {
230
+ case "eq":
231
+ result = value === conditionValue;
232
+ break;
233
+ case "not-eq":
234
+ result = value !== conditionValue;
235
+ break;
236
+ case "greater-then":
237
+ result = value > conditionValue;
238
+ break;
239
+ case "greater-then-inclusive":
240
+ result = value >= conditionValue;
241
+ break;
242
+ case "less-then":
243
+ result = value < conditionValue;
244
+ break;
245
+ case "less-then-inclusive":
246
+ result = value <= conditionValue;
247
+ break;
248
+ default:
249
+ const check = op;
250
+ }
251
+ return result;
252
+ };
253
+ const _getAllSimple = (condition) => {
254
+ const simple = [];
255
+ switch (condition.kind) {
256
+ case "complex-condition":
257
+ simple.push(...condition.all);
258
+ simple.push(...condition.some);
259
+ break;
260
+ case "numeric-condition":
261
+ simple.push(condition);
262
+ break;
263
+ case "string-condition":
264
+ simple.push(condition);
265
+ break;
266
+ default:
267
+ DUtil.neverCheck(condition);
268
+ }
269
+ return simple;
270
+ };
271
+ Condition2.getAllSimpleConditions = (condition) => {
272
+ const simple = [];
273
+ if (Array.isArray(condition)) {
274
+ condition.forEach((c) => {
275
+ simple.push(..._getAllSimple(c));
276
+ });
277
+ } else {
278
+ simple.push(..._getAllSimple(condition));
279
+ }
280
+ return simple;
281
+ };
282
+ })(Condition || (Condition = {}));
283
+
284
+ // src/rules/rule.ts
285
+ var Rule;
286
+ ((Rule2) => {
287
+ Rule2.isEmpty = (rule) => {
288
+ const emptyConditions = rule.all.length === 0 && rule.some.length === 0;
289
+ const emptyActions = rule.onSuccess.length === 0 && rule.onFailure.length === 0;
290
+ return emptyConditions || emptyActions;
291
+ };
292
+ Rule2.solve = (rule, facts) => {
293
+ if (rule.some.length === 0 && rule.all.length === 0) {
294
+ return false;
295
+ }
296
+ const someSolved = rule.some.map((condition) => Condition.evaluate(condition, facts));
297
+ const someResult = someSolved.length === 0 || someSolved.some(DUtil.isTrue);
298
+ const allSolved = rule.all.map((condition) => Condition.evaluate(condition, facts)).every(DUtil.isTrue);
299
+ return allSolved && someResult;
300
+ };
301
+ })(Rule || (Rule = {}));
302
+
303
+ // src/rules/rule-engine.ts
304
+ var RuleEngine = class {
305
+ constructor() {
306
+ }
307
+ solveAll(rules, facts) {
308
+ const errors = [];
309
+ const matching = [];
310
+ rules.forEach((rule) => {
311
+ if (Rule.isEmpty(rule)) {
312
+ errors.push({ message: "Empty rule: " + rule.id });
313
+ } else if (Rule.solve(rule, facts)) {
314
+ const match = {
315
+ ruleDescription: rule.description,
316
+ matchingRuleId: rule.id ?? "no-id-given",
317
+ actionList: [...rule.onSuccess]
318
+ };
319
+ matching.push(match);
320
+ }
321
+ });
322
+ return { matching, errors };
323
+ }
324
+ solve(rule, facts) {
325
+ return Rule.solve(rule, facts);
326
+ }
327
+ };
328
+
329
+ // src/engine/next-que.ts
330
+ var NextQue = class {
331
+ originalOrder = [];
332
+ allPages = [];
333
+ excludedTags = /* @__PURE__ */ new Set();
334
+ excludedByPageId = /* @__PURE__ */ new Set();
335
+ remaining = [];
336
+ constructor(pages = []) {
337
+ this.resetQue(pages);
338
+ }
339
+ /**
340
+ * Will reset que with the new pages.
341
+ * @param pages
342
+ */
343
+ resetQue(pages) {
344
+ this.allPages = [...pages];
345
+ this.remaining = [...pages];
346
+ this.excludedTags = /* @__PURE__ */ new Set();
347
+ this.excludedByPageId = /* @__PURE__ */ new Set();
348
+ this.originalOrder = this.allPages.map((p) => p.id);
349
+ }
350
+ pop() {
351
+ const next = this.remaining.shift();
352
+ return next ?? false;
353
+ }
354
+ peek() {
355
+ const next = this.remaining[0];
356
+ return next ?? false;
357
+ }
358
+ jumpToPageById(pageId) {
359
+ const index = this.remaining.findIndex((p) => p.id === pageId);
360
+ if (index < 0) {
361
+ return false;
362
+ }
363
+ this.remaining = this.remaining.slice(index);
364
+ return true;
365
+ }
366
+ removeByTag(tag) {
367
+ if (Array.isArray(tag)) {
368
+ tag.forEach((tag2) => {
369
+ this.excludedTags.add(tag2);
370
+ });
371
+ } else {
372
+ this.excludedTags.add(tag);
373
+ }
374
+ this.filterRemaining();
375
+ }
376
+ /**
377
+ * Will not be included
378
+ * @param pages
379
+ */
380
+ insertAsNextByForce(pages) {
381
+ this.remaining.unshift(...pages);
382
+ }
383
+ removeByPageId(pageId) {
384
+ if (Array.isArray(pageId)) {
385
+ pageId.forEach((id) => {
386
+ this.excludedByPageId.add(id);
387
+ });
388
+ } else {
389
+ this.excludedByPageId.add(pageId);
390
+ }
391
+ this.filterRemaining();
392
+ }
393
+ filterRemaining() {
394
+ this.remaining = this.remaining.filter((p) => {
395
+ const tags = p.tags ?? [];
396
+ const isIncluededByTag = !tags.some((tag) => this.excludedTags.has(tag));
397
+ const isIncludedByPageId = !this.excludedByPageId.has(p.id);
398
+ return isIncludedByPageId && isIncluededByTag;
399
+ });
400
+ }
401
+ get isEmpty() {
402
+ return this.remaining.length === 0;
403
+ }
404
+ /**
405
+ * Total number of pages left in que
406
+ */
407
+ get size() {
408
+ return this.remaining.length;
409
+ }
410
+ /**
411
+ * Total number of pages in test
412
+ */
413
+ get pageCount() {
414
+ return this.originalOrder.length;
415
+ }
416
+ };
417
+
418
+ // src/engine/dplayer.ts
419
+ var DPlayer = class {
420
+ eventLog = [];
421
+ history = new HistoryQue();
422
+ ruleEngine = new RuleEngine();
423
+ nextQue = new NextQue();
424
+ data;
425
+ predefinedFacts;
426
+ constructor(data) {
427
+ this.data = data;
428
+ const pages = data.pages ?? [];
429
+ this.predefinedFacts = data.predefinedFacts ? [...data.predefinedFacts] : [];
430
+ this.nextQue.resetQue(pages);
431
+ }
432
+ saveEvent(event) {
433
+ this.eventLog.push(event);
434
+ }
435
+ saveHistory(pageHistory) {
436
+ this.history.addToHistory(pageHistory);
437
+ this.eventLog.push(...pageHistory.eventLog);
438
+ const userGeneratedFact = this.history.getFacts();
439
+ const predefinedFacts = this.predefinedFacts;
440
+ const facts = [...userGeneratedFact, ...predefinedFacts];
441
+ const result = this.ruleEngine.solveAll(this.data.rules, facts);
442
+ const matchingRules = result.matching;
443
+ const actions = matchingRules.map((r) => r.actionList).flat(1);
444
+ actions.forEach((a) => {
445
+ switch (a.kind) {
446
+ case "jumpToPage":
447
+ this.nextQue.jumpToPageById(a.pageId);
448
+ break;
449
+ case "excludeByPageId":
450
+ this.nextQue.removeByPageId(a.pageIds);
451
+ break;
452
+ case "excludeByTag":
453
+ this.nextQue.removeByTag(a.tagIds);
454
+ break;
455
+ default:
456
+ console.log("UNKNOWN ACTION", a);
457
+ const check = a;
458
+ }
459
+ });
460
+ }
461
+ getResults() {
462
+ const pagesLeft = this.nextQue.size;
463
+ const answerFacts = this.history.getFacts();
464
+ const predefinedFacts = this.predefinedFacts;
465
+ const eventLog = [...this.eventLog];
466
+ return { answerFacts, predefinedFacts, eventLog, pagesLeft };
467
+ }
468
+ insertSequence(sequenceId) {
469
+ this.insertSequenceById(sequenceId);
470
+ }
471
+ getNextPage() {
472
+ const next = this.nextQue.pop();
473
+ return next ?? false;
474
+ }
475
+ insertSequenceById(id) {
476
+ const seq = this.data.pageSequences?.find((s) => s.id === id);
477
+ if (seq) {
478
+ this.nextQue.insertAsNextByForce([...seq.pages]);
479
+ } else {
480
+ }
481
+ }
482
+ /**
483
+ * Total number of pages left in que
484
+ */
485
+ get pagesLeft() {
486
+ return this.nextQue.pageCount;
487
+ }
488
+ /**
489
+ * Total number of pages in test
490
+ */
491
+ get totalPageCount() {
492
+ return this.data.pages.length;
493
+ }
494
+ };
495
+
496
+ // src/engine/scale.ts
497
+ var ScaleService = class {
498
+ baseHeight;
499
+ baseWidth;
500
+ containerHeight = 1300;
501
+ containerWidth = 1300;
502
+ get scale() {
503
+ return this._scale;
504
+ }
505
+ get pageHeight() {
506
+ return this.baseHeight * this.scale;
507
+ }
508
+ get pageWidth() {
509
+ return this.baseWidth * this._scale;
510
+ }
511
+ _scale = 1;
512
+ subscribers = /* @__PURE__ */ new Set();
513
+ constructor(config) {
514
+ this.baseHeight = config.baseHeight;
515
+ this.baseWidth = config.baseWidth;
516
+ this.containerHeight = config.containerHeight;
517
+ this.containerWidth = config.containerWidth;
518
+ this.updateScale();
519
+ }
520
+ setContainerBounds(bounds) {
521
+ this.containerWidth = bounds.width;
522
+ this.containerHeight = bounds.height;
523
+ this.updateScale();
524
+ }
525
+ updateScale() {
526
+ const scaleFn = Scale.calc(this.baseHeight, this.baseWidth);
527
+ const scale = scaleFn({ height: this.containerHeight, width: this.containerWidth });
528
+ const hasChanged = this.scale !== scale;
529
+ this._scale = scale;
530
+ if (hasChanged) {
531
+ this.subscribers.forEach((fn) => {
532
+ fn(this._scale);
533
+ });
534
+ }
535
+ }
536
+ onChange(scaleChangeHandler, subscriberId) {
537
+ this.subscribers.add(scaleChangeHandler);
538
+ scaleChangeHandler(this._scale);
539
+ return () => {
540
+ this.subscribers.delete(scaleChangeHandler);
541
+ };
542
+ }
543
+ };
544
+ var Scale;
545
+ ((Scale2) => {
546
+ Scale2.calc = (baseHeight, baseWidth) => {
547
+ return (container) => {
548
+ const heightRatio = container.height / baseHeight;
549
+ const widthRatio = container.width / baseWidth;
550
+ return Math.min(heightRatio, widthRatio);
551
+ };
552
+ };
553
+ Scale2.scaleFunctionCreator = (scale) => {
554
+ return (value) => value * scale;
555
+ };
556
+ })(Scale || (Scale = {}));
557
+
558
+ // src/Delement/css.ts
559
+ var DCss;
560
+ ((DCss2) => {
561
+ DCss2.toString = (unit, scale) => {
562
+ const clampedScale = Math.max(scale, 0.03);
563
+ if (unit._unit === "px") {
564
+ if (unit.value < 0.1) {
565
+ return "0px";
566
+ }
567
+ const rounded = Math.round(unit.value * clampedScale);
568
+ const clamped = Math.max(rounded, 1);
569
+ return clamped + "px";
570
+ }
571
+ return unit.value + "%";
572
+ };
573
+ DCss2.isLengthUnit = (unit) => {
574
+ if (!unit) {
575
+ return false;
576
+ }
577
+ const unitKey = "_unit";
578
+ const valueKey = "value";
579
+ const hasUnitKey = DUtil.hasKey(unit, unitKey);
580
+ const hasValueKey = DUtil.hasKey(unit, valueKey);
581
+ return hasUnitKey && hasValueKey;
582
+ };
583
+ })(DCss || (DCss = {}));
584
+
585
+ // src/Delement/DStyle.ts
586
+ var DStyle;
587
+ ((DStyle2) => {
588
+ DStyle2.normalize = (el) => {
589
+ el.style.padding = "0";
590
+ el.style.margin = "0";
591
+ el.style.position = "absolute";
592
+ el.style.boxSizing = "border-box";
593
+ return el;
594
+ };
595
+ DStyle2.applyStyles = (el, style, scale) => {
596
+ const {
597
+ x,
598
+ y,
599
+ backgroundColor,
600
+ borderColor,
601
+ borderWidth,
602
+ borderRadius,
603
+ borderStyle,
604
+ w,
605
+ opacity,
606
+ cursor,
607
+ fontSize,
608
+ fontWeight,
609
+ textColor,
610
+ textAlign,
611
+ translate,
612
+ margin,
613
+ padding,
614
+ letterSpacing,
615
+ h,
616
+ transform,
617
+ visibility
618
+ } = style;
619
+ if (backgroundColor) {
620
+ el.style.backgroundColor = backgroundColor;
621
+ }
622
+ if (cursor) {
623
+ el.style.cursor = cursor;
624
+ }
625
+ if (transform) {
626
+ el.style.transform = transform;
627
+ }
628
+ if (textColor) {
629
+ el.style.color = textColor;
630
+ }
631
+ if (textAlign) {
632
+ el.style.textAlign = textAlign;
633
+ }
634
+ if (borderColor) {
635
+ el.style.borderColor = borderColor;
636
+ }
637
+ if (borderWidth) {
638
+ el.style.borderWidth = DCss.toString(borderWidth, scale);
639
+ }
640
+ if (fontWeight) {
641
+ el.style.fontWeight = fontWeight + "";
642
+ }
643
+ if (borderStyle) {
644
+ el.style.borderStyle = borderStyle;
645
+ }
646
+ if (fontSize) {
647
+ el.style.fontSize = DCss.toString(fontSize, scale);
648
+ }
649
+ if (DUtil.isNumber(x)) {
650
+ el.style.left = x + "%";
651
+ }
652
+ if (DUtil.isNumber(y)) {
653
+ el.style.bottom = y + "%";
654
+ }
655
+ if (DUtil.isNumber(h)) {
656
+ el.style.height = h + "%";
657
+ }
658
+ if (DUtil.isNumber(w)) {
659
+ el.style.width = w + "%";
660
+ }
661
+ if (DCss.isLengthUnit(borderRadius)) {
662
+ el.style.borderRadius = DCss.toString(borderRadius, scale);
663
+ }
664
+ if (letterSpacing) {
665
+ el.style.letterSpacing = DCss.toString(letterSpacing, scale);
666
+ }
667
+ if (margin) {
668
+ el.style.margin = DCss.toString(margin, scale);
669
+ }
670
+ if (padding) {
671
+ el.style.padding = DCss.toString(padding, scale);
672
+ }
673
+ if (DUtil.isNumber(opacity)) {
674
+ el.style.opacity = opacity + "";
675
+ }
676
+ if (visibility) {
677
+ el.style.visibility = visibility;
678
+ }
679
+ return el;
680
+ };
681
+ })(DStyle || (DStyle = {}));
682
+
683
+ // src/Delement/DElement.ts
684
+ var DElement = class {
685
+ constructor(el, dto, scale) {
686
+ this.el = el;
687
+ this.dto = dto;
688
+ this.scale = scale;
689
+ if (dto.innerText) {
690
+ this.el.innerText = dto.innerText;
691
+ }
692
+ this.setStyle = this.setStyle.bind(this);
693
+ this.normalize = this.normalize.bind(this);
694
+ this.appendYourself = this.appendYourself.bind(this);
695
+ this.updateStyles = this.updateStyles.bind(this);
696
+ const { onMouseEnter, onMouseLeave } = dto;
697
+ if (onMouseEnter) {
698
+ this.el.onmouseenter = () => {
699
+ this.setStyle(onMouseEnter);
700
+ };
701
+ }
702
+ if (onMouseLeave) {
703
+ this.el.onmouseleave = () => {
704
+ this.setStyle(onMouseLeave);
705
+ };
706
+ }
707
+ this.el.onclick = () => {
708
+ this.onclick();
709
+ };
710
+ this.normalize();
711
+ if (dto) {
712
+ this.updateStyles(dto?.style);
713
+ }
714
+ }
715
+ currStyle = {
716
+ fontSize: { _unit: "px", value: 100 },
717
+ fontWeight: 500,
718
+ textColor: "black",
719
+ opacity: 1
720
+ };
721
+ /**
722
+ * This method is called when the element is clicked.
723
+ * This method shall be overridden by the pageClass.
724
+ * @param actions
725
+ */
726
+ onclick() {
727
+ }
728
+ setStyle(style) {
729
+ this.updateStyles(style);
730
+ }
731
+ appendYourself(parent) {
732
+ parent.append(this.el);
733
+ }
734
+ normalize() {
735
+ this.el.style.padding = "0";
736
+ this.el.style.margin = "0";
737
+ this.el.style.position = "absolute";
738
+ this.el.style.boxSizing = "border-box";
739
+ }
740
+ updateStyles(style) {
741
+ this.currStyle = Object.assign(this.currStyle, style);
742
+ DStyle.applyStyles(this.el, this.currStyle, this.scale.scale);
743
+ window.getComputedStyle(this.el);
744
+ }
745
+ };
746
+
747
+ // src/Delement/Ddiv.ts
748
+ var DDiv = class extends DElement {
749
+ TAG = "[ DDiv ]: ";
750
+ defaultStyle = { x: 22, y: 4 };
751
+ children = [];
752
+ constructor(dto, scale, children) {
753
+ const d = document.createElement("div");
754
+ super(d, dto, scale);
755
+ this.children = children;
756
+ this.children.forEach((child) => {
757
+ child.appendYourself(this.el);
758
+ });
759
+ }
760
+ };
761
+
762
+ // src/common/DTimestamp.ts
763
+ var DTimestamp;
764
+ ((DTimestamp2) => {
765
+ DTimestamp2.now = () => Date.now();
766
+ DTimestamp2.addMills = (t, ms) => {
767
+ const res = t + Math.abs(ms);
768
+ return res;
769
+ };
770
+ DTimestamp2.diff = (t1, t2) => {
771
+ const t1Abs = Math.abs(t1);
772
+ const t2Abs = Math.abs(t2);
773
+ return Math.abs(t1Abs - t2Abs);
774
+ };
775
+ DTimestamp2.diffNow = (t) => {
776
+ return (0, DTimestamp2.diff)(t, (0, DTimestamp2.now)());
777
+ };
778
+ })(DTimestamp || (DTimestamp = {}));
779
+
780
+ // src/Delement/DImg.ts
781
+ var DImg = class _DImg extends DElement {
782
+ constructor(dto, scaleService) {
783
+ super(document.createElement("img"), dto, scaleService);
784
+ this.dto = dto;
785
+ this.scaleService = scaleService;
786
+ _DImg.IMAGE_COUNT += 1;
787
+ this.imageCount = _DImg.IMAGE_COUNT;
788
+ this.TAG = "[D_IMG " + _DImg.IMAGE_COUNT + " ]: ";
789
+ this.TIMING_TAG = "load-time (" + _DImg.IMAGE_COUNT + ") ";
790
+ this.el.loading = "eager";
791
+ this.el.style.position = "absolute";
792
+ this.setStyle(dto.style);
793
+ this.loadStart = DTimestamp.now();
794
+ this.el.onload = () => {
795
+ };
796
+ this.el.onerror = () => {
797
+ };
798
+ this.el.src = dto.url;
799
+ console.time(this.TIMING_TAG);
800
+ }
801
+ static IMAGE_COUNT = 0;
802
+ imageCount;
803
+ TAG;
804
+ TIMING_TAG;
805
+ loadStart;
806
+ log() {
807
+ }
808
+ };
809
+
810
+ // src/Delement/DText.ts
811
+ var DText = class extends DElement {
812
+ constructor(dto, scale) {
813
+ super(document.createElement("p"), dto, scale);
814
+ }
815
+ };
816
+
817
+ // src/Delement/element-factory.ts
818
+ var createDElement = (dto, scale) => {
819
+ switch (dto._tag) {
820
+ case "div":
821
+ const childEls = createChildrenForDiv(dto, scale);
822
+ const newDiv = new DDiv(dto, scale, childEls);
823
+ return newDiv;
824
+ case "img":
825
+ return new DImg(dto, scale);
826
+ case "p":
827
+ return new DText(dto, scale);
828
+ default:
829
+ const check = dto;
830
+ throw new Error("Unknown dto given to the createDElement function.");
831
+ }
832
+ };
833
+ var createChildrenForDiv = (dto, scale) => {
834
+ const childDto = dto.children;
835
+ const childEls = [];
836
+ childDto.forEach((dto2) => {
837
+ if (dto2._tag === "p") {
838
+ const newText = new DText(dto2, scale);
839
+ childEls.push(newText);
840
+ }
841
+ if (dto2._tag === "img") {
842
+ const newImage = new DImg(dto2, scale);
843
+ childEls.push(newImage);
844
+ }
845
+ });
846
+ return childEls;
847
+ };
848
+
849
+ // src/Delement/button-click-action.ts
850
+ var ButtonClickAction = {
851
+ describe: (a) => {
852
+ switch (a.kind) {
853
+ case "next-page":
854
+ return "go to next page";
855
+ case "play-video":
856
+ return "VideoId = " + a.task.videoId;
857
+ case "play-audio":
858
+ return "AudioId = " + a.task.audioId;
859
+ case "pause-video":
860
+ return "";
861
+ case "pause-audio":
862
+ return "";
863
+ case "submit-fact":
864
+ return a.fact.label + " = " + a.fact.value;
865
+ case "submit-form":
866
+ return "";
867
+ default:
868
+ const _exhaustiveCheck = a;
869
+ return "";
870
+ }
871
+ }
872
+ };
873
+
874
+ // src/page/task-state.ts
875
+ var TaskState = {
876
+ eq: (a, b) => {
877
+ return a.audioIsPlaying === b.audioIsPlaying && a.isGifMode === b.isGifMode && a.videoIsPlaying === b.videoIsPlaying && a.blockFormInput === b.blockFormInput && a.blockResponseButton === b.blockResponseButton && a.blockAudio === b.blockAudio && a.blockVideo === b.blockVideo;
878
+ },
879
+ getDiff: (curr, prev) => {
880
+ if (prev === false) {
881
+ return curr;
882
+ }
883
+ const diff = {};
884
+ if (curr.audioIsPlaying !== prev.audioIsPlaying) {
885
+ diff.audioIsPlaying = curr.audioIsPlaying;
886
+ }
887
+ if (curr.isGifMode !== prev.isGifMode) {
888
+ diff.isGifMode = curr.isGifMode;
889
+ }
890
+ if (curr.videoIsPlaying !== prev.videoIsPlaying) {
891
+ diff.videoIsPlaying = curr.videoIsPlaying;
892
+ }
893
+ if (curr.blockFormInput !== prev.blockFormInput) {
894
+ diff.blockFormInput = curr.blockFormInput;
895
+ }
896
+ if (curr.blockResponseButton !== prev.blockResponseButton) {
897
+ diff.blockResponseButton = curr.blockResponseButton;
898
+ }
899
+ if (curr.blockAudio !== prev.blockAudio) {
900
+ diff.blockAudio = curr.blockAudio;
901
+ }
902
+ if (curr.blockVideo !== prev.blockVideo) {
903
+ diff.blockVideo = curr.blockVideo;
904
+ }
905
+ return diff;
906
+ }
907
+ };
908
+
909
+ // src/page/page-component.ts
910
+ var isFalse = (bool) => bool === false;
911
+ var isTrue = (bool) => bool === true;
912
+ var PageComponent = class {
913
+ constructor(dto, scale) {
914
+ this.dto = dto;
915
+ this.scale = scale;
916
+ this.el = createDElement(dto.el, scale);
917
+ this.el.onclick = () => {
918
+ if (dto.onClick) {
919
+ this.onClick(dto.onClick);
920
+ } else {
921
+ console.warn(this.TAG + "onClick not implemented");
922
+ }
923
+ };
924
+ }
925
+ TAG = "[ PageComponent ]: ";
926
+ el;
927
+ prevState = false;
928
+ onClick(action) {
929
+ console.warn(this.TAG + "onclick not implemented");
930
+ }
931
+ updateState(state) {
932
+ this.handleStateChanges(state);
933
+ }
934
+ setState(state) {
935
+ const prev = this.prevState;
936
+ const diff = TaskState.getDiff(state, prev);
937
+ this.prevState = state;
938
+ if (Object.keys(diff).length > 0) {
939
+ this.handleStateChanges(diff);
940
+ }
941
+ }
942
+ handleStateChanges(diff) {
943
+ const {
944
+ videoIsPlaying,
945
+ audioIsPlaying,
946
+ blockAudio,
947
+ blockVideo,
948
+ blockResponseButton,
949
+ blockFormInput,
950
+ isGifMode
951
+ } = diff;
952
+ const {
953
+ whenAudioPaused,
954
+ whenVideoPaused,
955
+ whenAudioPlaying,
956
+ whenFormInputBlocked,
957
+ whenResponseBlocked,
958
+ whenVideoPlay,
959
+ whenAudioBlocked,
960
+ whenVideoBlocked,
961
+ whenAudioUnblocked,
962
+ whenVideoUnblocked,
963
+ whenResponseUnblocked,
964
+ whenFormInputUnblocked
965
+ } = this.dto;
966
+ if (isTrue(audioIsPlaying) && whenAudioPlaying) {
967
+ this.el.setStyle(whenAudioPlaying);
968
+ }
969
+ if (isFalse(audioIsPlaying) && whenAudioPaused) {
970
+ this.el.setStyle(whenAudioPaused);
971
+ }
972
+ if (isTrue(videoIsPlaying) && whenVideoPlay) {
973
+ this.el.setStyle(whenVideoPlay);
974
+ }
975
+ if (isFalse(videoIsPlaying) && whenVideoPaused) {
976
+ this.el.setStyle(whenVideoPaused);
977
+ }
978
+ }
979
+ appendToParent(parent) {
980
+ this.el.appendYourself(parent);
981
+ }
982
+ };
983
+
984
+ // src/events/mq-events.ts
985
+ var MqEvent = {
986
+ engineStart(schemaId, schemaPrefix) {
987
+ return {
988
+ kind: "engine-start",
989
+ timestamp: DTimestamp.now(),
990
+ payload: { schemaId, schemaPrefix }
991
+ };
992
+ },
993
+ pageEnter(pageId, pagePrefix) {
994
+ return {
995
+ kind: "page-enter",
996
+ timestamp: DTimestamp.now(),
997
+ payload: { pageId, pagePrefix }
998
+ };
999
+ },
1000
+ pageLeave(pageId, pagePrefix) {
1001
+ return {
1002
+ kind: "page-leave",
1003
+ timestamp: DTimestamp.now(),
1004
+ payload: { pageId, pagePrefix }
1005
+ };
1006
+ },
1007
+ userClicked(data) {
1008
+ const { pageId, pagePrefix, action, descriptions } = data;
1009
+ return {
1010
+ kind: "user-clicked",
1011
+ timestamp: DTimestamp.now(),
1012
+ payload: { pageId, pagePrefix, action, descriptions }
1013
+ };
1014
+ }
1015
+ };
1016
+
1017
+ // src/page/Page.ts
1018
+ var PageDto = {
1019
+ createDummy: (id) => {
1020
+ return {
1021
+ id: "id" + id,
1022
+ prefix: "prefix" + id,
1023
+ tags: [],
1024
+ staticElements: [],
1025
+ background: "white",
1026
+ // videoList: [],
1027
+ components: [
1028
+ {
1029
+ el: {
1030
+ _tag: "div",
1031
+ style: { x: 10, y: 0, w: 40, h: 20, backgroundColor: "red" },
1032
+ children: [],
1033
+ innerText: "Next btn " + id
1034
+ }
1035
+ }
1036
+ ],
1037
+ initialTasks: []
1038
+ };
1039
+ }
1040
+ };
1041
+ var Page = class {
1042
+ constructor(dto, taskManager, scaleService, onCompleted) {
1043
+ this.dto = dto;
1044
+ this.taskManager = taskManager;
1045
+ this.scaleService = scaleService;
1046
+ this.onCompleted = onCompleted;
1047
+ this.components = dto.components.map((el) => {
1048
+ const component = new PageComponent(el, scaleService);
1049
+ component.onClick = (action) => {
1050
+ this.handleButtonAction(action);
1051
+ };
1052
+ this.components.push(component);
1053
+ return component;
1054
+ });
1055
+ dto.staticElements.forEach((el) => {
1056
+ const element = createDElement(el, scaleService);
1057
+ this.staticElements.push(element);
1058
+ });
1059
+ if (dto.videoPlayer) {
1060
+ this.taskManager.loadVideo(dto.videoPlayer.playUrl);
1061
+ if (dto.videoPlayer.style) {
1062
+ this.taskManager.setVideoStyles(dto.videoPlayer.style);
1063
+ }
1064
+ }
1065
+ if (dto.initialTasks.length) {
1066
+ this.taskManager.autoPlaySequence(dto.initialTasks);
1067
+ }
1068
+ }
1069
+ TAG = "[ DPage ]: ";
1070
+ staticElements = [];
1071
+ components = [];
1072
+ pageEntered = DTimestamp.now();
1073
+ previousState = false;
1074
+ eventLog = new Array();
1075
+ createPageResult(facts) {
1076
+ const pageExited = DTimestamp.now();
1077
+ const pageTime = DTimestamp.diff(this.pageEntered, pageExited);
1078
+ const pageExit = MqEvent.pageLeave(this.dto.id, this.dto.prefix);
1079
+ this.eventLog.push(pageExit);
1080
+ const eventLog = [...this.eventLog];
1081
+ return {
1082
+ pagePrefix: this.dto.prefix,
1083
+ pageId: this.dto.id,
1084
+ eventLog,
1085
+ pageTime,
1086
+ collectedFacts: facts
1087
+ };
1088
+ }
1089
+ handleButtonAction(a) {
1090
+ const event = MqEvent.userClicked({
1091
+ pageId: this.dto.id,
1092
+ pagePrefix: this.dto.prefix,
1093
+ action: a.kind,
1094
+ descriptions: ButtonClickAction.describe(a)
1095
+ });
1096
+ this.eventLog.push(event);
1097
+ switch (a.kind) {
1098
+ case "next-page":
1099
+ const nextPageResult = this.createPageResult([]);
1100
+ this.onCompleted(nextPageResult);
1101
+ break;
1102
+ case "play-video":
1103
+ this.taskManager.execute(a.task);
1104
+ break;
1105
+ case "play-audio":
1106
+ this.taskManager.execute(a.task);
1107
+ break;
1108
+ case "pause-video":
1109
+ this.taskManager.pauseVideo();
1110
+ break;
1111
+ case "pause-audio":
1112
+ this.taskManager.pauseAudio();
1113
+ break;
1114
+ case "submit-fact":
1115
+ const submitFactResult = this.createPageResult([a.fact]);
1116
+ this.onCompleted(submitFactResult);
1117
+ break;
1118
+ case "submit-form":
1119
+ const submitFormResult = this.createPageResult([]);
1120
+ this.onCompleted(submitFormResult);
1121
+ break;
1122
+ default:
1123
+ const _exhaustiveCheck = a;
1124
+ console.log(_exhaustiveCheck);
1125
+ }
1126
+ }
1127
+ appendYourself(parent) {
1128
+ const pageEnterEvent = MqEvent.pageEnter(this.dto.id, this.dto.prefix);
1129
+ this.pageEntered = DTimestamp.now();
1130
+ this.eventLog.push(pageEnterEvent);
1131
+ this.staticElements.forEach((el) => {
1132
+ el.appendYourself(parent);
1133
+ });
1134
+ this.components.forEach((comp) => {
1135
+ comp.appendToParent(parent);
1136
+ });
1137
+ }
1138
+ destroy() {
1139
+ this.taskManager.clear();
1140
+ }
1141
+ tick() {
1142
+ const prev = this.previousState;
1143
+ const curr = this.taskManager.getState();
1144
+ const diff = TaskState.getDiff(curr, prev);
1145
+ this.components.forEach((comp) => {
1146
+ comp.updateState(diff);
1147
+ });
1148
+ }
1149
+ };
1150
+
1151
+ // src/page/task.ts
1152
+ var eq = (a, b) => {
1153
+ if (!b) {
1154
+ return false;
1155
+ }
1156
+ if (a.kind !== b.kind) {
1157
+ return false;
1158
+ }
1159
+ if (a.kind === "delay-task" && b.kind === "delay-task") {
1160
+ return a.duration === b.duration;
1161
+ }
1162
+ if (a.kind === "play-audio-task" && b.kind === "play-audio-task") {
1163
+ return a.url === b.url && a.audioId === b.audioId;
1164
+ }
1165
+ if (a.kind === "play-video-task" && b.kind === "play-video-task") {
1166
+ return a.url === b.url && a.videoId === b.videoId && a.startAt === b.startAt && a.stopAt === b.stopAt && a.volume === b.volume;
1167
+ }
1168
+ return false;
1169
+ };
1170
+ var deleteTaskList = (task) => {
1171
+ return task.priority === "replace-all" || task.priority === "replace-queue";
1172
+ };
1173
+ var shallRemoveCurrent = (task) => {
1174
+ return task.priority === "replace-current" || task.priority === "replace-all";
1175
+ };
1176
+ var notEq = (a, b) => !eq(a, b);
1177
+ var is = (task) => {
1178
+ if (!task) {
1179
+ return false;
1180
+ }
1181
+ if (typeof task !== "object") {
1182
+ return false;
1183
+ }
1184
+ if (Array.isArray(task)) {
1185
+ return false;
1186
+ }
1187
+ return true;
1188
+ };
1189
+ var Task = {
1190
+ eq,
1191
+ is,
1192
+ notEq,
1193
+ deleteTaskList,
1194
+ shallRemoveCurrent
1195
+ };
1196
+
1197
+ // src/page/task-manager.ts
1198
+ var TaskManager = class {
1199
+ constructor(mediaLayer, scale, onError) {
1200
+ this.mediaLayer = mediaLayer;
1201
+ this.scale = scale;
1202
+ this.onError = onError;
1203
+ this.hideVideo();
1204
+ this.mediaLayer.appendChild(this.videoElement);
1205
+ this.mediaLayer.appendChild(this.audioElement);
1206
+ DStyle.normalize(this.videoElement);
1207
+ DStyle.applyStyles(this.videoElement, this.videoStyles, this.scale.scale);
1208
+ this.videoElement.onended = () => {
1209
+ const next = this.getNextTask();
1210
+ if (next) {
1211
+ this.execute(next);
1212
+ } else {
1213
+ this.runningTask = false;
1214
+ }
1215
+ };
1216
+ this.videoElement.onerror = (e) => {
1217
+ if (this.videoElement.src !== "") {
1218
+ onError("Error playing video: " + this.videoElement.src);
1219
+ }
1220
+ };
1221
+ this.audioElement.onended = () => {
1222
+ const next = this.getNextTask();
1223
+ if (next) {
1224
+ this.execute(next);
1225
+ } else {
1226
+ this.runningTask = false;
1227
+ }
1228
+ };
1229
+ }
1230
+ TAG = "[TaskManager]: ";
1231
+ videoElement = document.createElement("video");
1232
+ audioElement = document.createElement("audio");
1233
+ showConsoleLogs = false;
1234
+ videoStyles = {
1235
+ h: 40,
1236
+ w: 80,
1237
+ y: 60,
1238
+ x: 10
1239
+ };
1240
+ runningTask = false;
1241
+ taskList = [];
1242
+ delayRef = false;
1243
+ clear() {
1244
+ if (this.showConsoleLogs)
1245
+ console.log(this.TAG + "CLEAR");
1246
+ if (typeof this.delayRef === "number") {
1247
+ window.clearTimeout(this.delayRef);
1248
+ }
1249
+ this.pauseVideo();
1250
+ this.pauseAudio();
1251
+ this.videoElement.removeAttribute("src");
1252
+ this.videoElement.load();
1253
+ this.audioElement.removeAttribute("src");
1254
+ this.audioElement.load();
1255
+ this.taskList = [];
1256
+ this.runningTask = false;
1257
+ this.hideVideo();
1258
+ }
1259
+ getState() {
1260
+ const c = this.runningTask;
1261
+ const isGifMode = this.videoElement.loop;
1262
+ const audioIsPlaying = c && c.task.kind === "play-audio-task" && !this.audioElement.paused;
1263
+ const videoIsPlaying = c && c.task.kind === "play-video-task" && !this.videoElement.paused;
1264
+ const blockResponseButton = c && c.task.blockResponseButton;
1265
+ const blockAudio = c && c.task.blockAudio;
1266
+ const blockVideo = c && c.task.blockVideo;
1267
+ const blockFormInput = c && c.task.blockFormInput;
1268
+ return {
1269
+ audioIsPlaying,
1270
+ isGifMode,
1271
+ videoIsPlaying,
1272
+ blockFormInput,
1273
+ blockResponseButton,
1274
+ blockAudio,
1275
+ blockVideo
1276
+ };
1277
+ }
1278
+ execute(task) {
1279
+ const curr = this.runningTask;
1280
+ let isBackgroundTask = false;
1281
+ if (curr && Task.shallRemoveCurrent(task) && Task.notEq(curr.task, task)) {
1282
+ this.pauseAudio();
1283
+ this.pauseVideo();
1284
+ this.runningTask = false;
1285
+ }
1286
+ if (!curr) {
1287
+ this.runningTask = { startedAt: DTimestamp.now(), task };
1288
+ } else if (Task.notEq(curr.task, task)) {
1289
+ this.runningTask = { startedAt: DTimestamp.now(), task };
1290
+ }
1291
+ if (task.priority === "replace-all" || task.priority === "replace-queue") {
1292
+ this.taskList = [];
1293
+ }
1294
+ if (task.kind === "play-video-task") {
1295
+ this.showVideo();
1296
+ this.loadVideo(task.url);
1297
+ if (task.loop) {
1298
+ this.videoElement.loop = true;
1299
+ isBackgroundTask = true;
1300
+ } else {
1301
+ this.videoElement.loop = false;
1302
+ }
1303
+ try {
1304
+ this.videoElement.play();
1305
+ } catch (e) {
1306
+ console.error(e);
1307
+ }
1308
+ }
1309
+ if (task.kind === "play-audio-task") {
1310
+ if (!this.videoElement.loop) {
1311
+ this.pauseVideo();
1312
+ }
1313
+ if (task.url !== this.audioElement.src) {
1314
+ this.audioElement.src = task.url;
1315
+ }
1316
+ this.audioElement.play();
1317
+ }
1318
+ if (task.kind === "delay-task") {
1319
+ if (typeof this.delayRef === "number") {
1320
+ window.clearTimeout(this.delayRef);
1321
+ }
1322
+ this.delayRef = window.setTimeout(() => {
1323
+ const next = this.getNextTask();
1324
+ if (next) {
1325
+ this.execute(next);
1326
+ } else {
1327
+ this.runningTask = false;
1328
+ }
1329
+ }, task.duration);
1330
+ }
1331
+ if (isBackgroundTask) {
1332
+ const startNextTask = this.getNextTask();
1333
+ if (startNextTask) {
1334
+ this.execute(startNextTask);
1335
+ }
1336
+ }
1337
+ return true;
1338
+ }
1339
+ setVideoStyles(styles) {
1340
+ this.videoStyles = styles;
1341
+ DStyle.applyStyles(this.videoElement, this.videoStyles, this.scale.scale);
1342
+ }
1343
+ autoPlaySequence(tasks) {
1344
+ this.taskList = [...tasks];
1345
+ const next = this.getNextTask();
1346
+ if (next) {
1347
+ this.execute(next);
1348
+ if (next.kind === "play-video-task" && next.loop) {
1349
+ this.videoElement.loop = true;
1350
+ }
1351
+ }
1352
+ }
1353
+ loadVideo(url) {
1354
+ if (this.videoElement.src !== url) {
1355
+ this.videoElement.src = url;
1356
+ }
1357
+ this.showVideo();
1358
+ }
1359
+ playVideoElement() {
1360
+ this.videoElement.play().then(() => {
1361
+ }).catch((e) => {
1362
+ console.log(e);
1363
+ this.onError("Error playing video.");
1364
+ });
1365
+ }
1366
+ showVideo() {
1367
+ this.videoElement.style.display = "block";
1368
+ }
1369
+ hideVideo() {
1370
+ this.videoElement.style.display = "none";
1371
+ }
1372
+ pauseVideo() {
1373
+ try {
1374
+ if (!this.videoElement.loop) {
1375
+ this.videoElement.pause();
1376
+ }
1377
+ } catch (e) {
1378
+ console.log(e);
1379
+ this.onError("Error pausing video.");
1380
+ }
1381
+ }
1382
+ pauseAudio() {
1383
+ try {
1384
+ if (!this.audioElement.loop) {
1385
+ this.audioElement.pause();
1386
+ }
1387
+ } catch (e) {
1388
+ console.log(e);
1389
+ this.onError("Error pausing audio.");
1390
+ }
1391
+ }
1392
+ getNextTask() {
1393
+ console.log("Getting next task.");
1394
+ const first = this.taskList.shift();
1395
+ return first ?? false;
1396
+ }
1397
+ };
1398
+
1399
+ // src/engine/SchemaEngine.ts
1400
+ var voidLogger = {
1401
+ info: (message) => {
1402
+ },
1403
+ error: (message) => {
1404
+ },
1405
+ warn: (message) => {
1406
+ }
1407
+ };
1408
+ var SchemaEngine = class {
1409
+ constructor(hostEl, height, width, schema) {
1410
+ this.height = height;
1411
+ this.width = width;
1412
+ this.schema = schema;
1413
+ this.tickerRef = window.setInterval(() => {
1414
+ if (this.currentPage) {
1415
+ this.currentPage.tick();
1416
+ }
1417
+ }, 100);
1418
+ this.hostElement = hostEl;
1419
+ this.hostElement.appendChild(this.mediaLayer);
1420
+ this.hostElement.appendChild(this.uiLayer);
1421
+ this.scale = new ScaleService({
1422
+ baseHeight: schema.baseHeight,
1423
+ baseWidth: schema.baseWidth,
1424
+ containerWidth: width,
1425
+ containerHeight: height
1426
+ });
1427
+ this.logger.info(this.TAG + "Scale: " + JSON.stringify(this.scale));
1428
+ this.player = new DPlayer(this.schema);
1429
+ this.taskManager = new TaskManager(this.mediaLayer, this.scale, (error) => {
1430
+ console.log(error);
1431
+ });
1432
+ this.styleSelf();
1433
+ this.handlePageCompleted = this.handlePageCompleted.bind(this);
1434
+ this.nextPage();
1435
+ }
1436
+ TAG = "[ SCHEMA_ENGINE ] :";
1437
+ scale;
1438
+ hostElement;
1439
+ taskManager;
1440
+ logger = voidLogger;
1441
+ uiLayer = document.createElement("div");
1442
+ mediaLayer = document.createElement("div");
1443
+ player;
1444
+ currentPage = false;
1445
+ tickerRef = false;
1446
+ handlePageCompleted(result) {
1447
+ this.player.saveHistory(result);
1448
+ const currentResults = this.player.getResults();
1449
+ const a = {
1450
+ schemaId: this.schema.id,
1451
+ pagesLeft: currentResults.pagesLeft,
1452
+ predefinedFacts: currentResults.predefinedFacts,
1453
+ eventLog: currentResults.eventLog,
1454
+ answers: currentResults.answerFacts
1455
+ };
1456
+ if (this._onProgress) {
1457
+ this._onProgress(a);
1458
+ }
1459
+ this.nextPage();
1460
+ }
1461
+ styleSelf() {
1462
+ this.hostElement.style.height = this.scale.pageHeight + "px";
1463
+ this.hostElement.style.width = this.scale.pageWidth + "px";
1464
+ this.hostElement.style.backgroundColor = this.schema.backgroundColor ?? "white";
1465
+ this.hostElement.style.position = "relative";
1466
+ const makeStatic = (div) => {
1467
+ div.style.height = "0px";
1468
+ div.style.width = "0px";
1469
+ div.style.position = "static";
1470
+ };
1471
+ makeStatic(this.uiLayer);
1472
+ this.uiLayer.style.zIndex = "10";
1473
+ this.mediaLayer.style.zIndex = "8";
1474
+ makeStatic(this.mediaLayer);
1475
+ }
1476
+ nextPage() {
1477
+ const nextPage = this.player.getNextPage();
1478
+ if (this.currentPage) {
1479
+ this.currentPage.destroy();
1480
+ this.uiLayer.innerHTML = "";
1481
+ }
1482
+ if (!nextPage) {
1483
+ this.player = new DPlayer(this.schema);
1484
+ if (this.player.pagesLeft > 0) {
1485
+ this.nextPage();
1486
+ }
1487
+ return false;
1488
+ }
1489
+ const newPage = new Page(nextPage, this.taskManager, this.scale, (result) => {
1490
+ this.handlePageCompleted(result);
1491
+ });
1492
+ this.currentPage = newPage;
1493
+ newPage.appendYourself(this.uiLayer);
1494
+ return true;
1495
+ }
1496
+ destroy() {
1497
+ if (this.currentPage) {
1498
+ this.currentPage.destroy();
1499
+ this.uiLayer.innerHTML = "";
1500
+ }
1501
+ }
1502
+ _onProgress = false;
1503
+ onProgress(handler) {
1504
+ this._onProgress = handler;
1505
+ }
1506
+ _onFatalError = false;
1507
+ onFatalError(handler) {
1508
+ this._onFatalError = handler;
1509
+ }
1510
+ setLogger(logger) {
1511
+ this.logger = logger;
1512
+ }
1513
+ };
1514
+ // Annotate the CommonJS export names for ESM import in node:
1515
+ 0 && (module.exports = {
1516
+ ButtonClickAction,
1517
+ Condition,
1518
+ DCss,
1519
+ DDiv,
1520
+ DElement,
1521
+ DImg,
1522
+ DStyle,
1523
+ DText,
1524
+ DUtil,
1525
+ MqEvent,
1526
+ PageComponent,
1527
+ PageDto,
1528
+ Rule,
1529
+ RuleEngine,
1530
+ SchemaEngine,
1531
+ Task
1532
+ });
1533
+ //# sourceMappingURL=public-api.js.map