@pie-element/categorize 11.3.4-next.0 → 12.0.0-beta.0

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.
Files changed (63) hide show
  1. package/configure/lib/defaults.js +2 -5
  2. package/configure/lib/defaults.js.map +1 -1
  3. package/configure/lib/design/builder.js +15 -33
  4. package/configure/lib/design/builder.js.map +1 -1
  5. package/configure/lib/design/buttons.js +41 -95
  6. package/configure/lib/design/buttons.js.map +1 -1
  7. package/configure/lib/design/categories/RowLabel.js +32 -45
  8. package/configure/lib/design/categories/RowLabel.js.map +1 -1
  9. package/configure/lib/design/categories/alternateResponses.js +96 -251
  10. package/configure/lib/design/categories/alternateResponses.js.map +1 -1
  11. package/configure/lib/design/categories/category.js +135 -208
  12. package/configure/lib/design/categories/category.js.map +1 -1
  13. package/configure/lib/design/categories/choice-preview.js +61 -126
  14. package/configure/lib/design/categories/choice-preview.js.map +1 -1
  15. package/configure/lib/design/categories/droppable-placeholder.js +73 -165
  16. package/configure/lib/design/categories/droppable-placeholder.js.map +1 -1
  17. package/configure/lib/design/categories/index.js +195 -384
  18. package/configure/lib/design/categories/index.js.map +1 -1
  19. package/configure/lib/design/choices/choice.js +155 -264
  20. package/configure/lib/design/choices/choice.js.map +1 -1
  21. package/configure/lib/design/choices/config.js +42 -98
  22. package/configure/lib/design/choices/config.js.map +1 -1
  23. package/configure/lib/design/choices/index.js +148 -236
  24. package/configure/lib/design/choices/index.js.map +1 -1
  25. package/configure/lib/design/header.js +57 -111
  26. package/configure/lib/design/header.js.map +1 -1
  27. package/configure/lib/design/index.js +631 -476
  28. package/configure/lib/design/index.js.map +1 -1
  29. package/configure/lib/design/input-header.js +93 -149
  30. package/configure/lib/design/input-header.js.map +1 -1
  31. package/configure/lib/design/utils.js +4 -15
  32. package/configure/lib/design/utils.js.map +1 -1
  33. package/configure/lib/index.js +120 -183
  34. package/configure/lib/index.js.map +1 -1
  35. package/configure/lib/main.js +30 -74
  36. package/configure/lib/main.js.map +1 -1
  37. package/configure/lib/utils.js +22 -32
  38. package/configure/lib/utils.js.map +1 -1
  39. package/configure/package.json +15 -15
  40. package/controller/lib/defaults.js +2 -5
  41. package/controller/lib/defaults.js.map +1 -1
  42. package/controller/lib/index.js +237 -318
  43. package/controller/lib/index.js.map +1 -1
  44. package/controller/lib/utils.js +28 -65
  45. package/controller/lib/utils.js.map +1 -1
  46. package/controller/package.json +5 -5
  47. package/lib/categorize/categories.js +106 -164
  48. package/lib/categorize/categories.js.map +1 -1
  49. package/lib/categorize/category.js +73 -123
  50. package/lib/categorize/category.js.map +1 -1
  51. package/lib/categorize/choice.js +118 -245
  52. package/lib/categorize/choice.js.map +1 -1
  53. package/lib/categorize/choices.js +66 -131
  54. package/lib/categorize/choices.js.map +1 -1
  55. package/lib/categorize/droppable-placeholder.js +46 -103
  56. package/lib/categorize/droppable-placeholder.js.map +1 -1
  57. package/lib/categorize/grid-content.js +39 -87
  58. package/lib/categorize/grid-content.js.map +1 -1
  59. package/lib/categorize/index.js +341 -317
  60. package/lib/categorize/index.js.map +1 -1
  61. package/lib/index.js +285 -306
  62. package/lib/index.js.map +1 -1
  63. package/package.json +16 -14
