-
Notifications
You must be signed in to change notification settings - Fork 4
/
simple-scorm-wrapper.min.js
1 lines (1 loc) · 11.5 KB
/
simple-scorm-wrapper.min.js
1
var Scorm=function(){"use strict";var f=function(t,e){for(var i=String(t);i.length<e;)i="0"+i;return i};function c(t){return"function"==typeof t||!1}function i(t,e){if(void 0===e&&(e="1.2"),"1.2"!=e)return i=100*Math.round(t/1e3),s=!0,o="P",r=i,u=l=c=a=0,r=i=Math.max(i,0),r=Math.round(r),1==s||(r-=315576e4*(a=Math.floor(r/315576e4)),r-=26298e4*(c=Math.floor(r/26298e4))),l=Math.floor(r/864e4),r-=864e4*l,r-=36e4*(n=Math.floor(r/36e4)),r-=6e3*(u=Math.floor(r/6e3)),0<a&&(o+=a+"Y"),0<c&&(o+=c+"M"),0<l&&(o+=l+"D"),(0<n||0<u||0<r)&&(o+="T",0<n&&(o+=n+"H"),0<u&&(o+=u+"M"),0<r&&(o+=r/100+"S")),"P"==o&&(o="PT0H0M0S"),o;var i,s,n,o,r,a,c,l,u,h=parseInt(t%1e3/100),m=parseInt(t/1e3%60),d=parseInt(t/6e4%60),p=parseInt(t/36e5%24);return(p=f(p,4))+":"+(d=f(d,2))+":"+(m=f(m,2))+"."+h}function l(t,e){return-1<t.indexOf(e)}var n,s,o=n=function(i,e){if(e){var t=Array.isArray(e),s=t&&[]||{};return t?(i=i||[],s=s.concat(i),e.forEach(function(t,e){void 0===s[e]?s[e]=t:"object"==typeof t?s[e]=n(i[e],t):-1===i.indexOf(t)&&s.push(t)})):(i&&"object"==typeof i&&Object.keys(i).forEach(function(t){s[t]=i[t]}),Object.keys(e).forEach(function(t){"object"==typeof e[t]&&e[t]&&i[t]?s[t]=n(i[t],e[t]):s[t]=e[t]})),s}return i=i||[]},r=s=function(t,e){return e<t?s(t/10,e):t},a={1.2:{id:"cmi.core.student_id",name:"cmi.core.student_name",language:"cmi.student_preference.language",audio:"cmi.student_preference.audio",location:"cmi.core.lesson_location",credit:"cmi.core.credit",entry:"cmi.core.entry",launch_data:"cmi.launch_data",lesson_mode:"cmi.core.lesson_mode",max_time_allowed:"cmi.student_data.max_time_allowed",session_time:"cmi.core.session_time","score.min":"cmi.core.score.min","score.max":"cmi.core.score.max","score.raw":"cmi.core.score.raw","score.scaled":"cmi.core.score.raw",suspend_data:"cmi.suspend_data",status:"cmi.core.lesson_status",total_time:"cmi.core.total_time"},2004:{id:"cmi.learner_id",name:"cmi.learner_name",language:"cmi.learner_preference.language",audio:"cmi.learner_preference.audio_level",location:"cmi.location",credit:"cmi.credit",entry:"cmi.entry",launch_data:"cmi.launch_data",lesson_mode:"cmi.mode",max_time_allowed:"cmi.max_time_allowed",session_time:"cmi.session_time","score.min":"cmi.score.min","score.max":"cmi.score.max","score.raw":"cmi.score.raw","score.scaled":"cmi.score.scaled",suspend_data:"cmi.suspend_data",status:"cmi.completion_status",total_time:"cmi.total_time"}},u={code:"0",string:"No Error",diagnostic:"No Error"},h={code:"101",string:"General Exception",diagnostic:"General Exception"},m={confirm:!1,label:"Are you sure you want to quit the program ?"},d=["","normal","suspend","logout"],p=function(){},g=function(t){var e={code:u.code,string:u.string,diagnostic:u.diagnostic};return null==t.getApiHandle()?(e.code=h.code,e.string=h.string,e.diagnostic="Unable to locate the LMS's API Implementation. Cannot determine LMS error code.",t.message(e,"Unable to locate the LMS's API Implementation.\nCannot determine LMS error code.")):(e.code=t.getLastError().toString(),e.code!=u.code&&(e.string=t.getErrorString(e.code),e.diagnostic=t.getDiagnostic(""))),e},t=function(t,e){var i=this;void 0===t&&(t={}),this.version=t.version||null,null!=this.version&&(this.version=this.version.toString()),this.initialized=!1,this.api=null,this.exitValue=t.exitValue||null,this.prefix="",this.completion_status=null,this.message=t.debugger||p,this.finished=!1,this.startTime=(new Date).getTime(),this.idleTime=0,this.window=null,this.scoreMin=null,this.scoreMax=null,this.storage={},this.params=function(t){return a[i.version][t]||""};var s=this.initialize();return"true"!=s?(c(e)&&e.bind(this)(s),!1):(window.onunload=function(t){i.finished||i.terminate()},window.onbeforeunload=function(t){i.finished||i.terminate()},null!=t.score&&(t.score.min||(t.score.min=0),this.scoreMin=r(t.score.min,100),this.setValue(this.params("score.min"),t.score.min),t.score.max||(t.score.max=100),this.scoreMax=r(t.score.max,100),this.setValue(this.params("score.max"),t.score.max)),c(e)&&e.bind(this)(s),this)};return t.prototype.userName=function(){return this.getValue(this.params("name"))},t.prototype.userId=function(){return this.getValue(this.params("id"))},t.prototype.bindCloseToElement=function(t,e){var i,s=this,n=o(m,e);(i=t)&&"object"==typeof i&&1===i.nodeType&&t.addEventListener("click",function(){var t=!0;(n.confirm&&(t=!1,t=1==confirm(n.label)),t)&&("true"==s.terminate()&&window.top.close())})},t.prototype.status=function(t){var e="";switch(this.version){case"1.2":e="cmi.core.lesson_status";break;case"2004":e="cmi.completion_status"}if(null==t)return this.getValue(e);return l({1.2:["not attempted","completed","incomplete","passed","failed","browsed"],2004:["not attempted","completed","incomplete","unknown"]}[this.version],t)?this.setValue(e,t):void this.message(u,"status value not supported")},t.prototype.score=function(t){var e=null;if(null!=t){var i=0;t=r(t,100),"1.2"!=this.version?(i=(t-this.scoreMin)/(this.scoreMax-this.scoreMin),this.setValue(this.params("score.raw"),t),this.setValue("cmi.score.scaled",i)):(i=(t-this.scoreMin)*(100/(this.scoreMax-this.scoreMin)),this.setValue(this.params("score.raw"),i)),t>=this.scoreMax&&("1.2"!=this.version?this.setValue("cmi.success_status","passed"):this.status("passed"))}else e=parseInt(this.getValue(this.params("score.raw")));return e},t.prototype.success=function(t){if(null==t)return"1.2"!=this.version?this.getValue("cmi.success_status"):this.status();t?"1.2"!=this.version?this.setValue("cmi.success_status","passed"):this.status("passed"):"1.2"!=this.version?this.setValue("cmi.success_status","failed"):this.status("failed")},t.prototype.suspend_data=function(t){if(null==t){var e=this.getValue(this.params("suspend_data"));return e?this.storage=JSON.parse(e):this.setValue(this.params("suspend_data"),JSON.stringify(this.storage)),this.storage}this.storage=o(this.storage,t),this.setValue(this.params("suspend_data"),JSON.stringify(this.storage))},t.prototype.language=function(t){if(null==t)return this.getValue(this.params("language"));this.suspend_data({lng:t})},t.prototype.location=function(t){return null!=t&&this.setValue(this.params("location"),t),this.getValue(this.params("location"))},t.prototype.session_time=function(t){if(null!=this.startTime){var e=(new Date).getTime()-this.startTime+this.idleTime;return null!=t&&1e3<e&&this.setValue(this.params("session_time"),i(e,this.version)),i(e,this.version)}},t.prototype.total_time=function(){return this.getValue(this.params("total_time"))},t.prototype.initialize=function(){if(this.initialized)return"true";var t=this.getApiHandle();if(null==t)return this.message(h,"Unable to locate the LMS's API Implementation.\n"+this.prefix+"Initialize was not successful."),"false";var e=this.prefix+"Initialize",i=t[e]("");if("true"!=i.toString()){var s=g(this);this.message(s,e+" failed with error code: "+s.code,"initialize")}else{this.initialized=!0;var n=this.status();if(n)switch(n){case"not attempted":case"unknown":this.status("incomplete")}}return i.toString()},t.prototype.beforeTerminate=function(){},t.prototype.terminate=function(t){if(void 0===t&&(t=""),!this.initialized)return"true";var e=this.getApiHandle(),i="false";if(null==e)return this.message(h,"Unable to locate the LMS's API Implementation.\n"+this.prefix+"Finish was not successful."),"false";var s=!1,n=t;"logout"!==t&&"normal"!==t||(n="1.2"===this.version?"logout":"normal"),"completed"!==this.completion_status&&"passed"!==this.completion_status&&(n="suspend"),l(d,this.exitValue)&&(n=this.exitValue),this.session_time("save"),c(this.beforeTerminate)&&this.beforeTerminate(),s=this.commit();var o=p,r="Terminate";if(o="1.2"===this.version?(s=this.setValue("cmi.core.exit",n),r="LMSFinish",function(t){return e.LMSFinish(t)}):(s=this.setValue("cmi.exit",n),r="Terminate",function(t){return e.Terminate(t)}),"true"==s&&"true"!=(i=o("")).toString()){var a=g(this);this.message(a,r+" failed with error code: "+a.code,"terminate")}return this.initialized=!1,"true"!=this.finished&&(this.finished=i.toString()),i.toString()},t.prototype.getValue=function(t){var e=this.getApiHandle(),i="",s=this.prefix+"GetValue";if(null==e)this.message(h,"Unable to locate the LMS's API Implementation.\nLMSGetValue was not successful.");else if(this.initialized||this.initialize()){i=e[s](t);var n=g(this);if(n.code!=u.code)this.message(n,s+"("+t+") failed. \n"+n.code+": "+n.string,"getValue",t),i="";else switch(t){case"cmi.core.lesson_status":case"cmi.completion_status":this.completion_status=i}}else{var o=g(this);this.message(o,s+" failed - Could not initialize communication with the LMS - error code: "+o.code)}return i.toString()},t.prototype.setValue=function(t,e){var i=this.getApiHandle(),s="false",n=this.prefix+"SetValue";if(null==i)this.message(h,"Unable to locate the LMS's API Implementation.\nLMSSetValue was not successful.");else if(this.initialized||this.initialize())if("true"!=(s=i[n](t,e)).toString()){var o=g(this);this.message(o,n+"("+t+", "+e+") failed. \n"+o.code+": "+o.string,"setValue",t)}else"cmi.core.lesson_status"!==t&&"cmi.completion_status"!==t||(this.completion_status=e);else{var r=g(this);this.message(r,n+" failed - Could not initialize communication with the LMS - error code: "+r.code)}return s.toString()},t.prototype.commit=function(){var t=this.getApiHandle(),e="false",i=this.prefix+"Commit";if(null==t)this.message(h,"Unable to locate the LMS's API Implementation.\nLMSCommit was not successful.");else if(this.initialized||this.initialize()){if("true"!=(e=t[i](""))){var s=g(this);this.message(s,i+" failed - error code: "+s.code,"commit")}}else{var n=g(this);this.message(n,i+" failed - Could not initialize communication with the LMS - error code: "+n.code)}return e.toString()},t.prototype.getLastError=function(){var t=this.getApiHandle();return null==t?h.code:t[this.prefix+"GetLastError"]().toString()},t.prototype.getErrorString=function(t){var e=this.getApiHandle();return null==e?h.string:e[this.prefix+"GetErrorString"](t).toString()},t.prototype.getDiagnostic=function(t){var e=this.getApiHandle();return null==e?"Unable to locate the LMS's API Implementation. LMSGetDiagnostic was not successful.":e[this.prefix+"GetDiagnostic"](t).toString()},t.prototype.getApiHandle=function(){return null==this.api&&(this.api=this.getApi()),this.api},t.prototype.findAPI=function(t){for(var e=null,i=0;!t.API&&!t.API_1484_11&&t.parent&&t.parent!=t&&i<=500;)i++,t=t.parent;return null!=(this.window=t).API_1484_11&&null!=t.API&&null!=this.version?("1.2"==this.version&&(this.prefix="LMS",e=t.API),"2004"==this.version&&(this.prefix="",e=t.API_1484_11)):t.API_1484_11?(this.version="2004",this.prefix="",e=t.API_1484_11):t.API&&(this.version="1.2",this.prefix="LMS",e=t.API),e},t.prototype.getApi=function(){var t=this.findAPI(window);return!t&&window.parent&&window.parent!=window&&(t=this.findAPI(window.parent)),!t&&window.opener&&window.opener&&(t=this.findAPI(window.opener)),!t&&window.top&&window.top.opener&&(t=this.findAPI(window.top.opener)),!t&&window.top&&window.top.opener&&window.top.opener.document&&(t=this.findAPI(window.top.opener.document)),t},t.prototype.findObjective=function(t){if("2004"===this.version){for(var e=this.getValue("cmi.objectives._count"),i=-1,s=0;s<e;++s)if(this.getValue("cmi.objectives."+s+".id")==t){i=s;break}return-1==i&&(this.message(h,"Objective "+t+" not found."),i=e,this.message(h,"Creating new objective at index "+i),this.setValue("cmi.objectives."+i+".id",t)),i}},t.prototype.findDataStore=function(t){if("2004"===this.version){var e=this.getValue("adl.data._count"),i=-1;if(null!=e&&!isNaN(e)){for(var s=0;s<e;++s)if(this.getValue("adl.data."+s+".id")==t){i=s;break}-1==i&&this.message(h,"Data store "+t+" not found.")}return i}},t}();