jclic 2.2.1 → 2.3.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 (177) hide show
  1. package/README.md +5 -7
  2. package/dist-node/jclic-node.js +14157 -0
  3. package/dist-node/jclic-node.umd.cjs +530 -0
  4. package/package.json +38 -26
  5. package/.vscode/launch.json +0 -33
  6. package/.vscode/settings.json +0 -13
  7. package/CHANGELOG.md +0 -672
  8. package/TRANSLATIONS.md +0 -11
  9. package/build-locales.mjs +0 -82
  10. package/dist/jclic-node.js +0 -31680
  11. package/dist/jclic-node.js.map +0 -1
  12. package/dist/jclic.components.LICENSE +0 -2254
  13. package/dist/jclic.min.js +0 -27
  14. package/dist/jclic.min.js.map +0 -1
  15. package/eslint.config.mjs +0 -31
  16. package/jsdoc.config.js +0 -71
  17. package/locales/ar.po +0 -244
  18. package/locales/ast.po +0 -246
  19. package/locales/bs.po +0 -247
  20. package/locales/ca.po +0 -248
  21. package/locales/ca_ES@valencia.po +0 -248
  22. package/locales/cs.po +0 -244
  23. package/locales/da.po +0 -244
  24. package/locales/de.po +0 -246
  25. package/locales/el.po +0 -244
  26. package/locales/es.po +0 -248
  27. package/locales/eu.po +0 -244
  28. package/locales/fr.po +0 -244
  29. package/locales/gl.po +0 -244
  30. package/locales/he.po +0 -244
  31. package/locales/hr.po +0 -245
  32. package/locales/it.po +0 -246
  33. package/locales/ja.po +0 -242
  34. package/locales/jclic.js.pot +0 -241
  35. package/locales/nb_NO.po +0 -244
  36. package/locales/nl.po +0 -244
  37. package/locales/pl.po +0 -244
  38. package/locales/pt.po +0 -244
  39. package/locales/pt_BR.po +0 -248
  40. package/locales/ro.po +0 -248
  41. package/locales/ru.po +0 -245
  42. package/locales/ta.po +0 -244
  43. package/locales/tr.po +0 -246
  44. package/locales/uk.po +0 -247
  45. package/locales/vec.po +0 -244
  46. package/locales/zh_TW.po +0 -246
  47. package/patches/po2json+1.0.0-beta-3.patch +0 -12
  48. package/src/AWT.js +0 -2067
  49. package/src/Activity.js +0 -1311
  50. package/src/Deps.js +0 -232
  51. package/src/GlobalData.js +0 -5
  52. package/src/JClic.js +0 -196
  53. package/src/JClicPlayer.js +0 -1308
  54. package/src/PlayerHistory.js +0 -305
  55. package/src/Utils.js +0 -1355
  56. package/src/activities/associations/ComplexAssociation.js +0 -321
  57. package/src/activities/associations/SimpleAssociation.js +0 -519
  58. package/src/activities/memory/MemoryGame.js +0 -423
  59. package/src/activities/panels/Explore.js +0 -349
  60. package/src/activities/panels/Identify.js +0 -356
  61. package/src/activities/panels/InformationScreen.js +0 -262
  62. package/src/activities/panels/Menu.js +0 -209
  63. package/src/activities/panels/icons/ico00.png +0 -0
  64. package/src/activities/panels/icons/ico01.png +0 -0
  65. package/src/activities/panels/icons/ico02.png +0 -0
  66. package/src/activities/panels/icons/ico03.png +0 -0
  67. package/src/activities/panels/icons/icofolder.png +0 -0
  68. package/src/activities/puzzles/DoublePuzzle.js +0 -424
  69. package/src/activities/puzzles/ExchangePuzzle.js +0 -374
  70. package/src/activities/puzzles/HolePuzzle.js +0 -360
  71. package/src/activities/text/Complete.js +0 -127
  72. package/src/activities/text/Evaluator.js +0 -534
  73. package/src/activities/text/FillInBlanks.js +0 -426
  74. package/src/activities/text/IdentifyText.js +0 -253
  75. package/src/activities/text/OrderText.js +0 -421
  76. package/src/activities/text/TextActivityBase.js +0 -557
  77. package/src/activities/text/TextActivityDocument.js +0 -660
  78. package/src/activities/text/WrittenAnswer.js +0 -557
  79. package/src/activities/textGrid/CrossWord.js +0 -565
  80. package/src/activities/textGrid/WordSearch.js +0 -458
  81. package/src/activities/textGrid/icons/hIcon.svg +0 -3
  82. package/src/activities/textGrid/icons/vIcon.svg +0 -3
  83. package/src/automation/AutoContentProvider.js +0 -182
  84. package/src/automation/arith/Arith.js +0 -864
  85. package/src/bags/ActivitySequence.js +0 -318
  86. package/src/bags/ActivitySequenceElement.js +0 -161
  87. package/src/bags/ActivitySequenceJump.js +0 -140
  88. package/src/bags/ConditionalJumpInfo.js +0 -113
  89. package/src/bags/JumpInfo.js +0 -136
  90. package/src/bags/MediaBag.js +0 -215
  91. package/src/bags/MediaBagElement.js +0 -516
  92. package/src/boxes/AbstractBox.js +0 -699
  93. package/src/boxes/ActiveBagContent.js +0 -494
  94. package/src/boxes/ActiveBox.js +0 -810
  95. package/src/boxes/ActiveBoxBag.js +0 -357
  96. package/src/boxes/ActiveBoxContent.js +0 -484
  97. package/src/boxes/ActiveBoxGrid.js +0 -179
  98. package/src/boxes/BoxBag.js +0 -500
  99. package/src/boxes/BoxBase.js +0 -398
  100. package/src/boxes/BoxConnector.js +0 -325
  101. package/src/boxes/TextGrid.js +0 -887
  102. package/src/boxes/TextGridContent.js +0 -215
  103. package/src/init-jsdom.js +0 -65
  104. package/src/jclic-node.js +0 -219
  105. package/src/media/ActiveMediaBag.js +0 -145
  106. package/src/media/ActiveMediaPlayer.js +0 -297
  107. package/src/media/AudioBuffer.js +0 -219
  108. package/src/media/EventSounds.js +0 -169
  109. package/src/media/EventSoundsElement.js +0 -155
  110. package/src/media/MediaContent.js +0 -328
  111. package/src/media/MidiAudioPlayer.js +0 -254
  112. package/src/media/icons/audio.svg +0 -3
  113. package/src/media/icons/generic.svg +0 -3
  114. package/src/media/icons/mic.svg +0 -3
  115. package/src/media/icons/movie.svg +0 -3
  116. package/src/media/icons/music.svg +0 -3
  117. package/src/media/icons/url.svg +0 -3
  118. package/src/media/sounds/actionError.mp3 +0 -0
  119. package/src/media/sounds/actionOk.mp3 +0 -0
  120. package/src/media/sounds/click.mp3 +0 -0
  121. package/src/media/sounds/finishedError.mp3 +0 -0
  122. package/src/media/sounds/finishedOk.mp3 +0 -0
  123. package/src/media/sounds/start.mp3 +0 -0
  124. package/src/project/JClicProject.js +0 -282
  125. package/src/project/ProjectSettings.js +0 -273
  126. package/src/report/ActionReg.js +0 -123
  127. package/src/report/ActivityReg.js +0 -271
  128. package/src/report/EncryptMin.js +0 -210
  129. package/src/report/Reporter.js +0 -727
  130. package/src/report/SCORM.js +0 -272
  131. package/src/report/SequenceReg.js +0 -275
  132. package/src/report/SessionReg.js +0 -340
  133. package/src/report/SessionStorageReporter.js +0 -131
  134. package/src/report/TCPReporter.js +0 -628
  135. package/src/shapers/ClassicJigSaw.js +0 -138
  136. package/src/shapers/Holes.js +0 -77
  137. package/src/shapers/JigSaw.js +0 -161
  138. package/src/shapers/Rectangular.js +0 -78
  139. package/src/shapers/Shaper.js +0 -386
  140. package/src/shapers/TriangularJigSaw.js +0 -121
  141. package/src/skins/BlueSkin.js +0 -80
  142. package/src/skins/Counter.js +0 -152
  143. package/src/skins/CustomSkin.js +0 -412
  144. package/src/skins/DefaultSkin.js +0 -376
  145. package/src/skins/EmptySkin.js +0 -82
  146. package/src/skins/GreenSkin.js +0 -94
  147. package/src/skins/MiniSkin.js +0 -130
  148. package/src/skins/OrangeSkin.js +0 -78
  149. package/src/skins/SimpleSkin.js +0 -92
  150. package/src/skins/Skin.js +0 -1021
  151. package/src/skins/assets/actionsIcon.svg +0 -3
  152. package/src/skins/assets/appLogo.svg +0 -8
  153. package/src/skins/assets/basic.css +0 -41
  154. package/src/skins/assets/closeDialogIcon.svg +0 -3
  155. package/src/skins/assets/closeIcon.svg +0 -3
  156. package/src/skins/assets/copyIcon.svg +0 -3
  157. package/src/skins/assets/fullScreenExitIcon.svg +0 -3
  158. package/src/skins/assets/fullScreenIcon.svg +0 -3
  159. package/src/skins/assets/infoIcon.svg +0 -3
  160. package/src/skins/assets/main.css +0 -43
  161. package/src/skins/assets/mainHalf.css +0 -23
  162. package/src/skins/assets/mainTwoThirds.css +0 -23
  163. package/src/skins/assets/mini.css +0 -15
  164. package/src/skins/assets/nextIcon.svg +0 -3
  165. package/src/skins/assets/okDialogIcon.svg +0 -3
  166. package/src/skins/assets/prevIcon.svg +0 -3
  167. package/src/skins/assets/reports.css +0 -156
  168. package/src/skins/assets/reportsIcon.svg +0 -3
  169. package/src/skins/assets/scoreIcon.svg +0 -3
  170. package/src/skins/assets/simple.css +0 -16
  171. package/src/skins/assets/simpleHalf.css +0 -11
  172. package/src/skins/assets/simpleTwoThirds.css +0 -11
  173. package/src/skins/assets/timeIcon.svg +0 -4
  174. package/src/skins/assets/waitAnim.css +0 -54
  175. package/src/skins/assets/waitImgBig.svg +0 -3
  176. package/src/skins/assets/waitImgSmall.svg +0 -3
  177. package/webpack.config.mjs +0 -169