package/lib/index.js CHANGED
@@ -1,337 +1,316 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
- exports["default"] = void 0;
9
-
10
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
-
12
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
-
14
- var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
15
-
16
- var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
17
-
18
- var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
19
-
20
- var _wrapNativeSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapNativeSuper"));
21
-
7
+ exports.default = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
22
9
  var _react = _interopRequireDefault(require("react"));
23
-
24
- var _reactDom = _interopRequireDefault(require("react-dom"));
25
-
10
+ var _client = require("react-dom/client");
26
11
  var _mathRendering = require("@pie-lib/math-rendering");
27
-
28
12
  var _renderUi = require("@pie-lib/render-ui");
29
-
30
13
  var _piePlayerEvents = require("@pie-framework/pie-player-events");
31
-
32
14
  var _categorize = _interopRequireDefault(require("./categorize"));
33
-
34
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
35
-
36
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
37
-
38
- var Categorize = /*#__PURE__*/function (_HTMLElement) {
39
- (0, _inherits2["default"])(Categorize, _HTMLElement);
40
-
41
- var _super = _createSuper(Categorize);
42
-
43
- function Categorize() {
44
- (0, _classCallCheck2["default"])(this, Categorize);
45
- return _super.apply(this, arguments);
46
- }
47
-
48
- (0, _createClass2["default"])(Categorize, [{
49
- key: "model",
50
- set: function set(m) {
51
- this._model = m;
52
- this.eliminateBlindAnswersFromSession();
53
- this.dispatchEvent(new _piePlayerEvents.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
54
-
55
- this._audioInitialized = false;
56
- this.render();
57
- }
58
- }, {
59
- key: "isComplete",
60
- value: function isComplete() {
61
- var _ref = this._model || {},
62
- autoplayAudioEnabled = _ref.autoplayAudioEnabled,
63
- choices = _ref.choices,
64
- completeAudioEnabled = _ref.completeAudioEnabled,
65
- hasUnplacedChoices = _ref.hasUnplacedChoices,
66
- possibleResponses = _ref.possibleResponses,
67
- responseAreasToBeFilled = _ref.responseAreasToBeFilled;
68
-
69
- var elementContext = this; // check audio completion if audio settings are enabled and audio actually exists
70
-
71
- if (autoplayAudioEnabled && completeAudioEnabled && !this.audioComplete) {
72
- if (elementContext) {
73
- var audio = elementContext.querySelector('audio');
74
- var isInsidePrompt = audio && audio.closest('#preview-prompt'); // only require audio completion if audio exists and is inside the prompt
75
-
76
- if (audio && isInsidePrompt) {
77
- return false;
78
- }
15
+ class Categorize extends HTMLElement {
16
+ constructor() {
17
+ super();
18
+ (0, _defineProperty2.default)(this, "_scheduleMathRender", () => {
19
+ if (this._mathRenderPending) return;
20
+ this._mathRenderPending = true;
21
+ requestAnimationFrame(() => {
22
+ if (this._mathObserver && !this._mathObserverPaused) {
23
+ this._mathObserver.disconnect();
79
24
  }
80
- }
81
-
82
- if (!this._session || !this._session.answers) {
83
- return false;
84
- }
85
-
86
- var answers = this._session.answers;
87
-
88
- if (!Array.isArray(answers)) {
89
- return false;
90
- } // filter answers by category and count the ones with content
91
-
92
-
93
- var filledResponseAreas = answers.filter(function (answer) {
94
- return answer.choices.length;
95
- }).length; // check if an answer choice was added to at least as many response areas
96
- // as the number of populated response areas in the correct answer
97
-
98
- var areResponseAreasFilled = filledResponseAreas >= responseAreasToBeFilled; // check if multiple placements are allowed
99
-
100
- var duplicatesAllowed = (choices || []).some(function (choice) {
101
- return choice.categoryCount === 0;
25
+ (0, _mathRendering.renderMath)(this);
26
+ this._mathRenderPending = false;
27
+ setTimeout(() => {
28
+ if (this._mathObserver && !this._mathObserverPaused) {
29
+ this._mathObserver.observe(this, {
30
+ childList: true,
31
+ subtree: true,
32
+ characterData: false
33
+ });
34
+ }
35
+ }, 50);
102
36
  });
103
-
104
- if (duplicatesAllowed) {
105
- // an answer choice can be used multiple times
106
- return areResponseAreasFilled;
107
- } // any correct answer have any unplaced answer choices (by the author)
108
-
109
-
110
- if (hasUnplacedChoices) {
111
- return areResponseAreasFilled;
37
+ });
38
+ (0, _defineProperty2.default)(this, "pauseMathObserver", () => {
39
+ if (this._mathObserver) {
40
+ this._mathObserver.disconnect();
41
+ this._mathObserverPaused = true;
112
42
  }
113
-
114
- var allAnswersIds = answers.map(function (answer) {
115
- return answer.choices;
116
- }).flat(); // check if any correct answer have any unplaced answer choices (by the student)
117
-
118
- var requiredAnswersPlaced = (possibleResponses || []).some(function (response) {
119
- return response.every(function (val) {
120
- return allAnswersIds.includes(val);
121
- });
122
- }); // true - all choices (required for a correct response) were placed into a response area
123
-
124
- return requiredAnswersPlaced;
125
- }
126
- }, {
127
- key: "session",
128
- get: function get() {
129
- return this._session;
130
- },
131
- set: function set(s) {
132
- if (s && !s.answers) {
133
- s.answers = [];
43
+ });
44
+ (0, _defineProperty2.default)(this, "resumeMathObserver", () => {
45
+ if (this._mathObserverPaused) {
46
+ this._mathObserverPaused = false;
47
+ if (this._mathObserver) {
48
+ this._mathObserver.observe(this, {
49
+ childList: true,
50
+ subtree: true,
51
+ characterData: false
52
+ });
53
+ }
134
54
  }
135
-
136
- this._session = s;
137
- this.render();
55
+ });
56
+ this._root = null;
57
+ this._mathObserver = null;
58
+ this._mathRenderPending = false;
59
+ }
60
+ _initMathObserver() {
61
+ if (this._mathObserver) return;
62
+ this._mathObserver = new MutationObserver(() => {
63
+ this._scheduleMathRender();
64
+ });
65
+ this._mathObserver.observe(this, {
66
+ childList: true,
67
+ subtree: true,
68
+ characterData: false
69
+ });
70
+ }
71
+ _disconnectMathObserver() {
72
+ if (this._mathObserver) {
73
+ this._mathObserver.disconnect();
74
+ this._mathObserver = null;
138
75
  }
139
- }, {
140
- key: "eliminateBlindAnswersFromSession",
141
- value: function eliminateBlindAnswersFromSession() {
142
- var _ref2 = this._session || {},
143
- _ref2$answers = _ref2.answers,
144
- answers = _ref2$answers === void 0 ? [] : _ref2$answers;
145
-
146
- var _ref3 = this._model || {},
147
- _ref3$choices = _ref3.choices,
148
- choices = _ref3$choices === void 0 ? [] : _ref3$choices;
149
-
150
- var mappedChoices = choices.map(function (c) {
151
- return c.id;
152
- }) || [];
153
- var filteredAnswers = answers.map(function (answer) {
154
- var answerChoices = (answer === null || answer === void 0 ? void 0 : answer.choices) || [];
155
- answer.choices = answerChoices.filter(function (c) {
156
- return mappedChoices.includes(c);
157
- });
158
- return answer;
159
- });
160
-
161
- if (filteredAnswers.length > 0) {
162
- this.changeAnswers(filteredAnswers);
76
+ }
77
+ set model(m) {
78
+ this._model = m;
79
+ this.eliminateBlindAnswersFromSession();
80
+ this.dispatchEvent(new _piePlayerEvents.ModelSetEvent(this.tagName.toLowerCase(), this.isComplete(), !!this._model));
81
+ // reset the audioInitialized to false since the model changed, and we might need to reinitialize the audio
82
+ this._audioInitialized = false;
83
+ this.render();
84
+ }
85
+ isComplete() {
86
+ const {
87
+ autoplayAudioEnabled,
88
+ choices,
89
+ completeAudioEnabled,
90
+ hasUnplacedChoices,
91
+ possibleResponses,
92
+ responseAreasToBeFilled
93
+ } = this._model || {};
94
+ const elementContext = this;
95
+
96
+ // check audio completion if audio settings are enabled and audio actually exists
97
+ if (autoplayAudioEnabled && completeAudioEnabled && !this.audioComplete) {
98
+ if (elementContext) {
99
+ const audio = elementContext.querySelector('audio');
100
+ const isInsidePrompt = audio && audio.closest('#preview-prompt');
101
+
102
+ // only require audio completion if audio exists and is inside the prompt
103
+ if (audio && isInsidePrompt) {
104
+ return false;
105
+ }
163
106
  }
164
107
  }
165
- }, {
166
- key: "changeAnswers",
167
- value: function changeAnswers(answers) {
168
- this._session.answers = answers;
169
- this._session.selector = 'Mouse';
170
- this.dispatchEvent(new _piePlayerEvents.SessionChangedEvent(this.tagName.toLowerCase(), this.isComplete()));
171
- this.render();
108
+ if (!this._session || !this._session.answers) {
109
+ return false;
172
110
  }
173
- }, {
174
- key: "onShowCorrectToggle",
175
- value: function onShowCorrectToggle() {
176
- (0, _mathRendering.renderMath)(this);
111
+ const {
112
+ answers
113
+ } = this._session;
114
+ if (!Array.isArray(answers)) {
115
+ return false;
177
116
  }
178
- }, {
179
- key: "_createAudioInfoToast",
180
- value: function _createAudioInfoToast() {
181
- var info = document.createElement('div');
182
- info.id = 'play-audio-info';
183
- Object.assign(info.style, {
184
- position: 'absolute',
185
- top: 0,
186
- width: '100%',
187
- height: '100%',
188
- display: 'flex',
189
- justifyContent: 'center',
190
- alignItems: 'center',
191
- background: 'white',
192
- zIndex: '1000',
193
- cursor: 'pointer'
194
- });
195
- var img = document.createElement('img');
196
- img.src = _renderUi.EnableAudioAutoplayImage;
197
- img.alt = 'Click anywhere to enable audio autoplay';
198
- img.width = 500;
199
- img.height = 300;
200
- info.appendChild(img);
201
- return info;
202
- }
203
- }, {
204
- key: "connectedCallback",
205
- value: function connectedCallback() {
206
- var _this = this;
207
-
208
- // Observation: audio in Chrome will have the autoplay attribute,
209
- // while other browsers will not have the autoplay attribute and will need a user interaction to play the audio
210
- // This workaround fixes the issue of audio being cached and played on any user interaction in Safari and Firefox
211
- var observer = new MutationObserver(function (mutationsList, observer) {
212
- mutationsList.forEach(function (mutation) {
213
- if (mutation.type === 'childList') {
214
- if (_this._audioInitialized) return;
215
117
 
216
- var audio = _this.querySelector('audio');
217
-
218
- var isInsidePrompt = audio && audio.closest('#preview-prompt');
219
- if (!_this._model) return;
220
- if (!_this._model.autoplayAudioEnabled) return;
221
- if (audio && !isInsidePrompt) return;
222
- if (!audio) return;
223
-
224
- var info = _this._createAudioInfoToast();
118
+ // filter answers by category and count the ones with content
119
+ const filledResponseAreas = answers.filter(answer => answer.choices.length).length;
120
+ // check if an answer choice was added to at least as many response areas
121
+ // as the number of populated response areas in the correct answer
122
+ const areResponseAreasFilled = filledResponseAreas >= responseAreasToBeFilled;
123
+ // check if multiple placements are allowed
124
+ const duplicatesAllowed = (choices || []).some(choice => choice.categoryCount === 0);
125
+ if (duplicatesAllowed) {
126
+ // an answer choice can be used multiple times
127
+ return areResponseAreasFilled;
128
+ }
225
129
 
226
- var container = _this.querySelector('#main-container');
130
+ // any correct answer have any unplaced answer choices (by the author)
131
+ if (hasUnplacedChoices) {
132
+ return areResponseAreasFilled;
133
+ }
134
+ const allAnswersIds = answers.map(answer => answer.choices).flat();
227
135
 
228
- var enableAudio = function enableAudio() {
229
- if (_this.querySelector('#play-audio-info')) {
230
- audio.play();
231
- container.removeChild(info);
232
- }
136
+ // check if any correct answer have any unplaced answer choices (by the student)
137
+ const requiredAnswersPlaced = (possibleResponses || []).some(response => response.every(val => allAnswersIds.includes(val)));
233
138
 
139
+ // true - all choices (required for a correct response) were placed into a response area
140
+ return requiredAnswersPlaced;
141
+ }
142
+ set session(s) {
143
+ if (s && !s.answers) {
144
+ s.answers = [];
145
+ }
146
+ this._session = s;
147
+ this.render();
148
+ }
149
+ get session() {
150
+ return this._session;
151
+ }
152
+ eliminateBlindAnswersFromSession() {
153
+ const {
154
+ answers = []
155
+ } = this._session || {};
156
+ const {
157
+ choices = []
158
+ } = this._model || {};
159
+ const mappedChoices = choices.map(c => c.id) || [];
160
+ const filteredAnswers = answers.map(answer => {
161
+ const answerChoices = answer?.choices || [];
162
+ answer.choices = answerChoices.filter(c => mappedChoices.includes(c));
163
+ return answer;
164
+ });
165
+ if (filteredAnswers.length > 0) {
166
+ this.changeAnswers(filteredAnswers);
167
+ }
168
+ }
169
+ changeAnswers(answers) {
170
+ this._session.answers = answers;
171
+ this._session.selector = 'Mouse';
172
+ this.dispatchEvent(new _piePlayerEvents.SessionChangedEvent(this.tagName.toLowerCase(), this.isComplete()));
173
+ this.render();
174
+ }
175
+ onShowCorrectToggle() {
176
+ (0, _mathRendering.renderMath)(this);
177
+ }
178
+ _createAudioInfoToast() {
179
+ const info = document.createElement('div');
180
+ info.id = 'play-audio-info';
181
+ Object.assign(info.style, {
182
+ position: 'absolute',
183
+ top: 0,
184
+ width: '100%',
185
+ height: '100%',
186
+ display: 'flex',
187
+ justifyContent: 'center',
188
+ alignItems: 'center',
189
+ background: 'white',
190
+ zIndex: '1000',
191
+ cursor: 'pointer'
192
+ });
193
+ const img = document.createElement('img');
194
+ img.src = _renderUi.EnableAudioAutoplayImage;
195
+ img.alt = 'Click anywhere to enable audio autoplay';
196
+ img.width = 500;
197
+ img.height = 300;
198
+ info.appendChild(img);
199
+ return info;
200
+ }
201
+ connectedCallback() {
202
+ this._initMathObserver();
203
+
204
+ // Observation: audio in Chrome will have the autoplay attribute,
205
+ // while other browsers will not have the autoplay attribute and will need a user interaction to play the audio
206
+ // This workaround fixes the issue of audio being cached and played on any user interaction in Safari and Firefox
207
+ const observer = new MutationObserver((mutationsList, observer) => {
208
+ mutationsList.forEach(mutation => {
209
+ if (mutation.type === 'childList') {
210
+ if (this._audioInitialized) return;
211
+ const audio = this.querySelector('audio');
212
+ const isInsidePrompt = audio && audio.closest('#preview-prompt');
213
+ if (!this._model) return;
214
+ if (!this._model.autoplayAudioEnabled) return;
215
+ if (audio && !isInsidePrompt) return;
216
+ if (!audio) return;
217
+ const info = this._createAudioInfoToast();
218
+ const container = this.querySelector('#main-container');
219
+ const enableAudio = () => {
220
+ if (this.querySelector('#play-audio-info')) {
221
+ audio.play();
222
+ container.removeChild(info);
223
+ }
224
+ document.removeEventListener('click', enableAudio);
225
+ };
226
+
227
+ // if the audio is paused, it means the user has not interacted with the page yet and the audio will not play
228
+ // FIX FOR SAFARI: play with a slight delay to check if autoplay was blocked
229
+ setTimeout(() => {
230
+ if (audio.paused && !this.querySelector('#play-audio-info')) {
231
+ // add info message as a toast to enable audio playback
232
+ container.appendChild(info);
233
+ document.addEventListener('click', enableAudio);
234
+ } else {
234
235
  document.removeEventListener('click', enableAudio);
235
- }; // if the audio is paused, it means the user has not interacted with the page yet and the audio will not play
236
- // FIX FOR SAFARI: play with a slight delay to check if autoplay was blocked
237
-
238
-
239
- setTimeout(function () {
240
- if (audio.paused && !_this.querySelector('#play-audio-info')) {
241
- // add info message as a toast to enable audio playback
242
- container.appendChild(info);
243
- document.addEventListener('click', enableAudio);
244
- } else {
245
- document.removeEventListener('click', enableAudio);
246
- }
247
- }, 500); // we need to listen for the playing event to remove the toast in case the audio plays because of re-rendering
248
-
249
- var handlePlaying = function handlePlaying() {
250
- //timestamp when auto-played audio started playing
251
- _this._session.audioStartTime = _this._session.audioStartTime || new Date().getTime();
252
-
253
- var info = _this.querySelector('#play-audio-info');
254
-
255
- if (info) {
256
- container.removeChild(info);
257
- }
258
-
259
- audio.removeEventListener('playing', handlePlaying);
260
- };
261
-
262
- audio.addEventListener('playing', handlePlaying); // we need to listen for the ended event to update the isComplete state
263
-
264
- var handleEnded = function handleEnded() {
265
- //timestamp when auto-played audio completed playing
266
- _this._session.audioEndTime = _this._session.audioEndTime || new Date().getTime();
267
- var _this$_session = _this._session,
268
- audioStartTime = _this$_session.audioStartTime,
269
- audioEndTime = _this$_session.audioEndTime,
270
- waitTime = _this$_session.waitTime;
271
-
272
- if (!waitTime && audioStartTime && audioEndTime) {
273
- // waitTime is elapsed time the user waited for auto-played audio to finish
274
- _this._session.waitTime = audioEndTime - audioStartTime;
275
- }
276
-
277
- _this.audioComplete = true;
278
-
279
- _this.dispatchEvent(new _piePlayerEvents.SessionChangedEvent(_this.tagName.toLowerCase(), _this.isComplete()));
280
-
281
- audio.removeEventListener('ended', handleEnded);
282
- };
283
-
284
- audio.addEventListener('ended', handleEnded); // store references to remove later
285
-
286
- _this._audio = audio;
287
- _this._handlePlaying = handlePlaying;
288
- _this._handleEnded = handleEnded;
289
- _this._enableAudio = enableAudio; // set to true to prevent multiple initializations
290
-
291
- _this._audioInitialized = true;
292
- observer.disconnect();
293
- }
294
- });
295
- });
296
- observer.observe(this, {
297
- childList: true,
298
- subtree: true
236
+ }
237
+ }, 500);
238
+
239
+ // we need to listen for the playing event to remove the toast in case the audio plays because of re-rendering
240
+ const handlePlaying = () => {
241
+ //timestamp when auto-played audio started playing
242
+ this._session.audioStartTime = this._session.audioStartTime || new Date().getTime();
243
+ const info = this.querySelector('#play-audio-info');
244
+ if (info) {
245
+ container.removeChild(info);
246
+ }
247
+ audio.removeEventListener('playing', handlePlaying);
248
+ };
249
+ audio.addEventListener('playing', handlePlaying);
250
+
251
+ // we need to listen for the ended event to update the isComplete state
252
+ const handleEnded = () => {
253
+ //timestamp when auto-played audio completed playing
254
+ this._session.audioEndTime = this._session.audioEndTime || new Date().getTime();
255
+ let {
256
+ audioStartTime,
257
+ audioEndTime,
258
+ waitTime
259
+ } = this._session;
260
+ if (!waitTime && audioStartTime && audioEndTime) {
261
+ // waitTime is elapsed time the user waited for auto-played audio to finish
262
+ this._session.waitTime = audioEndTime - audioStartTime;
263
+ }
264
+ this.audioComplete = true;
265
+ this.dispatchEvent(new _piePlayerEvents.SessionChangedEvent(this.tagName.toLowerCase(), this.isComplete()));
266
+ audio.removeEventListener('ended', handleEnded);
267
+ };
268
+ audio.addEventListener('ended', handleEnded);
269
+
270
+ // store references to remove later
271
+ this._audio = audio;
272
+ this._handlePlaying = handlePlaying;
273
+ this._handleEnded = handleEnded;
274
+ this._enableAudio = enableAudio;
275
+ // set to true to prevent multiple initializations
276
+ this._audioInitialized = true;
277
+ observer.disconnect();
278
+ }
299
279
  });
280
+ });
281
+ observer.observe(this, {
282
+ childList: true,
283
+ subtree: true
284
+ });
285
+ }
286
+ disconnectedCallback() {
287
+ this._disconnectMathObserver();
288
+ document.removeEventListener('click', this._enableAudio);
289
+ if (this._audio) {
290
+ this._audio.removeEventListener('playing', this._handlePlaying);
291
+ this._audio.removeEventListener('ended', this._handleEnded);
292
+ this._audio = null;
300
293
  }
301
- }, {
302
- key: "disconnectedCallback",
303
- value: function disconnectedCallback() {
304
- document.removeEventListener('click', this._enableAudio);
305
-
306
- if (this._audio) {
307
- this._audio.removeEventListener('playing', this._handlePlaying);
308
-
309
- this._audio.removeEventListener('ended', this._handleEnded);
310
-
311
- this._audio = null;
312
- }
294
+ if (this._root) {
295
+ this._root.unmount();
313
296
  }
314
- }, {
315
- key: "render",
316
- value: function render() {
317
- var _this2 = this;
318
-
319
- if (this._model && this._session) {
320
- var el = /*#__PURE__*/_react["default"].createElement(_categorize["default"], {
321
- model: this._model,
322
- session: this._session,
323
- onAnswersChange: this.changeAnswers.bind(this),
324
- onShowCorrectToggle: this.onShowCorrectToggle.bind(this)
325
- });
326
-
327
- _reactDom["default"].render(el, this, function () {
328
- (0, _mathRendering.renderMath)(_this2);
329
- });
297
+ }
298
+ render() {
299
+ if (this._model && this._session) {
300
+ const el = /*#__PURE__*/_react.default.createElement(_categorize.default, {
301
+ model: this._model,
302
+ session: this._session,
303
+ onAnswersChange: this.changeAnswers.bind(this),
304
+ onShowCorrectToggle: this.onShowCorrectToggle.bind(this),
305
+ pauseMathObserver: this.pauseMathObserver,
306
+ resumeMathObserver: this.resumeMathObserver
307
+ });
308
+ if (!this._root) {
309
+ this._root = (0, _client.createRoot)(this);
330
310
  }
311
+ this._root.render(el);
331
312
  }
332
- }]);
333
- return Categorize;
334
- }( /*#__PURE__*/(0, _wrapNativeSuper2["default"])(HTMLElement));
335
-
336
- exports["default"] = Categorize;
313
+ }
314
+ }
315
+ exports.default = Categorize;
337
316
  //# sourceMappingURL=index.js.map