scorm-again
Version:
A modern SCORM JavaScript run-time library for AICC, SCORM 1.2, and SCORM 2004
1,155 lines (1,110 loc) • 167 kB
JavaScript
/******/ (function() { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ 429:
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// EXPORTS
__webpack_require__.d(__webpack_exports__, {
"default": function() { return /* binding */ src_BaseAPI; }
});
// EXTERNAL MODULE: ./node_modules/tslib/tslib.es6.mjs
var tslib_es6 = __webpack_require__(635);
// EXTERNAL MODULE: ./src/cmi/common/array.ts
var array = __webpack_require__(589);
// EXTERNAL MODULE: ./src/exceptions.ts
var exceptions = __webpack_require__(784);
// EXTERNAL MODULE: ./src/constants/api_constants.ts
var api_constants = __webpack_require__(340);
// EXTERNAL MODULE: ./src/utilities.ts
var utilities = __webpack_require__(864);
// EXTERNAL MODULE: ./src/constants/enums.ts
var enums = __webpack_require__(56);
;// ./src/constants/default_settings.ts
var DefaultSettings = {
autocommit: false,
autocommitSeconds: 10,
asyncCommit: false,
sendFullCommit: true,
lmsCommitUrl: false,
dataCommitFormat: "json",
commitRequestDataType: "application/json;charset=UTF-8",
autoProgress: false,
logLevel: enums.LogLevelEnum.ERROR,
selfReportSessionTime: false,
alwaysSendTotalTime: false,
renderCommonCommitFields: false,
strict_errors: true,
xhrHeaders: {},
xhrWithCredentials: false,
fetchMode: "cors",
responseHandler: function (response) {
return (0,tslib_es6.__awaiter)(this, void 0, void 0, function () {
var responseText, httpResult;
return (0,tslib_es6.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
if (!(typeof response !== "undefined")) return [3, 2];
return [4, response.text()];
case 1:
responseText = _a.sent();
httpResult = null;
if (responseText) {
httpResult = JSON.parse(responseText);
}
if (httpResult === null ||
!{}.hasOwnProperty.call(httpResult, "result")) {
if (response.status === 200) {
return [2, {
result: api_constants.global_constants.SCORM_TRUE,
errorCode: 0
}];
}
else {
return [2, {
result: api_constants.global_constants.SCORM_FALSE,
errorCode: 101
}];
}
}
else {
return [2, {
result: httpResult.result,
errorCode: httpResult.errorCode
? httpResult.errorCode
: httpResult.result === api_constants.global_constants.SCORM_TRUE
? 0
: 101
}];
}
_a.label = 2;
case 2: return [2, {
result: api_constants.global_constants.SCORM_FALSE,
errorCode: 101
}];
}
});
});
},
requestHandler: function (commitObject) {
return commitObject;
},
onLogMessage: function (messageLevel, logMessage) {
switch (messageLevel) {
case "4":
case 4:
case "ERROR":
case enums.LogLevelEnum.ERROR:
console.error(logMessage);
break;
case "3":
case 3:
case "WARN":
case enums.LogLevelEnum.WARN:
console.warn(logMessage);
break;
case "2":
case 2:
case "INFO":
case enums.LogLevelEnum.INFO:
console.info(logMessage);
break;
case "1":
case 1:
case "DEBUG":
case enums.LogLevelEnum.DEBUG:
if (console.debug) {
console.debug(logMessage);
}
else {
console.log(logMessage);
}
break;
}
},
scoItemIds: [],
scoItemIdValidator: false,
globalObjectiveIds: []
};
;// ./src/helpers/scheduled_commit.ts
var ScheduledCommit = (function () {
function ScheduledCommit(API, when, callback) {
this._cancelled = false;
this._API = API;
this._timeout = setTimeout(this.wrapper.bind(this), when);
this._callback = callback;
}
ScheduledCommit.prototype.cancel = function () {
this._cancelled = true;
if (this._timeout) {
clearTimeout(this._timeout);
}
};
ScheduledCommit.prototype.wrapper = function () {
var _this = this;
if (!this._cancelled) {
(function () { return (0,tslib_es6.__awaiter)(_this, void 0, void 0, function () { return (0,tslib_es6.__generator)(this, function (_a) {
switch (_a.label) {
case 0: return [4, this._API.commit(this._callback)];
case 1: return [2, _a.sent()];
}
}); }); })();
}
};
return ScheduledCommit;
}());
;// ./src/BaseAPI.ts
var BaseAPI = (function () {
function BaseAPI(error_codes, settings) {
var _newTarget = this.constructor;
var _a, _c;
this._settings = DefaultSettings;
if (_newTarget === BaseAPI) {
throw new TypeError("Cannot construct BaseAPI instances directly");
}
this.currentState = api_constants.global_constants.STATE_NOT_INITIALIZED;
this.lastErrorCode = "0";
this.listenerArray = [];
this._error_codes = error_codes;
if (settings) {
this.settings = (0,tslib_es6.__assign)((0,tslib_es6.__assign)({}, DefaultSettings), settings);
}
this.apiLogLevel = (_a = this.settings.logLevel) !== null && _a !== void 0 ? _a : enums.LogLevelEnum.ERROR;
this.selfReportSessionTime = (_c = this.settings.selfReportSessionTime) !== null && _c !== void 0 ? _c : false;
}
BaseAPI.prototype.commonReset = function (settings) {
this.apiLog("reset", "Called", enums.LogLevelEnum.INFO);
this.settings = (0,tslib_es6.__assign)((0,tslib_es6.__assign)({}, this.settings), settings);
this.clearScheduledCommit();
this.currentState = api_constants.global_constants.STATE_NOT_INITIALIZED;
this.lastErrorCode = "0";
this.listenerArray = [];
this.startingData = {};
};
BaseAPI.prototype.initialize = function (callbackName, initializeMessage, terminationMessage) {
var returnValue = api_constants.global_constants.SCORM_FALSE;
if (this.isInitialized()) {
this.throwSCORMError(this._error_codes.INITIALIZED, initializeMessage);
}
else if (this.isTerminated()) {
this.throwSCORMError(this._error_codes.TERMINATED, terminationMessage);
}
else {
if (this.selfReportSessionTime) {
this.cmi.setStartTime();
}
this.currentState = api_constants.global_constants.STATE_INITIALIZED;
this.lastErrorCode = "0";
returnValue = api_constants.global_constants.SCORM_TRUE;
this.processListeners(callbackName);
}
this.apiLog(callbackName, "returned: " + returnValue, enums.LogLevelEnum.INFO);
this.clearSCORMError(returnValue);
return returnValue;
};
BaseAPI.prototype.apiLog = function (functionName, logMessage, messageLevel, CMIElement) {
logMessage = (0,utilities.formatMessage)(functionName, logMessage, CMIElement);
if (messageLevel >= this.apiLogLevel) {
this.settings.onLogMessage(messageLevel, logMessage);
}
};
Object.defineProperty(BaseAPI.prototype, "error_codes", {
get: function () {
return this._error_codes;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseAPI.prototype, "settings", {
get: function () {
return this._settings;
},
set: function (settings) {
this._settings = (0,tslib_es6.__assign)((0,tslib_es6.__assign)({}, this._settings), settings);
},
enumerable: false,
configurable: true
});
BaseAPI.prototype.terminate = function (callbackName, checkTerminated) {
return (0,tslib_es6.__awaiter)(this, void 0, void 0, function () {
var returnValue, result;
return (0,tslib_es6.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
returnValue = api_constants.global_constants.SCORM_FALSE;
if (!this.checkState(checkTerminated, this._error_codes.TERMINATION_BEFORE_INIT, this._error_codes.MULTIPLE_TERMINATION)) return [3, 2];
this.currentState = api_constants.global_constants.STATE_TERMINATED;
return [4, this.storeData(true)];
case 1:
result = _a.sent();
if (typeof result.errorCode !== "undefined" && result.errorCode > 0) {
this.throwSCORMError(result.errorCode);
}
returnValue =
typeof result !== "undefined" && (result.result === true || result.result === api_constants.global_constants.SCORM_TRUE)
? api_constants.global_constants.SCORM_TRUE
: api_constants.global_constants.SCORM_FALSE;
if (checkTerminated)
this.lastErrorCode = "0";
returnValue = api_constants.global_constants.SCORM_TRUE;
this.processListeners(callbackName);
_a.label = 2;
case 2:
this.apiLog(callbackName, "returned: " + returnValue, enums.LogLevelEnum.INFO);
this.clearSCORMError(returnValue);
return [2, returnValue];
}
});
});
};
BaseAPI.prototype.getValue = function (callbackName, checkTerminated, CMIElement) {
var returnValue = "";
if (this.checkState(checkTerminated, this._error_codes.RETRIEVE_BEFORE_INIT, this._error_codes.RETRIEVE_AFTER_TERM)) {
if (checkTerminated)
this.lastErrorCode = "0";
try {
returnValue = this.getCMIValue(CMIElement);
}
catch (e) {
returnValue = this.handleValueAccessException(e, returnValue);
}
this.processListeners(callbackName, CMIElement);
}
this.apiLog(callbackName, ": returned: " + returnValue, enums.LogLevelEnum.INFO, CMIElement);
if (returnValue === undefined) {
return "";
}
this.clearSCORMError(returnValue);
return returnValue;
};
BaseAPI.prototype.setValue = function (callbackName, commitCallback, checkTerminated, CMIElement, value) {
if (value !== undefined) {
value = String(value);
}
var returnValue = api_constants.global_constants.SCORM_FALSE;
if (this.checkState(checkTerminated, this._error_codes.STORE_BEFORE_INIT, this._error_codes.STORE_AFTER_TERM)) {
if (checkTerminated)
this.lastErrorCode = "0";
try {
returnValue = this.setCMIValue(CMIElement, value);
}
catch (e) {
this.handleValueAccessException(e, returnValue);
}
this.processListeners(callbackName, CMIElement, value);
}
if (returnValue === undefined) {
returnValue = api_constants.global_constants.SCORM_FALSE;
}
if (String(this.lastErrorCode) === "0") {
if (this.settings.autocommit) {
this.scheduleCommit(this.settings.autocommitSeconds * 1000, commitCallback);
}
}
this.apiLog(callbackName, ": " + value + ": result: " + returnValue, enums.LogLevelEnum.INFO, CMIElement);
this.clearSCORMError(returnValue);
return returnValue;
};
BaseAPI.prototype.commit = function (callbackName_1) {
return (0,tslib_es6.__awaiter)(this, arguments, void 0, function (callbackName, checkTerminated) {
var returnValue, result;
if (checkTerminated === void 0) { checkTerminated = false; }
return (0,tslib_es6.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
this.clearScheduledCommit();
returnValue = api_constants.global_constants.SCORM_FALSE;
if (!this.checkState(checkTerminated, this._error_codes.COMMIT_BEFORE_INIT, this._error_codes.COMMIT_AFTER_TERM)) return [3, 2];
return [4, this.storeData(false)];
case 1:
result = _a.sent();
if (result.errorCode && result.errorCode > 0) {
this.throwSCORMError(result.errorCode);
}
returnValue =
typeof result !== "undefined" && (result.result === true || result.result === api_constants.global_constants.SCORM_TRUE)
? api_constants.global_constants.SCORM_TRUE
: api_constants.global_constants.SCORM_FALSE;
this.apiLog(callbackName, " Result: " + returnValue, enums.LogLevelEnum.DEBUG, "HttpRequest");
if (checkTerminated)
this.lastErrorCode = "0";
this.processListeners(callbackName);
_a.label = 2;
case 2:
this.apiLog(callbackName, "returned: " + returnValue, enums.LogLevelEnum.INFO);
this.clearSCORMError(returnValue);
return [2, returnValue];
}
});
});
};
BaseAPI.prototype.getLastError = function (callbackName) {
var returnValue = String(this.lastErrorCode);
this.processListeners(callbackName);
this.apiLog(callbackName, "returned: " + returnValue, enums.LogLevelEnum.INFO);
return returnValue;
};
BaseAPI.prototype.getErrorString = function (callbackName, CMIErrorCode) {
var returnValue = "";
if (CMIErrorCode !== null && CMIErrorCode !== "") {
returnValue = this.getLmsErrorMessageDetails(CMIErrorCode);
this.processListeners(callbackName);
}
this.apiLog(callbackName, "returned: " + returnValue, enums.LogLevelEnum.INFO);
return returnValue;
};
BaseAPI.prototype.getDiagnostic = function (callbackName, CMIErrorCode) {
var returnValue = "";
if (CMIErrorCode !== null && CMIErrorCode !== "") {
returnValue = this.getLmsErrorMessageDetails(CMIErrorCode, true);
this.processListeners(callbackName);
}
this.apiLog(callbackName, "returned: " + returnValue, enums.LogLevelEnum.INFO);
return returnValue;
};
BaseAPI.prototype.checkState = function (checkTerminated, beforeInitError, afterTermError) {
if (this.isNotInitialized()) {
this.throwSCORMError(beforeInitError);
return false;
}
else if (checkTerminated && this.isTerminated()) {
this.throwSCORMError(afterTermError);
return false;
}
return true;
};
BaseAPI.prototype.getLmsErrorMessageDetails = function (_errorNumber, _detail) {
if (_detail === void 0) { _detail = false; }
throw new Error("The getLmsErrorMessageDetails method has not been implemented");
};
BaseAPI.prototype.getCMIValue = function (_CMIElement) {
throw new Error("The getCMIValue method has not been implemented");
};
BaseAPI.prototype.setCMIValue = function (_CMIElement, _value) {
throw new Error("The setCMIValue method has not been implemented");
};
BaseAPI.prototype._commonSetCMIValue = function (methodName, scorm2004, CMIElement, value) {
if (!CMIElement || CMIElement === "") {
return api_constants.global_constants.SCORM_FALSE;
}
var structure = CMIElement.split(".");
var refObject = this;
var returnValue = api_constants.global_constants.SCORM_FALSE;
var foundFirstIndex = false;
var invalidErrorMessage = "The data model element passed to ".concat(methodName, " (").concat(CMIElement, ") is not a valid SCORM data model element.");
var invalidErrorCode = scorm2004
? this._error_codes.UNDEFINED_DATA_MODEL
: this._error_codes.GENERAL;
for (var idx = 0; idx < structure.length; idx++) {
var attribute = structure[idx];
if (!attribute) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
return api_constants.global_constants.SCORM_FALSE;
}
if (idx === structure.length - 1) {
if (scorm2004 && attribute.substring(0, 8) === "{target=") {
if (this.isInitialized()) {
this.throwSCORMError(this._error_codes.READ_ONLY_ELEMENT);
}
else {
refObject = (0,tslib_es6.__assign)((0,tslib_es6.__assign)({}, refObject), { attribute: value });
}
}
else if (!this._checkObjectHasProperty(refObject, attribute)) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
}
else {
if ((0,utilities.stringMatches)(CMIElement, "\\.correct_responses\\.\\d+") &&
this.isInitialized()) {
this.validateCorrectResponse(CMIElement, value);
}
if (!scorm2004 || this.lastErrorCode === "0") {
refObject[attribute] = value;
returnValue = api_constants.global_constants.SCORM_TRUE;
}
}
}
else {
refObject = refObject[attribute];
if (!refObject) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
break;
}
if (refObject instanceof array.CMIArray) {
var index = parseInt(structure[idx + 1] || "0", 10);
if (!isNaN(index)) {
var item = refObject.childArray[index];
if (item) {
refObject = item;
foundFirstIndex = true;
}
else {
var newChild = this.getChildElement(CMIElement, value, foundFirstIndex);
foundFirstIndex = true;
if (!newChild) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
}
else {
if (refObject.initialized)
newChild.initialize();
refObject.childArray.push(newChild);
refObject = newChild;
}
}
idx++;
}
}
}
}
if (returnValue === api_constants.global_constants.SCORM_FALSE) {
this.apiLog(methodName, "There was an error setting the value for: ".concat(CMIElement, ", value of: ").concat(value), enums.LogLevelEnum.WARN);
}
return returnValue;
};
BaseAPI.prototype._commonGetCMIValue = function (methodName, scorm2004, CMIElement) {
if (!CMIElement || CMIElement === "") {
return "";
}
var structure = CMIElement.split(".");
var refObject = this;
var attribute = null;
var uninitializedErrorMessage = "The data model element passed to ".concat(methodName, " (").concat(CMIElement, ") has not been initialized.");
var invalidErrorMessage = "The data model element passed to ".concat(methodName, " (").concat(CMIElement, ") is not a valid SCORM data model element.");
var invalidErrorCode = scorm2004
? this._error_codes.UNDEFINED_DATA_MODEL
: this._error_codes.GENERAL;
for (var idx = 0; idx < structure.length; idx++) {
attribute = structure[idx];
if (!attribute) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
return;
}
if (!scorm2004) {
if (idx === structure.length - 1) {
if (!this._checkObjectHasProperty(refObject, attribute)) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
return;
}
}
}
else {
if (String(attribute).substring(0, 8) === "{target=" &&
typeof refObject._isTargetValid == "function") {
var target = String(attribute).substring(8, String(attribute).length - 9);
return refObject._isTargetValid(target);
}
else if (!this._checkObjectHasProperty(refObject, attribute)) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
return;
}
}
refObject = refObject[attribute];
if (refObject === undefined) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
break;
}
if (refObject instanceof array.CMIArray) {
var index = parseInt(structure[idx + 1] || "", 10);
if (!isNaN(index)) {
var item = refObject.childArray[index];
if (item) {
refObject = item;
}
else {
this.throwSCORMError(this._error_codes.VALUE_NOT_INITIALIZED, uninitializedErrorMessage);
break;
}
idx++;
}
}
}
if (refObject === null || refObject === undefined) {
if (!scorm2004) {
if (attribute === "_children") {
this.throwSCORMError(this._error_codes.CHILDREN_ERROR);
}
else if (attribute === "_count") {
this.throwSCORMError(this._error_codes.COUNT_ERROR);
}
}
}
else {
return refObject;
}
};
BaseAPI.prototype.isInitialized = function () {
return this.currentState === api_constants.global_constants.STATE_INITIALIZED;
};
BaseAPI.prototype.isNotInitialized = function () {
return this.currentState === api_constants.global_constants.STATE_NOT_INITIALIZED;
};
BaseAPI.prototype.isTerminated = function () {
return this.currentState === api_constants.global_constants.STATE_TERMINATED;
};
BaseAPI.prototype.on = function (listenerName, callback) {
var _a, _c;
if (!callback)
return;
var listenerFunctions = listenerName.split(" ");
for (var i = 0; i < listenerFunctions.length; i++) {
var listenerSplit = (_a = listenerFunctions[i]) === null || _a === void 0 ? void 0 : _a.split(".");
if (!listenerSplit || listenerSplit.length === 0)
return;
var functionName = listenerSplit[0];
var CMIElement = null;
if (listenerSplit.length > 1) {
CMIElement = (_c = listenerFunctions[i]) === null || _c === void 0 ? void 0 : _c.replace(functionName + ".", "");
}
this.listenerArray.push({
functionName: functionName,
CMIElement: CMIElement,
callback: callback
});
this.apiLog("on", "Added event listener: ".concat(this.listenerArray.length), enums.LogLevelEnum.INFO, functionName);
}
};
BaseAPI.prototype.off = function (listenerName, callback) {
var _a, _c;
if (!callback)
return;
var listenerFunctions = listenerName.split(" ");
var _loop_1 = function (i) {
var listenerSplit = (_a = listenerFunctions[i]) === null || _a === void 0 ? void 0 : _a.split(".");
if (!listenerSplit || listenerSplit.length === 0)
return { value: void 0 };
var functionName = listenerSplit[0];
var CMIElement = null;
if (listenerSplit.length > 1) {
CMIElement = (_c = listenerFunctions[i]) === null || _c === void 0 ? void 0 : _c.replace(functionName + ".", "");
}
var removeIndex = this_1.listenerArray.findIndex(function (obj) {
return obj.functionName === functionName &&
obj.CMIElement === CMIElement &&
obj.callback === callback;
});
if (removeIndex !== -1) {
this_1.listenerArray.splice(removeIndex, 1);
this_1.apiLog("off", "Removed event listener: ".concat(this_1.listenerArray.length), enums.LogLevelEnum.INFO, functionName);
}
};
var this_1 = this;
for (var i = 0; i < listenerFunctions.length; i++) {
var state_1 = _loop_1(i);
if (typeof state_1 === "object")
return state_1.value;
}
};
BaseAPI.prototype.clear = function (listenerName) {
var _a, _c;
var listenerFunctions = listenerName.split(" ");
var _loop_2 = function (i) {
var listenerSplit = (_a = listenerFunctions[i]) === null || _a === void 0 ? void 0 : _a.split(".");
if (!listenerSplit || (listenerSplit === null || listenerSplit === void 0 ? void 0 : listenerSplit.length) === 0)
return { value: void 0 };
var functionName = listenerSplit[0];
var CMIElement = null;
if (listenerSplit.length > 1) {
CMIElement = (_c = listenerFunctions[i]) === null || _c === void 0 ? void 0 : _c.replace(functionName + ".", "");
}
this_2.listenerArray = this_2.listenerArray.filter(function (obj) {
return obj.functionName !== functionName && obj.CMIElement !== CMIElement;
});
};
var this_2 = this;
for (var i = 0; i < listenerFunctions.length; i++) {
var state_2 = _loop_2(i);
if (typeof state_2 === "object")
return state_2.value;
}
};
BaseAPI.prototype.processListeners = function (functionName, CMIElement, value) {
this.apiLog(functionName, value, enums.LogLevelEnum.INFO, CMIElement);
for (var i = 0; i < this.listenerArray.length; i++) {
var listener = this.listenerArray[i];
var functionsMatch = listener.functionName === functionName;
var listenerHasCMIElement = !!listener.CMIElement;
var CMIElementsMatch = false;
if (CMIElement &&
listener.CMIElement &&
listener.CMIElement.substring(listener.CMIElement.length - 1) === "*") {
CMIElementsMatch =
CMIElement.indexOf(listener.CMIElement.substring(0, listener.CMIElement.length - 1)) === 0;
}
else {
CMIElementsMatch = listener.CMIElement === CMIElement;
}
if (functionsMatch && (!listenerHasCMIElement || CMIElementsMatch)) {
this.apiLog("processListeners", "Processing listener: ".concat(listener.functionName), enums.LogLevelEnum.INFO, CMIElement);
listener.callback(CMIElement, value);
}
}
};
BaseAPI.prototype.throwSCORMError = function (errorNumber, message) {
if (!message) {
message = this.getLmsErrorMessageDetails(errorNumber);
}
this.apiLog("throwSCORMError", errorNumber + ": " + message, enums.LogLevelEnum.ERROR);
this.lastErrorCode = String(errorNumber);
};
BaseAPI.prototype.clearSCORMError = function (success) {
if (success !== undefined && success !== api_constants.global_constants.SCORM_FALSE) {
this.lastErrorCode = "0";
}
};
BaseAPI.prototype.loadFromFlattenedJSON = function (json, CMIElement) {
var _this = this;
if (!CMIElement) {
CMIElement = "";
}
if (!this.isNotInitialized()) {
console.error("loadFromFlattenedJSON can only be called before the call to lmsInitialize.");
return;
}
function testPattern(a, c, a_pattern) {
var a_match = a.match(a_pattern);
var c_match;
if (a_match !== null && (c_match = c.match(a_pattern)) !== null) {
var a_num = Number(a_match[2]);
var c_num = Number(c_match[2]);
if (a_num === c_num) {
if (a_match[3] === "id") {
return -1;
}
else if (a_match[3] === "type") {
if (c_match[3] === "id") {
return 1;
}
else {
return -1;
}
}
else {
return 1;
}
}
return a_num - c_num;
}
return null;
}
var int_pattern = /^(cmi\.interactions\.)(\d+)\.(.*)$/;
var obj_pattern = /^(cmi\.objectives\.)(\d+)\.(.*)$/;
var result = Object.keys(json).map(function (key) {
return [String(key), json[key]];
});
result.sort(function (_a, _c) {
var a = _a[0], _b = _a[1];
var c = _c[0], _d = _c[1];
var test;
if ((test = testPattern(a, c, int_pattern)) !== null) {
return test;
}
if ((test = testPattern(a, c, obj_pattern)) !== null) {
return test;
}
if (a < c) {
return -1;
}
if (a > c) {
return 1;
}
return 0;
});
var obj;
result.forEach(function (element) {
obj = {};
obj[element[0]] = element[1];
_this.loadFromJSON((0,utilities.unflatten)(obj), CMIElement);
});
};
BaseAPI.prototype.loadFromJSON = function (json, CMIElement) {
if (CMIElement === void 0) { CMIElement = ""; }
if ((!CMIElement || CMIElement === "") &&
!Object.hasOwnProperty.call(json, "cmi") &&
!Object.hasOwnProperty.call(json, "adl")) {
CMIElement = "cmi";
}
if (!this.isNotInitialized()) {
console.error("loadFromJSON can only be called before the call to lmsInitialize.");
return;
}
CMIElement = CMIElement !== undefined ? CMIElement : "cmi";
this.startingData = json;
for (var key in json) {
if ({}.hasOwnProperty.call(json, key) && json[key]) {
var currentCMIElement = (CMIElement ? CMIElement + "." : "") + key;
var value = json[key];
if (value["childArray"]) {
for (var i = 0; i < value["childArray"].length; i++) {
this.loadFromJSON(value["childArray"][i], currentCMIElement + "." + i);
}
}
else if (value.constructor === Object) {
this.loadFromJSON(value, currentCMIElement);
}
else {
this.setCMIValue(currentCMIElement, value);
}
}
}
};
BaseAPI.prototype.renderCMIToJSONString = function () {
var cmi = this.cmi;
if (this.settings.sendFullCommit) {
return JSON.stringify({ cmi: cmi });
}
return JSON.stringify({ cmi: cmi }, function (k, v) { return (v === undefined ? null : v); }, 2);
};
BaseAPI.prototype.renderCMIToJSONObject = function () {
return JSON.parse(this.renderCMIToJSONString());
};
BaseAPI.prototype.processHttpRequest = function (url_1, params_1) {
return (0,tslib_es6.__awaiter)(this, arguments, void 0, function (url, params, immediate) {
var api, genericError, process;
var _this = this;
if (immediate === void 0) { immediate = false; }
return (0,tslib_es6.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
api = this;
genericError = {
result: api_constants.global_constants.SCORM_FALSE,
errorCode: this.error_codes.GENERAL
};
if (immediate) {
params = this.settings.requestHandler(params);
this.performFetch(url, params).then(function (response) { return (0,tslib_es6.__awaiter)(_this, void 0, void 0, function () {
return (0,tslib_es6.__generator)(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.transformResponse(response)];
case 1:
_a.sent();
return [2];
}
});
}); });
return [2, {
result: api_constants.global_constants.SCORM_TRUE,
errorCode: 0
}];
}
process = function (url, params, settings) { return (0,tslib_es6.__awaiter)(_this, void 0, void 0, function () {
var response, e_1, message;
return (0,tslib_es6.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
params = settings.requestHandler(params);
return [4, this.performFetch(url, params)];
case 1:
response = _a.sent();
return [2, this.transformResponse(response)];
case 2:
e_1 = _a.sent();
message = e_1 instanceof Error ? e_1.message : String(e_1);
this.apiLog("processHttpRequest", message, enums.LogLevelEnum.ERROR);
api.processListeners("CommitError");
return [2, genericError];
case 3: return [2];
}
});
}); };
return [4, process(url, params, this.settings)];
case 1: return [2, _a.sent()];
}
});
});
};
BaseAPI.prototype.scheduleCommit = function (when, callback) {
if (!this._timeout) {
this._timeout = new ScheduledCommit(this, when, callback);
this.apiLog("scheduleCommit", "scheduled", enums.LogLevelEnum.DEBUG, "");
}
};
BaseAPI.prototype.clearScheduledCommit = function () {
if (this._timeout) {
this._timeout.cancel();
this._timeout = undefined;
this.apiLog("clearScheduledCommit", "cleared", enums.LogLevelEnum.DEBUG, "");
}
};
BaseAPI.prototype._checkObjectHasProperty = function (refObject, attribute) {
return (Object.hasOwnProperty.call(refObject, attribute) ||
Object.getOwnPropertyDescriptor(Object.getPrototypeOf(refObject), attribute) != null ||
attribute in refObject);
};
BaseAPI.prototype.handleValueAccessException = function (e, returnValue) {
if (e instanceof exceptions.ValidationError) {
this.lastErrorCode = String(e.errorCode);
returnValue = api_constants.global_constants.SCORM_FALSE;
}
else {
if (e instanceof Error && e.message) {
console.error(e.message);
}
else {
console.error(e);
}
this.throwSCORMError(this._error_codes.GENERAL);
}
return returnValue;
};
BaseAPI.prototype.getCommitObject = function (terminateCommit) {
var shouldTerminateCommit = terminateCommit || this.settings.alwaysSendTotalTime;
var commitObject = this.settings.renderCommonCommitFields
? this.renderCommitObject(shouldTerminateCommit)
: this.renderCommitCMI(shouldTerminateCommit);
if ([enums.LogLevelEnum.DEBUG, "1", 1, "DEBUG"].includes(this.apiLogLevel)) {
console.debug("Commit (terminated: " + (terminateCommit ? "yes" : "no") + "): ");
console.debug(commitObject);
}
return commitObject;
};
BaseAPI.prototype.performFetch = function (url, params) {
return (0,tslib_es6.__awaiter)(this, void 0, void 0, function () {
var init;
return (0,tslib_es6.__generator)(this, function (_a) {
init = {
method: "POST",
mode: this.settings.fetchMode,
body: params instanceof Array ? params.join("&") : JSON.stringify(params),
headers: (0,tslib_es6.__assign)((0,tslib_es6.__assign)({}, this.settings.xhrHeaders), { "Content-Type": this.settings.commitRequestDataType }),
keepalive: true
};
if (this.settings.xhrWithCredentials) {
init = (0,tslib_es6.__assign)((0,tslib_es6.__assign)({}, init), { credentials: "include" });
}
return [2, fetch(url, init)];
});
});
};
BaseAPI.prototype.transformResponse = function (response) {
return (0,tslib_es6.__awaiter)(this, void 0, void 0, function () {
var result, _a;
return (0,tslib_es6.__generator)(this, function (_c) {
switch (_c.label) {
case 0:
if (!(typeof this.settings.responseHandler === "function")) return [3, 2];
return [4, this.settings.responseHandler(response)];
case 1:
_a = _c.sent();
return [3, 4];
case 2: return [4, response.json()];
case 3:
_a = _c.sent();
_c.label = 4;
case 4:
result = _a;
if (response.status >= 200 &&
response.status <= 299 &&
(result.result === true || result.result === api_constants.global_constants.SCORM_TRUE)) {
this.processListeners("CommitSuccess");
}
else {
this.processListeners("CommitError");
}
return [2, result];
}
});
});
};
return BaseAPI;
}());
/* harmony default export */ var src_BaseAPI = (BaseAPI);
/***/ }),
/***/ 589:
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ CMIArray: function() { return /* binding */ CMIArray; }
/* harmony export */ });
/* harmony import */ var tslib__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(635);
/* harmony import */ var _base_cmi__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(319);
/* harmony import */ var _exceptions__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(784);
/* harmony import */ var _constants_error_codes__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(797);
var CMIArray = (function (_super) {
(0,tslib__WEBPACK_IMPORTED_MODULE_0__.__extends)(CMIArray, _super);
function CMIArray(params) {
var _this = _super.call(this) || this;
_this.__children = params.children;
_this._errorCode = params.errorCode || _constants_error_codes__WEBPACK_IMPORTED_MODULE_1__.scorm12_errors.GENERAL;
_this._errorClass = params.errorClass || _exceptions__WEBPACK_IMPORTED_MODULE_2__.BaseScormValidationError;
_this.childArray = [];
return _this;
}
CMIArray.prototype.reset = function (wipe) {
if (wipe === void 0) { wipe = false; }
this._initialized = false;
if (wipe) {
this.childArray = [];
}
else {
for (var i = 0; i < this.childArray.length; i++) {
this.childArray[i].reset();
}
}
};
Object.defineProperty(CMIArray.prototype, "_children", {
get: function () {
return this.__children;
},
set: function (_children) {
throw new this._errorClass(this._errorCode);
},
enumerable: false,
configurable: true
});
Object.defineProperty(CMIArray.prototype, "_count", {
get: function () {
return this.childArray.length;
},
set: function (_count) {
throw new this._errorClass(this._errorCode);
},
enumerable: false,
configurable: true
});
CMIArray.prototype.toJSON = function () {
this.jsonString = true;
var result = {};
for (var i = 0; i < this.childArray.length; i++) {
result[i + ""] = this.childArray[i];
}
delete this.jsonString;
return result;
};
return CMIArray;
}(_base_cmi__WEBPACK_IMPORTED_MODULE_3__.BaseCMI));
/***/ }),
/***/ 319:
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ BaseCMI: function() { return /* binding */ BaseCMI; },
/* harmony export */ BaseRootCMI: function() { return /* binding */ BaseRootCMI; }
/* harmony export */ });
/* harmony import */ var tslib__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(635);
var BaseCMI = (function () {
function BaseCMI() {
this.jsonString = false;
this._initialized = false;
}
Object.defineProperty(BaseCMI.prototype, "initialized", {
get: function () {
return this._initialized;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseCMI.prototype, "start_time", {
get: function () {
return this._start_time;
},
enumerable: false,
configurable: true
});
BaseCMI.prototype.initialize = function () {
this._initialized = true;
};
BaseCMI.prototype.setStartTime = function () {
this._start_time = new Date().getTime();
};
return BaseCMI;
}());
var BaseRootCMI = (function (_super) {
(0,tslib__WEBPACK_IMPORTED_MODULE_0__.__extends)(BaseRootCMI, _super);
function BaseRootCMI() {
return _super !== null && _super.apply(this, arguments) || this;
}
return BaseRootCMI;
}(BaseCMI));
/***/ }),
/***/ 434:
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ CMIScore: function() { return /* binding */ CMIScore; }
/* harmony export */ });
/* harmony import */ var tslib__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(635);
/* harmony import */ var _constants_api_constants__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(340);
/* harmony import */ var _constants_regex__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(417);
/* harmony import */ var _base_cmi__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(319);
/* harmony import */ var _validation__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(449);
/* harmony import */ var _constants_error_codes__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(797);
var CMIScore = (function (_super) {
(0,tslib__WEBPACK_IMPORTED_MODULE_0__.__extends)(CMIScore, _super);
function CMIScore(params) {
var _this = _super.call(this) || this;
_this._raw = "";
_this._min = "";
_this.__children = params.score_children || _constants_api_constants__WEBPACK_IMPORTED_MODULE_1__.scorm12_constants.score_children;
_this.__score_range = !params.score_range
? false
: _constants_regex__WEBPACK_IMPORTED_MODULE_2__.scorm12_regex.score_range;
_this._max = params.max || params.max === "" ? params.max : "100";
_this.__invalid_error_code =
params.invalidErrorCode || _constants_error_codes__WEBPACK_IMPORTED_MODULE_3__.scorm12_errors.INVALID_SET_VALUE;
_this.__invalid_type_code =
params.invalidTypeCode || _constants_error_codes__WEBPACK_IMPORTED_MODULE_3__.scorm12_errors.TYPE_MISMATCH;
_this.__invalid_range_code =
params.invalidRangeCode || _constants_error_codes__WEBPACK_IMPORTED_MODULE_3__.scorm12_errors.VALUE_OUT_OF_RANGE;
_this.__decimal_regex = params.decimalRegex || _constants_regex__WEBPACK_IMPORTED_MODULE_2__.scorm12_regex.CMIDecimal;
_this.__error_class = params.errorClass;
return _this;
}
CMIScore.prototype.reset = function () {
this._initialized = false;
};
Object.defineProperty(CMIScore.prototype, "_children", {
get: function () {
return this.__children;
},
set: function (_children) {
throw new this.__error_class(this.__invalid_error_code);
},
enumerable: false,
configurable: true
});
Object.defineProperty(CMIScore.prototype, "raw", {
get: function () {
return this._raw;
},
set: function (raw) {
if ((0,_validation__WEBPACK_IMPORTED_MODULE_4__.checkValidFormat)(raw, this.__decimal_regex, this.__invalid_type_code, this.__error_class) &&
(!this.__score_range ||
(0,_validation__WEBPACK_IMPORTED_MODULE_4__.checkValidRange)(raw, this.__score_range, this.__invalid_range_code, this.__error_class))) {
this._raw = raw;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(CMIScore.prototype, "min", {
get: function () {
return this._min;
},
set: function (min) {
if ((0,_validation__WEBPACK_IMPORTED_MODULE_4__.checkValidFormat)(min, this.__decimal_regex, this.__invalid_type_code, this.__error_class) &&
(!this.__score_range ||
(0,_validation__WEBPACK_IMPORTED_MODULE_4__.checkValidRange)(min, this.__score_range, this.__invalid_range_code, this.__error_class))) {
this._min = min;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(CMIScore.prototype, "max", {
get: function () {
return this._max;
},
set: function (max) {
if ((0,_validation__WEBPACK_IMPORTED_MODULE_4__.checkValidFormat)(max, this.__decimal_regex, this.__invalid_type_code, this.__error_class) &&
(!this.__score_range ||
(0,_validation__WEBPACK_IMPORTED_MODULE_4__.checkValidRange)(max, this.__score_range, this.__invalid_range_code, this.__error_class))) {
this._max = max;
}
},
enumerable: false,
configurable: true
});
CMIScore.prototype.toJSON = functi