Fixing an issue with CMI interactions not being loaded in the proper order
Interaction ID and Type need to be loaded before most other interaction fields, so I added a built in sort when loading using the flattened JSON load.
This commit is contained in:
80
dist/scorm-again.js
vendored
80
dist/scorm-again.js
vendored
File diff suppressed because one or more lines are too long
2
dist/scorm-again.js.map
vendored
2
dist/scorm-again.js.map
vendored
File diff suppressed because one or more lines are too long
4
dist/scorm-again.min.js
vendored
4
dist/scorm-again.min.js
vendored
File diff suppressed because one or more lines are too long
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "scorm-again",
|
||||
"version": "1.3.1",
|
||||
"version": "1.3.2",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "scorm-again",
|
||||
"version": "1.3.1",
|
||||
"version": "1.3.2",
|
||||
"description": "A modern SCORM JavaScript run-time library for AICC, SCORM 1.2, and SCORM 2004",
|
||||
"main": "dist/scorm-again.min.js",
|
||||
"directories": {
|
||||
|
||||
@@ -3,7 +3,7 @@ import {CMIArray} from './cmi/common';
|
||||
import {ValidationError} from './exceptions';
|
||||
import ErrorCodes from './constants/error_codes';
|
||||
import APIConstants from './constants/api_constants';
|
||||
import {unflatten} from './utilities';
|
||||
import {sortFlattenedCMIElements, unflatten} from './utilities';
|
||||
import debounce from 'lodash.debounce';
|
||||
|
||||
const global_constants = APIConstants.global;
|
||||
@@ -848,7 +848,55 @@ export default class BaseAPI {
|
||||
* @param {string} CMIElement
|
||||
*/
|
||||
loadFromFlattenedJSON(json, CMIElement) {
|
||||
this.loadFromJSON(unflatten(json), CMIElement);
|
||||
if (!this.isNotInitialized()) {
|
||||
console.error(
|
||||
'loadFromFlattenedJSON can only be called before the call to lmsInitialize.');
|
||||
return;
|
||||
}
|
||||
|
||||
console.info(json);
|
||||
|
||||
const result = Object.keys(json).map(function(key) {
|
||||
return [String(key), json[key]];
|
||||
});
|
||||
|
||||
// CMI interactions need to have id and type loaded before any other fields
|
||||
result.sort(function([a, b], [c, d]) {
|
||||
const id_pattern = /^cmi\.interactions\.(\d+)\.id$/;
|
||||
const type_pattern = /^cmi\.interactions\.(\d+)\.type$/;
|
||||
const pattern = /^cmi\.interactions\.(\d+)/;
|
||||
|
||||
if (a.match(id_pattern) && c.match(pattern)) {
|
||||
if (a.match(id_pattern)[1] < c.match(pattern)[1]) return -1;
|
||||
if (a.match(id_pattern)[1] > c.match(pattern)[1]) return 1;
|
||||
return -1;
|
||||
} else if (a.match(pattern) && c.match(id_pattern)) {
|
||||
if (a.match(pattern)[1] < c.match(id_pattern)[1]) return -1;
|
||||
if (a.match(pattern)[1] > c.match(id_pattern)[1]) return 1;
|
||||
return 1;
|
||||
} else if (a.match(type_pattern) && c.match(pattern)) {
|
||||
if (a.match(type_pattern)[1] < c.match(pattern)[1]) return -1;
|
||||
if (a.match(type_pattern)[1] > c.match(pattern)[1]) return 1;
|
||||
return -1;
|
||||
} else if (a.match(pattern) && c.match(type_pattern)) {
|
||||
if (a.match(pattern)[1] < c.match(type_pattern)[1]) return -1;
|
||||
if (a.match(pattern)[1] > c.match(type_pattern)[1]) return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (a < c) return -1;
|
||||
if (a > c) return 1;
|
||||
return 0;
|
||||
});
|
||||
|
||||
console.info(result);
|
||||
|
||||
let obj;
|
||||
result.forEach((element) => {
|
||||
obj = {};
|
||||
obj[element[0]] = element[1];
|
||||
this.loadFromJSON(unflatten(obj), CMIElement);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -819,8 +819,13 @@ export class CMIInteractionsObject extends BaseCMI {
|
||||
* @param {string} type
|
||||
*/
|
||||
set type(type) {
|
||||
if (check2004ValidFormat(type, scorm2004_regex.CMIType)) {
|
||||
this.#type = type;
|
||||
if (typeof this.id === 'undefined') {
|
||||
throw new ValidationError(
|
||||
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
|
||||
} else {
|
||||
if (check2004ValidFormat(type, scorm2004_regex.CMIType)) {
|
||||
this.#type = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -874,7 +879,7 @@ export class CMIInteractionsObject extends BaseCMI {
|
||||
* @param {string} learner_response
|
||||
*/
|
||||
set learner_response(learner_response) {
|
||||
if (typeof this.type === 'undefined') {
|
||||
if (typeof this.type === 'undefined' || typeof this.id === 'undefined') {
|
||||
throw new ValidationError(
|
||||
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user