-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
222 lines (177 loc) · 7.05 KB
/
index.js
File metadata and controls
222 lines (177 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import React from 'react';
import ReactDOM from 'react-dom';
import { renderMath } from '@pie-lib/pie-toolbox/math-rendering';
import { EnableAudioAutoplayImage } from '@pie-lib/pie-toolbox/render-ui';
import { SessionChangedEvent, ModelSetEvent } from '@pie-framework/pie-player-events';
import CategorizeComponent from './categorize';
export default class Categorize extends HTMLElement {
set model(m) {
this._model = m;
this.eliminateBlindAnswersFromSession();
this.dispatchEvent(new ModelSetEvent(this.tagName.toLowerCase(), this.isComplete(), !!this._model));
// reset the audioInitialized to false since the model changed, and we might need to reinitialize the audio
this._audioInitialized = false;
this.render();
}
isComplete() {
const { autoplayAudioEnabled, completeAudioEnabled } =this._model || {};
if (autoplayAudioEnabled && completeAudioEnabled && !this.audioComplete) {
return false;
}
if (!this._session || !this._session.answers) {
return false;
}
if (!Array.isArray(this._session.answers)) {
return false;
}
return this._session.answers.some((answer) => answer.choices && answer.choices.length > 0);
}
set session(s) {
if (s && !s.answers) {
s.answers = [];
}
this._session = s;
this.render();
}
get session() {
return this._session;
}
eliminateBlindAnswersFromSession() {
const { answers = [] } = this._session || {};
const { choices = [] } = this._model || {};
const mappedChoices = choices.map((c) => c.id) || [];
const filteredAnswers = answers.map((answer) => {
const answerChoices = answer?.choices || [];
answer.choices = answerChoices.filter((c) => mappedChoices.includes(c));
return answer;
});
if (filteredAnswers.length > 0) {
this.changeAnswers(filteredAnswers);
}
}
changeAnswers(answers) {
this._session.answers = answers;
this._session.selector = 'Mouse';
this.dispatchEvent(new SessionChangedEvent(this.tagName.toLowerCase(), this.isComplete()));
this.render();
}
onShowCorrectToggle() {
renderMath(this);
}
_createAudioInfoToast() {
const info = document.createElement('div');
info.id = 'play-audio-info';
Object.assign(info.style, {
position: 'absolute',
top: 0,
width:'100%',
height: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
background: 'white',
zIndex: '1000',
cursor: 'pointer'
});
const img = document.createElement('img');
img.src = EnableAudioAutoplayImage;
img.alt = 'Click anywhere to enable audio autoplay';
img.width = 500;
img.height = 300;
info.appendChild(img);
return info;
}
connectedCallback(){
// Observation: audio in Chrome will have the autoplay attribute,
// while other browsers will not have the autoplay attribute and will need a user interaction to play the audio
// This workaround fixes the issue of audio being cached and played on any user interaction in Safari and Firefox
const observer = new MutationObserver((mutationsList, observer) => {
mutationsList.forEach((mutation) => {
if (mutation.type === 'childList') {
if (this._audioInitialized) return;
const audio = this.querySelector('audio');
const isInsidePrompt = audio && audio.closest('#preview-prompt');
if (!this._model) return;
if (!this._model.autoplayAudioEnabled) return;
if (audio && !isInsidePrompt) return;
if (!audio) return;
const info = this._createAudioInfoToast();
const container = this.querySelector('#main-container');
const enableAudio = () => {
if (this.querySelector('#play-audio-info')) {
audio.play();
container.removeChild(info);
}
document.removeEventListener('click', enableAudio);
};
// if the audio is paused, it means the user has not interacted with the page yet and the audio will not play
// FIX FOR SAFARI: play with a slight delay to check if autoplay was blocked
setTimeout(() => {
if (audio.paused && !this.querySelector('#play-audio-info')) {
// add info message as a toast to enable audio playback
container.appendChild(info);
document.addEventListener('click', enableAudio);
} else {
document.removeEventListener('click', enableAudio);
}
}, 500);
// we need to listen for the playing event to remove the toast in case the audio plays because of re-rendering
const handlePlaying = () => {
//timestamp when auto-played audio started playing
this._session.audioStartTime = this._session.audioStartTime || new Date().getTime();
const info = this.querySelector('#play-audio-info');
if (info) {
container.removeChild(info);
}
audio.removeEventListener('playing', handlePlaying);
};
audio.addEventListener('playing', handlePlaying);
// we need to listen for the ended event to update the isComplete state
const handleEnded = () => {
//timestamp when auto-played audio completed playing
this._session.audioEndTime = this._session.audioEndTime || new Date().getTime();
let { audioStartTime, audioEndTime, waitTime } = this._session;
if(!waitTime && audioStartTime && audioEndTime) {
// waitTime is elapsed time the user waited for auto-played audio to finish
this._session.waitTime = (audioEndTime - audioStartTime);
}
this.audioComplete = true;
this.dispatchEvent(new SessionChangedEvent(this.tagName.toLowerCase(), this.isComplete()));
audio.removeEventListener('ended', handleEnded);
};
audio.addEventListener('ended', handleEnded);
// store references to remove later
this._audio = audio;
this._handlePlaying = handlePlaying;
this._handleEnded = handleEnded;
this._enableAudio = enableAudio;
// set to true to prevent multiple initializations
this._audioInitialized = true;
observer.disconnect();
}
});
});
observer.observe(this, { childList: true, subtree: true });
}
disconnectedCallback() {
document.removeEventListener('click', this._enableAudio);
if (this._audio) {
this._audio.removeEventListener('playing', this._handlePlaying);
this._audio.removeEventListener('ended', this._handleEnded);
this._audio = null;
}
}
render() {
if (this._model && this._session) {
const el = React.createElement(CategorizeComponent, {
model: this._model,
session: this._session,
onAnswersChange: this.changeAnswers.bind(this),
onShowCorrectToggle: this.onShowCorrectToggle.bind(this),
});
ReactDOM.render(el, this, () => {
renderMath(this);
});
}
}
}