UNPKG

react-cmi5

Version:

React wrapper component for an xapi/cmi5 assignable unit @see https://github.com/AICC/CMI-5_Spec_Current/blob/quartz/cmi5_spec.md#au_requirements

1 lines 5.57 kB
module.exports=function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([function(e,t){e.exports=require("react")},function(e,t,n){"use strict";n.r(t);var r=n(0),o=n.n(r);function i(e){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function a(e){return function(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function c(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(t){s(e,t,n[t])})}return e}function s(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function u(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function l(e){return(l=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function f(e,t){return(f=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function m(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}var d={NONE:0,LOADING:1,READY:2,RESULT_SENT:3,ERROR:-1},p=function(e){function t(e){var n;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t),(n=function(e,t){return!t||"object"!==i(t)&&"function"!=typeof t?m(e):t}(this,l(t).call(this,e))).state={cmiStatus:d.NONE,loading:!0,answered:!1},n.passed=n.passed.bind(m(m(n))),n.failed=n.failed.bind(m(m(n))),n.completed=n.completed.bind(m(m(n))),n.terminate=n.terminate.bind(m(m(n))),n._submitScore=n._submitScore.bind(m(m(n))),n._execCmiOrQueue=n._execCmiOrQueue.bind(m(m(n))),n}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&f(e,t)}(t,r["Component"]),function(e,t,n){t&&u(e.prototype,t),n&&u(e,n)}(t,[{key:"passed",value:function(e){this._submitScore(e,!0)}},{key:"failed",value:function(e){this._submitScore(e,!1)}},{key:"completed",value:function(){var e=this;this._execCmiOrQueue(function(){e.state.cmi.completed()})}},{key:"terminate",value:function(){var e=this;this._execCmiOrQueue(function(){e.state.cmi.terminate()})}},{key:"_submitScore",value:function(e,t){var n=this,r=isNaN(Number(e))?e:{scaled:Number(e)};this._execCmiOrQueue(function(){t?n.state.cmi.passed(r):n.state.cmi.failed(r)}),this.setState(c({},this.state,{answered:!0}))}},{key:"_execCmiOrQueue",value:function(e){if(this.state.cmiStatus===d.READY)try{e()}catch(e){console.error("cmi action failed: ".concat(e.message,"\n").concat(e.stack))}else this.setState(c({},this.state,{actionsPendingCmiReady:Array.isArray(this.state.actionsPendingCmiReady)?[].concat(a(this.state.actionsPendingCmiReady),[e]):[e]}))}},{key:"componentDidMount",value:function(){var e=this;try{var t=window.Cmi5,n=this.props.url||window.location.href;console.log("launch cmi with url ".concat(n));var r=new t(n);r.start(function(t,n){if(t)return e.setState(c({},e.state,{cmiStatus:d.ERROR,cmiMessage:t.message})),void console.error(t);e.setState(c({},e.state,{cmiStatus:d.READY,loading:!1}))}),this.setState(c({},this.state,{cmi:r}))}catch(e){console.error("error loading cmi: ".concat(e.message)),this.setState(c({},this.state,{cmiStatus:d.NONE,cmiMessage:e.message}))}}},{key:"render",value:function(){var e=this;switch(console.log("render state.cmiStatus=".concat(this.state.cmiStatus)),this.state.cmiStatus){case d.READY:Array.isArray(this.state.actionsPendingCmiReady)&&this.actionsPendingCmiReady.forEach(function(e){return e()});break;case d.ERROR:console.error("cmi error: ".concat(this.state.cmiMessage));break;default:console.log("cmi status updated to ".concat(this.state.cmiStatus))}var t=this.props.children,n=o.a.Children.map(t,function(t){return o.a.cloneElement(t,{cmi:e.state.cmi,terminate:e.terminate,passed:e.passed,failed:e.failed,completed:e.completed,loading:e.state.loading,answered:e.state.answered})});return o.a.createElement("div",null,n)}}]),t}();t.default=p}]);