@@ -1,516 +0,0 @@
1
- /**
2
- * File : bags/MediaBagElement.js
3
- * Created : 07/04/2015
4
- * By : Francesc Busquets <francesc@gmail.com>
5
- *
6
- * JClic.js
7
- * An HTML5 player of JClic activities
8
- * https://projectestac.github.io/jclic.js
9
- *
10
- * @source https://github.com/projectestac/jclic.js
11
- *
12
- * @license EUPL-1.2
13
- * @licstart
14
- * (c) 2000-2020 Educational Telematic Network of Catalonia (XTEC)
15
- *
16
- * Licensed under the EUPL, Version 1.1 or -as soon they will be approved by
17
- * the European Commission- subsequent versions of the EUPL (the "Licence");
18
- * You may not use this work except in compliance with the Licence.
19
- *
20
- * You may obtain a copy of the Licence at:
21
- * https://joinup.ec.europa.eu/software/page/eupl
22
- *
23
- * Unless required by applicable law or agreed to in writing, software
24
- * distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
25
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26
- * Licence for the specific language governing permissions and limitations
27
- * under the Licence.
28
- * @licend
29
- * @module
30
- */
31
-
32
- /* global URL, Uint8Array, XMLHttpRequest, Image, document */
33
-
34
- import $ from 'jquery';
35
- import MidiAudioPlayer from '../media/MidiAudioPlayer.js';
36
- import { log, settings, nSlash, getAttr, isEmpty, getPathPromise, parseXmlNode, appendStyleAtHead } from '../Utils.js';
37
- import { Font } from '../AWT.js';
38
-
39
- /**
40
- * This kind of objects are the components of {@link module:bags/MediaBag.MediaBag MediaBag}.
41
- *
42
- * Media elements have a name, a reference to a file (the `file` field) and, when initialized,
43
- * a `data` field pointing to a object containing the real media. They have also a flag indicating
44
- * if the data must be saved on the {@link module:project/JClicProject.JClicProject JClicProject} zip file or just maintained as a reference
45
- * to an external file.
46
- */
47
- export class MediaBagElement {
48
- /**
49
- * MediaBagElement constructor
50
- * @param {string} basePath - Path to be used as a prefix of the file name
51
- * @param {string} file - The media file name
52
- * @param {external:JSZip} [zip] - An optional JSZip object from which the file must be extracted.
53
- */
54
- constructor(basePath, file, zip) {
55
- if (basePath)
56
- this.basePath = basePath;
57
- if (file) {
58
- this.file = nSlash(file);
59
- this.name = nSlash(file);
60
- this.ext = this.file.toLowerCase().split('.').pop();
61
- this.type = this.getFileType(this.ext);
62
- if (this.ext === 'gif')
63
- this.checkAnimatedGif();
64
- }
65
- if (zip)
66
- this.zip = zip;
67
- this.timeout = Date.now() + settings.LOAD_TIMEOUT;
68
- }
69
-
70
-
71
- /**
72
- * Private static array of {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement HTMLAudioElements},
73
- * to be reused between all media elements of type 'audio'. One for each priority level
74
- * @name module:bags/MediaBagElement#_audioPlayers
75
- * @type {external:HTMLAudioElement[]}
76
- */
77
- static _audioPlayers = [];
78
-
79
- /**
80
- * Gets the static {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement HTMLAudioElement}
81
- * associated to the requested priority level.
82
- * @param {number} level=1 - The priority level
83
- * @returns {external:HTMLAudioElement}
84
- */
85
- static getAudioPlayer(level = 1) {
86
- if (!MediaBagElement._audioPlayers[level])
87
- MediaBagElement._audioPlayers[level] = document.createElement('audio');
88
- return MediaBagElement._audioPlayers[level];
89
- }
90
-
91
- /**
92
- * Private static array of {@link bags/MediaBagElement MediaBagElements},
93
- * used to store a reference to the element using each `audioPlayer`
94
- * @name module:bags/MediaBagElement#_currentAudioElements
95
- * @type {bags/MediaBagElement[]}
96
- */
97
- static _currentAudioElements = [];
98
-
99
- /**
100
- * Clear all references to audio players and audio elements
101
- * To be called when a new activity starts
102
- */
103
- static resetAudioElements() {
104
- MediaBagElement._audioPlayers.fill(null);
105
- MediaBagElement._currentAudioElements.fill(null);
106
- }
107
-
108
- /**
109
- * Loads this object settings from a specific JQuery XML element
110
- * @param {external:jQuery} $xml - The XML element to parse
111
- */
112
- setProperties($xml) {
113
- this.name = nSlash($xml.attr('name'));
114
- this.file = nSlash($xml.attr('file'));
115
- this.ext = this.file.toLowerCase().split('.').pop();
116
- this.type = this.getFileType(this.ext);
117
- // Check if it's an animated GIF
118
- if (this.ext === 'gif') {
119
- const anim = $xml.attr('animated');
120
- if (typeof anim === 'undefined')
121
- this.checkAnimatedGif();
122
- else
123
- this.animated = anim === 'true';
124
- }
125
- if (this.type === 'font') {
126
- this.fontName = this.name === this.file && this.name.lastIndexOf('.') > 0 ?
127
- this.name.substring(0, this.name.lastIndexOf('.')) :
128
- this.name;
129
- }
130
- return this;
131
- }
132
-
133
- /**
134
- * Gets a object with the basic attributes needed to rebuild this instance excluding functions,
135
- * parent references, constants and also attributes retaining the default value.
136
- * The resulting object is commonly usued to serialize elements in JSON format.
137
- * @returns {object} - The resulting object, with minimal attrributes
138
- */
139
- getAttributes() {
140
- return getAttr(this, ['name', 'file', 'animated']);
141
- }
142
-
143
- /**
144
- * Loads the element properties from a data object
145
- * @param {object} data - The data object to parse
146
- */
147
- setAttributes(data) {
148
- ['name', 'file', 'animated'].forEach(attr => {
149
- if (!isEmpty(data[attr]))
150
- this[attr] = data[attr];
151
- });
152
-
153
- this.ext = this.file.toLowerCase().split('.').pop();
154
- this.type = this.getFileType(this.ext);
155
-
156
- // Check if it's an animated GIF
157
- if (this.ext === 'gif' && this.animated === 'undefined')
158
- this.checkAnimatedGif();
159
-
160
- if (this.type === 'font') {
161
- this.fontName = this.name === this.file && this.name.lastIndexOf('.') > 0 ?
162
- this.name.substring(0, this.name.lastIndexOf('.')) :
163
- this.name;
164
- }
165
- return this;
166
- }
167
-
168
- /**
169
- * Checks if the image associated with this MediaBagElement is an animated GIF
170
- *
171
- * Based on: {@link https://gist.github.com/marckubischta/261ad8427a214022890b}
172
- * Thanks to `@lakenen` and `@marckubischta`
173
- */
174
- checkAnimatedGif() {
175
- const request = new XMLHttpRequest();
176
- // Set `responseType` moved after calling `open`
177
- // see: https://stackoverflow.com/questions/20760635/why-does-setting-xmlhttprequest-responsetype-before-calling-open-throw
178
- // request.responseType = 'arraybuffer'
179
- request.addEventListener('load', () => {
180
- const
181
- arr = new Uint8Array(request.response),
182
- length = arr.length;
183
-
184
- // make sure it's a gif (GIF8)
185
- if (arr[0] !== 0x47 || arr[1] !== 0x49 ||
186
- arr[2] !== 0x46 || arr[3] !== 0x38) {
187
- this.animated = false;
188
- return;
189
- }
190
-
191
- // Ported from PHP [http://www.php.net/manual/en/function.imagecreatefromgif.php#104473]
192
- // an animated gif contains multiple "frames", with each frame having a
193
- // header made up of:
194
- // * a static 3-byte sequence (\x00\x21\xF9
195
- // * one byte indicating the length of the header (usually \x04)
196
- // * variable length header (usually 4 bytes)
197
- // * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?)
198
- // We read through the file as long as we haven't reached the end of the file
199
- // and we haven't yet found at least 2 frame headers
200
- for (let i = 0, len = length - 3, frames = 0; i < len && frames < 2; ++i) {
201
- if (arr[i] === 0x00 && arr[i + 1] === 0x21 && arr[i + 2] === 0xF9) {
202
- const
203
- blocklength = arr[i + 3],
204
- afterblock = i + 4 + blocklength;
205
- if (afterblock + 1 < length &&
206
- arr[afterblock] === 0x00 &&
207
- (arr[afterblock + 1] === 0x2C || arr[afterblock + 1] === 0x21)) {
208
- if (++frames > 1) {
209
- this.animated = true;
210
- log('debug', `Animated GIF detected: ${this.file}`);
211
- break;
212
- }
213
- }
214
- }
215
- }
216
- });
217
-
218
- this.getFullPathPromise()
219
- .then(fullPath => {
220
- request.open('GET', fullPath, true);
221
- request.responseType = 'arraybuffer';
222
- request.send();
223
- });
224
- }
225
-
226
- /**
227
- * Checks if the MediaBagElement has been initiated
228
- * @returns {boolean}
229
- */
230
- isEmpty() {
231
- return this.data === null;
232
- }
233
-
234
- /**
235
- * Determines the type of a file from its extension
236
- * @param {string} ext - The file name extension
237
- * @returns {string}
238
- */
239
- getFileType(ext) {
240
- let result = null;
241
- for (let type in settings.FILE_TYPES) {
242
- if (settings.FILE_TYPES[type].indexOf(ext) >= 0) {
243
- result = type;
244
- break;
245
- }
246
- }
247
- return result;
248
- }
249
-
250
- /**
251
- * Instantiates the media content
252
- * @param {function} callback - Callback method called when the referred resource is ready
253
- * @param {module:JClicPlayer.JClicPlayer} ps=null - An optional `PlayStation` (currently a {@link module:JClicPlayer.JClicPlayer JClicPlayer}) used to dynamically load fonts
254
- * @param {boolean} force=false - Used only in media of type 'audio'. When `true`, a static {@link MediaBagElement._audioPlayers audioPlayer element} will be loaded with this media source
255
- * @param {number} level=1 - Priority level of the media content to be built. Used only n audio elements.
256
- */
257
- build(callback, ps = null, force = false, level = 1) {
258
- // Mock data when running in NodeJS
259
- if (settings.NODEJS) {
260
- this.data = [];
261
- this.ready = true;
262
- }
263
-
264
- if (callback) {
265
- if (!this._whenReady)
266
- this._whenReady = [];
267
- this._whenReady.push(callback);
268
- }
269
-
270
- if (!this.data)
271
- this.getFullPathPromise()
272
- .then(fullPath => {
273
- switch (this.type) {
274
- case 'font':
275
- const
276
- format = this.ext === 'ttf' ? 'truetype' : this.ext === 'otf' ? 'embedded-opentype' : this.ext,
277
- css = `@font-face{font-family:"${this.fontName}";src:url(${fullPath}) format("${format}");}`;
278
-
279
- appendStyleAtHead(css, ps);
280
- this.data = new Font(this.name);
281
- this.ready = true;
282
- break;
283
-
284
- case 'image':
285
- this.data = new Image();
286
- this.data.addEventListener('load', () => { this._onReady.call(this); }, { once: true });
287
- this.data.src = fullPath;
288
- break;
289
-
290
- case 'video':
291
- this.data = document.createElement(this.type);
292
- this.data.addEventListener('canplay', () => { this._onReady.call(this); }, { once: true });
293
- this.data.src = fullPath;
294
- this.data.load();
295
- this.data.pause();
296
- break;
297
-
298
- case 'audio':
299
- // HTML Audio objects will be created on demand, when the param 'force' is set to true
300
- if (force) {
301
- // Clean up state in current audio element, if any
302
- const currentAudioElement = MediaBagElement._currentAudioElements[level];
303
- if (currentAudioElement && currentAudioElement !== this) {
304
- currentAudioElement.data = null;
305
- currentAudioElement.ready = false;
306
- }
307
- // Register as a current audio element
308
- MediaBagElement._currentAudioElements[level] = this;
309
- // Configure the audio player
310
- const audioPlayer = MediaBagElement.getAudioPlayer(level);
311
- if (audioPlayer.src !== fullPath) {
312
- log('trace', `Loading static player #${level} with new audio: ${fullPath}`);
313
- this.data = audioPlayer;
314
- this.ready = false;
315
- audioPlayer.addEventListener('canplay', () => { this._onReady.call(this); }, { once: true });
316
- audioPlayer.src = fullPath;
317
- audioPlayer.load();
318
- audioPlayer.pause();
319
- }
320
- else
321
- log('trace', `Reusing existing audio in player #${level}: ${fullPath}`);
322
- }
323
- else
324
- this.ready = true;
325
- break;
326
-
327
- case 'anim':
328
- // TODO: Use [Ruffle](https://ruffle.rs/) to play Flash movies
329
- this.data = $(`<object type"application/x-shockwave-flash" width="300" height="200" data="${fullPath}"/>`).get(-1);
330
- // Unable to check the loading progress in elements of type `object`. so we mark it always as `ready`:
331
- this.ready = true;
332
- break;
333
-
334
- case 'xml':
335
- $.get(fullPath, null, null, 'xml').done(xmlData => {
336
- const children = xmlData ? xmlData.children || xmlData.childNodes : null;
337
- this.data = children && children.length > 0 ? parseXmlNode(children[0]) : null;
338
- this._onReady();
339
- }).fail(err => {
340
- log('error', `Error loading ${this.name}: ${err}`);
341
- this._onReady();
342
- });
343
- break;
344
-
345
- case 'midi':
346
- const request = new XMLHttpRequest();
347
- request.onreadystatechange = () => {
348
- if (request.readyState === 4) {
349
- if (request.status === 200)
350
- this.data = new MidiAudioPlayer(request.response, ps && ps.options);
351
- else
352
- log('error', `Error loading ${this.name}: ${request.statusText}`);
353
- this._onReady();
354
- }
355
- };
356
- request.open('GET', fullPath, true);
357
- request.responseType = 'arraybuffer';
358
- request.send();
359
- break;
360
-
361
- default:
362
- log('trace', `Media currently not supported: ${this.name}`);
363
- this.ready = true;
364
- }
365
-
366
- if (this.ready)
367
- this._onReady();
368
- });
369
- else if (this.ready)
370
- this._onReady();
371
-
372
- return this;
373
- }
374
-
375
- /**
376
- * Checks if this media element is ready to start
377
- * @returns {boolean} - `true` if ready, `false` otherwise
378
- */
379
- checkReady() {
380
- if (this.data && !this.ready) {
381
- switch (this.type) {
382
- case 'image':
383
- this.ready = this.data.complete === true;
384
- break;
385
- case 'audio':
386
- case 'video':
387
- case 'anim':
388
- this.ready = this.data.readyState >= 1;
389
- break;
390
- default:
391
- this.ready = true;
392
- }
393
- }
394
- return this.ready;
395
- }
396
-
397
- /**
398
- * Checks if this resource has timed out.
399
- * @returns {boolean} - `true` if the resource has exhausted the allowed time to load, `false` otherwise
400
- */
401
- checkTimeout() {
402
- const result = Date.now() > this.timeout;
403
- if (result)
404
- log('warn', `Timeout while loading: ${this.name}`);
405
- return result;
406
- }
407
-
408
- /**
409
- * Notify listeners that the resource is ready
410
- */
411
- _onReady() {
412
- this.ready = true;
413
- if (this._whenReady) {
414
- this._whenReady.forEach(fn => fn.call(this, this));
415
- this._whenReady = null;
416
- }
417
- }
418
-
419
- /**
420
- * Gets the full path of the file associated to this element.
421
- * WARNING: This function should be called only after a successful call to `getFullPathPromise`
422
- * @returns {string}
423
- */
424
- getFullPath() {
425
- return this._fullPath;
426
- }
427
-
428
- /**
429
- * Gets a promise with the full path of the file associated to this element.
430
- * @returns {external:Promise}
431
- */
432
- getFullPathPromise() {
433
- return getPathPromise(this.basePath, this.file, this.zip)
434
- .then(fullPath => {
435
- // Process full URL only when running in a browser
436
- this._fullPath = settings.NODEJS
437
- ? fullPath
438
- : (new URL(fullPath, document.location.href)).toString();
439
- return this._fullPath;
440
- });
441
- }
442
- }
443
-
444
- Object.assign(MediaBagElement.prototype, {
445
- /**
446
- * The name of this element. Usually is the same as `file`
447
- * @name module:bags/MediaBagElement.MediaBagElement#name
448
- * @type {string} */
449
- name: '',
450
- /**
451
- * The name of the file where this element is stored
452
- * @name module:bags/MediaBagElement.MediaBagElement#file
453
- * @type {string} */
454
- file: '',
455
- /**
456
- * The font family name, used only in elements of type 'font'
457
- * @name module:bags/MediaBagElement.MediaBagElement#fontName
458
- * @type {string} */
459
- fontName: '',
460
- /**
461
- * The path to be used as base to access this media element
462
- * @name module:bags/MediaBagElement.MediaBagElement#basePath
463
- * @type {string} */
464
- basePath: '',
465
- /**
466
- * An optional JSZip object that can act as a container of this media
467
- * @name module:bags/MediaBagElement.MediaBagElement#zip
468
- * @type {external:JSZip} */
469
- zip: null,
470
- /**
471
- * When loaded, this field will store the realized media object
472
- * @name module:bags/MediaBagElement.MediaBagElement#data
473
- * @type {object} */
474
- data: null,
475
- /**
476
- * Flag indicating that `data` is ready to be used
477
- * @name module:bags/MediaBagElement.MediaBagElement#ready
478
- * @type {boolean} */
479
- ready: false,
480
- /**
481
- * Array of callback methods to be called when the resource becomes ready
482
- * @name module:bags/MediaBagElement.MediaBagElement#_whenReady
483
- * @private
484
- * @type {function[]} */
485
- _whenReady: null,
486
- /**
487
- * Normalized extension of `file`, useful to guess the media type
488
- * @name module:bags/MediaBagElement.MediaBagElement#ext
489
- * @type {string} */
490
- ext: '',
491
- /**
492
- * The resource type ('audio', 'image', 'midi', 'video', 'font')
493
- * @name module:bags/MediaBagElement.MediaBagElement#type
494
- * @type {string} */
495
- type: null,
496
- /**
497
- * Time set to load the resource before leaving
498
- * @name module:bags/MediaBagElement.MediaBagElement#timeout
499
- * @type {number} */
500
- timeout: 0,
501
- //
502
- /**
503
- * Flag used for animated GIFs
504
- * @name module:bags/MediaBagElement.MediaBagElement#animated
505
- * @type {boolean} */
506
- animated: false,
507
- /**
508
- * Full path obtained after a successful call to getFullPathPromise
509
- * @name module:bags/MediaBagElement.MediaBagElement#_fullPath
510
- * @private
511
- * @type {string}
512
- */
513
- _fullPath: null,
514
- });
515
-
516
- export default MediaBagElement;